Skip to content

Commit ea5270e

Browse files
update migration, add query to alerts json in demo script
1 parent 62f9459 commit ea5270e

File tree

2 files changed

+61
-23
lines changed

2 files changed

+61
-23
lines changed

resources/ingest_demo_data.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ create_alerts() {
415415
echo "Creating alerts with target ID: $target_id"
416416

417417
# Alert 1: Error Count (severity_number = 18)
418-
alert1_json="{\"severity\":\"high\",\"title\":\"error count\",\"alertType\":\"threshold\",\"thresholdConfig\":{\"operator\":\">\",\"value\":1000},\"evalConfig\":{\"rollingWindow\":{\"evalStart\":\"5h\",\"evalEnd\":\"now\",\"evalFrequency\":1}},\"targets\":[\"$target_id\"]}"
418+
alert1_json="{\"severity\":\"high\",\"title\":\"error count\",\"alertType\":\"threshold\",\"query\": \"select count(severity_number) as count_severity_number from demodata where severity_number=18\",\"thresholdConfig\":{\"operator\":\">\",\"value\":1000},\"evalConfig\":{\"rollingWindow\":{\"evalStart\":\"5h\",\"evalEnd\":\"now\",\"evalFrequency\":1}},\"targets\":[\"$target_id\"]}"
419419

420420
response1=$(curl_with_retry "$P_URL/api/v1/alerts" "POST" "$alert1_json" "application/json" 3)
421421
if [[ $? -eq 0 ]]; then
@@ -426,7 +426,7 @@ create_alerts() {
426426
fi
427427

428428
# Alert 2: 400 Errors
429-
alert2_json="{\"severity\":\"critical\",\"title\":\"400 Errors\",\"alertType\":\"threshold\",\"thresholdConfig\":{\"operator\":\">\",\"value\":10},\"evalConfig\":{\"rollingWindow\":{\"evalStart\":\"5h\",\"evalEnd\":\"now\",\"evalFrequency\":1}},\"targets\":[\"$target_id\"]}"
429+
alert2_json="{\"severity\":\"critical\",\"title\":\"400 Errors\",\"alertType\":\"threshold\",\"query\": \"select count(body) as count_body from demodata where body like '%400%'\",\"thresholdConfig\":{\"operator\":\">\",\"value\":10},\"evalConfig\":{\"rollingWindow\":{\"evalStart\":\"5h\",\"evalEnd\":\"now\",\"evalFrequency\":1}},\"targets\":[\"$target_id\"]}"
430430

431431
response2=$(curl_with_retry "$P_URL/api/v1/alerts" "POST" "$alert2_json" "application/json" 3)
432432
if [[ $? -eq 0 ]]; then
@@ -437,7 +437,7 @@ create_alerts() {
437437
fi
438438

439439
# Alert 3: Trace ID null
440-
alert3_json="{\"severity\":\"high\",\"title\":\"Trace ID null\",\"alertType\":\"threshold\",\"thresholdConfig\":{\"operator\":\"is null\",\"value\":null},\"evalConfig\":{\"rollingWindow\":{\"evalStart\":\"5h\",\"evalEnd\":\"now\",\"evalFrequency\":1}},\"targets\":[\"$target_id\"]}"
440+
alert3_json="{\"severity\":\"high\",\"title\":\"Trace ID null\",\"alertType\":\"threshold\",\"query\": \"select count(trace_id) as count_trace_id from demodata where trace_id is null\",\"thresholdConfig\":{\"operator\":\"is null\",\"value\":null},\"evalConfig\":{\"rollingWindow\":{\"evalStart\":\"5h\",\"evalEnd\":\"now\",\"evalFrequency\":1}},\"targets\":[\"$target_id\"]}"
441441
response3=$(curl_with_retry "$P_URL/api/v1/alerts" "POST" "$alert3_json" "application/json" 3)
442442
if [[ $? -eq 0 ]]; then
443443
echo "Alert 3 (Trace ID null) created successfully"

src/alerts/mod.rs

Lines changed: 58 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -681,19 +681,30 @@ impl AlertConfig {
681681
) -> Result<String, AlertError> {
682682
let column = aggregate_config["column"].as_str().unwrap_or("*");
683683

684-
let query = if column == "*" {
685-
format!(
686-
"SELECT {}(*) as alert_value FROM \"{stream}\"",
687-
aggregate_function.to_string().to_uppercase()
688-
)
689-
} else if matches!(aggregate_function, AggregateFunction::Count) && column != "*" {
690-
// COUNT with specific column should handle NULLs differently
691-
format!("SELECT COUNT(\"{column}\") as alert_value FROM \"{stream}\"",)
692-
} else {
693-
format!(
694-
"SELECT {}(\"{column}\") as alert_value FROM \"{stream}\"",
695-
aggregate_function.to_string().to_uppercase()
696-
)
684+
let query = match aggregate_function {
685+
AggregateFunction::CountDistinct => {
686+
if column == "*" {
687+
format!("SELECT COUNT(DISTINCT *) as alert_value FROM \"{stream}\"")
688+
} else {
689+
format!("SELECT COUNT(DISTINCT \"{column}\") as alert_value FROM \"{stream}\"")
690+
}
691+
}
692+
_ => {
693+
if column == "*" {
694+
format!(
695+
"SELECT {}(*) as alert_value FROM \"{stream}\"",
696+
aggregate_function.to_string().to_uppercase()
697+
)
698+
} else if matches!(aggregate_function, AggregateFunction::Count) && column != "*" {
699+
// COUNT with specific column should handle NULLs differently
700+
format!("SELECT COUNT(\"{column}\") as alert_value FROM \"{stream}\"")
701+
} else {
702+
format!(
703+
"SELECT {}(\"{column}\") as alert_value FROM \"{stream}\"",
704+
aggregate_function.to_string().to_uppercase()
705+
)
706+
}
707+
}
697708
};
698709
Ok(query)
699710
}
@@ -760,13 +771,40 @@ impl AlertConfig {
760771

761772
/// Format a single WHERE clause
762773
fn format_where_clause(column: &str, operator: &WhereConfigOperator, value: &str) -> String {
763-
if matches!(
764-
operator,
765-
WhereConfigOperator::IsNull | WhereConfigOperator::IsNotNull
766-
) {
767-
format!("\"{}\" {}", column, operator.as_str())
768-
} else {
769-
format!("\"{}\" {} '{}'", column, operator.as_str(), value)
774+
match operator {
775+
WhereConfigOperator::IsNull | WhereConfigOperator::IsNotNull => {
776+
format!("\"{}\" {}", column, operator.as_str())
777+
}
778+
WhereConfigOperator::Contains => {
779+
format!("\"{}\" LIKE '%{}%'", column, value.replace('\'', "''"))
780+
}
781+
WhereConfigOperator::BeginsWith => {
782+
format!("\"{}\" LIKE '{}%'", column, value.replace('\'', "''"))
783+
}
784+
WhereConfigOperator::EndsWith => {
785+
format!("\"{}\" LIKE '%{}'", column, value.replace('\'', "''"))
786+
}
787+
WhereConfigOperator::DoesNotContain => {
788+
format!("\"{}\" NOT LIKE '%{}%'", column, value.replace('\'', "''"))
789+
}
790+
WhereConfigOperator::DoesNotBeginWith => {
791+
format!("\"{}\" NOT LIKE '{}%'", column, value.replace('\'', "''"))
792+
}
793+
WhereConfigOperator::DoesNotEndWith => {
794+
format!("\"{}\" NOT LIKE '%{}'", column, value.replace('\'', "''"))
795+
}
796+
WhereConfigOperator::ILike => {
797+
format!("\"{}\" ILIKE '{}'", column, value.replace('\'', "''"))
798+
}
799+
_ => {
800+
// Standard operators: =, !=, <, >, <=, >=
801+
format!(
802+
"\"{}\" {} '{}'",
803+
column,
804+
operator.as_str(),
805+
value.replace('\'', "''")
806+
)
807+
}
770808
}
771809
}
772810

0 commit comments

Comments
 (0)