-
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 5 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
CREATE OR REPLACE VIEW ecocounter.recent_outages AS ( | ||
|
||
WITH ongoing_outages AS ( | ||
SELECT | ||
f.flow_id, | ||
f.site_id, | ||
dates.dt, | ||
dates.dt - lag(dates.dt) OVER w = interval '1 day' AS consecutive | ||
FROM ecocounter.flows_unfiltered AS f | ||
CROSS JOIN | ||
generate_series( | ||
now()::date - interval '31 days', | ||
now()::date - interval '2 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 - interval '31 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 | ||
COALESCE(SUM(c.volume), 0) = 0 --null or zero | ||
WINDOW w AS (PARTITION BY f.flow_id ORDER BY dates.dt) | ||
ORDER BY | ||
f.flow_id, | ||
dates.dt | ||
), | ||
|
||
group_ids AS ( | ||
SELECT | ||
flow_id, | ||
site_id, | ||
dt, | ||
SUM(CASE WHEN consecutive IS TRUE THEN 0 ELSE 1 END) OVER w AS group_id | ||
FROM ongoing_outages | ||
WINDOW w AS (PARTITION BY flow_id ORDER BY dt) | ||
) | ||
|
||
SELECT | ||
flow_id, | ||
site_id, | ||
MIN(dt) AS date_start, | ||
gabrielwol marked this conversation as resolved.
Show resolved
Hide resolved
|
||
MAX(dt) + interval '1 day' AS date_end | ||
FROM group_ids | ||
GROUP BY | ||
flow_id, | ||
site_id, | ||
group_id | ||
); | ||
|
||
ALTER VIEW ecocounter.recent_outages OWNER TO ecocounter_admins; | ||
GRANT ALL ON TABLE ecocounter.recent_outages TO ecocounter_admins; | ||
|
||
REVOKE ALL ON TABLE ecocounter.recent_outages FROM bdit_humans; | ||
GRANT SELECT ON TABLE ecocounter.recent_outages TO bdit_humans; | ||
|
||
GRANT SELECT ON TABLE ecocounter.recent_outages TO ecocounter_bot; | ||
|
||
COMMENT ON VIEW ecocounter.recent_outages | ||
IS 'A view to identify recent outages in Ecocounter data and group | ||
them into runs for ease of pulling.'; |
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.
can we rename to sth like
last_week_outages
or sth similar?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.
month**
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.
even more generic:
identify_outages