Skip to content

Commit

Permalink
fix: add some space, delete fn into_payload, and impl Display for Tab…
Browse files Browse the repository at this point in the history
…leResponse
  • Loading branch information
YCCDSZXH committed Mar 19, 2024
1 parent dfc621c commit e84e7da
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions src/servers/src/http/table_result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// limitations under the License.

use std::cmp::max;
use std::fmt::Write;
use std::fmt::{Display, Write};

use axum::http::{header, HeaderValue};
use axum::response::{IntoResponse, Response};
Expand Down Expand Up @@ -67,8 +67,11 @@ impl TableResponse {
pub fn execution_time_ms(&self) -> u64 {
self.execution_time_ms
}
fn into_payload(&mut self) -> String {
match self.output.pop() {
}

impl Display for TableResponse {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let payload = match self.output.first() {
None => String::default(),
Some(GreptimeQueryOutput::AffectedRows(n)) => {
format!("{n}\n")
Expand Down Expand Up @@ -115,12 +118,13 @@ impl TableResponse {
writeln!(result, "└{}┘", footer).unwrap();
result
}
}
};
write!(f, "{}", payload)
}
}

impl IntoResponse for TableResponse {
fn into_response(mut self) -> Response {
fn into_response(self) -> Response {
debug_assert!(
self.output.len() <= 1,
"self.output has extra elements: {}",
Expand All @@ -134,7 +138,7 @@ impl IntoResponse for TableResponse {
header::CONTENT_TYPE,
HeaderValue::from_static(mime::PLAIN.as_ref()),
)],
self.into_payload(),
self.to_string(),
)
.into_response();
resp.headers_mut().insert(
Expand All @@ -156,8 +160,8 @@ mod test {
#[tokio::test]
async fn test_table_format() {
let data = r#"{"output":[{"records":{"schema":{"column_schemas":[{"name":"host","data_type":"String"},{"name":"ts","data_type":"TimestampMillisecond"},{"name":"cpu","data_type":"Float64"},{"name":"memory","data_type":"Float64"}]},"rows":[["127.0.0.1",1702433141000,0.5,0.2],["127.0.0.1",1702433146000,0.3,0.2],["127.0.0.1",1702433151000,0.4,0.3],["127.0.0.2",1702433141000,0.3,0.1],["127.0.0.2",1702433146000,0.2,0.4],["127.0.0.2",1702433151000,0.2,0.4]]}}],"execution_time_ms":13}"#;
let mut table_response: TableResponse = serde_json::from_str(data).unwrap();
let payload = table_response.into_payload();
let table_response: TableResponse = serde_json::from_str(data).unwrap();
let payload = table_response.to_string();
let expected_payload = r#"┌─host────────┬─ts────────────┬─cpu─┬─memory─┐
│ "127.0.0.1" │ 1702433141000 │ 0.5 │ 0.2 │
│ "127.0.0.1" │ 1702433146000 │ 0.3 │ 0.2 │
Expand Down

0 comments on commit e84e7da

Please sign in to comment.