Compare commits
2 Commits
b4d0fc75a8
...
1e3cb62822
Author | SHA1 | Date |
---|---|---|
|
1e3cb62822 | |
|
05f01c2117 |
|
@ -64,7 +64,14 @@ pub fn blocked_in(
|
|||
let mut blocked_in = HashSet::<String>::new();
|
||||
let today = date;
|
||||
for (country, info) in &bridge_info.info_by_country {
|
||||
// If we haven't seen this bridge yet, return empty set
|
||||
if today < info.first_seen {
|
||||
return HashSet::<String>::new();
|
||||
}
|
||||
|
||||
// (The part above prevents potential errors here.)
|
||||
let age = today - info.first_seen;
|
||||
|
||||
if info.blocked {
|
||||
// Assume bridges never become unblocked
|
||||
blocked_in.insert(country.to_string());
|
||||
|
|
30
src/lib.rs
30
src/lib.rs
|
@ -794,7 +794,7 @@ pub fn guess_blockages(
|
|||
// Guess for each bridge
|
||||
for fingerprint in bridges {
|
||||
let today = get_date();
|
||||
let mut bridge_info: BridgeInfo =
|
||||
let bridge_info: BridgeInfo =
|
||||
bincode::deserialize(&db.get(fingerprint).unwrap().unwrap()).unwrap();
|
||||
let mut new_blockages = HashSet::<String>::new();
|
||||
let fpr_str = array_bytes::bytes2hex("", fingerprint);
|
||||
|
@ -817,19 +817,13 @@ pub fn guess_blockages(
|
|||
max_historical_days,
|
||||
);
|
||||
for country in blocked_in {
|
||||
let bridge_country_info = bridge_info.info_by_country.get_mut(&country).unwrap();
|
||||
let bridge_country_info = bridge_info.info_by_country.get(&country).unwrap();
|
||||
if !bridge_country_info.blocked {
|
||||
new_blockages.insert(country.to_string());
|
||||
// Mark bridge as blocked when db gets updated
|
||||
bridge_country_info.blocked = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
blockages.insert(fingerprint, new_blockages);
|
||||
|
||||
// Commit changes to database
|
||||
db.insert(fingerprint, bincode::serialize(&bridge_info).unwrap())
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
// Remove all bridges to re-evaluate from DB
|
||||
|
@ -843,6 +837,26 @@ pub fn guess_blockages(
|
|||
blockages
|
||||
}
|
||||
|
||||
/// Commit blocked bridges to database
|
||||
pub fn commit_blockages(db: &Db, blockages: &HashMap<[u8; 20], HashSet<String>>) {
|
||||
// For each bridge:<set of countries>, mark the bridge as blocked in each country
|
||||
for (fingerprint, countries) in blockages {
|
||||
if let Some(v) = db.get(&fingerprint).unwrap() {
|
||||
let mut bridge_info: BridgeInfo = bincode::deserialize(&v).unwrap();
|
||||
|
||||
for country in countries {
|
||||
if let Some(bridge_country_info) = bridge_info.info_by_country.get_mut(country) {
|
||||
bridge_country_info.blocked = true;
|
||||
}
|
||||
}
|
||||
|
||||
// Commit changes to database
|
||||
db.insert(fingerprint, bincode::serialize(&bridge_info).unwrap())
|
||||
.unwrap();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Report blocked bridges to bridge distributor
|
||||
pub async fn report_blockages(
|
||||
distributors: &BTreeMap<BridgeDistributor, String>,
|
||||
|
|
|
@ -109,6 +109,8 @@ async fn update_daily_info(
|
|||
min_historical_days,
|
||||
max_historical_days,
|
||||
);
|
||||
// TODO: Verify the new blockages
|
||||
commit_blockages(db, &new_blockages);
|
||||
report_blockages(distributors, new_blockages.clone()).await;
|
||||
|
||||
// Generate tomorrow's key if we don't already have it
|
||||
|
|
Loading…
Reference in New Issue