diff --git a/dbt_jardiner/models/jardiner/marts/_exposures.yaml b/dbt_jardiner/models/jardiner/marts/_exposures.yaml index 7358cce2..02dd0c3f 100644 --- a/dbt_jardiner/models/jardiner/marts/_exposures.yaml +++ b/dbt_jardiner/models/jardiner/marts/_exposures.yaml @@ -13,7 +13,8 @@ exposures: En un futur també mostrarà l'estat d'alarmes. depends_on: - - ref('dm_dset_energy_inverter__daily') + - ref('dm_dset_energy_inverter__daily__accumulated') + - ref('dm_dset_energy_inverter__daily__reset_daily') - ref('dm_dashboard_overview_monthly') - ref('dm_dashboard_overview_instant') diff --git a/dbt_jardiner/models/jardiner/marts/_marts__models.yaml b/dbt_jardiner/models/jardiner/marts/_marts__models.yaml index 5ec1d1f0..ecc42b01 100644 --- a/dbt_jardiner/models/jardiner/marts/_marts__models.yaml +++ b/dbt_jardiner/models/jardiner/marts/_marts__models.yaml @@ -137,11 +137,17 @@ models: config: tags: [dset] description: > - Mínima granularitat de l'energia dels inversors. És acumulada amb resets diaris, - per tant per granularitats superiors o iguals a dia feu servir `dm_dset_energy_inverter__daily`. + Mínima granularitat de l'energia dels inversors. Conté l'energia acumulada i la que es reseteja de matinada. + Per granularitats superiors o iguals a dia feu servir `dm_dset_energy_inverter__daily`. - - name: dm_dset_energy_inverter__daily + - name: dm_dset_energy_inverter__daily__reset_daily config: tags: [dset] description: > Agregació diaria de l'energia d'inversor, que fa un max del dia (Europe/Madrid). Recordeu que hem descartat les hores de 0 a 3 perquè és en el rang de temps que solen haver-hi resets. + + - name: dm_dset_energy_inverter__daily__accumulated + config: + tags: [dset] + description: > + Agregació diaria de l'energia d'inversor, que fa un max del dia (Europe/Madrid) i li resta el mínim. diff --git a/dbt_jardiner/models/jardiner/marts/dm_dset_energy_inverter__5m.sql b/dbt_jardiner/models/jardiner/marts/dm_dset_energy_inverter__5m.sql index a5354cc8..7e26e230 100644 --- a/dbt_jardiner/models/jardiner/marts/dm_dset_energy_inverter__5m.sql +++ b/dbt_jardiner/models/jardiner/marts/dm_dset_energy_inverter__5m.sql @@ -1,23 +1,26 @@ -{{ config(materialized='view') }} +{{ config(materialized="view") }} -with inverters_energy as ( - select - ts, - plant_uuid, - plant_name, - device_uuid, - device_name, - metric_name, - signal_unit, - case - when signal_unit = 'MWh' then signal_value*1000 - when signal_unit = 'kWh' then signal_value - else NULL - end as inverter_energy_kwh - from {{ ref('int_dset_responses__values_incremental') }} - where device_type in ('inverter') and metric_name = 'energia_activa_exportada' - {# filter to discard previous day data which gets reset early morning #} - and extract(hour from ts at time zone 'Europe/Madrid') > 3 -) -select * from inverters_energy +with + inverters_energy as ( + select + ts, + plant_uuid, + plant_name, + device_uuid, + device_name, + metric_name, + signal_uuid, + case + when signal_unit = 'MWh' then signal_value * 1000 + when signal_unit = 'kWh' then signal_value + else null + end as inverter_energy_kwh + from {{ ref("int_dset_responses__values_incremental") }} + where + device_type in ('inverter') + and metric_name + in ('energia_activa_exportada', 'energia_activa_exportada_total') + ) +select * +from inverters_energy order by ts desc diff --git a/dbt_jardiner/models/jardiner/marts/dm_dset_energy_inverter__daily.sql b/dbt_jardiner/models/jardiner/marts/dm_dset_energy_inverter__daily.sql deleted file mode 100644 index bf8600c8..00000000 --- a/dbt_jardiner/models/jardiner/marts/dm_dset_energy_inverter__daily.sql +++ /dev/null @@ -1,15 +0,0 @@ -{{ config(materialized='view') }} - -with inverter_energy_daily as ( - select - date_trunc('day', ts, 'Europe/Madrid') as day, - plant_uuid, - plant_name, - device_uuid, - device_name, - max(inverter_energy_kwh) as inverter_energy_kwh - from {{ ref("dm_dset_energy_inverter__5m") }} - group by date_trunc('day', ts, 'Europe/Madrid'), plant_uuid, plant_name, device_uuid, device_name -) -select * from inverter_energy_daily - diff --git a/dbt_jardiner/models/jardiner/marts/dm_dset_energy_inverter__daily__accumulated.sql b/dbt_jardiner/models/jardiner/marts/dm_dset_energy_inverter__daily__accumulated.sql new file mode 100644 index 00000000..bcd430b9 --- /dev/null +++ b/dbt_jardiner/models/jardiner/marts/dm_dset_energy_inverter__daily__accumulated.sql @@ -0,0 +1,32 @@ +{{ config(materialized="view") }} + +with + inverter_energy_daily_metrics as ( + select + date_trunc('day', ts, 'Europe/Madrid') as day, + plant_uuid, + plant_name, + device_uuid, + device_name, + metric_name, + max(inverter_energy_kwh) as max_inverter_energy_kwh, + min(inverter_energy_kwh) as min_inverter_energy_kwh, + max(inverter_energy_kwh) - min(inverter_energy_kwh) as diff_inverter_energy_kwh, + max(inverter_energy_kwh) - min(nullif(inverter_energy_kwh, 0)) as diff_nozero_inverter_energy_kwh + from {{ ref("dm_dset_energy_inverter__5m") }} + where metric_name = 'energia_activa_exportada_total' + group by + date_trunc('day', ts, 'Europe/Madrid'), + plant_uuid, + plant_name, + device_uuid, + device_name, + metric_name + ), + inverter_energy_daily as ( + select *, diff_inverter_energy_kwh as inverter_energy_kwh + from inverter_energy_daily_metrics + order by day desc, plant_name, device_name + ) +select * +from inverter_energy_daily diff --git a/dbt_jardiner/models/jardiner/marts/dm_dset_energy_inverter__daily__reset_daily.sql b/dbt_jardiner/models/jardiner/marts/dm_dset_energy_inverter__daily__reset_daily.sql new file mode 100644 index 00000000..31c710e0 --- /dev/null +++ b/dbt_jardiner/models/jardiner/marts/dm_dset_energy_inverter__daily__reset_daily.sql @@ -0,0 +1,35 @@ +{{ config(materialized="view") }} + +with + inverter_energy_daily_metrics as ( + select + date_trunc('day', ts, 'Europe/Madrid') as day, + plant_uuid, + plant_name, + device_uuid, + device_name, + metric_name, + max(inverter_energy_kwh) as max_inverter_energy_kwh, + min(inverter_energy_kwh) as min_inverter_energy_kwh, + max(inverter_energy_kwh) - min(inverter_energy_kwh) as diff_inverter_energy_kwh, + max(inverter_energy_kwh) - min(nullif(inverter_energy_kwh, 0)) as diff_nozero_inverter_energy_kwh + from {{ ref("dm_dset_energy_inverter__5m") }} + where + metric_name = 'energia_activa_exportada' + {# filter to discard previous day data which gets reset early morning #} + and extract(hour from ts at time zone 'Europe/Madrid') > 3 + group by + date_trunc('day', ts, 'Europe/Madrid'), + plant_uuid, + plant_name, + device_uuid, + device_name, + metric_name + ), + inverter_energy_daily as ( + select *, diff_inverter_energy_kwh as inverter_energy_kwh + from inverter_energy_daily_metrics + order by day desc, plant_name, device_name + ) +select * +from inverter_energy_daily diff --git a/dbt_jardiner/models/jardiner/raw/gestio_actius/_raw_gestio_actius__models.yaml b/dbt_jardiner/models/jardiner/raw/gestio_actius/_raw_gestio_actius__models.yaml index 8fabb93e..21d64c68 100644 --- a/dbt_jardiner/models/jardiner/raw/gestio_actius/_raw_gestio_actius__models.yaml +++ b/dbt_jardiner/models/jardiner/raw/gestio_actius/_raw_gestio_actius__models.yaml @@ -44,6 +44,7 @@ models: values: - "comunicacio_ok" - "energia_activa_exportada" + - "energia_activa_exportada_total" - "energia_activa_importada" - "energia_reactiva_q1" - "energia_reactiva_q2"