Skip to content

Commit

Permalink
Chore/sqlfluffing (et/somenergia-jardiner!141)
Browse files Browse the repository at this point in the history
Merge branch 'chore/sqlfluffing' into 'main'
  • Loading branch information
polmonso committed Apr 2, 2024
2 parents 7c153b0 + 578eb9f commit 2303ce3
Show file tree
Hide file tree
Showing 171 changed files with 2,858 additions and 2,074 deletions.
3 changes: 3 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[flake8]
max-line-length = 95
max-complexity = 10
82 changes: 51 additions & 31 deletions dbt_jardiner/analyses/dset_gaps_preview_20240110.sql
Original file line number Diff line number Diff line change
@@ -1,39 +1,59 @@
with
som_uuids as (
select distinct group_name, signal_code, signal_uuid
from {{ ref("int_dset_responses__materialized") }}
where ts >= '2024-01-10' and ts < '2024-01-11'
order by signal_uuid desc),
som_uuids as (
select distinct
group_name,
signal_code,
signal_uuid
from {{ ref("int_dset_responses__materialized") }}
where ts >= '2024-01-10' and ts < '2024-01-11'
order by signal_uuid desc
),

dset_uuids as (
select a.gru_codi, a.gru_nom, a.sen_codi, a.sen_descripcio, a.esperats_frequencia, a.trobats_senyal, b.signal_uuid
from analytics.se_forats_hornsby_dades_dia_10 as a
left join som_uuids as b
on a.sen_codi = b.signal_code
and a.gru_nom = b.group_name),
dset_uuids as (
select
a.gru_codi,
a.gru_nom,
a.sen_codi,
a.sen_descripcio,
a.esperats_frequencia,
a.trobats_senyal,
b.signal_uuid
from analytics.se_forats_hornsby_dades_dia_10 as a
left join som_uuids as b
on
a.sen_codi = b.signal_code
and a.gru_nom = b.group_name
),

som_count as (
select
signal_uuid, count(*) as cnt
from {{ ref("int_dset_responses__materialized") }}
where ts >= '2024-01-10' and ts < '2024-01-11'
and signal_value is not null
group by signal_uuid
order by signal_uuid desc, cnt desc),
som_count as (
select
signal_uuid,
count(*) as cnt
from {{ ref("int_dset_responses__materialized") }}
where
ts >= '2024-01-10' and ts < '2024-01-11'
and signal_value is not null
group by signal_uuid
order by signal_uuid desc, cnt desc
),

summary as (
select b.*, a.cnt as som_trobats_senyal from som_count as a
left join dset_uuids as b
on a.signal_uuid = b.signal_uuid
order by b.trobats_senyal desc, a.cnt desc
),
summary as (
select
b.*,
a.cnt as som_trobats_senyal
from som_count as a
left join dset_uuids as b
on a.signal_uuid = b.signal_uuid
order by b.trobats_senyal desc, a.cnt desc
),

final as (
select *,
esperats_frequencia - trobats_senyal as n_forats_dset,
288 - som_trobats_senyal as n_forats_som
from summary
)
final as (
select
*,
esperats_frequencia - trobats_senyal as n_forats_dset,
288 - som_trobats_senyal as n_forats_som
from summary
)

select * from final
order by gru_codi, gru_nom, sen_codi
79 changes: 40 additions & 39 deletions dbt_jardiner/analyses/plants_description.sql
Original file line number Diff line number Diff line change
@@ -1,40 +1,41 @@
-- to generate the seed for plants_descriptions.csv
SELECT
p.id AS plant_id,
p.name AS plant_name,
p2.latitude AS latitude,
p2.longitude AS longitude,
p3.connection_date AS connection_date,
FALSE AS is_tilted,
p3.peak_power_w / 1000 AS peak_power_kw,
p3.nominal_power_w / 1000 AS nominal_power_kw,
CASE
WHEN p.name = 'Torregrossa' THEN 'GAS'
WHEN p.name = 'Valteina' THEN 'HIDRO'
ELSE 'FV'
END AS technology,
p3.target_monthly_energy_wh / 100 AS target_monthly_energy_gwh,
CASE
WHEN
p.name IN ('Florida', 'Alcolea', 'Matallana') THEN 'energes'
WHEN
p.name IN ('Valteina') THEN 'energetica'
WHEN
p.name IN ('Fontivsolar') THEN 'ercam'
WHEN
p.name IN ('Llanillos', 'Asomada') THEN 'exiom'
END AS propietari
FROM
public.plant AS p
LEFT JOIN
public.plantparameters AS p3
ON
p.id = p3.plant
LEFT JOIN
public.plantlocation AS p2
ON
p.id = p2.plant
WHERE
"description" != 'SomRenovables'
ORDER BY
p.id;
select
p.id as plant_id,
p.name as plant_name,
p2.latitude as latitude,
p2.longitude as longitude,
p3.connection_date as connection_date,
false as is_tilted,
p3.peak_power_w / 1000 as peak_power_kw,
p3.nominal_power_w / 1000 as nominal_power_kw,
case
when p.name = 'Torregrossa' then 'GAS'
when p.name = 'Valteina' then 'HIDRO'
else 'FV'
end as technology,
p3.target_monthly_energy_wh / 100 as target_monthly_energy_gwh,
case
when
p.name in ('Florida', 'Alcolea', 'Matallana') then 'energes'
when
p.name in ('Valteina') then 'energetica'
when
p.name in ('Fontivsolar') then 'ercam'
when
p.name in ('Llanillos', 'Asomada') then 'exiom'
end as propietari
from
public.plant as p
left join
public.plantparameters as p3
on
p.id = p3.plant
left join
public.plantlocation as p2
on
p.id = p2.plant
where
plant.description != 'SomRenovables'
order by
p.id
;
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@


