|
| 1 | +"""Build the coverage-gap Markdown report from collected ``CoveragePoint`` s.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from collections import defaultdict |
| 6 | +from collections.abc import Iterable |
| 7 | + |
| 8 | +from .curated import curated_slugs |
| 9 | +from .sources.base import CoveragePoint |
| 10 | + |
| 11 | + |
| 12 | +def build_report(points: Iterable[CoveragePoint], *, top_n: int = 30) -> str: |
| 13 | + """Render the report. ``top_n`` caps lines per category × manufacturer.""" |
| 14 | + # Aggregate upstream slugs and remember the first (name, url) seen per slug. |
| 15 | + upstream: dict[tuple[str, str], set[str]] = defaultdict(set) |
| 16 | + meta: dict[tuple[str, str, str], tuple[str, str]] = {} |
| 17 | + for point in points: |
| 18 | + upstream[(point.category, point.manufacturer)].add(point.slug) |
| 19 | + meta.setdefault((point.category, point.manufacturer, point.slug), (point.name, point.url)) |
| 20 | + |
| 21 | + out: list[str] = ["# TechAPI coverage gaps", ""] |
| 22 | + out.append("_Auto-generated by TechEngine. Top entries shown per category × manufacturer._") |
| 23 | + out.append("") |
| 24 | + |
| 25 | + total_missing = 0 |
| 26 | + section_lines: list[str] = [] |
| 27 | + for (category, manufacturer) in sorted(upstream.keys()): |
| 28 | + upstream_slugs = upstream[(category, manufacturer)] |
| 29 | + curated = curated_slugs(category, manufacturer) |
| 30 | + missing = sorted(upstream_slugs - curated) |
| 31 | + total_missing += len(missing) |
| 32 | + section_lines.append( |
| 33 | + f"## {category} / {manufacturer} — {len(missing)} missing " |
| 34 | + f"(upstream {len(upstream_slugs)}, curated {len(curated)})" |
| 35 | + ) |
| 36 | + if not missing: |
| 37 | + section_lines.append("_(none)_") |
| 38 | + section_lines.append("") |
| 39 | + continue |
| 40 | + for slug in missing[:top_n]: |
| 41 | + name, url = meta.get((category, manufacturer, slug), (slug, "")) |
| 42 | + link = f" ([source]({url}))" if url else "" |
| 43 | + section_lines.append(f"- `{slug}` — {name}{link}") |
| 44 | + if len(missing) > top_n: |
| 45 | + section_lines.append(f"_… and {len(missing) - top_n} more._") |
| 46 | + section_lines.append("") |
| 47 | + |
| 48 | + out.append(f"**Total missing:** {total_missing}") |
| 49 | + out.append("") |
| 50 | + out.extend(section_lines) |
| 51 | + return "\n".join(out).rstrip() + "\n" |
0 commit comments