Cleanup names in solar api months endpoint

This commit is contained in:
2025-09-16 22:34:49 +02:00
parent c35ec6e7d7
commit e756e6f972

View File

@@ -269,30 +269,32 @@ fn get_months_solar_json(
.collect(), .collect(),
}; };
let cached_months = get_cached_months_solar_json(&start, &stop, &month_cache); let cached_months = get_cached_months(&start, &stop, &month_cache);
let mut missing_months; let mut missing_months_from_cache;
if cached_months.len() > 0 { if cached_months.len() > 0 {
missing_months = Vec::new(); missing_months_from_cache = Vec::new();
let mut cached_month_next_index = 0; let mut matching_cached_month_index = 0;
for month in response_model.month_logs.iter_mut() { for response_month in response_model.month_logs.iter_mut() {
if cached_month_next_index < cached_months.len() { if matching_cached_month_index < cached_months.len() {
let cached_month = &cached_months[cached_month_next_index]; let cached_month = &cached_months[matching_cached_month_index];
if month.year == cached_month.year && month.month == cached_month.month { if response_month.year == cached_month.year
month.envoy_total_watts = cached_month.envoy_total_watts; && response_month.month == cached_month.month
month.zever_total_watts = cached_month.zever_total_watts; {
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; continue;
} }
} }
missing_months.push(YearMonth { missing_months_from_cache.push(YearMonth {
year: month.year, year: response_month.year,
month: month.month, month: response_month.month,
}); });
} }
} else { } else {
missing_months = response_model missing_months_from_cache = response_model
.month_logs .month_logs
.iter() .iter()
.map(|i| YearMonth { .map(|i| YearMonth {
@@ -302,11 +304,11 @@ fn get_months_solar_json(
.collect(); .collect();
} }
if missing_months.len() > 0 { if missing_months_from_cache.len() > 0 {
let missing_ranges = compact_missing_dates_to_ranges(&missing_months); let missing_ranges = compact_missing_months_to_ranges(&missing_months_from_cache);
for range in missing_ranges { for range in missing_ranges {
let range_start = range.start(); let range_start = range.start();
let summaries = let database_months =
get_month_summary_range_from_database(&range_start, &range.stop(), &database_path); get_month_summary_range_from_database(&range_start, &range.stop(), &database_path);
let mut response_item_index = response_model let mut response_item_index = response_model
@@ -321,24 +323,24 @@ fn get_months_solar_json(
return None; return None;
}) })
.unwrap(); .unwrap();
for summary in summaries.iter() { for database_month in database_months.iter() {
let mut response_model_item = &mut response_model.month_logs[response_item_index]; let mut response_month = &mut response_model.month_logs[response_item_index];
// Database may return only partial results, i.e. not all dates // Database may return only partial results, i.e. not all dates
// for the range may be returned. Hence we need to check if the // for the range may be returned. Hence we need to check if the
// database result is for the same year month. // database result is for the same year month.
while summary.year != response_model_item.year while database_month.year != response_month.year
|| summary.month != response_model_item.month || database_month.month != response_month.month
{ {
response_item_index += 1; 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_month.envoy_total_watts = database_month.envoy_total_watts;
response_model_item.zever_total_watts = summary.zever_total_watts; response_month.zever_total_watts = database_month.zever_total_watts;
response_item_index += 1; 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; return results;
} }
fn get_cached_months_solar_json( fn get_cached_months(
start: &YearMonth, start: &YearMonth,
stop: &YearMonth, stop: &YearMonth,
month_cache: &std::sync::Arc<RwLock<Vec<CachedMonthsResponseItem>>>, month_cache: &std::sync::Arc<RwLock<Vec<CachedMonthsResponseItem>>>,
@@ -407,7 +409,7 @@ fn get_cached_months_solar_json(
} }
} }
fn compact_missing_dates_to_ranges(missing_year_months: &[YearMonth]) -> Vec<YearMonthRange> { fn compact_missing_months_to_ranges(missing_year_months: &[YearMonth]) -> Vec<YearMonthRange> {
let mut results = Vec::new(); let mut results = Vec::new();
if missing_year_months.len() < 1 { if missing_year_months.len() < 1 {
return results; return results;
@@ -440,7 +442,7 @@ fn compact_missing_dates_to_ranges(missing_year_months: &[YearMonth]) -> Vec<Yea
return results; return results;
} }
fn add_missing_entries_to_month_cache( fn insert_missing_months_in_month_cache(
summaries: &[MonthsResponseItem], summaries: &[MonthsResponseItem],
month_cache: &RwLock<Vec<CachedMonthsResponseItem>>, month_cache: &RwLock<Vec<CachedMonthsResponseItem>>,
) { ) {