diff --git a/services/IBF-pipeline/pipeline/lib/cronJob/processDynamicDataPostgresTrigger.sql b/services/IBF-pipeline/pipeline/lib/cronJob/processDynamicDataPostgresTrigger.sql index 1ab6d90a8..22348694b 100644 --- a/services/IBF-pipeline/pipeline/lib/cronJob/processDynamicDataPostgresTrigger.sql +++ b/services/IBF-pipeline/pipeline/lib/cronJob/processDynamicDataPostgresTrigger.sql @@ -98,33 +98,3 @@ ON (t1.lead_time = '7-day' and t0."station_code_7day" = t1.station_code) OR (t1. where t1.lead_time is not null ; --select * from "IBF-pipeline-output".dashboard_forecast_per_district where country_code = 'KEN' and fc_trigger = 1 - ---truncate table "IBF-pipeline-output".events --- NOTE: Determine events and save in events-table -update "IBF-pipeline-output".events set end_date = subquery.date -from ( - select today.country_code, to_date(today.date,'yyyy-mm-dd') - 1 as date - from "IBF-pipeline-output".triggers_per_day today - left join "IBF-pipeline-output".triggers_per_day yesterday - on today.country_code = yesterday.country_code - and to_date(today.date,'yyyy-mm-dd') = to_date(yesterday.date,'yyyy-mm-dd') + 1 - where 1=1 - and to_date(today.date,'yyyy-mm-dd') = current_date - and ((today."7" = 0 and yesterday."7" = 1) or (yesterday."7" is null)) -) subquery -where end_date is null and events.country_code = subquery.country_code -; -insert into "IBF-pipeline-output".events -select today.country_code - ,today.date - ,null -from "IBF-pipeline-output".triggers_per_day today -left join "IBF-pipeline-output".triggers_per_day yesterday - on today.country_code = yesterday.country_code - and to_date(today.date,'yyyy-mm-dd') = to_date(yesterday.date,'yyyy-mm-dd') + 1 -where 1=1 - and to_date(today.date,'yyyy-mm-dd') = current_date - and today."7" = 1 - and (yesterday."7" = 0 or yesterday."7" is null) -; ---select * from "IBF-pipeline-output".events diff --git a/services/IBF-pipeline/pipeline/lib/cronJob/processEventDistricts.sql b/services/IBF-pipeline/pipeline/lib/cronJob/processEventDistricts.sql index d9c5de2d9..014ba6bad 100644 --- a/services/IBF-pipeline/pipeline/lib/cronJob/processEventDistricts.sql +++ b/services/IBF-pipeline/pipeline/lib/cronJob/processEventDistricts.sql @@ -3,52 +3,55 @@ -- NOTE: Save districts to event. Each day check if there are new districts. Never delete any districts that are not triggered any more. -- First, update (potentially) updated population-affected figures for existing districts -update "IBF-pipeline-output".event_districts set population_affected = subquery.population_affected -from ( - select districtsToday.* - from ( - select id as event - ,t1.pcode - ,name - ,population_affected - from "IBF-pipeline-output".events t0 - left join "IBF-pipeline-output".dashboard_forecast_per_district t1 - on t0.start_date <= t1.date and coalesce(t0.end_date,'9999-99-99') >= t1.date - and t0.country_code = t1.country_code +update + "IBF-pipeline-output".event_pcode +set + end_date = (now() + interval '7 DAY')::date +from + ( + select + districtsToday.* + from + ( + select + t1.pcode , + population_affected, + t2.date + from + "IBF-pipeline-output".dashboard_forecast_per_district t1 left join ( - select * - from "IBF-API"."Admin_area_data2" - union all - select * - from "IBF-API"."Admin_area_data1" - ) t2 - on t1.pcode = t2.pcode - and to_date(t2.date,'yyyy-mm-dd') = current_date - where t1.fc_trigger=1 + select + * + from + "IBF-API"."Admin_area_data2" + union all + select + * + from + "IBF-API"."Admin_area_data1" ) t2 on + t1.pcode = t2.pcode + and to_date(t2.date, 'yyyy-mm-dd') = current_date + where + t1.fc_trigger = 1 and t1.current_prev = 'Current' - and t2.lead_time = '7-day' - ) districtsToday - left join "IBF-pipeline-output".event_districts districtsExisting - on districtsToday.event = districtsExisting.event - and districtsToday.pcode = districtsExisting.pcode - where districtsExisting.pcode is not null -) subquery -where event_districts.event = subquery.event and event_districts.pcode = subquery.pcode + and t2.lead_time = '7-day' ) districtsToday + left join "IBF-pipeline-output".event_pcode eventPcodeExisting on + districtsToday.pcode = eventPcodeExisting.pcode + where + eventPcodeExisting.pcode is not null + and eventPcodeExisting.closed is false ) subquery +where + event_pcode.pcode = subquery.pcode + and event_pcode.closed = false ; ; -- Second: add new districts (either within existing event, or completely new event) ---drop table if exists "IBF-pipeline-output".event_districts; ---create table "IBF-pipeline-output".event_districts as -insert into "IBF-pipeline-output".event_districts -select districtsToday.* +insert into "IBF-pipeline-output".event_pcode(pcode, start_date, end_date) +select districtsToday.* from ( - select id as event - ,t1.pcode - ,name - ,population_affected - from "IBF-pipeline-output".events t0 - left join "IBF-pipeline-output".dashboard_forecast_per_district t1 - on t0.start_date <= t1.date and coalesce(t0.end_date,'9999-99-99') >= t1.date - and t0.country_code = t1.country_code + select t1.pcode + ,now()::date as start_date + ,(now() + interval '7 DAY')::date as end_date + from "IBF-pipeline-output".dashboard_forecast_per_district t1 left join ( select * from "IBF-API"."Admin_area_data2" @@ -62,11 +65,19 @@ from ( and t1.current_prev = 'Current' and t2.lead_time = '7-day' ) districtsToday -left join "IBF-pipeline-output".event_districts districtsExisting - on districtsToday.event = districtsExisting.event - and districtsToday.pcode = districtsExisting.pcode +left join "IBF-pipeline-output".event_pcode districtsExisting + on districtsToday.pcode = districtsExisting.pcode + and districtsExisting.closed = false where districtsExisting.pcode is null ; --select * from "IBF-pipeline-output".event_districts where event = 91 --select * from "IBF-pipeline-output".events where country_code = 'ETH' +-- Close events older than 7 days +update + "IBF-pipeline-output".event_pcode +set + closed = true +where + end_date < now()::date + ; \ No newline at end of file