Finished code for building TSJson struct.
[?]
CrEcTsRjb1hHQjHuumqRfqdbVV4X58iLEubi4noaDPFa
Jul 22, 2021, 5:32 AM
XPXYFEZMZJSF2GOFG5HTCMLVHINNVRYIVWV7KJ6A7A3PG3CM2TGACDependencies
- [2]
BB2T6X3XImproved documentation. - [3]
5B2HBV3JCompleted first try at ts Json data-structure. - [4]
SPSFTMLRCompleted loading ts_data from specification. - [5]
2SABVMY3Finished into_json() functionality. - [6]
2CCG6KUPRedo time-series spec. - [7]
TTR5IFSGWorking on building generic TSSpec. - [8]
GQVS55HIFinished generate_ts_spec() function. - [9]
SAHJYVNBRemoved checking functionality. - [10]
UKQAGL5FChecked ts_json toolchain. - [11]
77SIQZ3ESeparating out spec generation. - [12]
K4CH53V4Added GPL2 license, included missing source files. - [13]
UUD3CJZLMaking error handling more comprehensive. - [14]
XI5ALEH6Take advantage of keytree FromStr functionality. - [15]
TSY4YBBZChanged ts Spec datastructures to new format. - [*]
GUXZCEWWAdded Country enum. - [*]
4MG5JFXTFirst record.
Change contents
- replacement in src/ts.rs at line 25
pub struct Json(BTreeMap<(Country, DataType, usize), Vec<GraphicJson>>);pub struct Json(BTreeMap<(Country, DataType, usize), (Vec<SeriesJson>, Vec<GraphicJson>)>); - edit in src/ts.rs at line 30
// Title is displayed if Some otherwise no or default title displayed. - replacement in src/ts.rs at line 32
graphics: Vec<SeriesJson>,// Reference to series data.series_ref: Vec<usize>,// Specifies what kind of text appears under the graphic. - replacement in src/ts.rs at line 78
pub (crate) fn into_json(&self, root_path: &str) -> Result<Json, Error> {pub fn into_json(&self, root_path: &str) -> Result<Json, Error> { - edit in src/ts.rs at line 81
// Iterate over pages. - replacement in src/ts.rs at line 85
- replacement in src/ts.rs at line 96
let mut v: Vec<GraphicJson> = Vec::new();let mut v_graphics: Vec<GraphicJson> = Vec::new();let mut m_series: BTreeMap<SeriesId, (usize, SeriesJson)> = BTreeMap::new(); - edit in src/ts.rs at line 100
// Iterate over graphics on one page - edit in src/ts.rs at line 118
// Iterate over series_ids and build series_json first. There is a one-to-one// correspondence between SeriesSpec and SeriesJson so we can use the indices in// SeriesSpec to build GraphicJson.// Push SeriesJson Vec<SeriesJson>for (i, series_id) in series_ids.iter().enumerate() {m_series.insert(series_id.clone(),(i,page_spec.into_series_json(series_id, *country, root_path)?,));} - edit in src/ts.rs at line 136
// Construct GraphicJson for a given Graphic - replacement in src/ts.rs at line 140
graphics: Vec::new(),series_ref: Vec::new(), - replacement in src/ts.rs at line 145
graphic_json.graphics.push(page_spec.into_series_json(series_id, *country, root_path)?);let series_ref = match m_series.get(series_id) {Some((series_ref, _)) => series_ref,None => {return Err(failed_to_reference_series(file!(),line!(),&series_id.to_string(),))},};graphic_json.series_ref.push(*series_ref); - replacement in src/ts.rs at line 158
// Push GraphicJson to temporary Vec.// Push GraphicJson to Vec<GraphicJson> - replacement in src/ts.rs at line 160
v.push(graphic_json);v_graphics.push(graphic_json); - replacement in src/ts.rs at line 163
// Push key and value to json// Now that we have iterated over all the graphics on one page we can insert the// collection of SeriesJsons and the collection of GraphicsJsons into Json.// Convert v_series to a Vec. - replacement in src/ts.rs at line 170
json.insert((country, data_type, index), v);let mut v_series: Vec<SeriesJson> = Vec::new();for (_, (_, series_json)) in m_series.into_iter() {v_series.push(series_json);};json.insert((*country, *data_type, *index), (v_series, v_graphics)); - replacement in src/ts.rs at line 177
Ok(Json(BTreeMap::new()))Ok(Json(json)) - edit in src/ts.rs at line 235
- replacement in src/ts.rs at line 248
let rts = series_spec.data_with_transforms()?;let meta = series_spec.meta(country, root_path);let rts = series_spec.read_data_with_transforms(country, root_path)?;let meta = series_spec.read_meta_from_file(country, root_path); - replacement in src/ts.rs at line 302
/// Specifies what text a graphic displays./// Specifies what kind of text is displayed under a graphic. - replacement in src/main.rs at line 45
let data_spec = DataSpec::from_file("source_data.keytree").unwrap();// let data_spec = DataSpec::from_file("source_data.keytree").unwrap(); - replacement in src/main.rs at line 50
println!("{}", data_spec.generic_ts_spec().keytree());// println!("{}", data_spec.generic_ts_spec().keytree()); - replacement in src/main.rs at line 56
// let ts_spec = ts::Spec::from_file("ts_spec.keytree").unwrap();let ts_spec = ts::Spec::from_file("ts_spec.keytree").unwrap();// println!("{}", ts_spec.keytree());ts_spec.into_json(&root_dir).unwrap(); - replacement in src/lib.rs at line 278
/// Represents a FRED series id./// Represents a FRED series id like `LRHUTTTTAUA156N` or a transformation on a FRED series_id/// like `LRHUTTTTAUA156N_a`. - edit in src/lib.rs at line 288
}/// Return the component without transformation modifications.pub fn stem(&self) -> Self {let inner = self.0.split('_').next().unwrap().clone();SeriesId(String::from(inner)) - edit in src/lib.rs at line 392
#[derive(Debug)] - replacement in src/lib.rs at line 572
series_spec.write_data(root_path)?;series_spec.write_meta(root_path)?;series_spec.write_data_to_file(root_path)?;series_spec.write_meta_to_file(root_path)?; - replacement in src/lib.rs at line 590
series_spec.write_data(root_path)?;series_spec.write_meta(root_path)?;series_spec.write_data_to_file(root_path)?;series_spec.write_meta_to_file(root_path)?; - replacement in src/lib.rs at line 651
series_spec.write_data(root_path)?;series_spec.write_meta(root_path)?;series_spec.write_data_to_file(root_path)?;series_spec.write_meta_to_file(root_path)?; - replacement in src/lib.rs at line 697
// We pass country because `series_spec.country` is None./// Return the meta data for a `SeriesSpec`.pub fn meta(&self, country: Country, root_path: &str) -> SeriesMetaData/// Read metadata from file.pub fn read_meta_from_file(&self, country: Country, root_path: &str) -> SeriesMetaData - replacement in src/lib.rs at line 704
self.series_id.clone(),self.series_id.stem(), - replacement in src/lib.rs at line 714
/// Check if a file exists./// Check if data and meta-data exist in a file. - replacement in src/lib.rs at line 741
/// Return `Self` given a `series_id`.pub fn from_series_id(series_id: &str, root_path: &str) -> Result<Self, Error> {let sid = SeriesId::from_str(series_id).unwrap();let data_spec = DataSpec::from_file(root_path)?;// /// Return `Self` given a `series_id`.// pub fn from_series_id(series_id: &str, root_path: &str) -> Result<Self, Error> {// let sid = SeriesId::from_str(series_id).unwrap();// let data_spec = DataSpec::from_file(root_path)?; - replacement in src/lib.rs at line 746[3.1498]→[3.1498:1582](∅→∅),[3.1582]→[3.17683:17895](∅→∅),[3.17895]→[3.1669:1680](∅→∅),[3.1669]→[3.1669:1680](∅→∅)
let key = match data_spec.reverse.get(&sid) {Some(key) => key,None => { return Err(series_id_not_in_dataspec(file!(),line!(),&series_id.to_string()))},};// let key = match data_spec.reverse.get(&sid) {// Some(key) => key,// None => { return Err(// series_id_not_in_dataspec(// file!(),// line!(),// &series_id.to_string()// )// )},// }; - replacement in src/lib.rs at line 757
match data_spec.map.get(&key) {Some(series_specs) => {Ok(series_specs.iter().find(|series_spec| series_spec.series_id == sid).unwrap().clone())},None => unreachable!(),}}pub fn data_with_transforms(&self) -> Result<RegularTimeSeries<1>, Error> {// match data_spec.map.get(&key) {// Some(series_specs) => {// Ok(// series_specs.iter()// .find(|series_spec| series_spec.series_id == sid)// .unwrap()// .clone()// )// },// None => unreachable!(),// }// } - replacement in src/lib.rs at line 770
let mut rts = self.data_without_transform()?;pub fn read_data_with_transforms(&self,country: Country,root_path: &str) -> Result<RegularTimeSeries<1>, Error>{let mut rts = self.read_data_without_transform(country, root_path)?; - replacement in src/lib.rs at line 803
pub fn read_data_without_transform(&self,country: Country,root_path: &str) -> Result<RegularTimeSeries<1>, Error>{let series_id_stem = self.series_id.stem(); - replacement in src/lib.rs at line 812
/// Use `Self` to make a Fred request for series data, and return a `RegularTimeSeries`.pub fn data_without_transform(&self) -> Result<RegularTimeSeries<1>, Error> {let csv_path = data_path(root_path,self.data_type,country,series_id_stem,"csv",);let ts = TimeSeries::<1>::from_csv(&csv_path).map_err(|err| {external(file!(), line!(), &err.to_string())})?;let rts = ts.try_into().map_err(|err: time_series::error::Error| {external(file!(), line!(), &err.to_string())})?;Ok(rts)}pub fn data_from_fred(&self,country: Country) -> Result<RegularTimeSeries<1>, Error>{ - replacement in src/lib.rs at line 870
&self.country.unwrap().to_string(),&country.to_string(), - edit in src/lib.rs at line 896
&country.to_string(), - edit in src/lib.rs at line 898
// Expect country to be Some while building source.&self.country.unwrap().to_string(), - edit in src/lib.rs at line 907
// pub fn data_without_transform(// &self,// country: Country,// root_path: &str) -> Result<RegularTimeSeries<1>, Error>// {// // Select the observation data from the FRED data.//// let observations = match Fred::series_observations(&self.series_id.to_string()) {// Ok(series_obs) => series_obs.observations,// Err(err) => { return Err(fred_error(file!(), line!(), &err.to_string())) },// };// // We need to build a RegularTimeSeries here, and then use RegularTimeSeries here// // to build csv file.// // Data is parsed into a RegularTimeSeries// // Want power to drop_first// let mut v = Vec::new();// // Drop the first n items// let skip = match self.drop_first {// None => 0,// Some(n) => n,// };// for (i, obs) in observations.iter().enumerate().skip(skip) {// let date = MonthlyDate::from_str(&obs.date)?;// let value: f32 = match obs.value.parse() {// Ok(n) => n,// Err(_) => {// let err = parse_fred_value_failed(// file!(),// line!(),// &self.data_type.to_string(),// // Expect country to be Some while building source.// &country.to_string(),// &self.series_id.to_string(),// &format!(// "{}, {}",// obs.date,// obs.value,// ),// i + 1,// );// self.soft_parse(observations, err);// unreachable!();// },// };// let date_point = DatePoint::<1>::new(date.0, [value]);// v.push(date_point)// }// let ts = TimeSeries::new(v);// let rts = match ts.try_into() {// Ok(rts) => {// rts// },// Err(_) => {// let err = expected_regular_time_series(// file!(),// line!(),// &country.to_string(),// &self.data_type.to_string(),// &self.series_id.to_string(),// );// self.soft_parse(observations, err);// unreachable!();// },// };// Ok(rts)// } - replacement in src/lib.rs at line 997
pub fn write_data(&self, root_path: &str) -> Result<(), Error> {pub fn write_data_to_file(&self, root_path: &str) -> Result<(), Error> {// When this function is used, Self.country should be Some.let rts = self.data_from_fred(self.country.unwrap())?; - replacement in src/lib.rs at line 1002
let rts = self.data_without_transform()?;// There should be a function here to get the data from Fred. - edit in src/lib.rs at line 1025
// Expect country to be Some while building source. - replacement in src/lib.rs at line 1059
pub fn write_meta(&self, root_path: &str) -> Result<(), Error> {pub fn write_meta_to_file(&self, root_path: &str) -> Result<(), Error> { - edit in src/error.rs at line 33
data_type: &str, - edit in src/error.rs at line 34
data_type: &str, - edit in src/error.rs at line 41
country, - edit in src/error.rs at line 43
country, - edit in src/error.rs at line 44
))}pub fn external(code_file: &str,code_line: u32,msg: &str) -> Error{Error(format!("[ui_data:20:{}:{}] External: {}",code_file,code_line,msg, - edit in src/error.rs at line 320
- edit in src/error.rs at line 341[3.20259]