/ eu-air-traffic / dbt / models / reports / data_freshness.sql
swadhinbiswas Rebuild around VPS collector + Kafka + MotherDuck-direct site · 9/11/2026
03cdeca
36 lines · 1.4 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
{{ config(materialized='view', tags=['reports']) }}

with flights_freshness as (
    select
        'fact_flights' as table_name,
        max(cast(ingestion_date as date)) as last_loaded,
        now() - max(cast(ingestion_date as timestamp)) as staleness_interval,
        case
            when now() - max(cast(ingestion_date as timestamp)) > interval '24 hours' then 'STALE'
            when now() - max(cast(ingestion_date as timestamp)) > interval '12 hours' then 'WARNING'
            else 'FRESH'
        end as freshness_status
    from {{ ref('stg_fact_flights') }}
    -- Only report tables that actually hold data; an empty fact must not yield
    -- a NULL freshness row.
    having max(cast(ingestion_date as timestamp)) is not null
),

weather_freshness as (
    select
        'weather' as table_name,
        max(cast(ingestion_date as date)) as last_loaded,
        now() - max(cast(ingestion_date as timestamp)) as staleness_interval,
        case
            when now() - max(cast(ingestion_date as timestamp)) > interval '24 hours' then 'STALE'
            when now() - max(cast(ingestion_date as timestamp)) > interval '12 hours' then 'WARNING'
            else 'FRESH'
        end as freshness_status
    from {{ ref('stg_weather') }}
    having max(cast(ingestion_date as timestamp)) is not null
)

select * from flights_freshness
union all
select * from weather_freshness