evaluate-blockages.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #!/usr/bin/env python3
  2. import csv
  3. import os
  4. # If abs is True, we need to have observed more than 32 connections on
  5. # some day to be considered blocked.
  6. def is_blocked (harshness, bridge_ips_max, bridge_ips_today, abs, no_min):
  7. if bridge_ips_max is None:
  8. return False
  9. threshold = 8 * harshness
  10. if abs:
  11. # absolute threshold
  12. return bridge_ips_today <= threshold and (no_min or bridge_ips_max > 32)
  13. else:
  14. # relative threshold based on prior connection counts
  15. return bridge_ips_today <= threshold and (no_min or bridge_ips_today <= bridge_ips_max - 32 + threshold)
  16. def evaluate (harshness, fingerprint, abs=True, no_min=False):
  17. fingerprint = fingerprint.upper()
  18. bridge_ips_max = None
  19. with open (f"data/bridge_data_cleaned/{fingerprint}", 'r') as file:
  20. bridge_data = csv.reader(file, delimiter=',')
  21. for row in bridge_data:
  22. bridge_ips_today = int(row[1])
  23. if not bridge_ips_max is None:
  24. if is_blocked (harshness, bridge_ips_max, bridge_ips_today, abs, no_min):
  25. return row[0]
  26. # Start bridge_ips_max only when we have a non-zero
  27. # connection count
  28. elif bridge_ips_today > 0:
  29. bridge_ips_max = bridge_ips_today
  30. # If we got here, the bridge is not blocked
  31. return None
  32. # Remove any previous blocked_* files, start over
  33. for i in range(5):
  34. if os.path.exists (f"data/blocked_{i}"):
  35. os.remove (f"data/blocked_{i}")
  36. if os.path.exists (f"data/blocked_{i}_abs"):
  37. os.remove (f"data/blocked_{i}_abs")
  38. if os.path.exists (f"data/blocked_{i}_nomin"):
  39. os.remove (f"data/blocked_{i}_nomin")
  40. with open ("data/all-bridges", 'r') as all_bridges:
  41. for fingerprint in all_bridges:
  42. fingerprint = fingerprint.strip()
  43. if fingerprint:
  44. # Go through all harshness values
  45. for harshness in range(5):
  46. blocked = evaluate (harshness, fingerprint, False)
  47. # If the bridge is blocked add its fingerprint and
  48. # blocked date to the list for that harshness level
  49. if not blocked is None:
  50. with open (f"data/blocked_{harshness}", 'a') as f:
  51. f.write(f"{fingerprint},{blocked}\n")
  52. blocked = evaluate (harshness, fingerprint, True)
  53. if not blocked is None:
  54. with open (f"data/blocked_{harshness}_abs", 'a') as f:
  55. f.write(f"{fingerprint},{blocked}\n")
  56. blocked = evaluate (harshness, fingerprint, False, True)
  57. if not blocked is None:
  58. with open (f"data/blocked_{harshness}_nomin", 'a') as f:
  59. f.write(f"{fingerprint},{blocked}\n")