summaryrefslogtreecommitdiff
path: root/src/web_frontend/chart.rs
blob: c5948fefa64f1917003716b3ffbf7416985b9cd8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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<String, f32>
}


#[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<banking::Transaction> ; 
		    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)
}