Skip to content

Commit

Permalink
send alerts for route screen
Browse files Browse the repository at this point in the history
  • Loading branch information
kylerchin committed Nov 13, 2024
1 parent 1b6f2ce commit 4477ed8
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 14 deletions.
53 changes: 49 additions & 4 deletions src/birch/route_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::CatenaryPostgresPool;
use actix_web::web;
use actix_web::HttpResponse;
use actix_web::Responder;
use catenary::aspen::lib::ChateauMetadataEtcd;
use catenary::aspen_dataset::AspenisedAlert;
use catenary::models::DirectionPatternMeta;
use catenary::models::DirectionPatternRow;
Expand All @@ -21,6 +22,7 @@ use std::sync::Arc;
use catenary::SerializableStop;
use std::collections::HashMap;
use std::collections::HashSet;
use tarpc::context;

#[derive(Serialize, Deserialize, Clone)]
pub struct RouteInfoResponse {
Expand Down Expand Up @@ -77,7 +79,7 @@ pub async fn route_info(
.body("Could not connect to etcd");
}

let etcd = etcd.unwrap();
let mut etcd = etcd.unwrap();

//connect to postgres
let conn_pool = pool.as_ref();
Expand Down Expand Up @@ -308,7 +310,50 @@ pub async fn route_info(

//query realtime data pool for alerts

//TODO!
let fetch_assigned_node_for_this_chateau = etcd
.get(
format!("/aspen_assigned_chateaus/{}", &query.chateau).as_str(),
None,
)
.await;

let mut alerts_for_route_send: BTreeMap<String, AspenisedAlert> = BTreeMap::new();
let mut alert_ids = vec![];

if let Ok(fetch_assigned_node_for_this_chateau) = fetch_assigned_node_for_this_chateau {
let fetch_assigned_node_for_this_chateau_kv_first =
fetch_assigned_node_for_this_chateau.kvs().first();

if let Some(fetch_assigned_node_for_this_chateau_data) =
fetch_assigned_node_for_this_chateau_kv_first
{
let assigned_chateau_data = bincode::deserialize::<ChateauMetadataEtcd>(
fetch_assigned_node_for_this_chateau_data.value(),
)
.unwrap();

let aspen_client =
catenary::aspen::lib::spawn_aspen_client_from_ip(&assigned_chateau_data.socket)
.await;

if let Ok(aspen_client) = aspen_client {
let alerts_for_route = aspen_client
.get_alerts_from_route_id(
context::current(),
query.chateau.clone(),
route.route_id.clone(),
)
.await;

if let Ok(Some(alerts_for_route)) = alerts_for_route {
for (alert_id, alert) in alerts_for_route {
alert_ids.push(alert_id.clone());
alerts_for_route_send.insert(alert_id.clone(), alert);
}
}
}
}
}

//return as struct
//pdf is none for now
Expand Down Expand Up @@ -352,8 +397,8 @@ pub async fn route_info(
)
})
.collect(),
alert_ids_for_this_route: vec![],
alert_id_to_alert: BTreeMap::new(),
alert_ids_for_this_route: alert_ids,
alert_id_to_alert: alerts_for_route_send,
};

HttpResponse::Ok().json(response)
Expand Down
21 changes: 11 additions & 10 deletions src/tile_save_and_get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,15 @@ pub async fn delete_tile(
Ok(())
}

pub async fn delete_in_bbox(conn: &mut bb8::PooledConnection<'_, AsyncDieselConnectionManager<AsyncPgConnection>>,
category: TileCategory,
z: u8,
min_x: i32,
max_x: i32,
min_y: i32,
max_y: i32
) -> Result<(), anyhow::Error> {
pub async fn delete_in_bbox(
conn: &mut bb8::PooledConnection<'_, AsyncDieselConnectionManager<AsyncPgConnection>>,
category: TileCategory,
z: u8,
min_x: i32,
max_x: i32,
min_y: i32,
max_y: i32,
) -> Result<(), anyhow::Error> {
let category_i16 = tile_enum_to_i16(category);

let _ = diesel::delete(
Expand All @@ -81,9 +82,9 @@ max_y: i32
.filter(crate::schema::gtfs::tile_storage::dsl::x.ge(min_x))
.filter(crate::schema::gtfs::tile_storage::dsl::x.le(max_x))
.filter(crate::schema::gtfs::tile_storage::dsl::y.ge(min_y))
.filter(crate::schema::gtfs::tile_storage::dsl::y.le(max_y))
.filter(crate::schema::gtfs::tile_storage::dsl::y.le(max_y)),
);

Ok(())
}

Expand Down

0 comments on commit 4477ed8

Please sign in to comment.