Skip to content

Commit

Permalink
Add test_metric_aggregation_scripted
Browse files Browse the repository at this point in the history
  • Loading branch information
dequis committed Nov 22, 2021
1 parent 71aaa25 commit c8760a5
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions tests/rules_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1261,6 +1261,22 @@ def test_metric_aggregation_complex_query_key_bucket_interval():
assert rule.matches[1]['sub_qk'] == 'sub_qk_val1'


def test_metric_aggregation_scripted():
script_body = "doc['some_threshold'] - doc['cpu_pct']"
rules = {'buffer_time': datetime.timedelta(minutes=5),
'timestamp_field': '@timestamp',
'metric_agg_type': 'avg',
'metric_agg_key': 'cpu_pct',
'metric_agg_script': {"script": script_body},
'min_threshold': 0.0}

rule = MetricAggregationRule(rules)
assert rule.rules['aggregation_query_element'] == {'metric_cpu_pct_avg': {'avg': {'script': script_body}}}

rule.check_matches(datetime.datetime.now(), None, {'metric_cpu_pct_avg': {'value': -0.5}})
assert rule.matches[0]['metric_cpu_pct_avg'] == -0.5


def test_percentage_match():
rules = {'match_bucket_filter': {'term': 'term_val'},
'buffer_time': datetime.timedelta(minutes=5),
Expand Down

0 comments on commit c8760a5

Please sign in to comment.