Skip to content

Commit

Permalink
work on bus alerts
Browse files Browse the repository at this point in the history
  • Loading branch information
jonerrr committed Aug 14, 2024
1 parent 03e3cba commit 677884d
Show file tree
Hide file tree
Showing 17 changed files with 266 additions and 221 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

This file was deleted.

59 changes: 3 additions & 56 deletions backend/src/alerts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,23 +269,9 @@ async fn parse_gtfs(pool: &PgPool) -> Result<(), DecodeFeedError> {
.execute(pool)
.await?;

// let start = Utc::now();
// Filter out cloned ids from alerts, active periods, and affected entities. There is probably a more elegant way to remove cloned ids
// let alerts: Vec<Alert> = alerts
// .into_par_iter()
// .filter(|a| !cloned_mta_ids.contains(&a.mta_id))
// .collect();
// let active_periods: Vec<ActivePeriod> = active_periods
// .into_par_iter()
// .filter(|ap| !cloned_ids.contains(&ap.alert_id))
// .collect();
// let affected_entities: Vec<AffectedEntity> = affected_entities
// .into_par_iter()
// .filter(|ae| !cloned_ids.contains(&ae.alert_id))
// .collect();

// Remove duplicate alerts and their active periods and affected entities
// TODO: check if this is required still
let mut cloned_ids = vec![];

alerts.retain(|a| {
if cloned_mta_ids.contains(&a.mta_id) {
cloned_ids.push(a.id);
Expand All @@ -294,48 +280,11 @@ async fn parse_gtfs(pool: &PgPool) -> Result<(), DecodeFeedError> {
true
}
});
// dbg!(&cloned_ids);
active_periods.retain(|a| !cloned_ids.contains(&a.alert_id));
affected_entities.retain(|a| !cloned_ids.contains(&a.alert_id));
// dbg!(&alerts.len());

// let end = Utc::now();
// let duration = end - start;
// println!("took {} ms", duration.num_milliseconds());

// Test for duplicate ids
// let mut duplicate_ids = vec![];
// for alert in &alerts {
// let mut count = 0;
// alerts.iter().for_each(|a| {
// if a.id == alert.id {
// count += 1;
// }
// });
// if count > 1 {
// duplicate_ids.push(alert)
// }
// }
// dbg!(duplicate_ids);
// Test for missing alerts
// let missing_ap_ids = active_periods
// .iter()
// .filter_map(|ap| {
// if !alerts.iter().any(|a| a.id == ap.alert_id) {
// Some(ap.alert_id)
// } else {
// None
// }
// })
// .collect::<Vec<_>>();
// let missing_ap_alerts = alerts
// .iter()
// .filter(|a| missing_ap_ids.contains(&a.id))
// .collect::<Vec<_>>();
// dbg!(&missing_ap_alerts);

// TODO: Use transaction https://github.com/launchbadge/sqlx/blob/main/examples/postgres/transaction/src/main.rs
// TODO: figure out what to do about old active periods that are now incorrect. Maybe delete all active periods and entities for alerts we are updating?
// TODO: remove old active periods and affected entities
let mut query_builder =
QueryBuilder::new("INSERT INTO alerts (id, mta_id, alert_type, header_plain, header_html, description_plain, description_html, created_at, updated_at, display_before_active) ");
query_builder.push_values(alerts, |mut b, alert| {
Expand All @@ -350,7 +299,6 @@ async fn parse_gtfs(pool: &PgPool) -> Result<(), DecodeFeedError> {
.push_bind(alert.updated_at)
.push_bind(alert.display_before_active);
});
// TODO: test if this prevents duplicates
query_builder
.push("ON CONFLICT (id) DO UPDATE SET alert_type = EXCLUDED.alert_type, header_plain = EXCLUDED.header_plain, header_html = EXCLUDED.header_html, description_plain = EXCLUDED.description_plain, description_html = EXCLUDED.description_html, created_at = EXCLUDED.created_at, updated_at = EXCLUDED.updated_at, display_before_active = EXCLUDED.display_before_active");
let query = query_builder.build();
Expand All @@ -363,7 +311,6 @@ async fn parse_gtfs(pool: &PgPool) -> Result<(), DecodeFeedError> {
.push_bind(active_period.start_time)
.push_bind(active_period.end_time);
});
// TODO: test if this prevents duplicates
query_builder
.push("ON CONFLICT (alert_id, start_time) DO UPDATE SET end_time = EXCLUDED.end_time");
let query = query_builder.build();
Expand Down
68 changes: 41 additions & 27 deletions backend/src/routes/alerts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,22 @@ pub async fn get(
a.updated_at,
ap.start_time,
ap.end_time,
jsonb_agg(DISTINCT ae) AS entities
jsonb_agg(DISTINCT jsonb_build_object('bus_route_id',
ae.bus_route_id,
'route_id',
ae.route_id,
'stop_id',
ae.stop_id,
'sort_order',
ae.sort_order)) AS entities
FROM
alerts a
LEFT JOIN active_periods ap ON
a.id = ap.alert_id
LEFT JOIN affected_entities ae ON
a.id = ae.alert_id
WHERE
ae.route_id IS NOT NULL
(ae.route_id IS NOT NULL OR ae.bus_route_id IS NOT NULL)
AND ap.start_time BETWEEN ($1::timestamptz - INTERVAL '24 hours') AND $1
AND (ap.end_time < $1)
GROUP BY
Expand All @@ -69,31 +76,38 @@ pub async fn get(
sqlx::query_as!(
Alert,
"SELECT
a.id,
a.alert_type,
a.header_html,
a.description_html,
a.created_at,
a.updated_at,
ap.start_time,
ap.end_time,
jsonb_agg(DISTINCT ae) AS entities
FROM
alerts a
LEFT JOIN active_periods ap ON
a.id = ap.alert_id
LEFT JOIN affected_entities ae ON
a.id = ae.alert_id
WHERE
a.in_feed IS TRUE
AND ae.route_id IS NOT NULL
AND ap.start_time < $1
AND (ap.end_time > $1
OR ap.end_time IS NULL)
GROUP BY
a.id,
ap.start_time,
ap.end_time",
a.id,
a.alert_type,
a.header_html,
a.description_html,
a.created_at,
a.updated_at,
ap.start_time,
ap.end_time,
jsonb_agg(DISTINCT jsonb_build_object('bus_route_id',
ae.bus_route_id,
'route_id',
ae.route_id,
'stop_id',
ae.stop_id,
'sort_order',
ae.sort_order)) AS entities
FROM
alerts a
LEFT JOIN active_periods ap ON
a.id = ap.alert_id
LEFT JOIN affected_entities ae ON
a.id = ae.alert_id
WHERE
a.in_feed IS TRUE
AND (ae.route_id IS NOT NULL OR ae.bus_route_id IS NOT NULL)
AND ap.start_time < $1
AND (ap.end_time > $1
OR ap.end_time IS NULL)
GROUP BY
a.id,
ap.start_time,
ap.end_time",
time.0
)
.fetch_all(&pool)
Expand Down
10 changes: 9 additions & 1 deletion frontend/src/app.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,15 @@ declare global {
dialog_open: boolean;
dialog_id: string | number;
// blank is not open
dialog_type: 'stop' | 'trip' | 'route_alert' | 'route' | 'bus_stop' | 'bus_trip' | '';
dialog_type:
| 'stop'
| 'trip'
| 'route_alert'
| 'bus_route_alert'
| 'route'
| 'bus_stop'
| 'bus_trip'
| '';
// list of bus routes to monitor
// monitor_routes?: string[];
}
Expand Down
1 change: 1 addition & 0 deletions frontend/src/lib/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ export interface Alert {
export interface Entity {
alert_id: string;
route_id: string | null;
bus_route_id: string | null;
stop_id: string | null;
sort_order: number;
}
Expand Down
11 changes: 9 additions & 2 deletions frontend/src/lib/components/BusIcon.svelte
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
<script lang="ts">
import { pushState } from '$app/navigation';
import type { BusRoute } from '$lib/bus_api';
export let route: BusRoute;
export let link = true;
</script>

<span
<button
aria-label={`Bus Route Alerts for ${route.short_name}`}
style={`background-color: #${route.color}`}
class="p-1 text-indigo-100 rounded font-bold shadow-2xl"
on:click={() => {
if (link)
pushState('', { dialog_open: true, dialog_id: route.id, dialog_type: 'bus_route_alert' });
}}
>
{route.short_name}
</span>
</button>
Loading

0 comments on commit 677884d

Please sign in to comment.