77SIQZ3EGGV6KSECMLPDKQFGEC7CCFAPWGER7ZARQ5STDKJNU6GQC
JTX5OHWHO647X4XLQPUDLH34QG2VDP7N7X6OL7XEIZQYVWELV6CAC
4MG5JFXTKAE3SOVKGGNKEUTNCKOWEBHTGKVZHJWLWE3PTZTQKHPAC
GUXZCEWWPBCHXO26JVWZ74CTSDFDDO775YR7FKY7UGVZCA7GCSYAC
AT753JPOOWZCIYNKAG27LZSZZ72ZILWVENG42Y6U2S34JD3B6ZZQC
UCSU3QE4352YC4VOVI7GCSTNNA5FAHYURNJLJQEHY32BB6S3CZVAC
CUADTSHQNPGMWHIJCWXKHNNIY4UJOYZ7XBZA5CJ2VVJJAMSNT4BAC
AIFRDCG2GLASIMF3WZYAQBHIGZZDGPMP2WBYH7HXIYORKAK2SUYAC
5POF332LJEBGWEJUGI34P33Q4BQP7CAQNV5ODQT3PWG7FI7VNLOQC
IKPVWWLKQZW7O23ZUSFFRE7IUUDFZR6UOCN6MVPNPMSTDSN7X4VAC
LURDUHBIQRLOMOMHK3OFOQ4N5S56C46JPAHRQ3PCXBJ4CDPE24HAC
//! Deserialize data to be served as JSON to build time-series plots.
use std::collections::HashMap;
use std::convert::{TryFrom, TryInto};
use std::env;
use std::fmt;
use std::fs;
use serde::{Serialize};
use serde_json;
use countries::Country;
use keytree::{KeyTree, KeyTreeRef};
use keytree::Error;
use time_series::{
RegularTimeSeries,
TimeSeries,
};
use crate::DataType;
/// The top level struct that holds all the source data.
pub struct SourceData(pub HashMap<PageKey, SourceJson>);
impl SourceData {
// Load the source data. Will panic on error.
pub fn new() -> Self {
let source_spec_file = fs::read_to_string("source_spec.keytree").unwrap();
let kt = match KeyTree::parse(&source_spec_file) {
Ok(kt) => kt,
Err(err) => {
println!("{}", err);
panic!();
},
};
let source_spec: SourceSpec = match kt.to_ref().try_into() {
Ok(source) => source,
Err(e) => {
println!("{}", e);
panic!();
}
};
let mut builder = HashMap::new();
for page_key in source_spec.0.keys() {
let source_json = SourceJson::new(page_key.data_type, page_key.country, &source_spec);
builder.insert(*page_key, source_json);
}
SourceData(builder)
}
}
/// The top-level specification for source data.
///
/// "source_spec.keytree" file.
/// ```text
/// source:
/// html_page:
/// key:
/// data_type: u
/// country: Australia
/// value:
/// graphic:
/// data: AUSURAMS_a
/// graphic:
/// data: AUSURANAA_a
/// ```
pub struct SourceSpec(pub HashMap<PageKey, PageValue>);
impl<'a> TryInto<SourceSpec> for KeyTreeRef<'a> {
type Error = Error;
fn try_into(self) -> Result<SourceSpec, Error> {
let html_pages: Vec<SourceHtmlPage> = self.vec("source::html_page")?;
let mut h = HashMap::new();
for html_page in html_pages {
h.insert(html_page.key, html_page.value);
}
Ok(SourceSpec(h))
}
}
impl SourceSpec {
/// Return a `Vec` of all series ids in the spec.
pub fn all_series(&self) -> Vec<String> {
let mut v = Vec::new();
for (_, page_value) in self.0.iter() {
for graphic_spec in &page_value.0 {
for series_id in &graphic_spec.series_ids {
v.push(series_id.clone())
}
}
}
v
}
}
/// A container for the specification for a particular datatype and country.
/// ```text
/// html_page:
/// key:
/// data_type: u
/// country: Australia
/// value:
/// graphic:
/// data: AUSURAMS_a
/// graphic:
/// data: AUSURANAA_a
/// ```
pub struct SourceHtmlPage {
pub key: PageKey,
pub value: PageValue,
}
// impl HtmlPage {
// json_data() -> String
// get csv data from file
//
// }
impl<'a> TryInto<SourceHtmlPage> for KeyTreeRef<'a> {
type Error = Error;
fn try_into(self) -> Result<SourceHtmlPage, Error> {
Ok(SourceHtmlPage {
key: self.at("html_page::key")?,
value: self.at("html_page::value")?,
})
}
}
/// `(DataType, Country)` key to lookup data or spec.
#[derive(Clone, Copy, Eq, Hash, PartialEq)]
pub struct PageKey {
pub data_type: DataType,
pub country: Country,
}
impl<'a> TryInto<PageKey> for KeyTreeRef<'a> {
type Error = Error;
fn try_into(self) -> Result<PageKey, Error> {
let data_type_str: String = self.at("key::data_type")?;
// All KeyTree strings are internal, so if this fails it is a bug.
let data_type = DataType::from_str(&data_type_str).unwrap();
let country_str: String = self.at("key::country")?;
let country = Country::from_str(&country_str).unwrap();
Ok(PageKey {
data_type: data_type,
country: country,
})
}
}
/// Collection of graphics specification.
pub struct PageValue(pub Vec<GraphicSpec>);
impl<'a> TryInto<PageValue> for KeyTreeRef<'a> {
type Error = Error;
fn try_into(self) -> Result<PageValue, Error> {
Ok(PageValue(self.vec("value::graphic")?))
}
}
/// Specifies the data files required for a graphic in the '/source' route.
pub struct GraphicSpec {
pub height: Option<f32>,
// e.g. AUSURHARMADSMEI, ...
pub series_ids: Vec<String>,
}
impl<'a> TryInto<GraphicSpec> for KeyTreeRef<'a> {
type Error = Error;
fn try_into(self) -> Result<GraphicSpec, Error> {
Ok(
GraphicSpec {
height: self.op("graphic::height")?,
series_ids: self.vec("graphic::data")?,
}
)
}
}
/// `SourceJson` is a collection of time-series convertible to JSON.
#[derive(Serialize)]
pub struct SourceJson {
graphs: Vec<SeriesJson>,
}
impl SourceJson {
/// Given `data_type`, `country` and source specification, return Self. `data_path` is the full
/// path to the root of the data files.
pub fn new(data_type: DataType, country: Country, spec: &SourceSpec) -> Self {
let page_key = PageKey {
data_type: data_type,
country: country,
};
let page_value = spec.0.get(&page_key).unwrap();
let mut v = Vec::new();
for graphic_spec in &page_value.0 {
v.push(SeriesJson::new(&graphic_spec, country, data_type));
}
SourceJson { graphs: v }
}
}
/// `SeriesJson` is a time-series with meta-data convertible to JSON.
#[derive(Serialize)]
pub struct SeriesJson {
data: Vec<(RegularTimeSeries<1>, SeriesMetaData)>,
}
impl SeriesJson {
pub fn new(graphic_spec: &GraphicSpec, country: Country, data_type: DataType) -> Self {
let mut v = Vec::new();
for series_id in &graphic_spec.series_ids {
let (time_series, meta) = data_from_csv(series_id, country, data_type);
v.push((time_series, meta));
}
SeriesJson { data: v }
}
// We want to break new() down into something that just takes a series_id and gets the date. Once
// we've done that we can use it for source or ui or anything. The common data-structure is
// (series_id) ??
}
fn no_label(filename: &str) -> String {
filename[..filename.len() - 2].into()
}
/// Source meta-data is stored in a file as a String:
///
/// ```text
/// series:
/// realtime_start: 2021-06-03
/// realtime_end: 2021-06-03
/// series_items:
/// series_item:
/// realtime: 2021-06-03
/// id: AUSCPALTT01IXNBQ
/// title: Consumer Price Index: All items: Total: Total for Australia
/// observation_start: 1960-01-01
/// observation_end: 2021-01-01
/// frequency: Quarterly
/// seasonal_adjustment: Not Seasonally Adjusted
/// notes: (see JSON data for notes)
/// ```
#[derive(Serialize)]
pub struct SeriesMetaData {
realtime: String,
id: String,
title: String,
observation_start: String,
observation_end: String,
frequency: String,
seasonal_adjustment: String,
}
impl<'a> TryInto<SeriesMetaData> for KeyTreeRef<'a> {
type Error = Error;
fn try_into(self) -> Result<SeriesMetaData, Error> {
Ok(
SeriesMetaData {
realtime: self.at("series::series_items::series_item::realtime_start")?,
id: self.at("series::series_items::series_item::id")?,
title: self.at("series::series_items::series_item::title")?,
observation_start: self.at("series::series_items::series_item::observation_start")?,
observation_end: self.at("series::series_items::series_item::observation_end")?,
frequency: self.at("series::series_items::series_item::frequency")?,
seasonal_adjustment: self.at("series::series_items::series_item::seasonal_adjustment")?,
}
)
}
}
/// Read a series from file.
pub fn data_from_csv(
series_id: &str,
country: Country,
data_type: DataType) -> (RegularTimeSeries<1>, SeriesMetaData)
{
let root = env::current_dir().unwrap();
let csv_path = &format!(
"{}/contents/data/{}/{}/{}.csv",
root.display(),
data_type,
country.as_path(),
series_id,
);
let meta_path = &format!(
"{}/contents/data/{}/{}/{}.meta",
root.display(),
data_type,
country.as_path(),
no_label(&series_id),
);
let time_series = match RegularTimeSeries::try_from(
TimeSeries::<1>::from_csv(csv_path)
) {
Ok(ts) => ts,
Err(_) => {
println!("country: {}", country);
println!("data_type: {}", data_type);
println!("series_id: {}", series_id);
println!("Data not contiguous.");
panic!();
},
};
let meta_str = fs::read_to_string(meta_path).unwrap();
let kt = KeyTree::parse(&meta_str).unwrap();
let meta: SeriesMetaData = kt.to_ref().try_into().unwrap();
(time_series, meta)
}
let root_dir = shellexpand::tilde("~/test_data");
save_cpi_data(&Country::UnitedStates, &format!("{}/cpi/united_states", root_dir));
// let root_dir = shellexpand::tilde("~/test_data/cpi");
// // "usa;interest",
// // "prime",
// // "australia;interest" -> nothing
// for item in Fred::tags_series("australia;interest")
// .unwrap()
// .series()
// .iter()
// {
// println!("{}", item);
// // println!("{}", item.tags());
// }
// let spec_path = shellexpand::tilde("~/currency.engineering/source_spec.keytree");
// let data_path = shellexpand::tilde("~/test_data");
// spec_to_data::save_data(&spec_path, &data_path);
println!("{}", build_data_spec());
//! Collect unemployment rate, inflation rate and interest rate server side.
//! Collect unemployment rate, inflation rate and interest rate data server side.
//!
//! Probably the most
//! effective way to find stuff, is to search on tags and then filter on series title, such as
//! ```
//! for item in Fred::tags_series("usa;interest")
//! .unwrap()
//! .series()
//! .has_phrase("Prime")
//! .iter()
//! {
//! println!("{}", item);
//! }
//! ```
//! The filters such as `has_phrase` are found in the fred_api crate.
use fred_api::response::SeriesItems;
/// Save FRED data to disk as csv, using full path.
/// ```
/// save_data_csv("LRUNTTTTSIQ156S");
/// ```
/// To handle breaks the data is saved in a collection of files "LRUNTTTTSIQ156S_a.csv",
/// "LRUNTTTTSIQ156S_b.csv" etc.
///
pub fn save_data_csv(series_id: &str, path: &str) {
let alphabet = "abcdefghijklmnopqrstuvwxyz";
let mut chars = alphabet.chars();
let mut contains_data = false;
let mut data = String::new();
let observations = match Fred::series_observations(series_id) {
Ok(series_obs) => series_obs.observations,
Err(err) => {
println!("{}", err);
panic! ();
},
};
for obs in observations.iter() {
let value: Result<f32, _> = obs.value.parse();
match value {
Ok(_) => {
contains_data = true;
data.push_str(&obs.to_string());
data.push('\n');
},
Err(_) => {
if contains_data {
data.pop();
fs::create_dir_all(&path).unwrap();
fs::write(
&format!(
"{}/{}_{}.csv",
path,
series_id,
chars.next().expect("Too many breaks"),
),
&data,
).unwrap();
data = String::new();
contains_data = false;
}
},
}
}
if contains_data {
fs::create_dir_all(&path).unwrap();
fs::write(
&format!(
"{}/{}_{}.csv",
path,
series_id,
chars.next().expect("Too many breaks"),
),
&data,
).unwrap();
}
}
use fred_api::SeriesItems;
/// Save FRED series metadata including title to disk as json.
/// ```
/// save_series_meta("LRUNTTTTSIQ156S");
/// ```
pub fn save_series_meta(series_id: &str, full_path: &str) {
let data = match Fred::series(series_id) {
Ok(series) => series.to_string(),
Err(err) => {
println!("{}", err);
panic!();
},
};
fs::write(
&format!(
"{}/{}.meta",
full_path,
series_id,
),
&data,
).unwrap();
/// Unemployment rate, Interest rate, Inflation rate etc.
#[derive(Clone, Copy, Eq, Hash, PartialEq, Serialize)]
pub enum DataType {
/// Unemployment rate
U,
/// CPI
Cpi,
/// Inflation rate
Inf,
/// Interest rate
Int,
/// Returns relevant CPI series for a country, by pre-selecting series titles for each country.
///
/// ```
/// println!("{}", cpi_series(&Country::NewZealand));
/// ```
pub fn cpi_series(country: &Country) -> SeriesItems {
let titles = match country {
Country::Australia => {
vec!(
"Consumer Price Index: All items: Total: Total for Australia",
"Consumer Price Index: All Items for Australia",
"Consumer Price Index in Australia (DISCONTINUED)",
"Consumer Price Index: Total All Items for Australia",
"Consumer Price Index for Australia",
"Inflation, consumer prices for Australia",
)
}
Country::Austria => {
vec!(
"Consumer Price Index for Austria",
"Consumer Price Index: All items: Total: Total for Austria",
"Consumer Price Index: All Items for Austria",
"Consumer Price Index in Austria (DISCONTINUED)",
"Consumer Price Index: Harmonized Prices: Total All Items for Austria",
"Consumer Price Index: Harmonized Prices: Total All Items for Austria // (DISCONTINUED)",
"Consumer Price Index: Total All Items for Austria",
"Harmonized Index of Consumer Prices: All Items for Austria",
"Harmonized Index of Consumer Prices in Austria (DISCONTINUED)",
"Inflation, consumer prices for Austria",
"Consumer Price Index: All Items (Harmonized Index of Consumer Prices) for // Austria",
"Consumer Price Index: All Items (Harmonized Index of Consumer Prices) for Austria (DISCONTINUED)",
)
}
Country::Belgium => {
vec!(
"Consumer Price Index for Belgium",
"Consumer Price Index: Harmonized Prices: Total All Items for Belgium",
"Consumer Price Index: Harmonized Prices: Total All Items for Belgium (DISCONTINUED)",
"Consumer Price Index: Total All Items for Belgium",
"Harmonized Index of Consumer Prices: All Items for Belgium",
"Harmonized Index of Consumer Prices in Belgium (DISCONTINUED)",
"Consumer Price Index: All Items for Belgium",
"Consumer Price Index in Belgium (DISCONTINUED)",
"Consumer Price Index: All items: Total: Total for Belgium",
)
}
Country::Canada => {
vec!(
"Inflation, consumer prices for Canada",
"Consumer Price Index for Canada",
"Consumer Price Index: Total All Items for Canada",
"Consumer Price Index in Canada (DISCONTINUED)",
"Consumer Price Index of All Items in Canada",
"Consumer Price Index: All items: Total: Total for Canada",
)
}
Country::Chile => {
vec!(
"Consumer Price Index for Chile",
"Inflation, consumer prices for Chile",
"Consumer Price Index: Total All Items for Chile",
"Consumer Price Index: All Items for Chile",
"Consumer Price Index: All items: Total: Total for Chile",
)
}
Country::CzechRepublic => {
vec!(
"Consumer Price Index for Czech Republic",
"Consumer Price Index: All Items (Harmonized Index of Consumer Prices) for Czech Republic",
"Consumer Price Index: All Items (Harmonized Index of Consumer Prices) for Czech Republic (DISCONTINUED)",
"Consumer Price Index: All Items for Czech Republic",
"Consumer Price Index: All items: Total: Total for the Czech Republic",
"Consumer Price Index: Harmonized Prices: Total All Items for the Czech Republic",
"Consumer Price Index: Harmonized Prices: Total All Items for the Czech Republic (DISCONTINUED)",
"Consumer Price Index: Total All Items for the Czech Republic",
"Harmonized Index of Consumer Prices: All Items for Czech Republic",
)
}
Country::Denmark => {
vec!(
"Inflation, consumer prices for Denmark",
"Consumer Price Index: All Items (Harmonized Index of Consumer Prices) for Denmark",
"Consumer Price Index: All Items (Harmonized Index of Consumer Prices) for Denmark (DISCONTINUED)",
"Harmonized Index of Consumer Prices in Denmark (DISCONTINUED)",
"Consumer Price Index: All Items for Denmark",
"Consumer Price Index in Denmark (DISCONTINUED)",
"Consumer Price Index: All items: Total: Total for Denmark",
"Consumer Price Index for Denmark",
"Consumer Price Index: Total All Items for Denmark",
"Harmonized Index of Consumer Prices: All Items for Denmark",
)
}
Country::Estonia => {
vec!(
"Inflation, consumer prices for Estonia",
"Consumer Price Index: All items: Total: Total for Estonia",
"Consumer Price Index for Estonia",
"Consumer Price Index: Harmonized Prices: Total All Items for Estonia",
"Consumer Price Index: Harmonized Prices: Total All Items for Estonia (DISCONTINUED)",
"Consumer Price Index: Total All Items for Estonia",
"Harmonized Index of Consumer Prices: All Items for Estonia",
)
}
Country::Finland => {
vec!(
"Inflation, consumer prices for Finland",
"Consumer Price Index: All Items (Harmonized Index of Consumer Prices) for Finland",
"Consumer Price Index: All Items (Harmonized Index of Consumer Prices) for Finland (DISCONTINUED)",
"Consumer Price Index: All Items for Finland",
"Consumer Price Index: OECD Groups: Services: Total for Finland",
"Consumer Price Index: All items: Total: Total for Finland",
"Consumer Price Index: Harmonized Prices: Total All Items for Finland (DISCONTINUED)",
"Consumer Price Index for Finland",
"Consumer Price Index: Harmonized Prices: Total All Items for Finland",
"Consumer Price Index: Total All Items for Finland",
"Harmonized Index of Consumer Prices: All Items for Finland",
)
}
Country::France => {
vec!(
"Harmonized Index of Consumer Prices in France (DISCONTINUED)",
"Consumer Price Index: All Items (Harmonized Index of Consumer Prices) for France (DISCONTINUED)",
"Consumer Price Index: All Items (Harmonized Index of Consumer Prices) for France",
"Consumer Price Index in France (DISCONTINUED)",
"Consumer Price Index of All Items in France",
"Consumer Price Index: All items: Total: Total for France",
"Inflation, consumer prices for France",
"Consumer Price Index for France",
"Consumer Price Index: Harmonized Prices: Total All Items for France (DISCONTINUED)",
"Consumer Price Index: Harmonized Prices: Total All Items for France",
"Consumer Price Index: Total All Items for France",
)
}
Country::Germany => {
vec!(
"Inflation, consumer prices for Germany",
"Harmonized Index of Consumer Prices in Germany (DISCONTINUED)",
"Consumer Price Index: All Items (Harmonized Index of Consumer Prices) for Germany (DISCONTINUED)",
"Consumer Price Index: All Items (Harmonized Index of Consumer Prices) for Germany",
"Consumer Price Index in Germany (DISCONTINUED)",
"Consumer Price Index of All Items in Germany",
"Consumer Price Index: All items: Total: Total for Germany",
"Consumer Price Index for Germany",
"Consumer Price Index: Harmonized Prices: Total All Items for Germany (DISCONTINUED)",
"Consumer Price Index: Harmonized Prices: Total All Items for Germany",
"Consumer Price Index: Total All Items for Germany",
"Harmonized Index of Consumer Prices: All Items for Germany (including former GDR from 1991)",
)
}
Country::Greece => {
vec!(
"Consumer Price Index: All Items (Harmonized Index of Consumer Prices) for Greece",
"Consumer Price Index: All Items (Harmonized Index of Consumer Prices) for Greece (DISCONTINUED)",
"Consumer Price Index: All Items for Greece",
"Consumer Price Index: All items: Total: Total for Greece",
"Inflation, consumer prices for Greece",
"Consumer Price Index for Greece",
"Consumer Price Index: Harmonized Prices: Total All Items for Greece",
"Consumer Price Index: Harmonized Prices: Total All Items for Greece (DISCONTINUED)",
"Consumer Price Index: Total All Items for Greece",
"Harmonized Index of Consumer Prices: All Items for Greece",
)
}
Country::Ireland => {
vec!(
"Consumer Price Index: All Items (Harmonized Index of Consumer Prices) for Ireland",
"Consumer Price Index: All Items (Harmonized Index of Consumer Prices) for Ireland (DISCONTINUED)",
"Consumer Price Index: OECD Groups: Services: Total for Ireland",
"Consumer Price Index: All Items for Ireland",
"Consumer Price Index: All items: Total: Total for Ireland",
"Consumer Price Index: Harmonized Prices: Total All Items for Ireland (DISCONTINUED)",
"Consumer Price Index for Ireland",
"Consumer Price Index: Harmonized Prices: Total All Items for Ireland",
"Harmonized Index of Consumer Prices: All Items for Ireland",
)
}
Country::Israel => {
vec!(
"Consumer Price Index: All Items for Israel",
"Consumer Price Index: All items: Total: Total for Israel",
"Consumer Price Index for Israel",
"Inflation, consumer prices for Israel",
"Consumer Price Index: Total All Items for Israel",
)
}
Country::Italy => {
vec!(
"Harmonized Index of Consumer Prices in Italy (DISCONTINUED)",
"Consumer Price Index: All Items (Harmonized Index of Consumer Prices) for Italy (DISCONTINUED)",
"Consumer Price Index: All Items (Harmonized Index of Consumer Prices) for Italy",
"Consumer Price Index: Food for Italy",
"Consumer Price Index in Italy (DISCONTINUED)",
"Consumer Price Index of All Items in Italy",
"Consumer Price Index: All items: Total: Total for Italy",
"Inflation, consumer prices for Italy",
"Consumer Price Index for Italy",
"Consumer Price Index: Harmonized Prices: Total All Items for Italy (DISCONTINUED)",
"Consumer Price Index: Harmonized Prices: Total All Items for Italy",
"Consumer Price Index: Total All Items for Italy",
"Harmonized Index of Consumer Prices: All Items for Italy",
)
}
Country::Japan => {
vec!(
"Harmonized Index of Consumer Prices in Japan (DISCONTINUED)",
"Consumer Price Index in Japan (DISCONTINUED)",
"Consumer Price Index of All Items in Japan",
"Consumer Price Index: All items: Total: Total for Japan",
"Inflation, consumer prices for Japan",
"Not Seasonally Adjusted",
)
}
Country::Latvia => {
vec!(
"Consumer Price Index: All items: Total: Total for Latvia",
"Inflation, consumer prices for Latvia",
"Harmonized Index of Consumer Prices: Unprocessed Food for Latvia",
"Consumer Price Index for Latvia",
)
}
Country::Netherlands => {
vec!(
"Harmonized Index of Consumer Prices in Netherlands (DISCONTINUED)",
"Consumer Price Index: All Items (Harmonized Index of Consumer Prices) for Netherlands (DISCONTINUED)",
"Consumer Price Index: All Items (Harmonized Index of Consumer Prices) for Netherlands",
"Consumer Price Index in Netherlands (DISCONTINUED)",
"Consumer Price Index: All Items for Netherlands",
"Consumer Price Index: All items: Total: Total for the Netherlands",
"Consumer Price Index for Netherlands",
"Consumer Price Index: Harmonized Prices: Total All Items for the Netherlands",
"Consumer Price Index: Harmonized Prices: Total All Items for the Netherlands (DISCONTINUED)",
"Consumer Price Index: Total All Items for the Netherlands",
"Harmonized Index of Consumer Prices: All Items for Netherlands",
)
}
Country::NewZealand => {
vec!(
"Consumer Price Index: All Items for New Zealand",
"Consumer Price Index: All Items Excluding Food and Energy for New Zealand",
"Consumer Price Index: All items: Total: Total for New Zealand",
"Consumer Price Index for New Zealand",
"Inflation, consumer prices for New Zealand",
"Consumer Price Index: Total All Items for New Zealand",
)
}
Country::Norway => {
vec!(
"Consumer Price Index: All Items (Harmonized Index of Consumer Prices) for Norway",
"Consumer Price Index: All Items (Harmonized Index of Consumer Prices) for Norway (DISCONTINUED)",
"Consumer Price Index: All Items for Norway",
"Consumer Price Index in Norway (DISCONTINUED)",
"Consumer Price Index: All items: Total: Total for Norway",
"Consumer Price Index: Harmonized Prices: Total All Items for Norway (DISCONTINUED)",
"Consumer Price Index for Norway",
"Consumer Price Index: Harmonized Prices: Total All Items for Norway",
"Consumer Price Index: Total All Items for Norway",
"Harmonized Index of Consumer Prices: All Items for Norway",
)
}
Country::Poland => {
vec!(
"Consumer Price Index: All Items (Harmonized Index of Consumer Prices) for Poland",
"Consumer Price Index: All Items (Harmonized Index of Consumer Prices) for Poland (DISCONTINUED)",
"Consumer Price Index: Food for Poland",
"Consumer Price Index: All Items for Poland",
"Consumer Price Index: All items: Total: Total for Poland",
"Inflation, consumer prices for Poland",
"Consumer Price Index for Poland",
"Consumer Price Index: Total All Items for Poland",
"Harmonized Index of Consumer Prices: All Items for Poland",
)
}
Country::Serbia => {
vec!(
"Consumer Price Index for Serbia",
"Inflation, consumer prices for Serbia",
)
}
Country::SouthKorea => {
vec!(
"Consumer Price Index: All Items for Korea",
"Consumer Price Index: All items: Total: Total for the Republic of Korea",
"Inflation, consumer prices for the Republic of Korea",
"Consumer Price Index for Republic of Korea",
"Consumer Price Index: Total All Items for the Republic of Korea",
)
}
Country::Spain => {
vec!(
"Inflation, consumer prices for Spain",
"Consumer Price Index: All Items (Harmonized Index of Consumer Prices) for Spain (DISCONTINUED)",
"Consumer Price Index: All Items (Harmonized Index of Consumer Prices) for Spain",
"Consumer Price Index in Spain (DISCONTINUED)",
"Consumer Price Index: All Items for Spain",
"Consumer Price Index: All items: Total: Total for Spain",
"Consumer Price Index for Spain",
"Consumer Price Index: Harmonized Prices: Total All Items for Spain (DISCONTINUED)",
"Consumer Price Index: Harmonized Prices: Total All Items for Spain",
"Consumer Price Index: Total All Items for Spain",
"Harmonized Index of Consumer Prices: All Items for Spain",
)
}
Country::Sweden => {
vec!(
"Harmonized Index of Consumer Prices in Sweden (DISCONTINUED)",
"Consumer Price Index: All Items (Harmonized Index of Consumer Prices) for Sweden",
"Consumer Price Index: All Items (Harmonized Index of Consumer Prices) for Sweden (DISCONTINUED)",
"Consumer Price Index: All Items for Sweden",
"Consumer Price Index in Sweden (DISCONTINUED)",
"Consumer Price Index: All items: Total: Total for Sweden",
"Consumer Price Index: Harmonized Prices: Total All Items for Sweden",
"Consumer Price Index: Harmonized Prices: Total All Items for Sweden (DISCONTINUED)",
"Consumer Price Index for Sweden",
"Consumer Price Index: Total, Net All Items for Sweden (DISCONTINUED)",
"Consumer Price Index: Total All Items for Sweden",
"Harmonized Index of Consumer Prices: All Items for Sweden",
)
}
Country::Switzerland => {
vec!(
"Consumer Price Index: Harmonized Prices: Total All Items for Switzerland",
"Consumer Price Index: Harmonized Prices: Total All Items for Switzerland (DISCONTINUED)",
"Consumer Price Index for Switzerland",
"Consumer Price Index: Total All Items for Switzerland",
"Harmonized Index of Consumer Prices: All Items for Switzerland",
"Consumer Price Index: All Items (Harmonized Index of Consumer Prices) for Switzerland",
"Consumer Price Index: All Items (Harmonized Index of Consumer Prices) for Switzerland (DISCONTINUED)",
)
}
Country::UnitedKingdom => {
vec!(
"Harmonized Index of Consumer Prices in the United Kingdom (DISCONTINUED)",
"Consumer Price Index: All Items (Harmonized Index of Consumer Prices) for United Kingdom (DISCONTINUED)",
"Consumer Price Index: All Items (Harmonized Index of Consumer Prices) for United Kingdom",
"Consumer Price Index in the United Kingdom (DISCONTINUED)",
"Consumer Price Index of All Items in the United Kingdom",
"Consumer Price Index: All Items for United Kingdom",
"Consumer Price Index: All items: Total: Total for the United Kingdom",
"Inflation, consumer prices for the United Kingdom",
"Consumer Price Index for United Kingdom",
"Consumer Price Index in the United Kingdom",
"Consumer Price Inflation in the United Kingdom",
"Consumer Price Index: Harmonized Prices: Total All Items for the United Kingdom (DISCONTINUED)",
"Consumer Price Index: Harmonized Prices: Total All Items for the United Kingdom",
"Consumer Price Index: Total All Items for the United Kingdom",
"Harmonized Index of Consumer Prices: All Items for United Kingdom",
)
}
Country::UnitedStates => {
vec!(
"Harmonized Index of Consumer Prices: All Items for United States",
"Consumer Price Index: Total All Items for the United States",
"Consumer Price Index: Harmonized Prices: Total All Items for the United States",
"Consumer Price Index: Harmonized Prices: Total All Items for the United States (DISCONTINUED)",
"Research Consumer Price Index: All Items",
"Consumer Price Index for United States",
"Flexible Price Consumer Price Index",
"Inflation, consumer prices for the United States",
"Consumer Price Index, All Items for United States",
"Rate of Change (6 Month Span at Annual Rate), Consumer Price Index, All Items (Centered) for United States",
"Median Consumer Price Index",
"Sticky Price Consumer Price Index",
"16% Trimmed-Mean Consumer Price Index",
"Consumer Price Index: All Items for the United States",
"Consumer Price Index of All Items in United States",
"Consumer Price Index in the United States (DISCONTINUED)",
"Consumer Price Index: All Items (Harmonized Index of Consumer Prices) for the United States",
"Consumer Price Index: All Items (Harmonized Index of Consumer Prices) for the United States (DISCONTINUED)",
"Harmonized Index of Consumer Prices in the United States (DISCONTINUED)",
)
impl DataType {
pub fn from_str(s: &str) -> Option<Self> {
match s {
"u" => Some(DataType::U),
"cpi" => Some(DataType::Cpi),
"inf" => Some(DataType::Inf),
"int" => Some(DataType::Int),
_ => None,
_ => panic!(),
};
match country {
Country::UnitedStates => {
match Fred::tags_series("cpi;usa") {
Ok(tags_series) => {
tags_series.seriess
.equals_one_of(titles)
},
Err(json_error) => {
println!("{}", json_error);
panic!();
},
}
},
_ => {
match Fred::tags_series(&to_tag("cpi", &country)) {
Ok(tags_series) => {
tags_series.seriess
.equals_one_of(titles)
},
Err(json_error) => {
println!("{}", json_error);
panic!();
},
}
},
/// Return relevant unemployment rate series for a country.
/// ```
/// println!("{}", unemployment_series(&Country::Canada));
/// ```
pub fn unemployment_series(country: &Country) -> SeriesItems {
if let Country::UnitedStates = country {
// Need to use a different search technique for US data.
let exclude_phrase = vec!(
"Male",
"Female",
"Men",
"Women",
"Youth",
);
let one_of = vec!(
"Unemployment Rate for United States",
"Unemployment Rate: Aged 15 and Over: All Persons for the United States",
"Unemployment Rate: Aged 15-74: All Persons for the United States",
"Harmonized Unemployment Rate: Total: All Persons for the United States",
"Unemployment Rate - 18 Years and Over",
);
let tag_series = Fred::tags_series("unemployment;rate;usa;nation").unwrap().seriess;
tag_series
.exclude_phrases(exclude_phrase)
.equals_one_of(one_of)
} else {
let (exclude_phrase, include_phrase) = match country {
Country::Australia => {
(
vec!(
"Male",
"Female",
"55-64",
"25-54",
"15-24",
"20 to 24",
"Youth",
"Women",
"Teenagers",
),
vec!(
"Rate"
),
)
}
Country::Austria => {
(
vec!(
"Male",
"Female",
"55-64",
"25-54",
"15-24",
"15-64", // series includes 15-74
"20 to 24",
"Youth",
"Women",
"Teenagers",
),
vec!(
"Rate"
),
)
}
Country::Belgium => {
(
vec!(
"Male",
"Female",
"55-64",
"25-54",
"15-24",
"15-64", // series includes 15-74
"20 to 24",
"Youth",
"Women",
"Teenagers",
),
vec!(
"Rate"
),
)
}
Country::Canada => {
(
vec!(
"Male",
"Female",
"15-64",
"55-64",
"25-54",
"15-24",
"20 to 24",
"Youth",
"Women",
"Teenagers",
),
vec!(
"Rate"
),
)
}
Country::Chile => {
(
vec!(
"Male",
"Female",
"Youth",
"15-24",
"15-64",
"25-54",
"55-64",
),
vec!(
"Rate"
),
)
}
Country::CzechRepublic => {
(
vec!(
"Male",
"Female",
"Youth",
"15-24",
"15-64",
"25-54",
"55-64",
),
vec!(
"Rate"
),
)
}
Country::Denmark => {
(
vec!(
"Male",
"Female",
"Youth",
"15-24",
"15-64",
"25-54",
"55-64",
),
vec!(
"Rate"
),
)
}
Country::Estonia => {
(
vec!(
"Male",
"Female",
"Youth",
"15-24",
"15-64",
"25-54",
"55-64",
),
vec!(
"Rate",
),
)
}
Country::Finland => {
(
vec!(
"Male",
"Female",
"Youth",
"15-24",
"15-64",
"25-54",
"55-64",
),
vec!(
"Rate",
),
)
}
Country::France => {
(
vec!(
"Male",
"Men",
"Female",
"Women",
"Youth",
"Teenagers",
"15-24",
"15-64",
"25-54",
"55-64",
),
vec!(
"Rate",
),
)
}
Country::Germany => {
(
vec!(
"Male",
"Men",
"Female",
"Youth",
"Women",
"Teenagers",
"20 to 24",
"15-24",
"15-64",
"25-54",
"55-64",
),
vec!(
"Rate",
),
)
}
Country::Greece => {
(
vec!(
"Male",
"Female",
"Youth",
"15-24",
"15-64",
"25-54",
"55-64",
),
vec!(
"Rate",
),
)
}
Country::Ireland => {
(
vec!(
"Male",
"Female",
"Youth",
"15-24",
"15-64",
"25-54",
"55-64",
),
vec!(
"Rate",
),
)
}
Country::Israel => {
(
vec!(
"Male",
"Female",
"Youth",
"15-24",
"15-64",
"25-54",
"55-64",
),
vec!(
"Rate",
),
)
}
Country::Italy => {
(
vec!(
"Male",
"Female",
"Youth",
"Men",
"Women",
"Teenagers",
"15-24",
"15-64",
"25-54",
"55-64",
),
vec!(
"Rate",
),
)
}
Country::Japan => {
(
vec!(
"Male",
"Female",
"Youth",
"Men",
"Women",
"Teenagers",
"20 to 24",
"15-24",
"15-64",
"25-54",
"55-64",
),
vec!(
"Rate",
),
)
}
Country::Latvia => {
(
vec!(
"Youth",
"Male",
"Female",
"25 and over",
"15-24",
"15-64",
"25-54",
"55-64",
),
vec!(
"nemployment",
),
)
}
Country::Netherlands => {
(
vec!(
"Male",
"Female",
"Youth",
"Women",
"Teenagers",
"Men",
"20 to 24",
"15-24",
"15-64",
"25-54",
"55-64",
),
vec!(
"Rate",
),
)
}
Country::NewZealand => {
(
vec!(
"Male",
"Female",
"55-64",
"25-54",
"15-24",
"Youth",
),
vec!(
"Rate",
),
)
}
Country::Norway => {
(
vec!(
"Male",
"Female",
"15-24",
"15-64",
"25-54",
"55-64",
),
vec!(
"Rate",
),
)
}
Country::Poland => {
(
vec!(
"Male",
"Female",
"Youth",
"15-24",
"15-64",
"25-54",
"55-64",
),
vec!(
"Rate",
),
)
}
Country::Serbia => {
(
vec!(
),
vec!(
"",
),
)
}
Country::SouthKorea => {
(
vec!(
"Male",
"Female",
"Youth",
"15-24",
"15-64",
"25-54",
"55-64",
),
vec!(
"Rate",
),
)
}
Country::Spain => {
(
vec!(
"Male",
"Female",
"Youth",
"15-24",
"15-64",
"25-54",
"55-64",
),
vec!(
"Rate",
),
)
}
Country::Sweden => {
(
vec!(
"Male",
"Female",
"Youth",
"Men",
"Women",
"Teenagers",
"15-24",
"15-64",
"25-54",
"55-64",
"20 to 24",
),
vec!(
"Rate",
),
)
}
Country::Switzerland => {
(
vec!(
"Male",
"Female",
"Youth",
"15-24",
"15-64",
"25-54",
"55-64",
),
vec!(
"Rate",
),
)
}
Country::UnitedKingdom => {
(
vec!(
"Male",
"Female",
"Youth",
"Men",
"Women",
"Teenagers",
"20 to 24",
"15-24",
"15-64",
"25-54",
"55-64",
),
vec!(
"Rate",
),
)
}
_ => panic!(),
impl fmt::Display for DataType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let s = match self {
DataType::U => "u",
DataType::Cpi => "cpi",
DataType::Inf => "inf",
DataType::Int => "int",
match Fred::tags_series(&to_tag("unemployment", country)) {
Ok(tags_series) => {
tags_series.seriess
.exclude_phrases(exclude_phrase)
.only_include(include_phrase)
},
Err(err) => {
println!("{}", err);
panic!();
},
}
write!(f, "{}", s)
pub fn to_tag(tag: &str, country: &Country) -> String {
format!(
"{};{}",
tag,
country.to_string().to_lowercase()
)
}
/// save_unemployment_data(&Country::NewZealand, "/fullpath/data/new_zealand");
/// ```
pub fn save_unemployment_data(country: &Country, path: &str) {
for series in unemployment_series(country).iter() {
save_data_csv(&series.id, &path);
save_series_meta(&series.id, &path);
println!("{}", series.id)
}
}
/// Save csv data and meta data to file, for a given `Country` using full path.
/// ```
/// save_cpi_data(&Country::NewZealand, "/fullpath/data/cpi/new_zealand");
/// ```
pub fn save_cpi_data(country: &Country, path: &str) {
for series in cpi_series(country).iter() {
save_data_csv(&series.id, &path);
save_series_meta(&series.id, &path);
println!("{}", series.id)
}
}
/// Save csv data and meta data to file, for a given `Country`, using full path.
/// ```
/// save_all_unemployment_data("/fullpath/data/u");
/// ```
pub fn save_all_unemployment_data(path: &str) {
for country in countries_with_data().iter() {
println!("\n{}", country.to_string());
save_unemployment_data(&country, &format!("{}/{}", path, country.as_path()));
}
}
/// Save csv data and meta data to file, for a given `Country`, using full path.
/// ```
/// save_all_cpi_data("/fullpath/data/cpi");
/// ```
pub fn save_all_cpi_data(path: &str) {
for country in countries_with_data().iter() {
println!("\n{}", country.to_string());
save_cpi_data(&country, &format!("{}/{}", path, country.as_path()));
}
}
/// Generate the base keytree spec for all countries, using full path.
/// ```
/// generate_keytree("/fullpath/data/generated.keytree");
/// ```
/// ```text
/// source:
/// html_page:
/// data_type: u
/// country: Australia
/// graphic:
/// height: 200
/// data: AUSURAMS_a
/// graphic:
/// data: AUSURANAA_a
/// graphic:
/// ```
// TODO: cpi, u
pub fn generate_keytree(path: &str) {
let mut s = String::from("source:\n");
// Unemployment
println!("u");
for country in countries_with_data().iter() {
s.push_str(" html_page:\n data_type: u\n");
s.push_str(&format!(" country: {}\n", country));
println!("{}", country.to_string());
for series in unemployment_series(country).iter() {
s.push_str(" graphic:\n data: ");
s.push_str(&series.id);
s.push('\n');
}
s.push('\n');
};
// CPI
println!("cpi");
for country in countries_with_data().iter() {
s.push_str(" html_page:\n data_type: u\n");
s.push_str(&format!(" country: {}\n", country));
println!("{}", country.to_string());
for series in cpi_series(country).iter() {
s.push_str(" graphic:\n data: ");
s.push_str(&series.id);
s.push('\n');
}
s.push('\n');
};
fs::create_dir_all(&path).unwrap();
fs::write(&path, s).unwrap();
}
/// Save csv data and meta data to file, for all countries after `Country` in the
//'countries_with_data' list.
pub fn save_all_cpi_data_after(after: &Country, path: &str) {
for country in countries_with_data().iter().skip_while(|&country| country != after) {
println!("\n{}", country.to_string());
save_cpi_data(&country, &format!("{}/{}", path, country.as_path()));
}
}
/// Return all the countries with good data as a `Vec`.
pub fn countries_with_data() -> Vec<Country> {
vec!(
Country::Australia,
Country::Austria,
Country::Belgium,
Country::Canada,
Country::Chile,
Country::CzechRepublic,
Country::Denmark,
Country::Estonia,
Country::Finland,
Country::France,
Country::Germany,
Country::Greece,
Country::Ireland,
Country::Israel,
Country::Italy,
Country::Japan,
Country::Latvia,
Country::Netherlands,
Country::NewZealand,
Country::Norway,
Country::Poland,
Country::Serbia,
Country::SouthKorea,
Country::Spain,
Country::Sweden,
Country::Switzerland,
Country::UnitedKingdom,
Country::UnitedStates,
)
}
/// ```
}
/// Return series from tags. Tags look like "loans;australia".
pub fn interest_rate_series(tags: &str) {
let tags_series = Fred::tags_series(tags).unwrap();
let series_items = tags_series.seriess;
let iter = series_items.iter();
for item in iter {
println!();
println!("{}", item.title);
println!("{}", item.id);
let tags = Fred::series_tags(&item.id).unwrap();
println!("{}", tags.one_line());
}