Skip to content

Commit 9374977

Browse files
committed
return metrics only if fail
1 parent 0e749a0 commit 9374977

File tree

1 file changed

+26
-8
lines changed

1 file changed

+26
-8
lines changed

macros/edr/tests/test_utils/get_anomaly_query.sql

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,27 @@
11
{%- macro get_anomaly_query(flattened_test=none) -%}
2-
{%- set query -%}
2+
{%- set anomaly_scores_query -%}
33
select * from ({{ elementary.get_read_anomaly_scores_query(flattened_test) }}) results
4+
{%- endset -%}
5+
6+
{% set anomalies_count_query -%}
7+
select count(*) as anomalies_count
8+
from ({{ anomaly_scores_query }})
9+
where is_anomalous = true
10+
{%- endset -%}
11+
12+
{% set non_anomalies_query -%}
13+
{{ anomaly_scores_query }}
414
where is_anomalous = true
515
{%- endset -%}
6-
{{- return(query) -}}
16+
17+
{% set anomalies_count_result = elementary.agate_to_dicts(elementary.run_query(anomalies_count_query)) %}
18+
{% set anomalies_count = anomalies_count_result[0]['anomalies_count'] %}
19+
20+
{% if anomalies_count > 0 %}
21+
{{- return(anomaly_scores_query) -}}
22+
{% else %}
23+
{{- return(non_anomalies_query) -}}
24+
{% endif %}
725
{%- endmacro -%}
826

927
{% macro get_read_anomaly_scores_query(flattened_test=none) %}
@@ -62,7 +80,7 @@ case when
6280

6381
final_results as (
6482
select
65-
metric_value as {{ elementary.escape_reserved_keywords('value') }},
83+
metric_value as value,
6684
training_avg as average,
6785
{# when there is an anomaly we would want to use the last value of the metric (lag), otherwise visually the expectations would look out of bounds #}
6886
case
@@ -71,15 +89,15 @@ case when
7189
when is_anomalous = TRUE and '{{ test_configuration.anomaly_direction }}' != 'spike' then
7290
{{ elementary.lag('min_metric_value') }} over (partition by full_table_name, column_name, metric_name, dimension, dimension_value, bucket_seasonality order by bucket_end)
7391
when '{{ test_configuration.anomaly_direction }}' = 'spike' then metric_value
74-
else min_metric_value
92+
else min_metric_value
7593
end as min_value,
7694
case
7795
when is_anomalous = TRUE and '{{ test_configuration.anomaly_direction }}' = 'drop' then
7896
{{ elementary.lag('metric_value') }} over (partition by full_table_name, column_name, metric_name, dimension, dimension_value, bucket_seasonality order by bucket_end)
7997
when is_anomalous = TRUE and '{{ test_configuration.anomaly_direction }}' != 'drop' then
8098
{{ elementary.lag('max_metric_value') }} over (partition by full_table_name, column_name, metric_name, dimension, dimension_value, bucket_seasonality order by bucket_end)
8199
when '{{ test_configuration.anomaly_direction }}' = 'drop' then metric_value
82-
else max_metric_value
100+
else max_metric_value
83101
end as max_value,
84102
bucket_start as start_time,
85103
bucket_end as end_time,
@@ -121,7 +139,7 @@ case when
121139
{% set drop_filter %}
122140
(metric_value < ((1 - {{ drop_failure_percent_threshold }}/100.0) * training_avg))
123141
{% endset %}
124-
142+
125143
{% if spike_failure_percent_threshold and drop_failure_percent_threshold and (anomaly_direction | lower) == 'both' %}
126144
{{ spike_filter }} or {{ drop_filter }}
127145
{% else %}
@@ -144,7 +162,7 @@ case when
144162

145163
{% macro fail_on_zero(fail_on_zero) %}
146164
(
147-
metric_value = 0 and
165+
metric_value = 0 and
148166
{% if fail_on_zero %}
149167
1 = 1
150168
{% else %}
@@ -163,7 +181,7 @@ case when
163181
test_configuration.ignore_small_changes.spike_failure_percent_threshold,
164182
test_configuration.ignore_small_changes.drop_failure_percent_threshold,
165183
test_configuration.anomaly_direction
166-
)
184+
)
167185
}}
168186
)
169187
))

0 commit comments

Comments
 (0)