From e756e6f972b2ad719cf7e630812048c78aaf10f3 Mon Sep 17 00:00:00 2001 From: Tijmen van Nesselrooij Date: Tue, 16 Sep 2025 22:34:49 +0200 Subject: [PATCH] Cleanup names in solar api months endpoint --- src/solar_api/src/main.rs | 60 ++++++++++++++++++++------------------- 1 file changed, 31 insertions(+), 29 deletions(-) diff --git a/src/solar_api/src/main.rs b/src/solar_api/src/main.rs index 28f8ae6..77af825 100644 --- a/src/solar_api/src/main.rs +++ b/src/solar_api/src/main.rs @@ -269,30 +269,32 @@ fn get_months_solar_json( .collect(), }; - let cached_months = get_cached_months_solar_json(&start, &stop, &month_cache); - let mut missing_months; + let cached_months = get_cached_months(&start, &stop, &month_cache); + let mut missing_months_from_cache; if cached_months.len() > 0 { - missing_months = Vec::new(); - let mut cached_month_next_index = 0; - for month in response_model.month_logs.iter_mut() { - if cached_month_next_index < cached_months.len() { - let cached_month = &cached_months[cached_month_next_index]; - if month.year == cached_month.year && month.month == cached_month.month { - month.envoy_total_watts = cached_month.envoy_total_watts; - month.zever_total_watts = cached_month.zever_total_watts; + missing_months_from_cache = Vec::new(); + let mut matching_cached_month_index = 0; + for response_month in response_model.month_logs.iter_mut() { + if matching_cached_month_index < cached_months.len() { + let cached_month = &cached_months[matching_cached_month_index]; + if response_month.year == cached_month.year + && response_month.month == cached_month.month + { + response_month.envoy_total_watts = cached_month.envoy_total_watts; + response_month.zever_total_watts = cached_month.zever_total_watts; - cached_month_next_index += 1; + matching_cached_month_index += 1; continue; } } - missing_months.push(YearMonth { - year: month.year, - month: month.month, + missing_months_from_cache.push(YearMonth { + year: response_month.year, + month: response_month.month, }); } } else { - missing_months = response_model + missing_months_from_cache = response_model .month_logs .iter() .map(|i| YearMonth { @@ -302,11 +304,11 @@ fn get_months_solar_json( .collect(); } - if missing_months.len() > 0 { - let missing_ranges = compact_missing_dates_to_ranges(&missing_months); + if missing_months_from_cache.len() > 0 { + let missing_ranges = compact_missing_months_to_ranges(&missing_months_from_cache); for range in missing_ranges { let range_start = range.start(); - let summaries = + let database_months = get_month_summary_range_from_database(&range_start, &range.stop(), &database_path); let mut response_item_index = response_model @@ -321,24 +323,24 @@ fn get_months_solar_json( return None; }) .unwrap(); - for summary in summaries.iter() { - let mut response_model_item = &mut response_model.month_logs[response_item_index]; + for database_month in database_months.iter() { + let mut response_month = &mut response_model.month_logs[response_item_index]; // Database may return only partial results, i.e. not all dates // for the range may be returned. Hence we need to check if the // database result is for the same year month. - while summary.year != response_model_item.year - || summary.month != response_model_item.month + while database_month.year != response_month.year + || database_month.month != response_month.month { response_item_index += 1; - response_model_item = &mut response_model.month_logs[response_item_index]; + response_month = &mut response_model.month_logs[response_item_index]; } - response_model_item.envoy_total_watts = summary.envoy_total_watts; - response_model_item.zever_total_watts = summary.zever_total_watts; + response_month.envoy_total_watts = database_month.envoy_total_watts; + response_month.zever_total_watts = database_month.zever_total_watts; response_item_index += 1; } - add_missing_entries_to_month_cache(&summaries, &month_cache); + insert_missing_months_in_month_cache(&database_months, &month_cache); } } @@ -375,7 +377,7 @@ fn get_month_summary_range_from_database( return results; } -fn get_cached_months_solar_json( +fn get_cached_months( start: &YearMonth, stop: &YearMonth, month_cache: &std::sync::Arc>>, @@ -407,7 +409,7 @@ fn get_cached_months_solar_json( } } -fn compact_missing_dates_to_ranges(missing_year_months: &[YearMonth]) -> Vec { +fn compact_missing_months_to_ranges(missing_year_months: &[YearMonth]) -> Vec { let mut results = Vec::new(); if missing_year_months.len() < 1 { return results; @@ -440,7 +442,7 @@ fn compact_missing_dates_to_ranges(missing_year_months: &[YearMonth]) -> Vec>, ) {