-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathcurrent_data_coverage.sql
More file actions
203 lines (175 loc) · 7.74 KB
/
Copy pathcurrent_data_coverage.sql
File metadata and controls
203 lines (175 loc) · 7.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
{{ config(materialized='table') }}
WITH source_specs AS (
SELECT *
FROM UNNEST([
STRUCT('sp500_companies_prices_raw' AS source_name, 'markets' AS source_domain, 'daily_market_prices' AS grain, 31 AS lookback_days, 5 AS freshness_warn_days, 10 AS freshness_error_days),
STRUCT('us_sector_etfs_raw' AS source_name, 'markets' AS source_domain, 'daily_market_prices' AS grain, 31 AS lookback_days, 5 AS freshness_warn_days, 10 AS freshness_error_days),
STRUCT('currency_etfs_raw' AS source_name, 'markets' AS source_domain, 'daily_market_prices' AS grain, 31 AS lookback_days, 5 AS freshness_warn_days, 10 AS freshness_error_days),
STRUCT('commodity_etfs_raw' AS source_name, 'markets' AS source_domain, 'daily_market_prices' AS grain, 31 AS lookback_days, 5 AS freshness_warn_days, 10 AS freshness_error_days),
STRUCT('major_indices_raw' AS source_name, 'markets' AS source_domain, 'daily_market_prices' AS grain, 31 AS lookback_days, 5 AS freshness_warn_days, 10 AS freshness_error_days),
STRUCT('fixed_income_etfs_raw' AS source_name, 'markets' AS source_domain, 'daily_market_prices' AS grain, 31 AS lookback_days, 5 AS freshness_warn_days, 10 AS freshness_error_days),
STRUCT('global_markets_raw' AS source_name, 'markets' AS source_domain, 'daily_market_prices' AS grain, 31 AS lookback_days, 5 AS freshness_warn_days, 10 AS freshness_error_days),
STRUCT('energy_commodities_raw' AS source_name, 'commodities' AS source_domain, 'daily_commodity_prices' AS grain, 31 AS lookback_days, 5 AS freshness_warn_days, 10 AS freshness_error_days),
STRUCT('input_commodities_raw' AS source_name, 'commodities' AS source_domain, 'daily_commodity_prices' AS grain, 31 AS lookback_days, 5 AS freshness_warn_days, 10 AS freshness_error_days),
STRUCT('agriculture_commodities_raw' AS source_name, 'commodities' AS source_domain, 'daily_commodity_prices' AS grain, 31 AS lookback_days, 5 AS freshness_warn_days, 10 AS freshness_error_days),
STRUCT('fred_raw' AS source_name, 'government' AS source_domain, 'economic_series' AS grain, 93 AS lookback_days, 45 AS freshness_warn_days, 75 AS freshness_error_days)
])
),
raw_source_observations AS (
SELECT
'sp500_companies_prices_raw' AS source_name,
symbol AS entity_id,
date AS observation_date
FROM {{ ref('stg_sp500_companies_prices') }}
WHERE symbol IS NOT NULL AND date IS NOT NULL
UNION ALL
SELECT
'us_sector_etfs_raw' AS source_name,
symbol AS entity_id,
date AS observation_date
FROM {{ ref('stg_us_sectors') }}
WHERE symbol IS NOT NULL AND date IS NOT NULL
UNION ALL
SELECT
'currency_etfs_raw' AS source_name,
symbol AS entity_id,
date AS observation_date
FROM {{ ref('stg_currency') }}
WHERE symbol IS NOT NULL AND date IS NOT NULL
UNION ALL
SELECT
'commodity_etfs_raw' AS source_name,
symbol AS entity_id,
date AS observation_date
FROM {{ ref('stg_commodity_etfs') }}
WHERE symbol IS NOT NULL AND date IS NOT NULL
UNION ALL
SELECT
'major_indices_raw' AS source_name,
symbol AS entity_id,
date AS observation_date
FROM {{ ref('stg_major_indices') }}
WHERE symbol IS NOT NULL AND date IS NOT NULL
UNION ALL
SELECT
'fixed_income_etfs_raw' AS source_name,
symbol AS entity_id,
date AS observation_date
FROM {{ ref('stg_fixed_income') }}
WHERE symbol IS NOT NULL AND date IS NOT NULL
UNION ALL
SELECT
'global_markets_raw' AS source_name,
symbol AS entity_id,
date AS observation_date
FROM {{ ref('stg_global_markets') }}
WHERE symbol IS NOT NULL AND date IS NOT NULL
UNION ALL
SELECT
'energy_commodities_raw' AS source_name,
CONCAT(commodity_name, ':', commodity_unit) AS entity_id,
date AS observation_date
FROM {{ ref('stg_energy_commodities') }}
WHERE commodity_name IS NOT NULL AND date IS NOT NULL
UNION ALL
SELECT
'input_commodities_raw' AS source_name,
CONCAT(commodity_name, ':', commodity_unit) AS entity_id,
date AS observation_date
FROM {{ ref('stg_input_commodities') }}
WHERE commodity_name IS NOT NULL AND date IS NOT NULL
UNION ALL
SELECT
'agriculture_commodities_raw' AS source_name,
CONCAT(commodity_name, ':', commodity_unit) AS entity_id,
date AS observation_date
FROM {{ ref('stg_agriculture_commodities') }}
WHERE commodity_name IS NOT NULL AND date IS NOT NULL
UNION ALL
SELECT
'fred_raw' AS source_name,
series_code AS entity_id,
date AS observation_date
FROM {{ ref('stg_fred_series') }}
WHERE series_code IS NOT NULL AND date IS NOT NULL
),
source_observations AS (
SELECT
source_name,
entity_id,
observation_date
FROM raw_source_observations
WHERE observation_date <= CURRENT_DATE()
),
expected_entities AS (
SELECT
source_name,
COUNT(DISTINCT entity_id) AS expected_entity_count
FROM source_observations
GROUP BY source_name
),
latest_observations AS (
SELECT
source_name,
MAX(observation_date) AS coverage_date
FROM source_observations
GROUP BY source_name
),
windowed_observations AS (
SELECT
observations.source_name,
observations.entity_id,
observations.observation_date
FROM source_observations AS observations
INNER JOIN latest_observations AS latest
ON observations.source_name = latest.source_name
INNER JOIN source_specs AS specs
ON observations.source_name = specs.source_name
WHERE observations.observation_date >= DATE_SUB(
latest.coverage_date,
INTERVAL specs.lookback_days DAY
)
),
coverage_counts AS (
SELECT
source_name,
COUNT(*) AS observed_row_count,
COUNT(DISTINCT entity_id) AS observed_entity_count
FROM windowed_observations
GROUP BY source_name
)
SELECT
CONCAT(specs.source_name, ':', CAST(latest.coverage_date AS STRING)) AS coverage_id,
specs.source_name,
specs.source_domain,
specs.grain,
latest.coverage_date,
DATE_SUB(latest.coverage_date, INTERVAL specs.lookback_days DAY) AS coverage_window_start,
specs.lookback_days,
expected.expected_entity_count,
COALESCE(counts.observed_entity_count, 0) AS observed_entity_count,
expected.expected_entity_count - COALESCE(counts.observed_entity_count, 0) AS missing_entity_count,
COALESCE(counts.observed_row_count, 0) AS observed_row_count,
SAFE_DIVIDE(
COALESCE(counts.observed_entity_count, 0),
NULLIF(expected.expected_entity_count, 0)
) AS coverage_pct,
DATE_DIFF(CURRENT_DATE(), latest.coverage_date, DAY) AS freshness_lag_days,
specs.freshness_warn_days,
specs.freshness_error_days,
CASE
WHEN expected.expected_entity_count = 0 THEN 'no_expected_entities'
WHEN DATE_DIFF(CURRENT_DATE(), latest.coverage_date, DAY) > specs.freshness_error_days THEN 'stale'
WHEN SAFE_DIVIDE(COALESCE(counts.observed_entity_count, 0), NULLIF(expected.expected_entity_count, 0)) < 0.80 THEN 'coverage_gap'
WHEN DATE_DIFF(CURRENT_DATE(), latest.coverage_date, DAY) > specs.freshness_warn_days THEN 'lagging'
WHEN SAFE_DIVIDE(COALESCE(counts.observed_entity_count, 0), NULLIF(expected.expected_entity_count, 0)) < 0.98 THEN 'partial'
ELSE 'healthy'
END AS coverage_status,
CURRENT_TIMESTAMP() AS generated_at
FROM source_specs AS specs
LEFT JOIN latest_observations AS latest
ON specs.source_name = latest.source_name
LEFT JOIN expected_entities AS expected
ON specs.source_name = expected.source_name
LEFT JOIN coverage_counts AS counts
ON specs.source_name = counts.source_name