with ts_last_registry as (
select signal_uuid, plant_uuid, max(ts) as ultim_registre
from {{ ref('int_dset_responses__values_incremental') }}
group by plant_uuid, signal_uuid
select
signal_uuid,
plant_uuid,
max(ts) as ultim_registre
from {{ ref('int_dset_responses__values_incremental') }}
group by plant_uuid, signal_uuid
)

select m.*
from ts_last_registry as tslr
left join {{ ref('int_dset_responses__values_incremental') }} as m
left join {{ ref('int_dset_responses__values_incremental') }} as m
on tslr.signal_uuid = m.signal_uuid and tslr.ultim_registre = m.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ with instant_energy as (
and metric_name = 'energia_activa_exportada_instantania'
and signal_value is not null
),

instant_energy_delta as (
select
ts,
Expand All @@ -23,26 +24,31 @@ instant_energy_delta as (
lead(ts) over plant_window as previous_ts,
lead(meter_energy_kwh) over plant_window as previous_energy,
ts - lead(ts) over plant_window as delta_ts,
meter_energy_kwh - lead(meter_energy_kwh) over plant_window as delta_meter_energy_kwh
meter_energy_kwh
- lead(meter_energy_kwh) over plant_window as delta_meter_energy_kwh
from instant_energy
window plant_window as (partition by plant_uuid order by ts desc)
order by ts desc
),

inferred_meter_power as (
select
ts,
plant_name,
plant_uuid,
meter_energy_kwh,
60 / extract(minute from delta_ts) * delta_meter_energy_kwh as inferred_meter_power_kw,
previous_energy,
delta_meter_energy_kwh,
previous_ts,
delta_ts,
60
/ extract(minute from delta_ts)
* delta_meter_energy_kwh as inferred_meter_power_kw,
60 / extract(minute from delta_ts) as dt_hour_normalized
from instant_energy_delta
order by ts desc
)

select
plant_uuid,
ts,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,25 @@
{{ config(materialized="view") }}

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')
)
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
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
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
with quarterhourly_spine as (
select generate_series('2023-12-01', now(), '15 minutes') as start_ts
),

meter_metadata as (
select
metadata.plant_uuid::uuid,
Expand All @@ -22,6 +23,7 @@ meter_metadata as (
and metadata.is_enabled
and metadata.metric_name ilike 'energia_%'
),

raw_meter_readings as (
select
meter_readings.start_ts,
Expand All @@ -33,11 +35,12 @@ raw_meter_readings as (
from {{ ref("raw_dset_meter__readings") }} as meter_readings
where meter_readings.signal_uuid is not null
),

meter_readings_with_metadata as (
select
meter_metadata.*,
quarterhourly_spine.start_ts,
raw_meter_readings.end_ts,
meter_metadata.*,
raw_meter_readings.signal_value,
raw_meter_readings.signal_unit,
raw_meter_readings.queried_at,
Expand All @@ -46,4 +49,5 @@ meter_readings_with_metadata as (
left join meter_metadata on true
left join raw_meter_readings using (signal_uuid, start_ts)
)

select * from meter_readings_with_metadata
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

with meter_readings_hourly as (
select
date_trunc('hour', start_ts) as start_hour,
plant_uuid,
plant_name,
date_trunc('hour', start_ts) as start_hour,
sum(meter_exported_energy) as meter_exported_energy,
sum(meter_imported_energy) as meter_imported_energy,
sum(meter_reactive_energy_q1) as meter_reactive_energy_q1,
Expand All @@ -16,4 +16,5 @@ with meter_readings_hourly as (
group by date_trunc('hour', start_ts), plant_uuid, plant_name
order by date_trunc('hour', start_ts) desc
)

select * from meter_readings_hourly
Loading

0 comments on commit 2303ce3

Please sign in to comment.