2026-04-10 05:18:44 -03:00
|
|
|
#!/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"}
|
2026-04-17 05:43:21 -03:00
|
|
|
- platform_quality_gate_workspace_line_coverage_percent{suite="atlasbot"}
|
|
|
|
|
- platform_quality_gate_source_lines_over_500_total{suite="atlasbot"}
|
2026-04-10 05:18:44 -03:00
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
|
|
import json
|
2026-01-28 11:46:52 -03:00
|
|
|
import os
|
2026-04-10 05:18:44 -03:00
|
|
|
import urllib.request
|
2026-01-28 11:46:52 -03:00
|
|
|
import xml.etree.ElementTree as ET
|
2026-04-10 05:18:44 -03:00
|
|
|
from pathlib import Path
|
|
|
|
|
|
2026-01-28 11:46:52 -03:00
|
|
|
|
2026-04-10 05:18:44 -03:00
|
|
|
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
|
2026-01-28 11:46:52 -03:00
|
|
|
|
|
|
|
|
|
2026-04-10 05:18:44 -03:00
|
|
|
def _load_junit(path: Path) -> dict[str, int]:
|
|
|
|
|
if not path.exists():
|
|
|
|
|
return {"tests": 0, "failures": 0, "errors": 0, "skipped": 0}
|
2026-01-28 11:46:52 -03:00
|
|
|
|
|
|
|
|
tree = ET.parse(path)
|
|
|
|
|
root = tree.getroot()
|
2026-04-10 05:18:44 -03:00
|
|
|
suites: list[ET.Element]
|
|
|
|
|
if root.tag == "testsuite":
|
|
|
|
|
suites = [root]
|
|
|
|
|
elif root.tag == "testsuites":
|
|
|
|
|
suites = list(root.findall("testsuite"))
|
|
|
|
|
else:
|
|
|
|
|
suites = []
|
2026-01-28 11:46:52 -03:00
|
|
|
|
2026-04-10 05:18:44 -03:00
|
|
|
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():
|
2026-01-28 11:46:52 -03:00
|
|
|
return 0.0
|
2026-04-10 05:18:44 -03:00
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
2026-04-17 05:43:21 -03:00
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
2026-04-10 05:18:44 -03:00
|
|
|
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"))
|
2026-04-17 05:43:21 -03:00
|
|
|
source_root = Path(os.getenv("SOURCE_ROOT", "atlasbot"))
|
2026-01-28 11:46:52 -03:00
|
|
|
|
2026-04-10 05:18:44 -03:00
|
|
|
totals = _load_junit(junit_path)
|
|
|
|
|
coverage_pct = _load_coverage_percent(coverage_path)
|
2026-04-17 05:43:21 -03:00
|
|
|
source_lines_over_500 = _count_source_lines_over_500(source_root)
|
2026-04-10 05:18:44 -03:00
|
|
|
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"
|
2026-01-28 11:46:52 -03:00
|
|
|
|
2026-04-10 05:18:44 -03:00
|
|
|
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
|
2026-01-28 11:46:52 -03:00
|
|
|
|
2026-04-10 05:18:44 -03:00
|
|
|
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}',
|
2026-04-17 05:43:21 -03:00
|
|
|
"# 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}',
|
2026-04-10 05:18:44 -03:00
|
|
|
]
|
|
|
|
|
) + "\n"
|
2026-01-28 11:46:52 -03:00
|
|
|
|
2026-04-10 05:18:44 -03:00
|
|
|
_post_text(f"{pushgateway_url}/metrics/job/platform-quality-ci/suite/{suite}", payload)
|
|
|
|
|
return 0
|
2026-01-28 11:46:52 -03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2026-04-10 05:18:44 -03:00
|
|
|
raise SystemExit(main())
|