Refactor tls_findings into smaller helper functions.

This change splits the large `tls_findings` function in `src/netsleuth/interpret.py` into three focused helper functions (`_check_tls_error`, `_check_tls_performance`, and `_check_tls_certificate`) to improve readability and maintainability.
Also fixes unused imports identified by ruff.

Co-authored-by: Horizontal42 <75383519+Horizontal42@users.noreply.github.com>
This commit is contained in:
google-labs-jules[bot] 2026-08-19 11:36:16 +00:00 • committed by Andrew Pimenov
parent 03d54b900c
commit 44ff8c6f85

View file

@ -755,12 +755,10 @@ def _cpu_bound_ratio(tls_handshake_ms: float | None, tcp_rtt_ms: float | None) -
return tls_handshake_ms / tcp_rtt_ms return tls_handshake_ms / tcp_rtt_ms
def tls_findings(results: list[TlsResult], t: Thresholds) -> list[Finding]: def _check_tls_error(r: TlsResult) -> Finding | None:
findings: list[Finding] = [] if not r.error:
for r in results: return None
if r.error: return Finding(
findings.append(
Finding(
id=f"tls.unreachable.{r.label}", id=f"tls.unreachable.{r.label}",
severity="warn", severity="warn",
title=f"TLS handshake to {r.host} failed", title=f"TLS handshake to {r.host} failed",
@ -772,8 +770,10 @@ def tls_findings(results: list[TlsResult], t: Thresholds) -> list[Finding]:
detail_ru=f"Не удалось установить TLS-соединение с {r.host}:{r.port}: {r.error}", detail_ru=f"Не удалось установить TLS-соединение с {r.host}:{r.port}: {r.error}",
advice_ru="Сервис может быть недоступен, либо порт заблокирован на пути к серверу.", advice_ru="Сервис может быть недоступен, либо порт заблокирован на пути к серверу.",
) )
)
continue
def _check_tls_performance(r: TlsResult, t: Thresholds) -> list[Finding]:
findings: list[Finding] = []
ratio = _cpu_bound_ratio(r.tls_handshake_ms, r.tcp_rtt_ms) ratio = _cpu_bound_ratio(r.tls_handshake_ms, r.tcp_rtt_ms)
if ratio is not None and ratio > t.tls_cpu_bound_ratio and (r.tls_handshake_ms or 0.0) >= 20.0: if ratio is not None and ratio > t.tls_cpu_bound_ratio and (r.tls_handshake_ms or 0.0) >= 20.0:
findings.append( findings.append(
@ -825,6 +825,11 @@ def tls_findings(results: list[TlsResult], t: Thresholds) -> list[Finding]:
advice_ru="Медленный TTFB при быстром рукопожатии указывает на обработку на стороне сервера, а не на сеть.", advice_ru="Медленный TTFB при быстром рукопожатии указывает на обработку на стороне сервера, а не на сеть.",
) )
) )
return findings
def _check_tls_certificate(r: TlsResult) -> list[Finding]:
findings: list[Finding] = []
if r.pin_verdict == "mismatch": if r.pin_verdict == "mismatch":
findings.append( findings.append(
Finding( Finding(
@ -878,6 +883,18 @@ def tls_findings(results: list[TlsResult], t: Thresholds) -> list[Finding]:
return findings return findings
def tls_findings(results: list[TlsResult], t: Thresholds) -> list[Finding]:
findings: list[Finding] = []
for r in results:
err = _check_tls_error(r)
if err:
findings.append(err)
continue
findings.extend(_check_tls_performance(r, t))
findings.extend(_check_tls_certificate(r))
return findings
def prefix_findings(bench: PrefixBenchmark, t: Thresholds) -> list[Finding]: def prefix_findings(bench: PrefixBenchmark, t: Thresholds) -> list[Finding]:
findings: list[Finding] = [] findings: list[Finding] = []
if not bench.results: if not bench.results: