Skip to content

Commit

Permalink
Merge pull request #538 from woshilapin/validity_period
Browse files Browse the repository at this point in the history
[tech] Calculate Validity period is done in Collections
  • Loading branch information
mergify[bot] authored Feb 6, 2020
2 parents f3be312 + 5058cea commit 205a311
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 24 deletions.
18 changes: 18 additions & 0 deletions src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -988,6 +988,24 @@ impl Collections {

self.stop_areas = CollectionWithId::new(updated_stop_areas).unwrap();
}

/// Calculate the validity period in the 'Model'.
/// The calculation is based on the minimum start date and the maximum end
/// date of all the datasets.
/// If no dataset is found, an error is returned.
pub fn calculate_validity_period(&self) -> Result<(Date, Date)> {
let start_date = self
.datasets
.values()
.map(|dataset| dataset.start_date)
.min();
let end_date = self.datasets.values().map(|dataset| dataset.end_date).max();
if let (Some(start_date), Some(end_date)) = (start_date, end_date) {
Ok((start_date, end_date))
} else {
bail!("Cannot calculate validity period because there is no dataset")
}
}
}

/// The navitia transit model.
Expand Down
11 changes: 3 additions & 8 deletions src/ntfs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ pub fn write<P: AsRef<path::Path>>(
let path = path.as_ref();
info!("Writing NTFS to {:?}", path);

write::write_feed_infos(path, &model.feed_infos, &model.datasets, current_datetime)?;
write::write_feed_infos(path, &model, current_datetime)?;
write_collection_with_id(path, "contributors.txt", &model.contributors)?;
write_collection_with_id(path, "datasets.txt", &model.datasets)?;
write_collection_with_id(path, "networks.txt", &model.networks)?;
Expand Down Expand Up @@ -379,15 +379,10 @@ mod tests {

let mut collections = Collections::default();
collections.datasets = CollectionWithId::from(dataset);
collections.feed_infos = feed_infos;

test_in_tmp_dir(|path| {
write::write_feed_infos(
path,
&feed_infos,
&collections.datasets,
get_test_datetime(),
)
.unwrap();
write::write_feed_infos(path, &collections, get_test_datetime()).unwrap();
read::manage_feed_infos(&mut collections, path).unwrap();
assert_eq!(
vec![
Expand Down
28 changes: 12 additions & 16 deletions src/ntfs/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use csv::Writer;
use failure::{bail, format_err, ResultExt};
use log::{info, warn};
use rust_decimal::{prelude::ToPrimitive, Decimal};
use std::collections::{BTreeMap, BTreeSet, HashMap};
use std::collections::{BTreeSet, HashMap};
use std::convert::TryFrom;
use std::fs::File;
use std::path;
Expand Down Expand Up @@ -54,13 +54,12 @@ impl TryFrom<(&Ticket, &TicketPrice)> for PriceV1 {

pub fn write_feed_infos(
path: &path::Path,
feed_infos: &BTreeMap<String, String>,
datasets: &CollectionWithId<Dataset>,
collections: &Collections,
current_datetime: NaiveDateTime,
) -> Result<()> {
info!("Writing feed_infos.txt");
let path = path.join("feed_infos.txt");
let mut feed_infos = feed_infos.clone();
let mut feed_infos = collections.feed_infos.clone();
feed_infos.insert(
"feed_creation_date".to_string(),
current_datetime.format("%Y%m%d").to_string(),
Expand All @@ -70,18 +69,15 @@ pub fn write_feed_infos(
current_datetime.format("%T").to_string(),
);
feed_infos.insert("ntfs_version".to_string(), NTFS_VERSION.to_string());
if let Some(d) = datasets.values().min_by_key(|d| d.start_date) {
feed_infos.insert(
"feed_start_date".to_string(),
d.start_date.format("%Y%m%d").to_string(),
);
}
if let Some(d) = datasets.values().max_by_key(|d| d.end_date) {
feed_infos.insert(
"feed_end_date".to_string(),
d.end_date.format("%Y%m%d").to_string(),
);
}
let (start_date, end_date) = collections.calculate_validity_period()?;
feed_infos.insert(
"feed_start_date".to_string(),
start_date.format("%Y%m%d").to_string(),
);
feed_infos.insert(
"feed_end_date".to_string(),
end_date.format("%Y%m%d").to_string(),
);

let mut wtr = csv::Writer::from_path(&path).with_context(ctx_from_path!(path))?;
wtr.write_record(&["feed_info_param", "feed_info_value"])
Expand Down

0 comments on commit 205a311

Please sign in to comment.