Skip to content

Commit 40cddb5

Browse files
nitishttheteachr
andauthored
Add support for multiple column names (#567)
Add support for multiple column names to be passed in the message field of an alert Fixes #564 --------- Co-authored-by: theteachr <[email protected]>
1 parent 68a9615 commit 40cddb5

File tree

2 files changed

+24
-32
lines changed

2 files changed

+24
-32
lines changed

server/src/alerts/mod.rs

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -135,40 +135,32 @@ pub struct Message {
135135

136136
impl Message {
137137
// checks if message (with a column name) is valid (i.e. the column name is present in the schema)
138-
pub fn valid(&self, schema: &Schema, column: Option<&str>) -> bool {
139-
if let Some(col) = column {
140-
return get_field(&schema.fields, col).is_some();
141-
}
142-
true
138+
pub fn valid(&self, schema: &Schema, column: &str) -> bool {
139+
return get_field(&schema.fields, column).is_some();
143140
}
144141

145-
pub fn extract_column_name(&self) -> Option<&str> {
146-
let re = Regex::new(r"\{(.*?)\}").unwrap();
147-
let tokens: Vec<&str> = re
142+
pub fn extract_column_names(&self) -> Vec<&str> {
143+
// the message can have either no column name ({column_name} not present) or any number of {column_name} present
144+
Regex::new(r"\{(.*?)\}")
145+
.unwrap()
148146
.captures_iter(self.message.as_str())
149147
.map(|cap| cap.get(1).unwrap().as_str())
150-
.collect();
151-
// the message can have either no column name ({column_name} not present) or one column name
152-
// return Some only if there is exactly one column name present
153-
if tokens.len() == 1 {
154-
return Some(tokens[0]);
155-
}
156-
None
148+
.collect()
157149
}
158150

159-
// returns the message with the column name replaced with the value of the column
151+
/// Returns the message with the column names replaced with the values in the column.
160152
fn get(&self, event: RecordBatch) -> String {
161-
if let Some(column) = self.extract_column_name() {
153+
let mut replace_message = self.message.clone();
154+
for column in self.extract_column_names() {
162155
if let Some(value) = event.column_by_name(column) {
163156
let arr = cast(value, &DataType::Utf8).unwrap();
164157
let value = as_string_array(&arr).value(0);
165158

166-
return self
167-
.message
168-
.replace(&format!("{{{column}}}"), value.to_string().as_str());
159+
replace_message =
160+
replace_message.replace(&format!("{{{column}}}"), value.to_string().as_str());
169161
}
170162
}
171-
self.message.clone()
163+
replace_message
172164
}
173165
}
174166

server/src/handlers/http/logstream.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -154,17 +154,17 @@ pub async fn put_alert(
154154

155155
let schema = STREAM_INFO.schema(&stream_name)?;
156156
for alert in &alerts.alerts {
157-
let column = alert.message.extract_column_name();
158-
let is_valid = alert.message.valid(&schema, column);
159-
if !is_valid {
160-
let col = column.unwrap_or("");
161-
return Err(StreamError::InvalidAlertMessage(
162-
alert.name.to_owned(),
163-
col.to_string(),
164-
));
165-
}
166-
if !alert.rule.valid_for_schema(&schema) {
167-
return Err(StreamError::InvalidAlert(alert.name.to_owned()));
157+
for column in alert.message.extract_column_names() {
158+
let is_valid = alert.message.valid(&schema, column);
159+
if !is_valid {
160+
return Err(StreamError::InvalidAlertMessage(
161+
alert.name.to_owned(),
162+
column.to_string(),
163+
));
164+
}
165+
if !alert.rule.valid_for_schema(&schema) {
166+
return Err(StreamError::InvalidAlert(alert.name.to_owned()));
167+
}
168168
}
169169
}
170170

0 commit comments

Comments
 (0)