Skip to content

Commit

Permalink
Merge pull request #953 from hove-io/feature/nav-3093/odt-reservation
Browse files Browse the repository at this point in the history
[NAV-3235] Feature: odt reservation
  • Loading branch information
datanel authored Jul 30, 2024
2 parents 04142c0 + 5b1eb73 commit b00bdbb
Show file tree
Hide file tree
Showing 19 changed files with 363 additions and 85 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
authors = ["Hove <[email protected]>", "Guillaume Pinot <[email protected]>"]
name = "transit_model"
version = "0.62.3"
version = "0.63.0"
license = "AGPL-3.0-only"
description = "Transit data management"
repository = "https://github.com/hove-io/transit_model"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ feed_creation_datetime,2024-07-03T12:49:20+00:00
feed_creation_time,12:49:20
feed_end_date,20181231
feed_start_date,20180101
ntfs_version,0.15.0
ntfs_version,0.16.0
1 change: 1 addition & 0 deletions src/add_prefix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ impl AddPrefix for Collections {
self.calendars.prefix(prefix_conf);
self.companies.prefix(prefix_conf);
self.comments.prefix(prefix_conf);
self.odt_reservations.prefix(prefix_conf);
self.equipments.prefix(prefix_conf);
self.transfers.prefix(prefix_conf);
self.trip_properties.prefix(prefix_conf);
Expand Down
1 change: 1 addition & 0 deletions src/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ struct Config {
/// - a Contributor
/// - a Dataset
/// - a list of key/value which will be used in 'feed_infos.txt'
///
/// Below is an example of this file
/// ```text
/// {
Expand Down
1 change: 1 addition & 0 deletions src/file_handler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ where
/// Return a file if exist
fn get_file_if_exists(self, name: &str) -> Result<(Option<Self::Reader>, PathBuf)>;

#[allow(dead_code)]
/// Return a file or an error if not exist
fn get_file(self, name: &str) -> Result<(Self::Reader, PathBuf)> {
let (reader, path) = self.get_file_if_exists(name)?;
Expand Down
22 changes: 12 additions & 10 deletions src/gtfs/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ use crate::{
file_handler::FileHandler,
model::Collections,
objects::{
self, Availability, CommentLinksT, Coord, KeysValues, Pathway, PropertiesMap, StopLocation,
StopPoint, StopTimePrecision, StopType, Time, TransportType,
self, Availability, Comment, Coord, KeysValues, LinksT, Pathway, PropertiesMap,
StopLocation, StopPoint, StopTimePrecision, StopType, Time, TransportType,
},
parser::{read_collection, read_objects, read_objects_loose},
serde_utils::de_with_empty_default,
Expand Down Expand Up @@ -113,7 +113,7 @@ impl TryFrom<Stop> for objects::StopArea {
name: stop.name,
codes,
object_properties: PropertiesMap::default(),
comment_links: objects::CommentLinksT::default(),
comment_links: objects::LinksT::default(),
coord,
timezone: stop.timezone,
visible: true,
Expand Down Expand Up @@ -207,7 +207,7 @@ impl TryFrom<Stop> for objects::StopLocation {
id: stop.id,
name: stop.name,
code: stop.code,
comment_links: CommentLinksT::default(),
comment_links: LinksT::default(),
visible: false, // disable for autocomplete
coord,
parent_id: stop.parent_station,
Expand Down Expand Up @@ -333,7 +333,8 @@ impl Trip {
id: self.id.clone(),
codes,
object_properties: PropertiesMap::default(),
comment_links: CommentLinksT::default(),
comment_links: LinksT::default(),
odt_reservation_links: LinksT::default(),
route_id: route.get_id_by_direction(self.direction),
physical_mode_id: physical_mode.id,
dataset_id: dataset.id.clone(),
Expand Down Expand Up @@ -634,7 +635,7 @@ fn generate_stop_comment(stop: &Stop) -> Option<objects::Comment> {
})
}

fn insert_comment<T: typed_index_collection::Id<T> + objects::CommentLinks>(
fn insert_comment<T: typed_index_collection::Id<T> + objects::Links<Comment>>(
collection: &mut CollectionWithId<T>,
comments: &mut CollectionWithId<objects::Comment>,
prefix: &str,
Expand All @@ -650,7 +651,7 @@ fn insert_comment<T: typed_index_collection::Id<T> + objects::CommentLinks>(

if let Some(comment) = opt_comment {
if let Some(mut object) = collection.get_mut(&gtfs_route.id) {
object.comment_links_mut().insert(comment.id.to_string());
object.links_mut().insert(comment.id.to_string());
comments
.push(comment)
.expect("Duplicated comment id that shouldn’t be possible");
Expand Down Expand Up @@ -775,7 +776,7 @@ where
let mut stop_points = vec![];
let mut stop_locations = vec![];
for stop in gtfs_stops {
let mut comment_links = CommentLinksT::default();
let mut comment_links = LinksT::default();
if let Some(comment) = generate_stop_comment(&stop) {
comment_links.insert(comment.id.to_string());
comments
Expand Down Expand Up @@ -1028,7 +1029,8 @@ fn make_lines(
code: line_code(r),
codes,
object_properties: PropertiesMap::default(),
comment_links: CommentLinksT::default(),
comment_links: LinksT::default(),
odt_reservation_links: LinksT::default(),
name: r.long_name.to_string(),
forward_name: None,
backward_name: None,
Expand Down Expand Up @@ -1084,7 +1086,7 @@ fn make_routes(gtfs_trips: &[Trip], map_line_routes: &MapLineRoutes<'_>) -> Vec<
direction_type: Some(get_direction_name(d)),
codes,
object_properties: PropertiesMap::default(),
comment_links: CommentLinksT::default(),
comment_links: LinksT::default(),
line_id: sr.id.clone(),
geometry_id: None,
destination_id: None,
Expand Down
17 changes: 10 additions & 7 deletions src/gtfs/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,11 @@ pub fn write_ticketing_deep_links(
}

/// get the first comment ordered by name
fn get_first_comment_name<T: objects::CommentLinks>(
fn get_first_comment_name<T: objects::Links<Comment>>(
obj: &T,
comments: &CollectionWithId<objects::Comment>,
) -> Option<String> {
obj.comment_links()
obj.links()
.iter()
.filter_map(|comment_id| comments.get(comment_id))
.map(|cmt| &cmt.name)
Expand Down Expand Up @@ -550,7 +550,7 @@ mod tests {
calendars::write_calendar_dates,
gtfs::{Route, RouteType, StopLocationType, Transfer, TransferType},
model::Collections,
objects::{Calendar, CommentLinksT, Coord, StopPoint, StopTime, Transfer as NtfsTransfer},
objects::{Calendar, Coord, LinksT, StopPoint, StopTime, Transfer as NtfsTransfer},
};
use geo::{line_string, point};
use pretty_assertions::assert_eq;
Expand Down Expand Up @@ -1053,7 +1053,7 @@ mod tests {
name: "sa:01".to_string(),
codes: sa_codes,
object_properties: PropertiesMap::default(),
comment_links: CommentLinksT::default(),
comment_links: LinksT::default(),
visible: true,
coord: Coord {
lon: 2.073,
Expand Down Expand Up @@ -1250,7 +1250,8 @@ mod tests {
id: "vj:01".to_string(),
codes: BTreeSet::new(),
object_properties: PropertiesMap::default(),
comment_links: CommentLinksT::default(),
comment_links: LinksT::default(),
odt_reservation_links: LinksT::default(),
route_id: "r:01".to_string(),
physical_mode_id: "pm:01".to_string(),
dataset_id: "ds:01".to_string(),
Expand Down Expand Up @@ -1324,7 +1325,8 @@ mod tests {
code: None,
codes: BTreeSet::default(),
object_properties: PropertiesMap::default(),
comment_links: BTreeSet::default(),
comment_links: LinksT::default(),
odt_reservation_links: LinksT::default(),
forward_name: None,
backward_name: None,
color: None,
Expand Down Expand Up @@ -1370,7 +1372,8 @@ mod tests {
code: Some("DEF".to_string()),
codes: BTreeSet::default(),
object_properties: PropertiesMap::default(),
comment_links: BTreeSet::default(),
comment_links: LinksT::default(),
odt_reservation_links: LinksT::default(),
forward_name: Some("Hôtels - Hôtels".to_string()),
backward_name: Some("Hôtels - Hôtels".to_string()),
color: Some(objects::Rgb {
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ pub mod vptranslator;
pub(crate) const STOP_TIMES_INIT_CAPACITY: usize = 50;

/// Current version of the NTFS format
pub const NTFS_VERSION: &str = "0.15.0";
pub const NTFS_VERSION: &str = "0.16.0";

/// The max distance in meters to compute the transfer
pub const TRANSFER_MAX_DISTANCE: &str = "300";
Expand Down
32 changes: 21 additions & 11 deletions src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ pub struct Collections {
pub calendars: CollectionWithId<Calendar>,
pub companies: CollectionWithId<Company>,
pub comments: CollectionWithId<Comment>,
pub odt_reservations: CollectionWithId<ODTReservation>,
pub equipments: CollectionWithId<Equipment>,
pub transfers: Collection<Transfer>,
pub trip_properties: CollectionWithId<TripProperty>,
Expand Down Expand Up @@ -217,6 +218,7 @@ impl Collections {
let mut data_sets_used = HashSet::<String>::new();
let mut physical_modes_used = HashSet::<String>::new();
let mut comments_used = HashSet::<String>::new();
let mut odt_reservations_used = HashSet::<String>::new();
let mut level_id_used = HashSet::<String>::new();
let mut calendars_used = HashSet::<String>::new();
let mut vjs_used = HashSet::<String>::new();
Expand Down Expand Up @@ -252,6 +254,8 @@ impl Collections {
data_sets_used.insert(vj.dataset_id.clone());
physical_modes_used.insert(vj.physical_mode_id.clone());
comments_used.extend(&mut vj.comment_links.iter().map(|cl| cl.to_string()));
odt_reservations_used
.extend(&mut vj.odt_reservation_links.iter().map(|id| id.to_string()));
vjs_used.insert(vj.id.clone());
true
} else {
Expand Down Expand Up @@ -383,6 +387,8 @@ impl Collections {
networks_used.insert(l.network_id.clone());
commercial_modes_used.insert(l.commercial_mode_id.clone());
comments_used.extend(&mut l.comment_links.iter().map(|cl| cl.to_string()));
odt_reservations_used
.extend(&mut l.odt_reservation_links.iter().map(|id| id.to_string()));
true
} else {
log_object_removed("Line", &l.id);
Expand Down Expand Up @@ -475,6 +481,11 @@ impl Collections {
comments_used.contains(&comment.id)
}));

self.odt_reservations.retain(log_predicate(
"ODTReservation",
|odt_reservation: &ODTReservation| odt_reservations_used.contains(&odt_reservation.id),
));

self.lines = CollectionWithId::new(lines)?;
self.stop_points = CollectionWithId::new(stop_points)?;
let stop_point_old_idx_to_new_idx: HashMap<Idx<StopPoint>, Idx<StopPoint>> = self
Expand Down Expand Up @@ -943,13 +954,13 @@ impl Collections {
collection: &mut CollectionWithId<T>,
duplicate2ref: &BTreeMap<String, String>,
) where
T: Id<T> + CommentLinks,
T: Id<T> + Links<Comment>,
{
let map_pt_object_duplicates: BTreeMap<Idx<T>, Vec<&str>> = collection
.iter()
.filter_map(|(idx, pt_object)| {
let intersection: Vec<&str> = pt_object
.comment_links()
.links()
.iter()
.filter_map(|comment_id| {
duplicate2ref
Expand All @@ -968,25 +979,23 @@ impl Collections {
for (idx, intersection) in map_pt_object_duplicates {
for i in intersection {
let mut pt_object = collection.index_mut(idx);
pt_object.comment_links_mut().remove(i);
pt_object
.comment_links_mut()
.insert(duplicate2ref[i].clone());
pt_object.links_mut().remove(i);
pt_object.links_mut().insert(duplicate2ref[i].clone());
}
}
}
}

/// Remove comments with empty message from the model
pub fn clean_comments(&mut self) {
fn remove_comment<T: Id<T> + CommentLinks>(
fn remove_comment<T: Id<T> + Links<Comment>>(
collection: &mut CollectionWithId<T>,
comment_id: &str,
) {
let object_idxs: Vec<Idx<T>> = collection
.iter()
.filter_map(|(idx, object)| {
if object.comment_links().contains(comment_id) {
if object.links().contains(comment_id) {
Some(idx)
} else {
None
Expand All @@ -996,7 +1005,7 @@ impl Collections {
for object_idx in object_idxs {
collection
.index_mut(object_idx)
.comment_links_mut()
.links_mut()
.remove(comment_id);
}
}
Expand Down Expand Up @@ -1893,7 +1902,7 @@ mod tests {
name: String::new(),
..Default::default()
};
let mut comment_links = CommentLinksT::default();
let mut comment_links = LinksT::default();
comment_links.insert(comment.id.clone());
comment_links.insert(empty_comment.id.clone());
collections.comments.push(comment).unwrap();
Expand Down Expand Up @@ -2072,7 +2081,8 @@ mod tests {
id: String::from(trip_id),
codes: KeysValues::default(),
object_properties: PropertiesMap::default(),
comment_links: CommentLinksT::default(),
comment_links: LinksT::default(),
odt_reservation_links: LinksT::default(),
route_id: String::from("route_id"),
physical_mode_id: String::new(),
dataset_id: String::new(),
Expand Down
5 changes: 3 additions & 2 deletions src/netex_france/route_points.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ mod tests {

mod build_route_points {
use super::*;
use crate::objects::{CommentLinksT, KeysValues, PropertiesMap, StopTime, Time};
use crate::objects::{KeysValues, LinksT, PropertiesMap, StopTime, Time};
use pretty_assertions::assert_eq;

fn stop_time(
Expand All @@ -126,7 +126,8 @@ mod tests {
id,
codes: KeysValues::default(),
object_properties: PropertiesMap::default(),
comment_links: CommentLinksT::default(),
comment_links: LinksT::default(),
odt_reservation_links: LinksT::default(),
route_id: String::from("route_id"),
physical_mode_id: String::from("Bus"),
dataset_id: String::from("dataset_id"),
Expand Down
Loading

0 comments on commit b00bdbb

Please sign in to comment.