-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
984 ecocounter pull recent outages #1014
Merged
Merged
Changes from 14 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
f7f5aae
#984 create view recent-outages
gabrielwol 34146c1
#984 add task to poll all the recent outages
gabrielwol fd73d73
#984 add date_decommissioned col
gabrielwol baaadce
#984 add truncate_and_insert to simplify dag
gabrielwol bc707ea
#984 change to 30 day lookback + fluff
gabrielwol b7669ce
#984 change view to function and up limit to 60 day lookback
gabrielwol c355ce3
#984 cast to date
gabrielwol b610141
#984 add date_decommissioned to sites table
gabrielwol 2912a13
#984 add aliases to fix collision with return table columns
gabrielwol cd08822
#984 remove timespec
gabrielwol de22cd8
#984 change dates to times in outage function
gabrielwol eb19ab2
Merge branch '984-ecocounter-pull-recent-outages' of https://github.c…
gabrielwol 2f4c419
#984 undo timespec change
gabrielwol b93778d
#984 update ecocounter readme
gabrielwol 311c130
#984 replace missing readme sections
gabrielwol File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
84 changes: 84 additions & 0 deletions
84
volumes/ecocounter/functions/create-function-identify-outages.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,84 @@ | ||
CREATE OR REPLACE FUNCTION ecocounter.identify_outages( | ||
num_days interval | ||
) | ||
RETURNS TABLE ( | ||
flow_id numeric, | ||
start_time timestamp, | ||
end_time timestamp | ||
) | ||
LANGUAGE plpgsql | ||
COST 100 | ||
VOLATILE | ||
|
||
AS $BODY$ | ||
|
||
BEGIN | ||
|
||
RETURN QUERY | ||
WITH ongoing_outages AS ( | ||
SELECT | ||
f.flow_id, | ||
f.site_id, | ||
dates.dt::date, | ||
dates.dt - lag(dates.dt) OVER w = interval '1 day' AS consecutive | ||
FROM ecocounter.flows_unfiltered AS f | ||
CROSS JOIN | ||
generate_series( | ||
now()::date - num_days, | ||
now()::date - interval '2 day', --2 bc last interval will be this + 1 day | ||
interval '1 day' | ||
) AS dates (dt) | ||
LEFT JOIN ecocounter.counts_unfiltered AS c | ||
ON c.flow_id = f.flow_id | ||
AND c.datetime_bin >= dates.dt | ||
AND c.datetime_bin < dates.dt + interval '1 day' | ||
--select counts partitions | ||
AND c.datetime_bin >= now()::date - num_days | ||
AND c.datetime_bin < now()::date - interval '1 day' | ||
WHERE | ||
f.validated | ||
AND dates.dt < COALESCE(f.date_decommissioned, now()::date - interval '1 day') | ||
GROUP BY | ||
f.flow_id, | ||
f.site_id, | ||
f.validated, | ||
f.last_active, | ||
f.date_decommissioned, | ||
dates.dt | ||
HAVING SUM(c.volume) IS NULL | ||
WINDOW w AS (PARTITION BY f.flow_id ORDER BY dates.dt) | ||
ORDER BY | ||
f.flow_id, | ||
dates.dt | ||
), | ||
|
||
group_ids AS ( | ||
SELECT | ||
oo.flow_id, | ||
oo.dt, | ||
SUM(CASE WHEN oo.consecutive IS TRUE THEN 0 ELSE 1 END) OVER w AS group_id | ||
FROM ongoing_outages AS oo | ||
WINDOW w AS (PARTITION BY oo.flow_id ORDER BY oo.dt) | ||
) | ||
|
||
SELECT | ||
gi.flow_id, | ||
MIN(gi.dt)::timestamp AS start_time, | ||
MAX(gi.dt) + interval '1 day' AS end_time | ||
FROM group_ids AS gi | ||
GROUP BY | ||
gi.flow_id, | ||
gi.group_id; | ||
|
||
END; | ||
$BODY$; | ||
|
||
ALTER FUNCTION ecocounter.identify_outages(interval) OWNER TO ecocounter_admins; | ||
GRANT ALL ON FUNCTION ecocounter.identify_outages(interval) TO ecocounter_admins; | ||
|
||
GRANT EXECUTE ON FUNCTION ecocounter.identify_outages(interval) TO bdit_humans; | ||
GRANT EXECUTE ON FUNCTION ecocounter.identify_outages(interval) TO ecocounter_bot; | ||
|
||
COMMENT ON FUNCTION ecocounter.identify_outages(interval) | ||
IS 'A function to identify day level outages (null volume) in Ecocounter data and group | ||
them into runs for ease of pulling. Used by Airflow ecocounter_pull.pull_recent_outages task.'; |
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
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
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
somehow formatted as code block
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed! And added back a missing section of readme 👀