From b4d0fc75a8eda7c53b91c58b509a5b07a667e361 Mon Sep 17 00:00:00 2001 From: Vecna Date: Sun, 27 Oct 2024 23:43:27 -0400 Subject: [PATCH] Ignore days with no published extra-info --- src/analysis.rs | 79 +++++++++++++++++++++++++++++++++++-------------- 1 file changed, 57 insertions(+), 22 deletions(-) diff --git a/src/analysis.rs b/src/analysis.rs index 6b6506f..7a06a09 100644 --- a/src/analysis.rs +++ b/src/analysis.rs @@ -69,12 +69,43 @@ pub fn blocked_in( // Assume bridges never become unblocked blocked_in.insert(country.to_string()); } else { - // Get today's values - let new_map_binding = BTreeMap::::new(); - // TODO: Evaluate on yesterday if we don't have data for today? + // Get today's values, or yesterday's if no bridge-ips for today let today_info = match info.info_by_day.get(&today) { - Some(v) => v, - None => &new_map_binding, + Some(v) => { + if v.contains_key(&BridgeInfoType::BridgeIps) { + v + } else { + // Evaluate on yesterday if we don't have data for today + match info.info_by_day.get(&(today - 1)) { + Some(v2) => { + if v2.contains_key(&BridgeInfoType::BridgeIps) { + v2 + } else { + // If we don't have data today or yesterday, + // assume the bridge is down, not blocked. + continue; + } + } + // If we don't have data today or yesterday, + // assume the bridge is down, not blocked. + None => continue, + } + } + } + None => match info.info_by_day.get(&(today - 1)) { + Some(v) => { + if v.contains_key(&BridgeInfoType::BridgeIps) { + v + } else { + // If we don't have data today or yesterday, + // assume the bridge is down, not blocked. + continue; + } + } + // If we don't have data today or yesterday, + // assume the bridge is down, not blocked. + None => continue, + }, }; let bridge_ips_today = match today_info.get(&BridgeInfoType::BridgeIps) { Some(&v) => v, @@ -92,9 +123,9 @@ pub fn blocked_in( let num_days = min(age, max_historical_days); // Get time series for last num_days - let mut bridge_ips = vec![0; num_days as usize]; - let mut negative_reports = vec![0; num_days as usize]; - let mut positive_reports = vec![0; num_days as usize]; + let mut bridge_ips = vec![]; + let mut negative_reports = vec![]; + let mut positive_reports = vec![]; for i in 0..num_days { let date = today - num_days + i - 1; @@ -103,20 +134,24 @@ pub fn blocked_in( Some(v) => v, None => &new_map_binding, }; - bridge_ips[i as usize] = match day_info.get(&BridgeInfoType::BridgeIps) { - Some(&v) => v, - None => 0, - }; - negative_reports[i as usize] = match day_info.get(&BridgeInfoType::NegativeReports) - { - Some(&v) => v, - None => 0, - }; - positive_reports[i as usize] = match day_info.get(&BridgeInfoType::PositiveReports) - { - Some(&v) => v, - None => 0, - }; + + // If the bridge did not publish bridge-ips, ignore this day + if day_info.contains_key(&BridgeInfoType::BridgeIps) { + let bip = *day_info.get(&BridgeInfoType::BridgeIps).unwrap(); + let nr = match day_info.get(&BridgeInfoType::NegativeReports) { + Some(&v) => v, + None => 0, + }; + let pr = match day_info.get(&BridgeInfoType::PositiveReports) { + Some(&v) => v, + None => 0, + }; + + // If we have bridge-ips for today, add all 3 values to our time series + bridge_ips.push(bip); + negative_reports.push(nr); + positive_reports.push(pr); + } } // Evaluate using appropriate stage based on age of the bridge