generalized test
This commit is contained in:
parent
aaf512a288
commit
bede588062
5 changed files with 66 additions and 92 deletions
33
macros/tests/kpis_outlier_detector.sql
Normal file
33
macros/tests/kpis_outlier_detector.sql
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
{% test kpis_outlier_detector(
|
||||
model, column_name, sigma_threshold=3, days_to_consider=14
|
||||
) %}
|
||||
with
|
||||
-- Retrieve recent data based on the defined days_to_consider
|
||||
recent_data as (
|
||||
select *
|
||||
from {{ model }}
|
||||
where
|
||||
date_day between (
|
||||
current_date - interval '{{ days_to_consider + 1 }} days'
|
||||
) and (current_date - interval '1 day')
|
||||
),
|
||||
metrics_stats as (
|
||||
select
|
||||
avg({{ column_name }}) as avg_value,
|
||||
stddev({{ column_name }}) as stddev_value
|
||||
from recent_data
|
||||
),
|
||||
outliers as (
|
||||
select
|
||||
{{ column_name }} as value,
|
||||
abs({{ column_name }} - metrics_stats.avg_value)
|
||||
> (metrics_stats.stddev_value * {{ sigma_threshold }}) as is_outlier
|
||||
from {{ model }}
|
||||
cross join metrics_stats
|
||||
where date_day = current_date - interval '1 day'
|
||||
)
|
||||
-- Return failing rows if any values are flagged as outliers
|
||||
select *
|
||||
from outliers
|
||||
where is_outlier = true
|
||||
{% endtest %}
|
||||
Loading…
Add table
Add a link
Reference in a new issue