generalized test

This commit is contained in:
Joaquin Ossa 2024-11-26 09:27:13 +01:00
parent aaf512a288
commit bede588062
5 changed files with 66 additions and 92 deletions

View 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 %}