diff --git a/volumes/miovision/sql/data_checks/select-open_issues.sql b/volumes/miovision/sql/data_checks/select-open_issues.sql index ac6caf712..e9ccb3734 100644 --- a/volumes/miovision/sql/data_checks/select-open_issues.sql +++ b/volumes/miovision/sql/data_checks/select-open_issues.sql @@ -6,23 +6,25 @@ WITH ars AS ( classification || ' (' || classification_uid || ')', 'All classifications' ) AS classification, - last_week_volume, + leg, + volume, notes FROM miovision_api.open_issues - WHERE last_week_volume > 0 + WHERE volume > 0 ORDER BY uid ) SELECT NOT(COUNT(*) > 0) AS _check, CASE WHEN COUNT(*) = 1 THEN 'There is ' ELSE 'There are ' END || COUNT(*) - || ' open ended anomalous_range(s) with non-zero volumes in the last 7 days:' + || ' open ended anomalous_range(s) with non-zero volumes within the last 7 days (or since AR began):' AS summ, array_agg( 'uid: `' || ars.uid || '`, `' || ars.intersection || '`, `' || ars.classification || '`, ' - || 'volume: `' || ars.last_week_volume || '`, ' + || CASE WHEN ars.leg IS NOT NULL THEN '`' || ars.leg || ' leg`, ' ELSE '' END + || 'volume: `' || to_char(ars.volume, 'FM9,999,999') || '`, ' || 'notes: `' || LEFT(ars.notes, 80) || CASE WHEN LENGTH(ars.notes) > 80 THEN '...' ELSE '' END || '`' ) AS gaps diff --git a/volumes/miovision/sql/views/create-view-open_issues.sql b/volumes/miovision/sql/views/create-view-open_issues.sql index 8fc959feb..c3f5b7311 100644 --- a/volumes/miovision/sql/views/create-view-open_issues.sql +++ b/volumes/miovision/sql/views/create-view-open_issues.sql @@ -1,6 +1,23 @@ --DROP VIEW miovision_api.open_issues; CREATE OR REPLACE VIEW miovision_api.open_issues AS +--DROP VIEW miovision_api.open_issues; + +WITH alerts AS ( + SELECT + ar.uid, + string_agg(DISTINCT alerts.alert, '; ') AS alerts + FROM miovision_api.anomalous_ranges AS ar + LEFT JOIN miovision_api.alerts + ON alerts.intersection_uid = ar.intersection_uid + AND alerts.start_time >= ar.range_start + AND ( + alerts.end_time < ar.range_end + OR ar.range_end IS NULL + ) + GROUP BY ar.uid +) + SELECT ar.uid, ar.intersection_uid, @@ -13,11 +30,12 @@ SELECT WHEN ar.classification_uid IS NULL THEN 'All modes' ELSE c.classification END, + ar.leg, ar.range_start::date, (current_timestamp AT TIME ZONE 'EST5EDT')::date - ar.range_start::date AS num_days, ar.notes, - SUM(v.volume) AS last_week_volume, - string_agg(DISTINCT alerts.alert, '; ') AS alerts + SUM(v.volume) AS volume, + alerts.alerts FROM miovision_api.anomalous_ranges AS ar --keep rows with null classification_uid LEFT JOIN miovision_api.classifications AS c USING (classification_uid) @@ -26,18 +44,19 @@ JOIN miovision_api.intersections AS i USING (intersection_uid) --find last week volume LEFT JOIN miovision_api.volumes AS v ON ar.intersection_uid = v.intersection_uid + --volume within the last 7 days and after AR started + AND v.datetime_bin >= ar.range_start + --prune the partitions AND v.datetime_bin >= current_date - interval '7 days' AND ( ar.classification_uid = v.classification_uid OR ar.classification_uid IS NULL ) -LEFT JOIN miovision_api.alerts - ON alerts.intersection_id = i.id - AND alerts.start_time >= ar.range_start AND ( - alerts.end_time < ar.range_end - OR ar.range_end IS NULL + ar.leg = v.leg + OR ar.leg IS NULL ) +LEFT JOIN alerts ON alerts.uid = ar.uid WHERE ar.problem_level <> 'valid-caveat' --currently active @@ -56,7 +75,8 @@ GROUP BY ar.classification_uid, c.classification, ar.range_start, - ar.notes + ar.notes, + alerts.alerts ORDER BY ar.intersection_uid, ar.range_start, @@ -68,5 +88,8 @@ Converts intersection_uid and classification_uid into formats familiar to Miovis (intersections.id, classifications.classification). anomalous_ranges.id col can be used to link response back to table.'''; +COMMENT ON COLUMN miovision_api.open_issues.volume +IS 'Volume recorded within the last 7 days or since anomalous_range began, whichever is later.' + ALTER TABLE miovision_api.open_issues OWNER TO miovision_admins; GRANT SELECT ON TABLE miovision_api.open_issues TO bdit_humans; \ No newline at end of file diff --git a/volumes/miovision/sql/views/create-view-volumes_daily.sql b/volumes/miovision/sql/views/create-view-volumes_daily.sql index a84221e3a..cd8fdb27b 100644 --- a/volumes/miovision/sql/views/create-view-volumes_daily.sql +++ b/volumes/miovision/sql/views/create-view-volumes_daily.sql @@ -24,6 +24,8 @@ CREATE VIEW miovision_api.volumes_daily AS ( ar.classification_uid = v.classification_uid OR ar.classification_uid IS NULL ) + --omitting join to ar.leg implies that a single leg outage is + --sufficient to exclude entire intersection. AND v.dt >= ar.range_start AND ( v.dt < ar.range_end