Explorar el Código

Ignore days with no published extra-info

Vecna hace 3 semanas
padre
commit
b4d0fc75a8
Se han modificado 1 ficheros con 57 adiciones y 22 borrados
  1. 57 22
      src/analysis.rs

+ 57 - 22
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::<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) {
-                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