lint: reformat python code with black
This commit is contained in:
parent
331beb01b4
commit
a406a7974b
88 changed files with 2579 additions and 1608 deletions
|
@ -12,19 +12,23 @@ from app.terraform import BaseAutomation
|
|||
|
||||
|
||||
def check_origin(domain_name: str) -> Dict[str, Any]:
|
||||
start_date = (datetime.now(tz=timezone.utc) - timedelta(days=1)).strftime("%Y-%m-%dT%H%%3A%M")
|
||||
start_date = (datetime.now(tz=timezone.utc) - timedelta(days=1)).strftime(
|
||||
"%Y-%m-%dT%H%%3A%M"
|
||||
)
|
||||
end_date = datetime.now(tz=timezone.utc).strftime("%Y-%m-%dT%H%%3A%M")
|
||||
api_url = f"https://api.ooni.io/api/v1/measurements?domain={domain_name}&since={start_date}&until={end_date}"
|
||||
result: Dict[str, Dict[str, int]] = defaultdict(lambda: {"anomaly": 0, "confirmed": 0, "failure": 0, "ok": 0})
|
||||
result: Dict[str, Dict[str, int]] = defaultdict(
|
||||
lambda: {"anomaly": 0, "confirmed": 0, "failure": 0, "ok": 0}
|
||||
)
|
||||
return _check_origin(api_url, result)
|
||||
|
||||
|
||||
def _check_origin(api_url: str, result: Dict[str, Any]) -> Dict[str, Any]:
|
||||
print(f"Processing {api_url}")
|
||||
req = requests.get(api_url, timeout=30).json()
|
||||
if 'results' not in req or not req['results']:
|
||||
if "results" not in req or not req["results"]:
|
||||
return result
|
||||
for r in req['results']:
|
||||
for r in req["results"]:
|
||||
not_ok = False
|
||||
for status in ["anomaly", "confirmed", "failure"]:
|
||||
if status in r and r[status]:
|
||||
|
@ -33,27 +37,28 @@ def _check_origin(api_url: str, result: Dict[str, Any]) -> Dict[str, Any]:
|
|||
break
|
||||
if not not_ok:
|
||||
result[r["probe_cc"]]["ok"] += 1
|
||||
if req['metadata']['next_url']:
|
||||
return _check_origin(req['metadata']['next_url'], result)
|
||||
if req["metadata"]["next_url"]:
|
||||
return _check_origin(req["metadata"]["next_url"], result)
|
||||
return result
|
||||
|
||||
|
||||
def threshold_origin(domain_name: str) -> Dict[str, Any]:
|
||||
ooni = check_origin(domain_name)
|
||||
for country in ooni:
|
||||
total = sum([
|
||||
ooni[country]["anomaly"],
|
||||
ooni[country]["confirmed"],
|
||||
ooni[country]["failure"],
|
||||
ooni[country]["ok"]
|
||||
])
|
||||
total_blocks = sum([
|
||||
ooni[country]["anomaly"],
|
||||
ooni[country]["confirmed"]
|
||||
])
|
||||
total = sum(
|
||||
[
|
||||
ooni[country]["anomaly"],
|
||||
ooni[country]["confirmed"],
|
||||
ooni[country]["failure"],
|
||||
ooni[country]["ok"],
|
||||
]
|
||||
)
|
||||
total_blocks = sum([ooni[country]["anomaly"], ooni[country]["confirmed"]])
|
||||
block_perc = round((total_blocks / total * 100), 1)
|
||||
ooni[country]["block_perc"] = block_perc
|
||||
ooni[country]["state"] = AlarmState.WARNING if block_perc > 20 else AlarmState.OK
|
||||
ooni[country]["state"] = (
|
||||
AlarmState.WARNING if block_perc > 20 else AlarmState.OK
|
||||
)
|
||||
ooni[country]["message"] = f"Blocked in {block_perc}% of measurements"
|
||||
return ooni
|
||||
|
||||
|
@ -72,8 +77,9 @@ class BlockOONIAutomation(BaseAutomation):
|
|||
for origin in origins:
|
||||
ooni = threshold_origin(origin.domain_name)
|
||||
for country in ooni:
|
||||
alarm = get_or_create_alarm(origin.brn,
|
||||
f"origin-block-ooni-{country.lower()}")
|
||||
alarm = get_or_create_alarm(
|
||||
origin.brn, f"origin-block-ooni-{country.lower()}"
|
||||
)
|
||||
alarm.update_state(ooni[country]["state"], ooni[country]["message"])
|
||||
db.session.commit()
|
||||
return True, ""
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue