Skip to content

Commit

Permalink
Split waters for legacy and jardiner (et/somenergia-jardiner!104)
Browse files Browse the repository at this point in the history
Merge branch 'fix/legacy-water-split' into 'main'
  • Loading branch information
polmonso committed Feb 12, 2024
2 parents c4c7e56 + 9159b68 commit cf3715e
Show file tree
Hide file tree
Showing 12 changed files with 85 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@


select
plant_uuid,
day,
sunrise_real,
sunset_real,
sunrise_generous,
sunset_generous,
solar_hours_real,
solar_hours_minimum
from {{ ref("raw_plantmonitordb_solarevent__generous") }}
left join {{ ref("raw_plantmonitor_plants") }} using(plant_id)
plantmonitor_plant.plant_uuid,
solarevent.day,
solarevent.sunrise_real,
solarevent.sunset_real,
solarevent.sunrise_generous,
solarevent.sunset_generous,
solarevent.solar_hours_real,
solarevent.solar_hours_minimum
from {{ ref("raw_plantmonitordb_solarevent__generous") }} as solarevent
left join {{ ref("raw_plantmonitor_plants") }} as plantmonitor_plant using(plant_id)
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{{ config(materialized='view') }}

SELECT
select
plant.id as plant_id,
plant.name as plant_name,
plant.codename as plant_codename,
plant.description as plant_description,
device_uuid as plant_uuid
FROM {{source('plantmonitor_legacy','plant')}}
plant.device_uuid as plant_uuid
from {{ source('plantmonitor_jardiner','plant') }} as plant
where plant.description != 'SomRenovables'
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ with sub_ir as (
solar.sunrise_generous as daylight_start,
solar.sunset_generous as daylight_end
FROM {{ref('inverterregistry_clean')}} as ir
left join {{ref('raw_plantmonitordb_solarevent__generous')}} as solar on ir.time::date = solar.day and ir.plant_id = solar.plant_id
left join {{ref('plantmonitordb_solarevent__generous')}} as solar on ir.time::date = solar.day and ir.plant_id = solar.plant_id
window w as (PARTITION BY ir.inverter_id ORDER By time ROWS BETWEEN 11 PRECEDING AND current row)
)

Expand All @@ -32,4 +32,4 @@ select
from sub_ir


{# TODO A from-to table can be made downstream if they want it #}
{# TODO A from-to table can be made downstream if they want it #}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ with sub_ir as (
solar.sunrise_generous as daylight_start,
solar.sunset_generous as daylight_end
FROM {{ref('alert_inverterregistry_clean_last_hour')}} as ir
left join {{ref('raw_plantmonitordb_solarevent__generous')}} as solar on ir.time::date = solar.day and ir.plant_id = solar.plant_id
left join {{ref('plantmonitordb_solarevent__generous')}} as solar on ir.time::date = solar.day and ir.plant_id = solar.plant_id
window w as (PARTITION BY ir.inverter_id ORDER By time ROWS BETWEEN 11 PRECEDING AND current row)
), sub_alarm as (
select
Expand Down Expand Up @@ -42,4 +42,4 @@ where (time,plant_id, inverter_name) in
from sub_alarm
group by plant_id, inverter_name
) -- get last row for each plant and deivce
order by plant_id, inverter_name desc
order by plant_id, inverter_name desc
4 changes: 2 additions & 2 deletions dbt_jardiner/models/legacy/alerts/alert_meter_zero_energy.sql
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ meterregistry_daylight as (
solar.sunset_generous as daylight_end
from meterregistry_last_readings as mr
left join {{ ref('meters_with_thresholds') }} as m on m.meter_id = mr.meter_id
left join {{ ref('raw_plantmonitordb_solarevent__generous') }} as solar on mr.time::date = solar.day and m.plant_id = solar.plant_id
left join {{ ref('plantmonitordb_solarevent__generous') }} as solar on mr.time::date = solar.day and m.plant_id = solar.plant_id
),

meter_registry_hourly_raw as (
Expand Down Expand Up @@ -117,4 +117,4 @@ select
alarm_zero_energy as is_alarmed
FROM meter_registry_group as mrg
inner join {{ ref('som_plants') }} as plants using(plant_id)
left join {{ ref('alert_meter_newest_reading') }} as nr on nr.meter_id = mrg.meter_id
left join {{ ref('alert_meter_newest_reading') }} as nr on nr.meter_id = mrg.meter_id
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ meterregistry_daylight as (
solar.sunset_generous as daylight_end
from meterregistry_last_readings as mr
left join {{ ref('meters_with_thresholds') }} as m on m.meter_id = mr.meter_id
left join {{ ref('raw_plantmonitordb_solarevent__generous') }} as solar on mr.time::date = solar.day and m.plant_id = solar.plant_id
left join {{ ref('plantmonitordb_solarevent__generous') }} as solar on mr.time::date = solar.day and m.plant_id = solar.plant_id
),

meter_registry_hourly_raw as (
Expand Down Expand Up @@ -96,4 +96,4 @@ select
END as is_alarmed
FROM meter_registry_group as mrg
inner join {{ ref('som_plants') }} as plants using(plant_id)
left join {{ ref('alert_meter_newest_reading') }} as nr on nr.meter_id = mrg.meter_id
left join {{ ref('alert_meter_newest_reading') }} as nr on nr.meter_id = mrg.meter_id
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ with
on satellite_readings.plant_id = spine.plant_id
and satellite_readings.start_hour = spine.start_hour
left join
{{ ref("raw_plantmonitordb_solarevent__generous") }} as solar_events
{{ ref("plantmonitordb_solarevent__generous") }} as solar_events
on solar_events.plant_id = spine.plant_id
and solar_events.day = spine.start_hour::date
left join
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
version: 2

sources:
- name: meteologica_legacy
schema: public
tables:
- name: forecast
- name: forecastmetadata
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
version: 2

sources:
- name: plantmonitor_legacy
schema: public
tables:
- name: solarevent
columns:
- name: id
- name: plant
- name: sunrise
- name: sunset
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{{ config(
materialized = 'view'
) }}

WITH forecast_denormalized AS (

SELECT
forecast.time AS "time",
forecastmetadata.plant AS plant_id,
forecastmetadata.forecastdate AS forecastdate,
ROUND(
forecast.percentil50 / 1000.0,
2
) AS energy_kwh
FROM
{{ source(
'meteologica_legacy',
'forecast'
) }}
LEFT JOIN {{ source(
'meteologica_legacy',
'forecastmetadata'
) }}
ON forecastmetadata.id = forecast.forecastmetadata
)
SELECT
*
FROM
forecast_denormalized
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ FROM
ORDER BY
forecastdate DESC) AS ranking
FROM
{{ ref('raw_energy_forecasts__denormalized_from_plantmonitordb') }}
{{ ref('energy_forecasts__denormalized_from_plantmonitordb') }}
) forecast
WHERE
ranking = 1
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{{ config(materialized='view') }}


select
plant as plant_id,
date_trunc('day', sunrise) as day,
sunrise as sunrise_real,
sunset as sunset_real,
sunrise + interval '2 hours' as sunrise_generous,
sunset - interval '2 hours' as sunset_generous,
round(EXTRACT(EPOCH FROM (sunset - sunrise))::numeric/3600,2) as solar_hours_real,
round(EXTRACT(EPOCH FROM (sunset - interval '2 hours') - (sunrise + interval '2 hours'))::numeric/3600,2) as solar_hours_minimum
from {{source('plantmonitor_legacy', 'solarevent')}}

0 comments on commit cf3715e

Please sign in to comment.