|
1 | 1 | /* |
2 | 2 | Template for generating the input data for the change detection job. |
| 3 | +
|
| 4 | +Written for SQLite but will be translated to target dialect based on `db` param via sqlglot. |
3 | 5 | */ |
4 | 6 |
|
5 | | -WITH |
6 | | - |
7 | | -metric_value_data AS ( |
8 | | - SELECT DISTINCT |
9 | | - metric_timestamp, |
10 | | - metric_batch, |
11 | | - metric_name, |
12 | | - AVG(metric_value) AS metric_value |
13 | | - FROM {{ table_key }} |
14 | | - WHERE metric_batch = '{{ metric_batch }}' |
15 | | - AND metric_type = 'metric' |
16 | | - AND DATE(metric_timestamp) >= DATE('now', '-{{ change_metric_timestamp_max_days_ago }} day') |
17 | | - GROUP BY metric_timestamp, metric_batch, metric_name |
| 7 | +with |
| 8 | + |
| 9 | +metric_value_data as |
| 10 | +( |
| 11 | +select distinct |
| 12 | + metric_timestamp, |
| 13 | + metric_batch, |
| 14 | + metric_name, |
| 15 | + avg(metric_value) as metric_value |
| 16 | +from |
| 17 | + {{ table_key }} |
| 18 | +where |
| 19 | + metric_batch = '{{ metric_batch }}' |
| 20 | + and |
| 21 | + metric_type = 'metric' |
| 22 | + and |
| 23 | + -- Filter to the last {{ change_metric_timestamp_max_days_ago }} days |
| 24 | + date(metric_timestamp) >= date('now', '-{{ change_metric_timestamp_max_days_ago }} day') |
| 25 | +group by |
| 26 | + metric_timestamp, metric_batch, metric_name |
| 27 | +), |
| 28 | + |
| 29 | +metric_change_alert_data as |
| 30 | +( |
| 31 | +select distinct |
| 32 | + metric_timestamp, |
| 33 | + metric_batch, |
| 34 | + metric_name, |
| 35 | + max(metric_value) as metric_change_alert |
| 36 | +from |
| 37 | + {{ table_key }} |
| 38 | +where |
| 39 | + metric_batch = '{{ metric_batch }}' |
| 40 | + and |
| 41 | + metric_type = 'change' |
| 42 | + and |
| 43 | + -- Filter to the last {{ change_metric_timestamp_max_days_ago }} days |
| 44 | + date(metric_timestamp) >= date('now', '-{{ change_metric_timestamp_max_days_ago }} day') |
| 45 | +group by |
| 46 | + metric_timestamp, metric_batch, metric_name |
| 47 | +), |
| 48 | + |
| 49 | +metric_value_recency_ranked as |
| 50 | +( |
| 51 | +select distinct |
| 52 | + metric_value_data.metric_timestamp, |
| 53 | + metric_value_data.metric_batch, |
| 54 | + metric_value_data.metric_name, |
| 55 | + metric_value_data.metric_value, |
| 56 | + -- If change alert not found found for the metric, default to 0 |
| 57 | + coalesce(metric_change_alert_data.metric_change_alert, 0) as metric_change_alert, |
| 58 | + -- Rank the metric values by recency, with 1 being the most recent |
| 59 | + row_number() over (partition by metric_value_data.metric_name order by metric_value_data.metric_timestamp desc) as metric_value_recency_rank |
| 60 | +from |
| 61 | + metric_value_data |
| 62 | +left outer join |
| 63 | + metric_change_alert_data |
| 64 | +on |
| 65 | + metric_value_data.metric_batch = metric_change_alert_data.metric_batch |
| 66 | + and |
| 67 | + metric_value_data.metric_name = metric_change_alert_data.metric_name |
| 68 | + and |
| 69 | + metric_value_data.metric_timestamp = metric_change_alert_data.metric_timestamp |
18 | 70 | ), |
19 | 71 |
|
20 | | -metric_value_recency_ranked AS ( |
21 | | - SELECT DISTINCT |
22 | | - metric_timestamp, |
23 | | - metric_batch, |
24 | | - metric_name, |
25 | | - metric_value, |
26 | | - ROW_NUMBER() OVER (PARTITION BY metric_name ORDER BY metric_timestamp DESC) AS metric_value_recency_rank |
27 | | - FROM metric_value_data |
| 72 | +-- Snooze any metrics with change alerts in the last {{ change_snooze_n }} values |
| 73 | +snoozed_metric_names as |
| 74 | +( |
| 75 | +select distinct |
| 76 | + metric_name |
| 77 | +from |
| 78 | + metric_value_recency_ranked |
| 79 | +where |
| 80 | + -- Exclude metrics with change alerts in the last {{ change_snooze_n }} values |
| 81 | + metric_change_alert = 1 |
| 82 | + and |
| 83 | + metric_value_recency_rank <= {{ change_snooze_n }} |
28 | 84 | ), |
29 | 85 |
|
30 | | -data_smoothed AS ( |
31 | | - SELECT |
32 | | - metric_timestamp, |
33 | | - metric_batch, |
34 | | - metric_name, |
35 | | - metric_value, |
36 | | - metric_value_recency_rank, |
37 | | - -- Smooth the metric value over the last {{ change_smooth_n }} values |
38 | | - (SELECT AVG(mv.metric_value) |
39 | | - FROM metric_value_recency_ranked mv |
40 | | - WHERE mv.metric_name = mr.metric_name |
41 | | - AND mv.metric_value_recency_rank BETWEEN mr.metric_value_recency_rank - {{ change_smooth_n }} AND mr.metric_value_recency_rank) AS metric_value_smooth |
42 | | - FROM metric_value_recency_ranked mr |
43 | | - WHERE metric_value_recency_rank <= {{ change_max_n }} |
| 86 | +data_smoothed as |
| 87 | +( |
| 88 | +select |
| 89 | + metric_timestamp, |
| 90 | + metric_batch, |
| 91 | + metric_name, |
| 92 | + metric_value, |
| 93 | + metric_change_alert, |
| 94 | + metric_value_recency_rank, |
| 95 | + -- Smooth the metric value over the last {{ change_smooth_n }} values |
| 96 | + ( |
| 97 | + select |
| 98 | + avg(mv.metric_value) |
| 99 | + from |
| 100 | + metric_value_recency_ranked mv |
| 101 | + where |
| 102 | + mv.metric_name = mr.metric_name |
| 103 | + and |
| 104 | + mv.metric_value_recency_rank between mr.metric_value_recency_rank - {{ change_smooth_n }} and mr.metric_value_recency_rank |
| 105 | + ) as metric_value_smooth |
| 106 | +from |
| 107 | + metric_value_recency_ranked mr |
| 108 | +where |
| 109 | + metric_value_recency_rank <= {{ change_max_n }} |
| 110 | + and |
| 111 | + -- Exclude snoozed metrics |
| 112 | + metric_name not in (select metric_name from snoozed_metric_names) |
44 | 113 | ) |
45 | 114 |
|
46 | | -SELECT |
| 115 | +select |
47 | 116 | metric_timestamp, |
48 | 117 | metric_batch, |
49 | 118 | metric_name, |
50 | 119 | metric_value, |
51 | 120 | metric_value_smooth |
52 | | -FROM data_smoothed |
| 121 | +from |
| 122 | + data_smoothed |
53 | 123 | ; |
0 commit comments