From 742b9fa46d74762f58ae939afd980a532cc4636f Mon Sep 17 00:00:00 2001 From: Yaroslav Date: Tue, 6 Oct 2020 14:13:14 +0300 Subject: Account for days elapsed based on latest date Instead of counting the number of days for the averge through the number of iterations, let it be the difference between the latest date on entry and the start of the period. Id est: * 'Missing' dates from the input are implicit. * The order of the days doesn't affect the output (although it doesn't make sense to put the days out of order). --- budget/src/lib.rs | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) (limited to 'budget/src/lib.rs') diff --git a/budget/src/lib.rs b/budget/src/lib.rs index e8bb8f3..7d7c390 100644 --- a/budget/src/lib.rs +++ b/budget/src/lib.rs @@ -51,6 +51,7 @@ pub struct Calculated { pub balance: f64, pub days_left: f64, pub days_left_essential: f64, + pub last_day: NaiveDate, } #[derive(PartialEq, Eq, Debug)] @@ -96,7 +97,11 @@ pub fn parse_account(path: &str) -> Result { } } -pub fn calculate(account: &Account) -> Calculated { +pub fn calculate(account: &Account) -> Option { + if account.days.len() < 1 { + return None; + } + let mut calculated = Calculated { all_day_average: 0.0, essential_day_average: 0.0, @@ -107,12 +112,13 @@ pub fn calculate(account: &Account) -> Calculated { balance: 0.0, days_left: 0.0, days_left_essential: 0.0, + last_day: account.days.last().unwrap().date, }; - let mut days_calculated = 0; - for day in account.days.iter() { - days_calculated += 1; + if day.date > calculated.last_day { + calculated.last_day = day.date; + } for expense in day.expenses.iter() { calculated.total += expense.price; @@ -135,15 +141,18 @@ pub fn calculate(account: &Account) -> Calculated { } } - calculated.all_day_average = calculated.total / days_calculated as f64; + let days_elapsed = + (calculated.last_day - account.start_date).num_days() + 1; + + calculated.all_day_average = calculated.total / days_elapsed as f64; calculated.essential_day_average = - calculated.essential_subtotal / days_calculated as f64; + calculated.essential_subtotal / days_elapsed as f64; for (category, subtotal) in calculated.categories_subtotal.iter() { calculated.categories_day_average .insert( category.clone(), - subtotal / days_calculated as f64, + subtotal / days_elapsed as f64, ); } @@ -153,11 +162,5 @@ pub fn calculate(account: &Account) -> Calculated { calculated.days_left_essential = calculated.balance / calculated.essential_day_average; - calculated -} - -pub fn get_calculated(path: &str) -> Result { - let account = parse_account(path)?; - - Ok(calculate(&account)) + Some(calculated) } -- cgit v1.2.3