Browse Source

Ignore days with no published extra-info

Vecna 3 weeks ago
parent
commit
b4d0fc75a8
1 changed files with 57 additions and 22 deletions
  1. 57 22
      src/analysis.rs

+ 57 - 22
src/analysis.rs

@@ -69,12 +69,43 @@ pub fn blocked_in(
             // Assume bridges never become unblocked
             // Assume bridges never become unblocked
             blocked_in.insert(country.to_string());
             blocked_in.insert(country.to_string());
         } else {
         } else {
-            // Get today's values
-            let new_map_binding = BTreeMap::<BridgeInfoType, u32>::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) {
             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) {
             let bridge_ips_today = match today_info.get(&BridgeInfoType::BridgeIps) {
                 Some(&v) => v,
                 Some(&v) => v,
@@ -92,9 +123,9 @@ pub fn blocked_in(
             let num_days = min(age, max_historical_days);
             let num_days = min(age, max_historical_days);
 
 
             // Get time series for last num_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 {
             for i in 0..num_days {
                 let date = today - num_days + i - 1;
                 let date = today - num_days + i - 1;
@@ -103,20 +134,24 @@ pub fn blocked_in(
                     Some(v) => v,
                     Some(v) => v,
                     None => &new_map_binding,
                     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
             // Evaluate using appropriate stage based on age of the bridge