mirror of
https://github.com/Horizontal42/netsleuth.git
synced 2026-11-11 01:02:10 +03:00
optimize overall_verdict by combining severity iterations
This commit is contained in:
parent
ab39922bf6
commit
fb3c6ca77f
2 changed files with 4 additions and 18 deletions
|
|
@ -1,6 +1,5 @@
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from typing import Iterable
|
|
||||||
|
|
||||||
from netsleuth.config import Band, BufferbloatBands, Thresholds, VpnBands
|
from netsleuth.config import Band, BufferbloatBands, Thresholds, VpnBands
|
||||||
from netsleuth.models import (
|
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}
|
_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:
|
def severity_for(value: float | None, band: Band, higher_is_worse: bool = True) -> str:
|
||||||
if value is None:
|
if value is None:
|
||||||
return "info"
|
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]:
|
def overall_verdict(findings: list[Finding]) -> tuple[str, int, str]:
|
||||||
score = 100
|
score = 100
|
||||||
|
status = "ok"
|
||||||
for f in findings:
|
for f in findings:
|
||||||
score -= _SCORE_PENALTY[f.severity]
|
score -= _SCORE_PENALTY[f.severity]
|
||||||
|
if _SEVERITY_ORDER[f.severity] > _SEVERITY_ORDER[status]:
|
||||||
|
status = f.severity
|
||||||
score = max(0, score)
|
score = max(0, score)
|
||||||
status = worst(f.severity for f in findings)
|
|
||||||
if status in ("ok", "info"):
|
if status in ("ok", "info"):
|
||||||
return "ok", score, "No problems found on this connection."
|
return "ok", score, "No problems found on this connection."
|
||||||
headline = [f.title for f in findings if f.severity == status]
|
headline = [f.title for f in findings if f.severity == status]
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,7 @@ import pytest
|
||||||
|
|
||||||
from netsleuth.config import Band, Thresholds
|
from netsleuth.config import Band, Thresholds
|
||||||
from netsleuth.models import PingResult, TraceHop, TraceResult
|
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()
|
@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"
|
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:
|
def ping(**kw) -> PingResult:
|
||||||
base = dict(
|
base = dict(
|
||||||
label="cloudflare-dns",
|
label="cloudflare-dns",
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue