#!/usr/bin/env python3 """Publish Atlasbot CI test metrics to Pushgateway. Inputs: - JUnit XML file and coverage JSON file. Outputs: - platform_quality_gate_runs_total{suite="atlasbot",status="ok|failed"} - atlasbot_quality_gate_tests_total{suite="atlasbot",result=*} - atlasbot_quality_gate_coverage_percent{suite="atlasbot"} - platform_quality_gate_workspace_line_coverage_percent{suite="atlasbot"} - platform_quality_gate_source_lines_over_500_total{suite="atlasbot"} """ from __future__ import annotations import json import os import urllib.request import xml.etree.ElementTree as ET from pathlib import Path def _as_int(node: ET.Element, name: str) -> int: raw = node.attrib.get(name) or "0" try: return int(float(raw)) except ValueError: return 0 def _load_junit(path: Path) -> dict[str, int]: if not path.exists(): return {"tests": 0, "failures": 0, "errors": 0, "skipped": 0} tree = ET.parse(path) root = tree.getroot() suites: list[ET.Element] if root.tag == "testsuite": suites = [root] elif root.tag == "testsuites": suites = list(root.findall("testsuite")) else: suites = [] totals = {"tests": 0, "failures": 0, "errors": 0, "skipped": 0} for suite in suites: totals["tests"] += _as_int(suite, "tests") totals["failures"] += _as_int(suite, "failures") totals["errors"] += _as_int(suite, "errors") totals["skipped"] += _as_int(suite, "skipped") return totals def _load_coverage_percent(path: Path) -> float: if not path.exists(): return 0.0 payload = json.loads(path.read_text(encoding="utf-8")) summary = payload.get("summary") or {} percent = summary.get("percent_covered") if isinstance(percent, (int, float)): return float(percent) return 0.0 def _count_source_lines_over_500(root: Path) -> int: if not root.exists(): return 0 over = 0 for path in root.rglob("*.py"): if not path.is_file(): continue line_count = sum(1 for _ in path.open("r", encoding="utf-8")) if line_count > 500: over += 1 return over def _read_text(url: str) -> str: try: with urllib.request.urlopen(url, timeout=10) as resp: return resp.read().decode("utf-8", errors="replace") except Exception: return "" def _counter(metrics: str, suite: str, status: str) -> float: for line in metrics.splitlines(): if not line.startswith("platform_quality_gate_runs_total{"): continue if f'job="platform-quality-ci"' not in line: continue if f'suite="{suite}"' not in line: continue if f'status="{status}"' not in line: continue parts = line.split() if len(parts) < 2: continue try: return float(parts[1]) except ValueError: return 0.0 return 0.0 def _post_text(url: str, payload: str) -> None: req = urllib.request.Request( url, data=payload.encode("utf-8"), method="POST", headers={"Content-Type": "text/plain"}, ) with urllib.request.urlopen(req, timeout=10) as resp: if resp.status >= 400: raise RuntimeError(f"push failed status={resp.status}") def main() -> int: suite = os.getenv("SUITE_NAME", "atlasbot") pushgateway_url = os.getenv( "PUSHGATEWAY_URL", "http://platform-quality-gateway.monitoring.svc.cluster.local:9091" ).rstrip("/") junit_path = Path(os.getenv("JUNIT_PATH", "build/junit.xml")) coverage_path = Path(os.getenv("COVERAGE_PATH", "build/coverage.json")) source_root = Path(os.getenv("SOURCE_ROOT", "atlasbot")) totals = _load_junit(junit_path) coverage_pct = _load_coverage_percent(coverage_path) source_lines_over_500 = _count_source_lines_over_500(source_root) passed = max(totals["tests"] - totals["failures"] - totals["errors"] - totals["skipped"], 0) outcome = "ok" if totals["tests"] > 0 and totals["failures"] == 0 and totals["errors"] == 0 else "failed" metrics = _read_text(f"{pushgateway_url}/metrics") ok_count = _counter(metrics, suite, "ok") failed_count = _counter(metrics, suite, "failed") if outcome == "ok": ok_count += 1 else: failed_count += 1 payload = "\n".join( [ "# TYPE platform_quality_gate_runs_total counter", f'platform_quality_gate_runs_total{{suite="{suite}",status="ok"}} {ok_count:.0f}', f'platform_quality_gate_runs_total{{suite="{suite}",status="failed"}} {failed_count:.0f}', "# TYPE atlasbot_quality_gate_tests_total gauge", f'atlasbot_quality_gate_tests_total{{suite="{suite}",result="passed"}} {passed}', f'atlasbot_quality_gate_tests_total{{suite="{suite}",result="failed"}} {totals["failures"]}', f'atlasbot_quality_gate_tests_total{{suite="{suite}",result="error"}} {totals["errors"]}', f'atlasbot_quality_gate_tests_total{{suite="{suite}",result="skipped"}} {totals["skipped"]}', "# TYPE atlasbot_quality_gate_coverage_percent gauge", f'atlasbot_quality_gate_coverage_percent{{suite="{suite}"}} {coverage_pct:.3f}', "# TYPE platform_quality_gate_workspace_line_coverage_percent gauge", f'platform_quality_gate_workspace_line_coverage_percent{{suite="{suite}"}} {coverage_pct:.3f}', "# TYPE platform_quality_gate_source_lines_over_500_total gauge", f'platform_quality_gate_source_lines_over_500_total{{suite="{suite}"}} {source_lines_over_500}', ] ) + "\n" _post_text(f"{pushgateway_url}/metrics/job/platform-quality-ci/suite/{suite}", payload) return 0 if __name__ == "__main__": raise SystemExit(main())