Skip to content

Commit 0bd273a

Browse files
authored
Add support for regex based rule in alerts (#353)
Fixes #305
1 parent f2209d4 commit 0bd273a

File tree

1 file changed

+24
-7
lines changed

1 file changed

+24
-7
lines changed

server/src/alerts/rule.rs

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ impl ColumnRule {
120120
NumericOperator::GreaterThanEquals => "greater than or equal to",
121121
NumericOperator::LessThan => "less than",
122122
NumericOperator::LessThanEquals => "less than or equal to",
123+
NumericOperator::Regex => "matches regex",
123124
},
124125
value,
125126
repeats
@@ -138,10 +139,11 @@ impl ColumnRule {
138139
"{} column {} {}, {} times",
139140
column,
140141
match operator {
141-
StringOperator::Exact => "was equal to",
142-
StringOperator::NotExact => "was not equal to",
143-
StringOperator::Contains => "contained",
144-
StringOperator::NotContains => "did not contain",
142+
StringOperator::Exact => "equal to",
143+
StringOperator::NotExact => "not equal to",
144+
StringOperator::Contains => "contains",
145+
StringOperator::NotContains => "does not contain",
146+
StringOperator::Regex => "matches regex",
145147
},
146148
value,
147149
repeats
@@ -292,9 +294,9 @@ mod tests {
292294
}
293295

294296
pub mod base {
295-
use serde::{Deserialize, Serialize};
296-
297297
use self::ops::{NumericOperator, StringOperator};
298+
use regex::Regex;
299+
use serde::{Deserialize, Serialize};
298300

299301
#[derive(Debug, Serialize, Deserialize)]
300302
#[serde(rename_all = "camelCase")]
@@ -328,6 +330,10 @@ pub mod base {
328330
NumericOperator::LessThanEquals => {
329331
number.as_f64().unwrap() <= self.value.as_f64().unwrap()
330332
}
333+
NumericOperator::Regex => {
334+
let re: Regex = regex::Regex::new(&self.value.to_string()).unwrap();
335+
re.is_match(&number.to_string())
336+
}
331337
};
332338

333339
Some(res)
@@ -361,13 +367,21 @@ pub mod base {
361367
StringOperator::NotContains => !string
362368
.to_ascii_lowercase()
363369
.contains(&self.value.to_ascii_lowercase()),
370+
StringOperator::Regex => {
371+
let re: Regex = regex::Regex::new(&self.value).unwrap();
372+
re.is_match(string)
373+
}
364374
}
365375
} else {
366376
match self.operator {
367377
StringOperator::Exact => string.eq(&self.value),
368378
StringOperator::NotExact => !string.eq(&self.value),
369379
StringOperator::Contains => string.contains(&self.value),
370380
StringOperator::NotContains => !string.contains(&self.value),
381+
StringOperator::Regex => {
382+
let re: Regex = regex::Regex::new(&self.value).unwrap();
383+
re.is_match(string)
384+
}
371385
}
372386
};
373387

@@ -393,6 +407,8 @@ pub mod base {
393407
LessThan,
394408
#[serde(alias = "<=")]
395409
LessThanEquals,
410+
#[serde(alias = "~")]
411+
Regex,
396412
}
397413

398414
impl Default for NumericOperator {
@@ -412,7 +428,8 @@ pub mod base {
412428
Contains,
413429
#[serde(alias = "!%")]
414430
NotContains,
415-
// =~ and !~ reserved for regex
431+
#[serde(alias = "~")]
432+
Regex,
416433
}
417434

418435
impl Default for StringOperator {

0 commit comments

Comments
 (0)