Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions volumes/miovision/api/intersection_tmc.py
Original file line number Diff line number Diff line change
Expand Up @@ -417,14 +417,14 @@ def agg_zero_volume_anomalous_ranges(conn, time_period, intersections = None):
#this function includes a delete query preceeding the insert.
anomalous_range_sql="""SELECT *
FROM generate_series(%s::date, %s::date - interval '1 day', interval '1 day') AS dates(start_date),
LATERAL (SELECT miovision_api.identify_zero_counts(start_date::date)) AS agg"""
LATERAL (SELECT miovision_api.identify_anomalous_ranges(start_date::date)) AS agg"""
cur.execute(anomalous_range_sql, time_period)
logger.info('Aggregation of zero volume periods into anomalous_ranges table complete')
else:
query_params = time_period + ([x.uid for x in intersections], )
anomalous_range_sql="""SELECT *
FROM generate_series(%s::date, %s::date - interval '1 day', interval '1 day') AS dates(start_date),
LATERAL (SELECT miovision_api.identify_zero_counts(start_date::date, %s::integer [])) AS agg"""
LATERAL (SELECT miovision_api.identify_anomalous_ranges(start_date::date, %s::integer [])) AS agg"""
cur.execute(anomalous_range_sql, query_params)
logger.info('Aggregation of zero volume periods into anomalous_ranges table complete for intersections %s',
[x.uid for x in intersections])
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
CREATE OR REPLACE FUNCTION miovision_api.delete_anomalous_ranges(
ds date,
intersections integer [] DEFAULT ARRAY[]::integer []
)
RETURNS void AS $$

DECLARE
target_intersections integer [] = miovision_api.get_intersections_uids(intersections);

BEGIN

WITH ranges_to_split AS (
DELETE FROM miovision_api.anomalous_ranges
WHERE
investigation_level = 'auto_flagged'
AND intersection_uid = ANY(target_intersections)
--overlaps
AND delete_anomalous_ranges.ds::timestamp <@ tsrange(range_start, range_end, '[)')
RETURNING intersection_uid, classification_uid, notes, uid, investigation_level, problem_level, range_start, range_end, leg
)

--inserts 0-2 anomalous ranges to replace deleted one
INSERT INTO miovision_api.anomalous_ranges (
intersection_uid, classification_uid, notes, investigation_level, problem_level, leg, range_start, range_end
)
SELECT intersection_uid, classification_uid, notes, investigation_level, problem_level, leg,
--rolls end back
range_start, LEAST(delete_anomalous_ranges.ds, range_end) AS range_end
FROM ranges_to_split
--new range is still valid after roll back
WHERE range_start < LEAST(delete_anomalous_ranges.ds, range_end)
UNION
SELECT intersection_uid, classification_uid, notes, investigation_level, problem_level, leg,
--rolls start forward
GREATEST(delete_anomalous_ranges.ds + 1, range_start) AS range_start, range_end
FROM ranges_to_split
--new range is still valid after roll forward
WHERE GREATEST(delete_anomalous_ranges.ds + 1, range_start) < range_end;

END;

$$ LANGUAGE plpgsql;

ALTER FUNCTION miovision_api.get_intersections_uids
OWNER TO miovision_admins;

GRANT EXECUTE ON FUNCTION miovision_api.get_intersections_uids TO miovision_api_bot;
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
CREATE OR REPLACE FUNCTION miovision_api.identify_anomalous_ranges(
ds date,
intersections integer [] DEFAULT ARRAY[]::integer []
)
RETURNS void AS $$

DECLARE
target_intersections integer [] = miovision_api.get_intersections_uids(intersections);

BEGIN
PERFORM miovision_api.delete_anomalous_ranges(identify_anomalous_ranges.ds, target_intersections);
PERFORM miovision_api.identify_zero_counts(identify_anomalous_ranges.ds, target_intersections);
PERFORM miovision_api.merge_consecutive_anomalous_ranges();
END;

$$
language plpgsql;

Check failure on line 17 in volumes/miovision/sql/function/function-identify_anomalous_ranges.sql

View workflow job for this annotation

GitHub Actions / SQLFluff Lint

SQLFluff

CP01: Keywords must be upper case.

ALTER FUNCTION miovision_api.identify_anomalous_ranges
OWNER TO miovision_admins;

GRANT EXECUTE ON FUNCTION miovision_api.identify_anomalous_ranges TO miovision_api_bot;
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
CREATE OR REPLACE FUNCTION miovision_api.merge_consecutive_anomalous_ranges()
RETURNS void AS $$

--identify back to back ranges:
WITH consecutive_ranges AS (
SELECT DISTINCT
LEAST(ar1.uid, ar2.uid) AS uid,
GREATEST(ar1.uid, ar2.uid) AS ar_uid_to_delete,
ar1.classification_uid,
ar1.notes,
ar1.investigation_level,
ar1.problem_level,
ar1.leg,
LEAST(ar1.range_start, ar2.range_start) AS new_range_start,
GREATEST(ar1.range_end, ar2.range_end) AS new_range_end
FROM miovision_api.anomalous_ranges AS ar1
JOIN miovision_api.anomalous_ranges AS ar2
ON
ar1.intersection_uid = ar2.intersection_uid
AND COALESCE(ar1.classification_uid, 0) = COALESCE(ar2.classification_uid, 0)
AND COALESCE(ar1.leg, '') = COALESCE(ar2.leg, '')
AND ar1.notes = ar2.notes
AND ar1.investigation_level = ar2.investigation_level
AND ar1.problem_level = ar2.problem_level
AND ar1.uid <> ar2.uid
AND (
--back to back or overlapping ranges
tsrange(ar1.range_start, ar1.range_end) && tsrange(ar2.range_start, ar2.range_end)
OR ar1.range_end = ar2.range_start
)
),

--delete the second one
deleted AS (
DELETE FROM miovision_api.anomalous_ranges AS ar
USING consecutive_ranges AS cr
WHERE ar.uid = cr.ar_uid_to_delete
)

--update the first one
UPDATE miovision_api.anomalous_ranges AS ar
SET
range_start = cr.new_range_start,
range_end = cr.new_range_end
FROM consecutive_ranges AS cr
WHERE ar.uid = cr.uid;

$$ LANGUAGE sql;

ALTER FUNCTION miovision_api.merge_consecutive_anomalous_ranges()
OWNER TO miovision_admins;

GRANT EXECUTE ON FUNCTION miovision_api.merge_consecutive_anomalous_ranges() TO miovision_api_bot;

COMMENT ON FUNCTION miovision_api.merge_consecutive_anomalous_ranges
IS 'Merges overlapping/consecutive anomalous ranges, namely
those created as a byproduct of `delete_anomalous_ranges`.';
Loading