Danielfonseca1212 commited on
Commit
b6c3d66
·
verified ·
1 Parent(s): 65cc6aa

Create Leaderboard.py

Browse files
Files changed (1) hide show
  1. Leaderboard.py +38 -0
Leaderboard.py ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Leaderboard — persiste ataques detectados em JSON local"""
2
+
3
+ import json
4
+ import os
5
+ from datetime import datetime
6
+
7
+ LEADERBOARD_FILE = "leaderboard.json"
8
+
9
+
10
+ def load_leaderboard() -> list:
11
+ if not os.path.exists(LEADERBOARD_FILE):
12
+ return []
13
+ try:
14
+ with open(LEADERBOARD_FILE, "r") as f:
15
+ return json.load(f)
16
+ except Exception:
17
+ return []
18
+
19
+
20
+ def save_attack(result) -> None:
21
+ data = load_leaderboard()
22
+ data.append({
23
+ "timestamp": datetime.utcnow().isoformat(),
24
+ "threat_level": result.threat_level.value,
25
+ "risk_score": result.risk_score,
26
+ "threats": result.threats_found,
27
+ "blocked_reason": result.blocked_reason or "",
28
+ "trace_id": result.trace_id,
29
+ "chars": result.char_count_original,
30
+ "processing_ms": result.processing_ms,
31
+ })
32
+ # manter apenas os últimos 500 registros
33
+ data = data[-500:]
34
+ try:
35
+ with open(LEADERBOARD_FILE, "w") as f:
36
+ json.dump(data, f)
37
+ except Exception:
38
+ pass