-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: build models matching dset gaps analysis (et/somenergia-jard…
…iner!89) Merge branch 'feature/build-gaps-models-matching-dset' into 'main'
- Loading branch information
Showing
18 changed files
with
172 additions
and
192 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
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), | ||
|
||
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), | ||
|
||
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 | ||
) | ||
|
||
select * from final | ||
order by gru_codi, gru_nom, sen_codi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
88 changes: 88 additions & 0 deletions
88
dbt_jardiner/tests/dset/test_dset_gaps_per_day_and_signal_last_month.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
{{ config(severity="warn") }} | ||
|
||
with | ||
window_observed as ( | ||
select | ||
signal_value, | ||
group_name, | ||
signal_code, | ||
signal_id, | ||
signal_device_type, | ||
signal_uuid, | ||
queried_at, | ||
ts as current_ts, | ||
signal_frequency::interval as signal_frequency | ||
from {{ ref("int_dset_responses__materialized") }} | ||
where current_date - interval '1 month' < ts | ||
and signal_value is not null | ||
), | ||
|
||
window_lagged as ( | ||
select | ||
*, | ||
lag(current_ts) over ( | ||
partition by signal_uuid | ||
order by current_ts asc) as previous_ts | ||
from window_observed | ||
), | ||
|
||
gaps_observed as ( | ||
select | ||
*, | ||
current_ts::date as "date", | ||
current_ts - previous_ts as gap | ||
from window_lagged | ||
where current_ts - previous_ts > signal_frequency | ||
), | ||
|
||
gaps_summarized as ( | ||
select | ||
"date", | ||
group_name, | ||
signal_code, | ||
signal_id, | ||
signal_device_type, | ||
signal_uuid, | ||
gap, | ||
signal_frequency, | ||
count(signal_uuid) as n_gaps | ||
from gaps_observed | ||
group by | ||
"date", | ||
group_name, | ||
signal_uuid, | ||
signal_code, | ||
signal_id, | ||
signal_device_type, | ||
gap, | ||
signal_frequency | ||
order by | ||
"date" desc, | ||
count(signal_uuid) desc, | ||
gap desc, | ||
group_name asc, | ||
signal_code asc, | ||
signal_id asc, | ||
signal_device_type asc | ||
), | ||
|
||
gaps_converted_to_n_missing_samples as ( | ||
select | ||
*, | ||
-- n_gaps * ceiling(gap/frequency - 1) as n_missing_samples. | ||
-- The -1 is because the starting point in the gap can't be counted as missing | ||
n_gaps * ceil(extract(epoch from gap) / extract(epoch from signal_frequency) - 1) as n_missing_samples, | ||
-- 24*60 are the minutes in a day | ||
(24 * 60) / (extract(epoch from signal_frequency) / 60) as n_samples_per_day | ||
from gaps_summarized | ||
), | ||
|
||
gaps_ratio as ( | ||
select | ||
*, | ||
n_missing_samples / n_samples_per_day as ratio_missing_samples | ||
from gaps_converted_to_n_missing_samples | ||
) | ||
|
||
select * | ||
from gaps_ratio |
File renamed without changes.
File renamed without changes.
File renamed without changes.
38 changes: 38 additions & 0 deletions
38
dbt_jardiner/tests/dset/test_dset_signals_receiver_last_hour.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
{{ config(error_if=">500") }} | ||
{# error limit is set on half the number of signal uuids available #} | ||
|
||
with | ||
uuids_received_recently as ( | ||
select | ||
signal_uuid, | ||
true as is_received_recently, | ||
max(ts) as last_received_ts | ||
from {{ ref("int_dset_responses__materialized") }} | ||
where ts >= (now() - interval '2 hours') | ||
group by signal_uuid | ||
{# interval used of two hours is depending on the natural delay of dset data + materialization cycle -#} | ||
), | ||
|
||
uuids_expected as ( | ||
select | ||
s.plant_uuid, | ||
s.plant_name, | ||
s.signal_name, | ||
s.signal_uuid, | ||
s.device_name, | ||
s.device_type, | ||
s.device_uuid, | ||
coalesce(r.is_received_recently, false) as received_from_dset | ||
from {{ ref("raw_gestio_actius__signal_denormalized") }} as s | ||
left join uuids_received_recently as r | ||
on s.signal_uuid = r.signal_uuid | ||
order by s.plant_name | ||
), | ||
|
||
uuids_not_received as ( | ||
select * | ||
from uuids_expected | ||
where received_from_dset is false | ||
) | ||
|
||
select * from uuids_not_received |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
77 changes: 0 additions & 77 deletions
77
dbt_jardiner/tests/test_dset_gaps_per_month_and_signal_all_time.sql
This file was deleted.
Oops, something went wrong.
77 changes: 0 additions & 77 deletions
77
dbt_jardiner/tests/test_dset_gaps_per_month_and_signal_last_hour.sql
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.