From 2e0a6909cbfb2479edd7fba78fa4d0135a79ae3f Mon Sep 17 00:00:00 2001 From: Benedict Börger Date: Sat, 23 Mar 2019 15:40:35 +0100 Subject: [global] refactoring code base --- src/Csv/mod.rs | 62 -------------------- src/banking/mod.rs | 59 ++++++++++++++++++++ src/exchange/mod.rs | 2 +- src/lib.rs | 0 src/main.rs | 46 ++++++++------- src/parsers/csv/mod.rs | 93 ++++++++++++++++++++++++++++++ src/parsers/ini/mod.rs | 64 +++++++++++++++++++++ src/parsers/mod.rs | 2 + src/web_frontend/balance.rs | 118 +++++++++++++++++++++++++++++++++++++++ src/web_frontend/chart.rs | 61 ++++++++++++++++++++ src/web_frontend/mod.rs | 29 ++++++++++ src/web_frontend/transactions.rs | 101 +++++++++++++++++++++++++++++++++ 12 files changed, 554 insertions(+), 83 deletions(-) delete mode 100644 src/Csv/mod.rs create mode 100644 src/banking/mod.rs delete mode 100644 src/lib.rs create mode 100644 src/parsers/csv/mod.rs create mode 100644 src/parsers/ini/mod.rs create mode 100644 src/parsers/mod.rs create mode 100644 src/web_frontend/balance.rs create mode 100644 src/web_frontend/chart.rs create mode 100644 src/web_frontend/mod.rs create mode 100644 src/web_frontend/transactions.rs (limited to 'src') diff --git a/src/Csv/mod.rs b/src/Csv/mod.rs deleted file mode 100644 index 29e1aca..0000000 --- a/src/Csv/mod.rs +++ /dev/null @@ -1,62 +0,0 @@ -use std::fs::File; -use std::io; -use std::io::BufRead; - -/** - Represents a CSV file - **/ - -pub struct CsvFile { - path : String, - delim : String, - pub header : Vec, - pub content : Vec>, - has_header : bool -} - -impl CsvFile { - pub fn new(path: &str, delim: &str, header: bool) -> CsvFile { - CsvFile { - path : String::from(path), - delim: String::from(delim), - header : Vec::new(), - content : Vec::new(), - has_header : header - } - - } - - // TODO nicht gleich viele spalten - fn parse_line(line : String, delim: &str) -> Vec { - let mut columns : Vec = Vec::new(); - for column in line.split(delim).collect::>() { - columns.push(column.to_string()); - } - columns - } - - - pub fn read_file(path: &str, delim: &str, header: bool) -> Result { - let fd = File::open(path)?; - let reader = io::BufReader::new(fd); - let mut column_names : Vec = Vec::new(); - let mut lines : Vec> = Vec::new(); - let mut first = true; - - for line in reader.lines() { - let line = line?; - if header && first { - column_names.append(&mut CsvFile::parse_line(line, delim)); - first = false; - } else { - lines.push(CsvFile::parse_line(line, delim)); - } - } - - Ok(CsvFile { path: String::from(path), - delim : String::from(delim), - header : column_names, - content : lines, - has_header: header } ) - } -} diff --git a/src/banking/mod.rs b/src/banking/mod.rs new file mode 100644 index 0000000..410bae1 --- /dev/null +++ b/src/banking/mod.rs @@ -0,0 +1,59 @@ + +use parsers::csv::CsvFile; + + +pub struct Account { + name : String, + iban : String, + transactions : Vec +} + + +#[derive(Serialize)] +pub struct Transaction { + pub sender_name : String, + pub amount : f32, + pub reference : String, + pub date : chrono::NaiveDate +} + +impl Transaction { + pub fn from_sparkasse_csv_file(file : CsvFile) -> Vec { + let mut ret = Vec::new(); + for line in file.iter() { + let mut sender_name_f = String::from(""); + let mut sender_iban_f = String::from(""); + let mut amount_f : f32 = 0.0; + let mut reference_f = String::from(""); + let mut date_f = chrono::NaiveDate::parse_from_str("01.01.2019", "%d.%m.%Y").unwrap(); + match line.get(&String::from("Kontonummer")) { + Some(value) => sender_iban_f = value.to_string(), + None => println!("missing sender") + } + match line.get(&String::from("Beguenstigter/Zahlungspflichtiger")) { + Some(value) => sender_name_f = value.to_string(), + None => println!("missing sender") + } + match line.get(&String::from("Verwendungszweck")) { + Some(value) => reference_f = value.to_string(), + None => println!("missing refernce") + } + match line.get(&String::from("Betrag")) { + Some(value) => amount_f = value.parse().unwrap(), + None => println!("missing amount") + } + match line.get(&String::from("Valutadatum")) { + Some(value) => { + date_f = chrono::NaiveDate::parse_from_str(value, "%d.%m.%y").unwrap();} , + None => println!("missing date") + } + ret.push(Transaction { + sender_name : sender_name_f, + amount : amount_f, + reference : reference_f, + date : date_f }); + + } + ret + } +} diff --git a/src/exchange/mod.rs b/src/exchange/mod.rs index 8d93ac0..94cdbca 100644 --- a/src/exchange/mod.rs +++ b/src/exchange/mod.rs @@ -1,5 +1,5 @@ use std::io; -use Csv::CsvFile; +use parsers::csv::CsvFile; use std::collections::HashMap; pub struct Stock { diff --git a/src/lib.rs b/src/lib.rs deleted file mode 100644 index e69de29..0000000 diff --git a/src/main.rs b/src/main.rs index 4c0768c..1325742 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,23 +1,29 @@ -mod exchange; -mod Csv; -//use exchange::Stock; -use Csv::CsvFile; +#![feature(proc_macro_hygiene,decl_macro)] +#[macro_use] +extern crate rocket; +extern crate rocket_contrib; +extern crate regex; +extern crate chrono; -fn main() { - println!("Welcome to your investment calculator!"); +#[macro_use] +extern crate serde_derive; + +//mod exchange; +mod parsers; +mod banking; +mod web_frontend; - // TODO Compute how the money would have benn developed on historical data - //Stock::read_from_csv_file("~/hih"); - let config = CsvFile::read_file("test.csv", ";", true); - let config = match config { - Ok(c) => c, - Err(error) => panic!("Error reading CSV file: {}", error), - }; - let mut i = 0; - for line in config.content { - i += 1; - for f in line { - println!("{}: {}",i, f) - } - } +use rocket_contrib::templates::Template; + +fn main() { + // handle command line parameters + // e.g. wheres is the asset config ini file + // TODO how pass config to the modules/handler ? + // launch web frontend + rocket::ignite() + .attach(Template::fairing()) + .mount("/", routes![web_frontend::account_handler, web_frontend::transactions::transaction_handler, + web_frontend::transactions::transaction_handler_post, + web_frontend::balance::balance_handler, web_frontend::static_handler]) + .launch(); } diff --git a/src/parsers/csv/mod.rs b/src/parsers/csv/mod.rs new file mode 100644 index 0000000..c906202 --- /dev/null +++ b/src/parsers/csv/mod.rs @@ -0,0 +1,93 @@ +use std::fs::File; +use std::io; +use std::io::BufRead; +use std::collections::HashMap; + +/** + Represents a CSV file + **/ + +pub struct CsvFile { + path : String, + delim : String, + pub header : Vec, + pub content : Vec>, + has_header : bool +} + +/* + * Iterator for the CSV file + */ +pub struct CsvFileIterator<'a> { + file : &'a CsvFile, + line : usize, + column : usize, + max_column : usize +} + +impl <'a> Iterator for CsvFileIterator<'a> { + type Item = HashMap<&'a String,&'a String>; + + fn next(&mut self) -> Option { + let k = self.file.content.get(self.line)?; + let it : HashMap<&'a String, &'a String> = self.file.header.iter().zip(k.iter()).collect(); + self.line = self.line + 1; + Some(it) + } +} + +impl CsvFile { + pub fn new(path: &str, delim: &str, header: bool) -> CsvFile { + CsvFile { + path : String::from(path), + delim: String::from(delim), + header : Vec::new(), + content : Vec::new(), + has_header : header + } + + } + + // TODO nicht gleich viele spalten + fn parse_line(line : String, delim: &str) -> Vec { + let mut columns : Vec = Vec::new(); + for column in line.split(delim).collect::>() { + columns.push(column.trim_matches(|c| c == '\'' || c == '\"').to_string()); + } + columns + } + + + pub fn from_file(path: &str, delim: &str, header: bool) -> Result { + let fd = File::open(path)?; + let reader = io::BufReader::new(fd); + let mut column_names : Vec = Vec::new(); + let mut lines : Vec> = Vec::new(); + let mut first = true; + + for line in reader.lines() { + let line = line?; + if header && first { + column_names.append(&mut CsvFile::parse_line(line, delim)); + first = false; + } else { + lines.push(CsvFile::parse_line(line, delim)); + } + } + + Ok(CsvFile { path: String::from(path), + delim : String::from(delim), + header : column_names, + content : lines, + has_header: header } ) + } + + pub fn iter(&self) -> CsvFileIterator { + CsvFileIterator { file : self, + line : 0, + column : 0, + max_column : self.header.len() + } + } + +} diff --git a/src/parsers/ini/mod.rs b/src/parsers/ini/mod.rs new file mode 100644 index 0000000..9633231 --- /dev/null +++ b/src/parsers/ini/mod.rs @@ -0,0 +1,64 @@ +use std::fs::File; +use std::io; +use std::io::BufRead; +use std::collections::HashMap; + +pub struct IniFile { + pub section : HashMap> +} + +pub struct IniSection { + pub section_name : String, + pub properties : HashMap> +} + +impl IniFile { + pub fn from_file(path : &str) -> Result { + let mut file = HashMap::new(); + let fd = File::open(path)?; + let reader = io::BufReader::new(fd); + let mut current_section = String::from(""); + + for line in reader.lines() { + let line = line?; + let line = line.trim().to_string(); + + if line.starts_with("#") || line.is_empty() { + println!("kommentar or empts: {}", line); + continue; + } + // another section begins + if line.starts_with("[") && line.ends_with("]") { + let nam = line.get(1..(line.len()-1)); + let n; + match nam { + Some(sec_name) => n = sec_name, + // TODO no name given, what should be done + None => n = "" + } + current_section = n.to_string(); + // TODO maybe the sections has been specified twice? + file.insert(n.to_string(), Vec::new() ); + continue; + + } + let kv : Vec<&str> = line.split("=").collect(); + let mut key = String::from(""); + if let Some(t) = kv.get(0) { + key = t.to_string(); + } + let mut value = String::from(""); + if let Some(t) = kv.get(1) { + value = t.to_string(); + } + if let Some(section) = file.get_mut(¤t_section) { + // get the entry with key from vector + if let Some(ent) = section.get_mut(&key) { + ent.insert(value.to_string()); + } + } + } + Ok(IniFile { sections : file }) + } + +} diff --git a/src/parsers/mod.rs b/src/parsers/mod.rs new file mode 100644 index 0000000..d8bce3c --- /dev/null +++ b/src/parsers/mod.rs @@ -0,0 +1,2 @@ +pub mod csv; +//pub mod ini; diff --git a/src/web_frontend/balance.rs b/src/web_frontend/balance.rs new file mode 100644 index 0000000..2cbebec --- /dev/null +++ b/src/web_frontend/balance.rs @@ -0,0 +1,118 @@ +use parsers::csv::CsvFile; +use banking::Account; +//use parsers::ini::IniFile; +use std::collections::HashMap; +use rocket_contrib::templates::Template; +use rocket::response::NamedFile; +use std::path::{PathBuf, Path}; +use rocket::request::Form; +use rocket::http::RawStr; +use regex::Regex; +use chrono::{NaiveDate, Utc}; +use chrono::Datelike; + +#[derive(Serialize)] +struct MonthEarnSpend { + name : String, + earned : f32, + spent : f32, +} + + +#[derive(Serialize)] +struct BalanceContext { + account_name : String, + months : Vec, + date_start : String, + date_end : String +} + +#[derive(Debug)] +struct DateRange { + start_year : i32, + start_month : u32, + end_year : i32, + end_month : u32, +} + +impl DateRange { + fn new(start : chrono::NaiveDate, end : chrono::NaiveDate) -> DateRange { + DateRange { + start_year : start.year(), + start_month : start.month(), + end_year : end.year(), + end_month : end.month(), + } + } +} + +impl Iterator for DateRange { + type Item = chrono::NaiveDate; + + fn next(&mut self) -> Option { + println!("next called"); + if (self.start_year <= self.end_year) { + if(self.start_month <= self.end_month) { + let mut tmp = self.start_year.to_string(); + if self.start_month < 10 { + tmp.push_str("-0"); + } else { + tmp.push_str("-"); + } + tmp.push_str(&self.start_month.to_string()); + tmp.push_str("-01"); + if self.start_month < 13 { + self.start_month = self.start_month + 1; + } else { + self.start_month = 1; + self.start_year = self.start_year + 1; + } + println!("{}", tmp); + return Some(chrono::NaiveDate::parse_from_str(&tmp, "%Y-%m-%d").unwrap()) + } + } + None + } +} + +#[get("/balance?&")] +pub fn balance_handler(start : Option<&RawStr>, end : Option<&RawStr>) -> rocket_contrib::templates::Template { + let date_start = match start { + Some(s) => { let mut tmp = s.to_string(); + tmp.push_str("-01"); + chrono::NaiveDate::parse_from_str(&tmp, "%Y-%m-%d").unwrap() }, + None => Utc::today().naive_utc() + }; + let date_end = match end { + Some(s) => { let mut tmp = s.to_string(); + tmp.push_str("-01"); + chrono::NaiveDate::parse_from_str(&tmp, "%Y-%m-%d").unwrap() }, + None => Utc::today().naive_utc() + }; + let date_range = DateRange::new(date_start, date_end); + + let mut earn_spend_v = Vec::new(); + for date in date_range { + let transactions = CsvFile::from_file("data/t.csv", ";", true); + let t : Vec ; + match transactions { + Ok(trans) => t = crate::banking::Transaction::from_sparkasse_csv_file(trans), + Err(e) => panic!("could not read file {:?}", e) + } + let result : Vec<_> = t.iter().filter(|x| x.date.month() == date.month()).collect(); + let mut earn = 0.0; + let mut spend = 0.0; + for r in &result { + if r.amount > 0.0 { + earn = earn + r.amount; + } else { + spend = spend + r.amount.abs(); + } + } + earn_spend_v.push(MonthEarnSpend { name : date.to_string(), earned : earn, spent : spend}); + } + let context = BalanceContext { account_name : String::from("Girokonto"), + months : earn_spend_v , date_start : date_start.to_string()[0..7].to_string(), + date_end : date_end.to_string()[0..7].to_string()}; + Template::render("balance", context) +} diff --git a/src/web_frontend/chart.rs b/src/web_frontend/chart.rs new file mode 100644 index 0000000..c5948fe --- /dev/null +++ b/src/web_frontend/chart.rs @@ -0,0 +1,61 @@ +use parsers::csv::CsvFile; +use banking::Account; +//use parsers::ini::IniFile; +use std::collections::HashMap; +use rocket_contrib::templates::Template; +use rocket::response::NamedFile; +use std::path::{PathBuf, Path}; +use rocket::request::Form; +use rocket::http::RawStr; +use regex::Regex; +use chrono::{NaiveDate, Utc}; +use chrono::Datelike; + +#[derive(Serialize)] +struct ChartContext { + account_name : String, + groups : HashMap +} + + +#[get("/chart")] +fn chart_handler() -> rocket_contrib::templates::Template { + // read group config + let chart_file = "data/giro"; + let chart_config = IniFile::from_file(chart_file); + let ini_file; + match chart_config { + Ok(file) => ini_file = file, + Err(e) => panic!("could not read group file {:?}", e) + } + let mut groups = HashMap::new(); + for (section_name, entries) in ini_file.sections { + let mut complete = 0.0; + println!("section name: {}", section_name); + for entrie in entries { + for val in entrie.values { + if entrie.name.is_empty() || val.is_empty() { + continue + } + println!("entrie is : {}", entrie.name); + let transactions = CsvFile::from_file("data/t.csv", ";", true); + let t : Vec ; + match transactions { + Ok(trans) => t = banking::Transaction::from_sparkasse_csv_file(trans), + Err(e) => panic!("could not read file {:?}", e) + } + let re = Regex::new(&val).unwrap(); + let tmp = t.into_iter().filter(|transaction| + re.is_match(&transaction.sender_name) ) + .fold(0.0, |acc, x| acc + x.amount); + complete = complete + tmp.abs(); + } + } + groups.insert(section_name, complete); + // ALSO INSERT OTHER, AKA THE REST + } + let context = ChartContext { account_name : String::from("Girokonto"), + groups : groups }; + Template::render("chart", context) +} + diff --git a/src/web_frontend/mod.rs b/src/web_frontend/mod.rs new file mode 100644 index 0000000..8577065 --- /dev/null +++ b/src/web_frontend/mod.rs @@ -0,0 +1,29 @@ +pub mod transactions; +pub mod balance; +use parsers::csv::CsvFile; +use crate::banking::Account; +//use parsers::ini::IniFile; +use std::collections::HashMap; +use rocket_contrib::templates::Template; +use rocket::response::NamedFile; +use std::path::{PathBuf, Path}; +use rocket::request::Form; +use rocket::http::RawStr; +use regex::Regex; +use chrono::{NaiveDate, Utc}; +use chrono::Datelike; +/* + * Overview over all accounts, complete asset overview? + */ +#[get("/")] +pub fn account_handler() -> rocket_contrib::templates::Template { + let context : HashMap = HashMap::new(); + Template::render("account", context) +} + +// allow always access +#[get("/static/")] +pub fn static_handler(file: PathBuf) -> Option { + NamedFile::open(Path::new("static/").join(file)).ok() +} + diff --git a/src/web_frontend/transactions.rs b/src/web_frontend/transactions.rs new file mode 100644 index 0000000..417ca96 --- /dev/null +++ b/src/web_frontend/transactions.rs @@ -0,0 +1,101 @@ +use parsers::csv::CsvFile; +use crate::banking::Account; +//use parsers::ini::IniFile; +use std::collections::HashMap; +use rocket_contrib::templates::Template; +use rocket::response::NamedFile; +use std::path::{PathBuf, Path}; +use rocket::request::Form; +use rocket::http::RawStr; +use regex::Regex; +use chrono::{NaiveDate, Utc}; +use chrono::Datelike; + +/* + This files contains the code to handle the /transactions requests +*/ + +/* + * This context is passed to the template rendering engine + * If you modify this structure, adapt also the template to include the + * changes! + */ +#[derive(Serialize)] +struct TransactionContext { + transactions : Vec, + account_name : String, + filter : String, + date_start : String, + date_end : String +} + +#[derive(FromForm)] +pub struct TransactionFilter { + filter : String +} + +impl TransactionFilter { + pub fn apply_filter(&self, transactions : Vec) -> Vec { + let transactions = CsvFile::from_file("data/t.csv", ";", true); + let t : Vec ; + match transactions { + Ok(trans) => t = crate::banking::Transaction::from_sparkasse_csv_file(trans), + Err(e) => panic!("could not read file {:?}", e) + } + //self.filter.split("=").collect::>(); + let re = Regex::new(&self.filter).unwrap(); + let tmp = t.into_iter().filter(|transaction| re.is_match(&transaction.sender_name) || re.is_match(&transaction.reference) ).collect(); + tmp + + } +} + +#[post("/transactions?&", data= "")] +pub fn transaction_handler_post(start : Option<&RawStr>, end : Option<&RawStr>, + transaction_filter : Form) -> rocket_contrib::templates::Template { + let date_start = match start { + Some(s) => { let mut tmp = s.to_string(); + tmp.push_str("-01"); + chrono::NaiveDate::parse_from_str(&tmp, "%Y-%m-%d").unwrap() }, + None => Utc::today().naive_utc() + }; + let date_end = match end { + Some(s) => { let mut tmp = s.to_string(); + tmp.push_str("-01"); + chrono::NaiveDate::parse_from_str(&tmp, "%Y-%m-%d").unwrap() }, + None => Utc::today().naive_utc() + }; + let input : TransactionFilter = transaction_filter.into_inner(); + let tmp = Vec::new(); + let ft = input.apply_filter(tmp); + let context = TransactionContext { transactions : ft, account_name : String::from("TEST"), + filter : input.filter, date_start : date_start.to_string(), + date_end : date_end.to_string()}; + Template::render("transaction", context) +} + +#[get("/transactions?&")] +pub fn transaction_handler(start : Option<&RawStr>, end : Option<&RawStr>) -> rocket_contrib::templates::Template { + let date_start = match start { + Some(s) => { let mut tmp = s.to_string(); + tmp.push_str("-01"); + chrono::NaiveDate::parse_from_str(&tmp, "%Y-%m-%d").unwrap() }, + None => Utc::today().naive_utc() + }; + let date_end = match end { + Some(s) => { let mut tmp = s.to_string(); + tmp.push_str("-01"); + chrono::NaiveDate::parse_from_str(&tmp, "%Y-%m-%d").unwrap() }, + None => Utc::today().naive_utc() + }; + let transactions = CsvFile::from_file("data/t.csv", ";", true); + let t : Vec ; + match transactions { + Ok(trans) => t = crate::banking::Transaction::from_sparkasse_csv_file(trans), + Err(e) => panic!("could not read file {:?}", e) + } + let context = TransactionContext { transactions: t, account_name : String::from("Girokonto"), + filter : String::from(""), date_start : date_start.to_string()[0..7].to_string(), + date_end : date_end.to_string()[0..7].to_string()}; + Template::render("transaction", context) +} -- cgit v1.2.3-70-g09d2