Skip to content

Commit dd65fbc

Browse files
committed
port default queries to duckdb
1 parent 6c80b98 commit dd65fbc

File tree

9 files changed

+496
-539
lines changed

9 files changed

+496
-539
lines changed

metrics/defaults/sql/alerts.sql

Lines changed: 51 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
/*
22
Template for generating the input data for the alert job.
33
4-
Written for SQLite but will be translated to target dialect based on `db` param via sqlglot.
4+
Written for DuckDB but will be translated to target dialect based on `db` param via sqlglot.
55
*/
66

77
with
88

99
-- Filter the data to the relevant metric batch for metrics
10-
metric_value_data as (
10+
metric_value_data as
11+
(
1112
select
1213
metric_timestamp,
1314
metric_batch,
@@ -17,20 +18,17 @@ from
1718
{{ table_key }}
1819
where
1920
metric_batch = '{{ metric_batch }}'
20-
and
21-
metric_type = 'metric'
22-
and
23-
metric_timestamp >= date('now', '-{{ alert_metric_timestamp_max_days_ago }} day')
21+
and metric_type = 'metric'
22+
and metric_timestamp >= current_date - interval '{{ alert_metric_timestamp_max_days_ago }} day'
2423
{% if alert_exclude_metrics is defined %}
25-
and
26-
-- Exclude the specified metrics
27-
metric_name not in ({{ ','.join(alert_exclude_metrics) }})
24+
and metric_name not in ({{ ','.join(alert_exclude_metrics) }})
2825
{% endif %}
2926
group by metric_timestamp, metric_batch, metric_name
3027
),
3128

3229
-- Filter the data to the relevant metric batch for scores
33-
metric_score_data as (
30+
metric_score_data as
31+
(
3432
select
3533
metric_timestamp,
3634
metric_batch,
@@ -40,15 +38,14 @@ from
4038
{{ table_key }}
4139
where
4240
metric_batch = '{{ metric_batch }}'
43-
and
44-
metric_type = 'score'
45-
and
46-
metric_timestamp >= date('now', '-{{ alert_metric_timestamp_max_days_ago }} day')
41+
and metric_type = 'score'
42+
and metric_timestamp >= current_date - interval '{{ alert_metric_timestamp_max_days_ago }} day'
4743
group by metric_timestamp, metric_batch, metric_name
4844
),
4945

5046
-- Filter the data to the relevant metric batch for alerts
51-
metric_alert_data as (
47+
metric_alert_data as
48+
(
5249
select
5350
metric_timestamp,
5451
metric_batch,
@@ -58,15 +55,14 @@ from
5855
{{ table_key }}
5956
where
6057
metric_batch = '{{ metric_batch }}'
61-
and
62-
metric_type = 'alert'
63-
and
64-
metric_timestamp >= date('now', '-{{ alert_metric_timestamp_max_days_ago }} day')
58+
and metric_type = 'alert'
59+
and metric_timestamp >= current_date - interval '{{ alert_metric_timestamp_max_days_ago }} day'
6560
group by metric_timestamp, metric_batch, metric_name
6661
),
6762

6863
-- Rank the score data by recency
69-
metric_score_recency_ranked as (
64+
metric_score_recency_ranked as
65+
(
7066
select
7167
metric_timestamp,
7268
metric_batch,
@@ -78,7 +74,8 @@ from
7874
),
7975

8076
-- Rank the value data by recency
81-
metric_value_recency_ranked as (
77+
metric_value_recency_ranked as
78+
(
8279
select
8380
metric_timestamp,
8481
metric_batch,
@@ -97,30 +94,26 @@ select
9794
m.metric_name,
9895
m.metric_value,
9996
s.metric_score,
100-
ifnull(a.metric_alert_historic, 0) as metric_alert_historic,
97+
coalesce(a.metric_alert_historic, 0) as metric_alert_historic,
10198
m.metric_value_recency_rank,
10299
s.metric_score_recency_rank
103100
from
104101
metric_value_recency_ranked m
105102
left join
106103
metric_score_recency_ranked s
107-
on
104+
on
108105
m.metric_name = s.metric_name
109-
and
110-
m.metric_batch = s.metric_batch
111-
and
112-
m.metric_timestamp = s.metric_timestamp
106+
and m.metric_batch = s.metric_batch
107+
and m.metric_timestamp = s.metric_timestamp
113108
left join
114109
metric_alert_data a
115-
on
110+
on
116111
m.metric_name = a.metric_name
117-
and
118-
m.metric_batch = a.metric_batch
119-
and
120-
m.metric_timestamp = a.metric_timestamp
112+
and m.metric_batch = a.metric_batch
113+
and m.metric_timestamp = a.metric_timestamp
121114
),
122115

123-
-- Smooth the data
116+
-- Smooth the data using window functions
124117
data_smoothed as (
125118
select
126119
metric_timestamp,
@@ -131,20 +124,18 @@ select
131124
metric_alert_historic,
132125
metric_value_recency_rank,
133126
metric_score_recency_rank,
134-
-- Smooth the metric score using a custom window
135-
(select avg(ds.metric_score)
136-
from data_ranked ds
137-
where ds.metric_name = dr.metric_name
138-
and ds.metric_score_recency_rank between dr.metric_score_recency_rank - {{ alert_smooth_n }} and dr.metric_score_recency_rank
127+
avg(metric_score) over (
128+
partition by metric_batch, metric_name
129+
order by metric_score_recency_rank
130+
rows between {{ alert_smooth_n }} preceding and current row
139131
) as metric_score_smooth,
140-
-- Check for recent alerts
141-
(select max(ds.metric_alert_historic)
142-
from data_ranked ds
143-
where ds.metric_name = dr.metric_name
144-
and ds.metric_score_recency_rank between dr.metric_score_recency_rank - {{ alert_snooze_n }} and dr.metric_score_recency_rank - 1
132+
max(metric_alert_historic) over (
133+
partition by metric_batch, metric_name
134+
order by metric_score_recency_rank
135+
rows between {{ alert_snooze_n }} preceding and 1 preceding
145136
) as metric_has_recent_alert
146-
from
147-
data_ranked dr
137+
from
138+
data_ranked
148139
),
149140

150141
-- Calculate the alerts
@@ -160,22 +151,15 @@ select
160151
metric_score_smooth,
161152
metric_has_recent_alert,
162153
case
163-
when
164-
metric_score_recency_rank <= {{ alert_recent_n }}
165-
and
166-
(metric_score_smooth >= {{ alert_threshold }} OR {{ alert_always }} = True)
167-
and
168-
ifnull(metric_has_recent_alert,0) = 0
154+
when metric_score_recency_rank <= {{ alert_recent_n }}
155+
and (metric_score_smooth >= {{ alert_threshold }} OR {{ alert_always }} = True)
156+
and coalesce(metric_has_recent_alert, 0) = 0
169157
then 1
170158
else 0
171159
end as metric_alert_calculated
172-
from
173-
data_smoothed
174-
where
175-
metric_score_recency_rank <= {{ alert_max_n }}
176-
or
177-
-- flag for always alerting if set
178-
{{ alert_always }} = True
160+
from data_smoothed
161+
where metric_score_recency_rank <= {{ alert_max_n }}
162+
or {{ alert_always }} = True
179163
),
180164

181165
-- Filter the data to the metrics with triggered alerts
@@ -184,8 +168,7 @@ select
184168
metric_batch,
185169
metric_name,
186170
max(metric_alert_calculated) as metric_alert_calculated_tmp
187-
from
188-
data_alerts
171+
from data_alerts
189172
group by metric_batch, metric_name
190173
having max(metric_alert_calculated) = 1 or {{ alert_always }} = True
191174
)
@@ -198,13 +181,12 @@ select
198181
metric_value,
199182
metric_score,
200183
metric_score_smooth,
201-
if(metric_score_recency_rank = 1, metric_alert_calculated, metric_alert_historic) as metric_alert
202-
from
203-
data_alerts
204-
join
205-
metrics_triggered
206-
on
207-
data_alerts.metric_batch = metrics_triggered.metric_batch
208-
and
209-
data_alerts.metric_name = metrics_triggered.metric_name
184+
case
185+
when metric_score_recency_rank = 1 then metric_alert_calculated
186+
else metric_alert_historic
187+
end as metric_alert
188+
from data_alerts
189+
join metrics_triggered
190+
on data_alerts.metric_batch = metrics_triggered.metric_batch
191+
and data_alerts.metric_name = metrics_triggered.metric_name
210192
;

0 commit comments

Comments
 (0)