Siddharth63 commited on
Commit
e37c394
·
verified ·
1 Parent(s): e85a2b5

Add review_server.py

Browse files
Files changed (1) hide show
  1. review_server.py +142 -0
review_server.py ADDED
@@ -0,0 +1,142 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ """Serve a cascade batch's review.html and write every human verdict straight into the Excel workbook.
3
+
4
+ python3 review_server.py --batch ~/cascade-review/batch # then open http://localhost:8765
5
+ python3 review_server.py --batch ... --host 0.0.0.0 # let colleagues on the same network review too
6
+
7
+ Each thumbs up / down or comment in the page is POSTed here and saved to <batch>/reviews.json (the source of truth, one
8
+ entry per product x view, last write wins). A couple of seconds after the last change, <batch>/cascade_report.xlsx is
9
+ rebuilt with those verdicts in its Human verdict / comment / reviewer columns (thumbs up = Pass, down = Fail), so the
10
+ Summary formulas update too. Excel does not reload a file it already has open: reopen the workbook to see new verdicts.
11
+ """
12
+ from __future__ import annotations
13
+
14
+ import argparse
15
+ import json
16
+ import os
17
+ import threading
18
+ import time
19
+ from datetime import datetime, timezone
20
+ from functools import partial
21
+ from http.server import SimpleHTTPRequestHandler, ThreadingHTTPServer
22
+ from pathlib import Path
23
+
24
+ from build_report_excel import build
25
+
26
+ FIELDS = ("source", "product_id", "view", "human_verdict", "human_comment", "reviewer", "reviewed_at")
27
+
28
+
29
+ class Store:
30
+ def __init__(self, batch: Path, debounce: float, after_build=None):
31
+ self.batch, self.debounce, self.after_build = batch, debounce, after_build # after_build(reviews_json, xlsx): e.g. push to the Hub
32
+ self.path, self.xlsx = batch / "reviews.json", batch / "cascade_report.xlsx"
33
+ self.lock, self.dirty = threading.Lock(), threading.Event()
34
+ self.reviews = {f"{r['source']}|{r['product_id']}|{r['view']}": r for r in json.loads(self.path.read_text())} if self.path.exists() else {}
35
+ self.excel = {"updated": None, "error": None, "pending": False}
36
+ threading.Thread(target=self._excel_worker, daemon=True).start()
37
+
38
+ def put(self, entry: dict) -> dict:
39
+ r = {k: str(entry.get(k) or "") for k in FIELDS}
40
+ if r["human_verdict"] not in ("", "pass", "fail") or not (r["source"] and r["product_id"] and r["view"]):
41
+ raise ValueError("bad review entry")
42
+ r["reviewed_at"] = datetime.now(timezone.utc).isoformat(timespec="seconds")
43
+ with self.lock:
44
+ key = f"{r['source']}|{r['product_id']}|{r['view']}"
45
+ if r["human_verdict"] or r["human_comment"]:
46
+ self.reviews[key] = r
47
+ else:
48
+ self.reviews.pop(key, None)
49
+ tmp = self.path.with_suffix(".json.tmp")
50
+ tmp.write_text(json.dumps(list(self.reviews.values()), indent=1, ensure_ascii=False))
51
+ os.replace(tmp, self.path)
52
+ self.excel["pending"] = True
53
+ self.dirty.set()
54
+ return r
55
+
56
+ def _excel_worker(self):
57
+ while True:
58
+ self.dirty.wait()
59
+ time.sleep(self.debounce) # batch quick successive clicks into one rebuild
60
+ self.dirty.clear()
61
+ with self.lock:
62
+ entries = list(self.reviews.values())
63
+ try:
64
+ tmp = self.xlsx.with_name(".cascade_report.tmp.xlsx")
65
+ msg = build(self.batch, entries, tmp)
66
+ os.replace(tmp, self.xlsx)
67
+ self.excel.update(updated=datetime.now().strftime("%H:%M:%S"), error=None)
68
+ print(msg.replace(str(tmp), str(self.xlsx)), flush=True)
69
+ if self.after_build:
70
+ self.after_build(self.path, self.xlsx)
71
+ except Exception as e: # keep serving; the page shows the error
72
+ self.excel["error"] = f"{type(e).__name__}: {e}"
73
+ print("Excel rebuild failed:", self.excel["error"], flush=True)
74
+ self.excel["pending"] = self.dirty.is_set()
75
+
76
+
77
+ class Handler(SimpleHTTPRequestHandler):
78
+ store: Store
79
+
80
+ def _json(self, obj, code=200):
81
+ body = json.dumps(obj).encode()
82
+ self.send_response(code)
83
+ self.send_header("Content-Type", "application/json")
84
+ self.send_header("Content-Length", str(len(body)))
85
+ self.send_header("Cache-Control", "no-store")
86
+ self.end_headers()
87
+ self.wfile.write(body)
88
+
89
+ def do_GET(self):
90
+ if self.path in ("/", "/index.html"):
91
+ self.path = "/review.html"
92
+ if self.path == "/api/reviews":
93
+ with self.store.lock:
94
+ return self._json(list(self.store.reviews.values()))
95
+ if self.path == "/api/excel" and self.store.xlsx.exists():
96
+ body = self.store.xlsx.read_bytes()
97
+ self.send_response(200)
98
+ self.send_header("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
99
+ self.send_header("Content-Disposition", 'attachment; filename="cascade_report.xlsx"')
100
+ self.send_header("Content-Length", str(len(body)))
101
+ self.end_headers()
102
+ return self.wfile.write(body)
103
+ if self.path == "/api/status":
104
+ return self._json({**self.store.excel, "reviews": len(self.store.reviews)})
105
+ if self.path.startswith("/api/") or self.path.endswith((".json", ".xlsx")) and "/" not in self.path.lstrip("/"):
106
+ return self._json({"error": "not found"}, 404) # don't serve reviews.json / the workbook as files
107
+ return super().do_GET()
108
+
109
+ def do_POST(self):
110
+ if self.path != "/api/review":
111
+ return self._json({"error": "not found"}, 404)
112
+ try:
113
+ n = int(self.headers.get("Content-Length", 0))
114
+ return self._json({"ok": True, "review": self.store.put(json.loads(self.rfile.read(n)))})
115
+ except (ValueError, KeyError) as e:
116
+ return self._json({"ok": False, "error": str(e)}, 400)
117
+
118
+ def log_message(self, fmt, *args):
119
+ if self.path.startswith("/api/review") and self.command == "POST":
120
+ return
121
+ if args and str(args[1]).startswith(("4", "5")):
122
+ super().log_message(fmt, *args)
123
+
124
+
125
+ def main() -> None:
126
+ ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
127
+ ap.add_argument("--batch", type=Path, required=True)
128
+ ap.add_argument("--host", default="127.0.0.1")
129
+ ap.add_argument("--port", type=int, default=8765)
130
+ ap.add_argument("--debounce", type=float, default=2.0, help="seconds after the last change before the workbook is rebuilt")
131
+ a = ap.parse_args()
132
+ batch = a.batch.expanduser().resolve()
133
+ if not (batch / "review.html").exists():
134
+ raise SystemExit(f"{batch}/review.html not found: run build_review_book.py --batch {batch} first")
135
+ Handler.store = Store(batch, a.debounce)
136
+ srv = ThreadingHTTPServer((a.host, a.port), partial(Handler, directory=str(batch)))
137
+ print(f"Reviewing {batch} ({len(Handler.store.reviews)} saved reviews) at http://{'localhost' if a.host == '127.0.0.1' else a.host}:{a.port}", flush=True)
138
+ srv.serve_forever()
139
+
140
+
141
+ if __name__ == "__main__":
142
+ main()