optimize overall_verdict by combining severity iterations

This commit is contained in:
Andrew Pimenov 2026-08-09 16:10:45 +03:00
parent ab39922bf6
commit fb3c6ca77f
2 changed files with 4 additions and 18 deletions

View file

@ -1,6 +1,5 @@
from __future__ import annotations
from typing import Iterable
from netsleuth.config import Band, BufferbloatBands, Thresholds, VpnBands
from netsleuth.models import (
@ -21,14 +20,6 @@ from netsleuth.netinfo import is_tunnel_iface, mtu_anomaly
_SEVERITY_ORDER = {"ok": 0, "info": 1, "warn": 2, "crit": 3}
def worst(severities: Iterable[str]) -> str:
best = "ok"
for severity in severities:
if _SEVERITY_ORDER[severity] > _SEVERITY_ORDER[best]:
best = severity
return best
def severity_for(value: float | None, band: Band, higher_is_worse: bool = True) -> str:
if value is None:
return "info"
@ -349,10 +340,12 @@ _SCORE_PENALTY = {"ok": 0, "info": 3, "warn": 10, "crit": 25}
def overall_verdict(findings: list[Finding]) -> tuple[str, int, str]:
score = 100
status = "ok"
for f in findings:
score -= _SCORE_PENALTY[f.severity]
if _SEVERITY_ORDER[f.severity] > _SEVERITY_ORDER[status]:
status = f.severity
score = max(0, score)
status = worst(f.severity for f in findings)
if status in ("ok", "info"):
return "ok", score, "No problems found on this connection."
headline = [f.title for f in findings if f.severity == status]

View file

@ -4,7 +4,7 @@ import pytest
from netsleuth.config import Band, Thresholds
from netsleuth.models import PingResult, TraceHop, TraceResult
from netsleuth.interpret import latency_findings, path_findings, severity_for, worst
from netsleuth.interpret import latency_findings, path_findings, severity_for
@pytest.fixture()
@ -33,13 +33,6 @@ def test_severity_can_be_inverted_for_metrics_where_higher_is_better():
assert severity_for(5.0, band, higher_is_worse=False) == "crit"
def test_worst_picks_the_highest_severity():
assert worst(["ok", "info", "warn"]) == "warn"
assert worst(["ok", "crit", "warn"]) == "crit"
assert worst([]) == "ok"
assert worst(["ok", "ok"]) == "ok"
def ping(**kw) -> PingResult:
base = dict(
label="cloudflare-dns",