Skip to content

Commit

Permalink
Create refresh metadata assignments function
Browse files Browse the repository at this point in the history
  • Loading branch information
kylerchin committed Mar 29, 2024
1 parent 38a3f1b commit 04aeb2c
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 8 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ db-pool = {version = "0.1.3", git = "https://github.com/catenarytransit/db-pool"
diesel-async = "0.4.1"
diesel-derive-newtype = "2.1.1"
async-std = {version = "1.5.3"}
geo-repair-polygon = "0.1.2"

[[bin]]
name = "maple"
Expand All @@ -116,4 +117,4 @@ path = "src/spruce/main.rs"
#Test binaries
[[bin]]
name = "pg_tests"
path = "src/pg_tests/main.rs"
path = "src/pg_tests/main.rs"
2 changes: 2 additions & 0 deletions migrations/2024-03-26-004608_init/up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ CREATE INDEX IF NOT EXISTS gtfs_static_download_attempts_file_hash ON gtfs.stati
CREATE TABLE gtfs.static_feeds (
onestop_feed_id text NOT NULL PRIMARY KEY,
chateau text NOT NULL,
default_lang text,
languages_avaliable text[] NOT NULL,
previous_chateau_name text NOT NULL,
hull GEOMETRY(POLYGON,4326)
);
Expand Down
52 changes: 52 additions & 0 deletions src/maple/refresh_metadata_tables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use diesel_async::{AsyncConnection, AsyncPgConnection, RunQueryDsl};
use dmfr_folder_reader::ReturnDmfrAnalysis;
use std::collections::HashMap;
use std::error::Error;
use std::collections::HashSet;
use std::sync::Arc;

// Written by Kyler Chin at Catenary Transit Initiatives
Expand All @@ -27,16 +28,67 @@ pub async fn refresh_metadata_assignments(
.load::<catenary::models::Chateau>(conn)
.await?;

// create HashMap
let existing_chateaus_map = existing_chateaus

Check warning

Code scanning / clippy

unused variable: existing_chateaus_map Warning

unused variable: existing\_chateaus\_map
.iter()
.map(|x| (x.chateau.clone(), x.clone()))
.collect::<HashMap<String, catenary::models::Chateau>>();

let existing_realtime_feeds = catenary::schema::gtfs::realtime_feeds::table
.select(catenary::models::RealtimeFeed::as_select())
.load::<catenary::models::RealtimeFeed>(conn)
.await?;

//create hashmap of realtime_feeds
let existing_realtime_feeds_map = existing_realtime_feeds

Check warning

Code scanning / clippy

unused variable: existing_realtime_feeds_map Warning

unused variable: existing\_realtime\_feeds\_map
.iter()
.map(|x| (x.onestop_feed_id.clone(), x.clone()))
.collect::<HashMap<String, catenary::models::RealtimeFeed>>();

let existing_static_feeds = catenary::schema::gtfs::static_feeds::table
.select(catenary::models::StaticFeed::as_select())
.load::<catenary::models::StaticFeed>(conn)
.await?;

//create hashmap of static_feeds

let existing_static_feeds_map = existing_static_feeds
.iter()
.map(|x| (x.onestop_feed_id.clone(), x.clone()))
.collect::<HashMap<String, catenary::models::StaticFeed>>();

let chateaus_pg = chateau_result
.iter()
.map(|(k, v)| {
let languages_avaliable_pg: HashSet<String> = {
let mut languages_avaliable_pg = HashSet::new();
for static_id in v.static_feeds.iter() {
if let Some(static_feed) = existing_static_feeds_map.get(static_id) {
languages_avaliable_pg.extend(static_feed.languages_avaliable.iter().filter(|x| x.is_some()).map(|x| x.clone().unwrap()));
}
}
languages_avaliable_pg
};

//use geo_repair_polygon to merge hulls from each feed together

catenary::models::Chateau {
chateau: k.to_string(),
hull: None,
static_feeds: v.static_feeds.iter().map(|x| Some(x.to_string())).collect(),
realtime_feeds: v.realtime_feeds.iter().map(|x| Some(x.to_string())).collect(),
languages_avaliable: languages_avaliable_pg.clone().into_iter().map(|x| Some(x)).collect::<Vec<Option<String>>>(),
}

//set each realtime feed to the new chateau id

//set each static feed to the new chateau id
// if static feed has a different chateau id, call on the update function
// update_chateau_id_for_gtfs_schedule(feed_id, new_chateau_id, conn).await?;

})
.collect::<Vec<catenary::models::Chateau>>();

// if the new chateau id is different for any of the feeds, run the update function
Ok(())
}
11 changes: 4 additions & 7 deletions src/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use serde_json::Value;

#[derive(Queryable, Selectable, Insertable, Clone)]
#[diesel(table_name = crate::schema::gtfs::shapes)]
#[diesel(check_for_backend(diesel::pg::Pg))]
pub struct Shape {
pub onestop_feed_id: String,
pub attempt_id: String,
Expand All @@ -26,7 +25,6 @@ pub struct Shape {

#[derive(Queryable, Selectable, Insertable, Debug, Clone)]
#[diesel(table_name = crate::schema::gtfs::static_download_attempts)]
#[diesel(check_for_backend(diesel::pg::Pg))]
pub struct StaticDownloadAttempt {
pub onestop_feed_id: String,
pub file_hash: Option<String>,
Expand All @@ -41,7 +39,6 @@ pub struct StaticDownloadAttempt {

#[derive(Queryable, Selectable, Insertable, Debug, Clone)]
#[diesel(table_name = crate::schema::gtfs::chateaus)]
#[diesel(check_for_backend(diesel::pg::Pg))]
pub struct Chateau {
pub chateau: String,
pub static_feeds: Vec<Option<String>>,
Expand All @@ -52,17 +49,17 @@ pub struct Chateau {

#[derive(Queryable, Selectable, Insertable, Debug, Clone)]
#[diesel(table_name = crate::schema::gtfs::static_feeds)]
#[diesel(check_for_backend(diesel::pg::Pg))]
pub struct StaticFeed {
pub onestop_feed_id: String,
pub chateau: String,
pub previous_chateau_name: String,
pub default_lang: Option<String>,
pub languages_avaliable: Vec<Option<String>>,
pub hull: Option<postgis_diesel::types::Polygon<postgis_diesel::types::Point>>,
}

#[derive(Queryable, Selectable, Insertable, Debug, Clone)]
#[diesel(table_name = crate::schema::gtfs::realtime_feeds)]
#[diesel(check_for_backend(diesel::pg::Pg))]
pub struct RealtimeFeed {
pub onestop_feed_id: String,
pub chateau: String,
Expand All @@ -72,7 +69,6 @@ pub struct RealtimeFeed {

#[derive(Queryable, Selectable, Insertable, Debug, Clone)]
#[diesel(table_name = crate::schema::gtfs::ingested_static)]
#[diesel(check_for_backend(diesel::pg::Pg))]
pub struct IngestedStatic {
pub onestop_feed_id: String,
pub ingest_start_unix_time_ms: i64,
Expand All @@ -87,8 +83,9 @@ pub struct IngestedStatic {
pub deleted: bool,
pub feed_expiration_date: Option<chrono::NaiveDate>,
pub feed_start_date: Option<chrono::NaiveDate>,
pub languages_avaliable: Vec<Option<String>>,
pub ingestion_version: i32,
pub default_lang: Option<String>,
pub languages_avaliable: Vec<Option<String>>,
}

#[derive(Queryable, Selectable, Insertable, Debug, Clone)]
Expand Down
2 changes: 2 additions & 0 deletions src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,8 @@ pub mod gtfs {
gtfs.static_feeds (onestop_feed_id) {
onestop_feed_id -> Text,
chateau -> Text,
default_lang -> Nullable<Text>,
languages_avaliable -> Array<Nullable<Text>>,
previous_chateau_name -> Text,
hull -> Nullable<Geometry>,
}
Expand Down

0 comments on commit 04aeb2c

Please sign in to comment.