Skip to content

Commit ca166e4

Browse files
authored
Create irrigation events (#210)
* create irrigation events * add schema
1 parent 53e3a0e commit ca166e4

1 file changed

Lines changed: 19 additions & 25 deletions

File tree

amiadapters/storage/snowflake.py

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -676,32 +676,26 @@ def exec_postprocessor(self, run_id: str, min_date: datetime, max_date: datetime
676676
ami_irrigation_detection_agg_sql = f"""
677677
create or replace table irrigation_detection_agg
678678
as
679-
select
680-
meter_id
679+
with cte as
680+
(
681+
select *
682+
, conditional_change_event(coalesce(irrigation_reading, 0) = 0) over (partition by source, device_id order by timestamp) as grp
683+
from (select distinct * from wavelet.global_irrigation_detection)
684+
)
685+
select
686+
source
687+
, device_id
688+
, min(timestamp) as event_start_ts
689+
, max(timestamp) as event_end_ts
690+
, datediff(hour, event_start_ts, event_end_ts) + 1 as event_hrs
691+
, max(irrigation_reading > 0) is_irrigation
692+
, to_varchar(event_start_ts, 'yyyy/mm/dd@hh - ') || event_hrs || iff(is_irrigation, 'h', 'h*') as event_id
681693
, array_agg(timestamp) within group (order by timestamp) as timestamp
682-
, array_agg(total_volume) within group (order by timestamp) as total_volume
683-
, array_agg(irrigation_volume) within group (order by timestamp) as irrigation_volume
684-
, array_agg(non_irrigation_volume) within group (order by timestamp) as non_irrigation_volume
685-
--
686-
, array_agg(irrigation_flag) within group (order by timestamp) as irrigation_flag
687-
, array_agg(daily_irrigation_detected) within group (order by timestamp) as daily_irrigation_detected
688-
--
689-
, array_agg(daily_confidence) within group (order by timestamp) as daily_confidence
690-
, array_agg(hourly_confidence) within group (order by timestamp) as hourly_confidence
691-
--
692-
, model_used
693-
, meter_baseline_days
694-
, meter_normal_days
695-
, meter_total_training_days
696-
, meter_needs_finetuning
697-
, meter_has_custom_model
698-
, district_source
699-
, meter_type
700-
from
701-
irrigation_detection
702-
where
703-
1=1
704-
group by all
694+
, array_agg(total_reading) within group (order by timestamp) as total_reading_cf
695+
, array_agg(irrigation_reading) within group (order by timestamp) as irrigation_reading_cf
696+
, array_agg(non_irrigation_reading) within group (order by timestamp) as non_irrigation_reading_cf
697+
from cte
698+
group by source, device_id, grp
705699
"""
706700
conn.cursor().execute(ami_irrigation_detection_agg_sql)
707701

0 commit comments

Comments
 (0)