-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub_traffic_local_api.py
More file actions
executable file
·374 lines (306 loc) · 11.6 KB
/
github_traffic_local_api.py
File metadata and controls
executable file
·374 lines (306 loc) · 11.6 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
#!/usr/bin/env python3
from __future__ import annotations
import argparse
import json
import sqlite3
import subprocess
import sys
from datetime import datetime, timezone
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
from pathlib import Path
from typing import Any
DEFAULT_HOST = "127.0.0.1"
DEFAULT_PORT = 8765
DEFAULT_DB = Path.home() / "github-traffic" / "github-traffic.sqlite3"
DEFAULT_PROJECT_DIR = Path(__file__).resolve().parent
def utc_now_iso() -> str:
return datetime.now(timezone.utc).replace(microsecond=0).isoformat()
def expand_path(raw: str | Path) -> Path:
return Path(raw).expanduser().resolve()
def connect_db(db_path: Path) -> sqlite3.Connection:
conn = sqlite3.connect(str(db_path))
conn.row_factory = sqlite3.Row
return conn
def ensure_column(conn: sqlite3.Connection, table: str, column: str, column_type: str) -> None:
rows = conn.execute(f"PRAGMA table_info({table})").fetchall()
existing = {row["name"] for row in rows}
if column not in existing:
conn.execute(f"ALTER TABLE {table} ADD COLUMN {column} {column_type}")
def init_schema(conn: sqlite3.Connection) -> None:
conn.execute(
"""
CREATE TABLE IF NOT EXISTS promotion_events (
id INTEGER PRIMARY KEY,
event_time_utc TEXT NOT NULL,
owner TEXT NOT NULL,
repo TEXT NOT NULL,
event_type TEXT NOT NULL,
platform TEXT NOT NULL,
location TEXT,
url TEXT,
title TEXT NOT NULL,
notes TEXT,
framework TEXT,
trail_days INTEGER NOT NULL DEFAULT 3,
created_at_utc TEXT NOT NULL,
updated_at_utc TEXT
)
"""
)
ensure_column(conn, "promotion_events", "framework", "TEXT")
ensure_column(conn, "promotion_events", "trail_days", "INTEGER NOT NULL DEFAULT 3")
ensure_column(conn, "promotion_events", "updated_at_utc", "TEXT")
conn.execute(
"""
CREATE INDEX IF NOT EXISTS idx_promotion_events_repo_time
ON promotion_events(owner, repo, event_time_utc)
"""
)
conn.commit()
def clean_text(value: Any) -> str | None:
if value is None:
return None
text = str(value).strip()
return text if text else None
def require_text(payload: dict[str, Any], key: str) -> str:
value = clean_text(payload.get(key))
if not value:
raise ValueError(f"missing required field: {key}")
return value
def parse_trail_days(value: Any) -> int:
if value is None or value == "":
return 3
trail_days = int(value)
if trail_days < 0 or trail_days > 365:
raise ValueError("trail_days must be between 0 and 365")
return trail_days
def upsert_event(db_path: Path, payload: dict[str, Any]) -> dict[str, Any]:
owner = clean_text(payload.get("owner")) or "TorMatzAndren"
repo = require_text(payload, "repo")
event_type = require_text(payload, "event_type")
platform = require_text(payload, "platform")
title = require_text(payload, "title")
event_time_utc = clean_text(payload.get("event_time_utc")) or utc_now_iso()
trail_days = parse_trail_days(payload.get("trail_days"))
location = clean_text(payload.get("location"))
url = clean_text(payload.get("url"))
notes = clean_text(payload.get("notes"))
framework = clean_text(payload.get("framework"))
event_id_raw = payload.get("id")
event_id = int(event_id_raw) if event_id_raw not in (None, "", 0, "0") else None
conn = connect_db(db_path)
init_schema(conn)
try:
now = utc_now_iso()
if event_id is not None:
existing = conn.execute(
"SELECT id FROM promotion_events WHERE id = ?",
(event_id,),
).fetchone()
if existing is None:
raise ValueError(f"event id not found: {event_id}")
conn.execute(
"""
UPDATE promotion_events
SET
event_time_utc = ?,
owner = ?,
repo = ?,
event_type = ?,
platform = ?,
location = ?,
url = ?,
title = ?,
notes = ?,
framework = ?,
trail_days = ?,
updated_at_utc = ?
WHERE id = ?
""",
(
event_time_utc,
owner,
repo,
event_type,
platform,
location,
url,
title,
notes,
framework,
trail_days,
now,
event_id,
),
)
conn.commit()
return {"ok": True, "action": "updated", "id": event_id}
cur = conn.execute(
"""
INSERT INTO promotion_events (
event_time_utc,
owner,
repo,
event_type,
platform,
location,
url,
title,
notes,
framework,
trail_days,
created_at_utc,
updated_at_utc
)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
""",
(
event_time_utc,
owner,
repo,
event_type,
platform,
location,
url,
title,
notes,
framework,
trail_days,
now,
now,
),
)
conn.commit()
return {"ok": True, "action": "inserted", "id": int(cur.lastrowid)}
finally:
conn.close()
def delete_event(db_path: Path, payload: dict[str, Any]) -> dict[str, Any]:
event_id = int(require_text(payload, "id"))
conn = connect_db(db_path)
init_schema(conn)
try:
cur = conn.execute("DELETE FROM promotion_events WHERE id = ?", (event_id,))
conn.commit()
return {"ok": cur.rowcount > 0, "deleted": cur.rowcount, "id": event_id}
finally:
conn.close()
def regenerate_dashboard(project_dir: Path, db_path: Path) -> dict[str, Any]:
script = project_dir / "generate_static_dashboard.py"
if not script.exists():
raise FileNotFoundError(f"dashboard generator not found: {script}")
cmd = [
sys.executable,
str(script),
"--db",
str(db_path),
]
result = subprocess.run(
cmd,
cwd=str(project_dir),
text=True,
capture_output=True,
check=False,
)
return {
"ok": result.returncode == 0,
"returncode": result.returncode,
"stdout": result.stdout,
"stderr": result.stderr,
}
class ApiHandler(BaseHTTPRequestHandler):
server_version = "GitHubTrafficLocalAPI/0.1"
def log_message(self, fmt: str, *args: Any) -> None:
print(f"{self.address_string()} - {fmt % args}", file=sys.stderr)
def send_json(self, status: int, payload: dict[str, Any]) -> None:
body = json.dumps(payload, ensure_ascii=False, sort_keys=True).encode("utf-8")
self.send_response(status)
self.send_header("Content-Type", "application/json; charset=utf-8")
self.send_header("Content-Length", str(len(body)))
self.send_header("Access-Control-Allow-Origin", "*")
self.send_header("Access-Control-Allow-Methods", "GET, POST, OPTIONS")
self.send_header("Access-Control-Allow-Headers", "Content-Type")
self.end_headers()
self.wfile.write(body)
def do_OPTIONS(self) -> None:
self.send_json(200, {"ok": True})
def do_GET(self) -> None:
if self.path == "/health":
self.send_json(
200,
{
"ok": True,
"service": "github_traffic_local_api",
"mode": "localhost-only",
"allowed_actions": [
"events/upsert",
"events/delete",
"dashboard/regenerate",
],
},
)
return
self.send_json(404, {"ok": False, "error": "not found"})
def read_payload(self) -> dict[str, Any]:
length = int(self.headers.get("Content-Length", "0"))
raw = self.rfile.read(length).decode("utf-8") if length else "{}"
payload = json.loads(raw)
if not isinstance(payload, dict):
raise ValueError("payload must be a JSON object")
return payload
def do_POST(self) -> None:
try:
payload = self.read_payload()
db_path = self.server.db_path
project_dir = self.server.project_dir
if self.path == "/events/upsert":
result = upsert_event(db_path, payload)
regen = regenerate_dashboard(project_dir, db_path)
result["dashboard"] = regen
self.send_json(200 if result.get("ok") else 500, result)
return
if self.path == "/events/delete":
result = delete_event(db_path, payload)
regen = regenerate_dashboard(project_dir, db_path)
result["dashboard"] = regen
self.send_json(200 if result.get("ok") else 404, result)
return
if self.path == "/dashboard/regenerate":
result = regenerate_dashboard(project_dir, db_path)
self.send_json(200 if result.get("ok") else 500, result)
return
self.send_json(404, {"ok": False, "error": "not found"})
except Exception as exc:
self.send_json(400, {"ok": False, "error": str(exc)})
class GitHubTrafficServer(ThreadingHTTPServer):
def __init__(self, server_address: tuple[str, int], handler_class: type[BaseHTTPRequestHandler], db_path: Path, project_dir: Path):
super().__init__(server_address, handler_class)
self.db_path = db_path
self.project_dir = project_dir
def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(description="Localhost-only API for the GitHub Traffic dashboard.")
parser.add_argument("--host", default=DEFAULT_HOST, help="Bind host. Default: 127.0.0.1")
parser.add_argument("--port", type=int, default=DEFAULT_PORT, help="Bind port. Default: 8765")
parser.add_argument("--db", default=str(DEFAULT_DB), help=f"SQLite database path. Default: {DEFAULT_DB}")
parser.add_argument("--project-dir", default=str(DEFAULT_PROJECT_DIR), help=f"Project dir. Default: {DEFAULT_PROJECT_DIR}")
return parser.parse_args()
def main() -> int:
args = parse_args()
if args.host not in {"127.0.0.1", "localhost"}:
print("ERROR: public API only supports localhost binding.", file=sys.stderr)
return 2
host = "127.0.0.1"
port = args.port
db_path = expand_path(args.db)
project_dir = expand_path(args.project_dir)
server = GitHubTrafficServer((host, port), ApiHandler, db_path, project_dir)
print(f"GitHub Traffic local API listening on http://{host}:{port}")
print("Allowed actions: /events/upsert, /events/delete, /dashboard/regenerate")
print("Press Ctrl+C to stop.")
try:
server.serve_forever()
except KeyboardInterrupt:
print()
print("Stopped.")
return 0
if __name__ == "__main__":
raise SystemExit(main())