-
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
Miovision Aggregation Improvements (ATR view, intersection_movements AFTER INSERT trigger, unacceptable_gaps) #1024
Open
gabrielwol
wants to merge
20
commits into
master
Choose a base branch
from
miovision_alt_aggregation
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
17af02e
new movement_map
gabrielwol 721502d
unfiltered 15min aggregation
gabrielwol 0fa44e4
filtered 15min aggregation (anomalous_ranges, intersection_movements,…
gabrielwol 7d9906e
atr view
gabrielwol 10aedd7
add trigger miovision_intersection_movements_pad
gabrielwol 3a10e49
update miovision_intersection_movements_pad: also add real volumes us…
gabrielwol 7365fcf
remove atr table files
gabrielwol 44f85f8
fluff
gabrielwol fbe8aff
#868, #879 remove unacceptable_gap filter from 15min insert
gabrielwol 28186a1
remove/replace references to old 15min tables #868, #879
gabrielwol 3a97b6b
#868, #879 rename table sql
gabrielwol 42629cb
#868, #879 update mermaid diagram
gabrielwol 7facccd
#868, #879 rename atr filtered view
gabrielwol 99e3401
#868, #879 remove intersection_movement filter (remains as insert fil…
gabrielwol 8d8354e
#868, #879 fluff
gabrielwol 335ed1c
#868, #879 update views
gabrielwol 4fb15d6
fix trigger name #868, #879
gabrielwol 7891ced
#868, #879 update padding trigger to return rows inserted
gabrielwol 8d330ed
#868, #879 change gwolofs reference to miovision
gabrielwol bb0c34c
#868, #879 remove section used for testing
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
130 changes: 130 additions & 0 deletions
130
volumes/miovision/sql/function/function-add_intersection_movement_padding_values.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,130 @@ | ||
CREATE OR REPLACE FUNCTION miovision_api.fn_add_intersection_movement_padding_values() | ||
RETURNS trigger | ||
LANGUAGE 'plpgsql' | ||
|
||
COST 100 | ||
VOLATILE SECURITY DEFINER PARALLEL UNSAFE | ||
AS $BODY$ | ||
|
||
DECLARE n_inserted numeric; | ||
|
||
BEGIN | ||
|
||
WITH temp AS ( | ||
-- Cross product of dates, intersections, legal movement for cars, bikes, and peds to aggregate | ||
SELECT | ||
im.intersection_uid, | ||
dt.datetime_bin, | ||
im.classification_uid, | ||
im.leg, | ||
im.movement_uid, | ||
0 AS volume | ||
FROM new_rows AS im | ||
JOIN miovision_api.intersections AS i USING (intersection_uid) | ||
CROSS JOIN generate_series( | ||
i.date_installed, | ||
LEAST( | ||
i.date_decommissioned, | ||
(CURRENT_TIMESTAMP AT TIME ZONE 'Canada/Eastern')::date | ||
) - interval '15 minutes', | ||
interval '15 minutes' | ||
) AS dt(datetime_bin) | ||
WHERE | ||
--0 padding for certain modes (padding) | ||
im.classification_uid IN (1,2,6,10) | ||
|
||
UNION ALL | ||
|
||
--real volumes | ||
SELECT | ||
v.intersection_uid, | ||
datetime_bin_15(v.datetime_bin) AS datetime_bin, | ||
v.classification_uid, | ||
v.leg, | ||
v.movement_uid, | ||
SUM(volume) | ||
FROM miovision_api.volumes AS v | ||
JOIN new_rows USING ( | ||
intersection_uid, classification_uid, leg, movement_uid | ||
) | ||
GROUP BY | ||
v.intersection_uid, | ||
datetime_bin_15(v.datetime_bin), | ||
v.classification_uid, | ||
v.leg, | ||
v.movement_uid | ||
), | ||
|
||
aggregate_insert AS ( | ||
INSERT INTO miovision_api.volumes_15min_mvt_unfiltered( | ||
intersection_uid, datetime_bin, classification_uid, leg, movement_uid, volume | ||
) | ||
SELECT DISTINCT ON ( | ||
v.intersection_uid, v.datetime_bin, v.classification_uid, v.leg, v.movement_uid | ||
) | ||
v.intersection_uid, | ||
v.datetime_bin, | ||
v.classification_uid, | ||
v.leg, | ||
v.movement_uid, | ||
v.volume | ||
FROM temp AS v | ||
JOIN miovision_api.intersections AS i USING (intersection_uid) | ||
--set unacceptable gaps as null | ||
WHERE | ||
-- Only include dates during which intersection is active | ||
-- (excludes entire day it was added/removed) | ||
v.datetime_bin >= i.date_installed + interval '1 day' | ||
AND ( | ||
i.date_decommissioned IS NULL | ||
OR (v.datetime_bin < i.date_decommissioned - interval '1 day') | ||
) | ||
ORDER BY | ||
v.intersection_uid, | ||
v.datetime_bin, | ||
v.classification_uid, | ||
v.leg, | ||
v.movement_uid, | ||
--select real value instead of padding value if available | ||
v.volume DESC | ||
RETURNING intersection_uid, volume_15min_mvt_uid, datetime_bin, classification_uid, leg, movement_uid, volume | ||
), | ||
|
||
updated AS ( | ||
--To update foreign key for 1min bin table | ||
UPDATE miovision_api.volumes AS v | ||
SET volume_15min_mvt_uid = a_i.volume_15min_mvt_uid | ||
FROM aggregate_insert AS a_i | ||
WHERE | ||
v.volume_15min_mvt_uid IS NULL | ||
AND a_i.volume > 0 | ||
AND v.intersection_uid = a_i.intersection_uid | ||
AND v.datetime_bin >= a_i.datetime_bin | ||
AND v.datetime_bin < a_i.datetime_bin + interval '15 minutes' | ||
AND v.classification_uid = a_i.classification_uid | ||
AND v.leg = a_i.leg | ||
AND v.movement_uid = a_i.movement_uid | ||
) | ||
|
||
SELECT COUNT(*) INTO n_inserted | ||
FROM aggregate_insert; | ||
|
||
RAISE NOTICE '% Done adding to 15min MVT bin based on intersection_movement new rows. % rows added.', timeofday(), n_inserted; | ||
RETURN NULL; | ||
END; | ||
|
||
$BODY$; | ||
|
||
ALTER FUNCTION miovision_api.fn_add_intersection_movement_padding_values() | ||
OWNER TO miovision_admins; | ||
|
||
GRANT EXECUTE ON FUNCTION miovision_api.fn_add_intersection_movement_padding_values() | ||
TO miovision_admins; | ||
|
||
GRANT EXECUTE ON FUNCTION miovision_api.fn_add_intersection_movement_padding_values() | ||
TO miovision_api_bot; | ||
|
||
COMMENT ON FUNCTION miovision_api.fn_add_intersection_movement_padding_values() IS | ||
'This function is called using a trigger after each statement on insert into | ||
miovision_api.intersection_movements. It uses newly inserted rows to update the zero padding | ||
values in miovision_api.volumes_15min_mvt_unfiltered'; |
108 changes: 0 additions & 108 deletions
108
volumes/miovision/sql/function/function-aggregate-volumes_15min.sql
This file was deleted.
Oops, something went wrong.
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
53 changes: 0 additions & 53 deletions
53
volumes/miovision/sql/function/function-clear-volumes_15min.sql
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
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.
I've realized... I think we should declare this in the classifications table and then reduce the chance of a bug if we change which ones are being zero-padded.
Let's spin this off into a separate issue.