risk-nexus/tools/coverage.py

66 lines
2.2 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
"""What has never been looked at.
RISK-WP-0005-T07, deliberately minimal. The register knows what was reported.
Without this it has no view of what was never assessed, so a system with zero
findings is indistinguishable from a system nobody has examined while
RISK-N-0003 records that every repo which *has* examined its own boundary this
month found a defect.
This does not assess anything and does not grade anyone. It counts.
"""
from __future__ import annotations
import json
import urllib.error
import urllib.request
import register_lib as lib
HUB = "http://127.0.0.1:8000/repos/"
def repos() -> list[dict] | None:
for url in (HUB, "http://127.0.0.1:8000/repos"):
try:
with urllib.request.urlopen(url, timeout=8) as r:
data = json.load(r)
return data if isinstance(data, list) else data.get("items", [])
except (urllib.error.URLError, TimeoutError, json.JSONDecodeError, OSError):
continue
return None
def main() -> int:
all_repos = repos()
seen: dict[str, list[str]] = {}
for f in lib.findings():
for key in (f.get("system"), f.get("fix_owner"), f.get("reported_by")):
if key and isinstance(key, str):
seen.setdefault(key, []).append(f["id"])
print("Coverage — what the register has heard from\n")
print(f" systems that have produced or carried a finding: {len(seen)}")
for name in sorted(seen):
print(f" {name}: {', '.join(sorted(set(seen[name])))}")
if all_repos is None:
print("\n Hub unreachable — the denominator is unknown, which is the whole point of this report.")
return 0
names = {r.get("slug") for r in all_repos if r.get("slug")}
unheard = sorted(names - set(seen))
print(f"\n registered repos: {len(names)}")
print(f" never appeared in any finding: {len(unheard)}")
print("\n A repo in that list has either nothing wrong with it or nobody looking.")
print(" This register cannot tell which, and does not guess.\n")
for name in unheard[:40]:
print(f" {name}")
if len(unheard) > 40:
print(f" … and {len(unheard) - 40} more")
return 0
if __name__ == "__main__":
raise SystemExit(main())