forge
Explore
Log in

Don't you dare go Hollow

hezarfen_zeka

Clone
★ 0

0 watching0 forksPython1.9 MiBe01a4faAhmet Kaan Gümüş2 months ago25 commits

check_integrity.py98 lines, 3.1 KiBHistoryRawDownload
1#!/usr/bin/env python3
2"""99_dogrula.surql icindeki butunluk sorgularini tek tek calistirir.
3
4Her sorgu 0 ihlal dondurmelidir (bos sonuc = 0). Sorgular tek tek gonderilir,
5boylece biri yavas ya da hatali olsa bile digerleri calisir ve hangisinin
6takildigi gorunur. `LET` satirlari her sorgunun onune eklenir.
7"""
8
9from __future__ import annotations
10
11import base64
12import json
13import re
14import sys
15import time
16import urllib.error
17import urllib.request
18
19ENDPOINT = "http://hzk-surreal:8000/sql"
20NS = "hezarfen"
21DB = "zeka"
22PER_QUERY_TIMEOUT = 240
23
24
25def sql(query: str):
26 req = urllib.request.Request(ENDPOINT, data=query.encode("utf-8"))
27 req.add_header("Accept", "application/json")
28 req.add_header("Content-Type", "text/plain; charset=utf-8")
29 req.add_header("surreal-ns", NS)
30 req.add_header("surreal-db", DB)
31 req.add_header("Authorization", "Basic " + base64.b64encode(b"root:root").decode())
32 with urllib.request.urlopen(req, timeout=PER_QUERY_TIMEOUT) as resp:
33 return json.loads(resp.read().decode("utf-8"))
34
35
36def main() -> int:
37 text = open("/seed/99_dogrula.surql", encoding="utf-8").read()
38 lets = [l.strip() for l in text.splitlines() if l.strip().startswith("LET ")]
39 prefix = "\n".join(lets) + "\n"
40
41 blocks = re.split(r"\n(?=--\s*I\d+:)", text)
42 passed = violations = errors = slow = 0
43 t_all = time.time()
44
45 for block in blocks:
46 m = re.match(r"--\s*(I\d+):\s*(.+)", block)
47 if not m:
48 continue
49 tag, desc = m.group(1), m.group(2).strip()
50 body = "\n".join(
51 l for l in block.splitlines() if not l.strip().startswith("--")
52 ).strip()
53 if not body:
54 continue
55
56 t0 = time.time()
57 try:
58 res = sql(prefix + body)
59 except urllib.error.HTTPError as e:
60 errors += 1
61 print(f"{tag:<5} HATA {e.code} {e.read().decode('utf-8','replace')[:120]}",
62 flush=True)
63 continue
64 except Exception as e:
65 slow += 1
66 print(f"{tag:<5} ZAMAN ASIMI ({type(e).__name__}) — {desc[:55]}", flush=True)
67 continue
68 dt = time.time() - t0
69
70 last = res[-1] if res else {}
71 if last.get("status") != "OK":
72 errors += 1
73 print(f"{tag:<5} HATA {str(last.get('result'))[:120]}", flush=True)
74 continue
75
76 out = last.get("result")
77 n = 0
78 if isinstance(out, list) and out and isinstance(out[0], dict):
79 n = out[0].get("count", 0) or 0
80 if n:
81 violations += 1
82 print(f"{tag:<5} IHLAL {n:>8} {desc[:60]}", flush=True)
83 else:
84 passed += 1
85 if dt > 5:
86 print(f"{tag:<5} ok ({dt:.0f}s)", flush=True)
87
88 print(f"\ngecen: {passed} | IHLAL: {violations} | hata: {errors} | "
89 f"zaman asimi: {slow} | sure: {time.time() - t_all:.0f}s")
90 ok = violations == 0 and errors == 0
91 print("SONUC: " + ("TEMIZ" if ok and slow == 0 else
92 "TEMIZ (bazi sorgular zaman asimina ugradi)" if ok else
93 "SORUN VAR"))
94 return 0 if ok else 1
95
96
97if __name__ == "__main__":
98 sys.exit(main())