-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython-get-stats.py
More file actions
73 lines (60 loc) · 2.08 KB
/
python-get-stats.py
File metadata and controls
73 lines (60 loc) · 2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
from __future__ import annotations
import json
import os
from pathlib import Path
from urllib import request
REPO_ROOT = Path(__file__).resolve().parents[2]
def load_json(relative_path: str) -> dict:
return json.loads((REPO_ROOT / relative_path).read_text(encoding="utf-8"))
route = "/api/validate/stats"
sample = load_json("examples/api/stats-response.sample.json")
base_url = os.environ.get("N1HUB_BASE_URL")
token = os.environ.get("N1HUB_TOKEN")
limit_raw = os.environ.get("N1HUB_STATS_LIMIT")
limit = None
if limit_raw is not None:
try:
limit = int(limit_raw)
except ValueError as exc:
raise ValueError("N1HUB_STATS_LIMIT must be a positive integer") from exc
if limit < 1:
raise ValueError("N1HUB_STATS_LIMIT must be a positive integer")
summary = {
"route": route,
"sampleTotal": sample["total"],
"samplePassRate": sample["passRate"],
"sampleRecentCount": len(sample["recent"]),
"statsLimit": limit,
}
if base_url and token:
route_suffix = route
if limit is not None:
route_suffix = f"{route}?limit={limit}"
http_request = request.Request(
f"{base_url.rstrip('/')}{route_suffix}",
method="GET",
headers={
"Authorization": f"Bearer {token}",
"Accept": "application/json",
},
)
with request.urlopen(http_request) as response:
parsed = json.loads(response.read().decode("utf-8"))
summary.update(
{
"mode": "live",
"httpStatus": response.status,
"requestUrl": f"{base_url.rstrip('/')}{route_suffix}",
"total": parsed.get("total"),
"passRate": parsed.get("passRate"),
"recentCount": len(parsed.get("recent", [])),
}
)
else:
summary.update(
{
"mode": "dry-run",
"nextStep": "Set N1HUB_BASE_URL and N1HUB_TOKEN to request the published stats route from a live validator. Optionally set N1HUB_STATS_LIMIT for the bounded stats query path.",
}
)
print(json.dumps(summary, indent=2))