Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: implement Display for EvalStmt #91

Merged
merged 3 commits into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ categories = ["parser-implementations"]

[dependencies]
cfgrammar = "0.13.5"
chrono = "0.4.38"
lazy_static = "1.4.0"
lrlex = "0.13.5"
lrpar = "0.13.5"
Expand Down
44 changes: 42 additions & 2 deletions src/parser/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use crate::parser::token::{Token, TokenId, TokenType};
use crate::parser::value::ValueType;
use crate::parser::{indent, Function, FunctionArgs, Prettier, MAX_CHARACTERS_PER_LINE};
use crate::util::display_duration;
use chrono::{DateTime, Utc};
use std::fmt::{self, Write};
use std::ops::Neg;
use std::sync::Arc;
Expand All @@ -35,9 +36,10 @@ use std::time::{Duration, SystemTime};
/// # Vector Match Modifier
///
/// - Exclude means `without` removes the listed labels from the result vector,
/// while all other labels are preserved in the output.
/// while all other labels are preserved in the output.
///
/// - Include means `by` does the opposite and drops labels that are not listed in the by clause,
/// even if their label values are identical between all elements of the vector.
/// even if their label values are identical between all elements of the vector.
///
/// if empty listed labels, meaning no grouping
#[derive(Debug, Clone, PartialEq, Eq)]
Expand Down Expand Up @@ -315,6 +317,20 @@ pub struct EvalStmt {
pub lookback_delta: Duration,
zyy17 marked this conversation as resolved.
Show resolved Hide resolved
}

impl fmt::Display for EvalStmt {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"[{}] @ [{}, {}, {}, {}]",
self.expr,
DateTime::<Utc>::from(self.start).to_rfc3339(),
DateTime::<Utc>::from(self.end).to_rfc3339(),
display_duration(&self.interval),
display_duration(&self.lookback_delta)
)
}
}

/// Grammar:
/// ``` norust
/// <aggr-op> [without|by (<label list>)] ([parameter,] <vector expression>)
Expand Down Expand Up @@ -2440,4 +2456,28 @@ or
assert_eq!(expect, crate::parser::parse(input).unwrap().prettify());
}
}

#[test]
fn test_eval_stmt_to_string() {
let query = r#"http_requests_total{job="apiserver", handler="/api/comments"}[5m]"#;
let start = "2024-10-08T07:15:00.022978+00:00";
let end = "2024-10-08T07:15:30.012978+00:00";
let expect = r#"[http_requests_total{handler="/api/comments",job="apiserver"}[5m]] @ [2024-10-08T07:15:00.022978+00:00, 2024-10-08T07:15:30.012978+00:00, 1m, 5m]"#;

let stmt = EvalStmt {
expr: crate::parser::parse(query).unwrap(),
start: DateTime::parse_from_rfc3339(start)
.unwrap()
.with_timezone(&Utc)
.into(),
end: DateTime::parse_from_rfc3339(end)
.unwrap()
.with_timezone(&Utc)
.into(),
interval: Duration::from_secs(60),
lookback_delta: Duration::from_secs(300),
};

assert_eq!(expect, stmt.to_string());
}
}