File size: 989 Bytes
cffa613
 
 
 
 
 
 
 
 
 
 
9541ba6
 
 
cffa613
 
 
 
 
 
9541ba6
 
 
458c5ca
 
cffa613
 
 
 
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
import json
import os

METRICS_FILE = "metrics.jsonl"
_current_step = 0

def get_next_step():
    global _current_step
    _current_step += 1
    return _current_step

def append_metric(reward: float, recall: float, fpr: float, 
                  baseline_reward: float = 0.0, baseline_recall: float = 0.0, baseline_fpr: float = 0.0,
                  graph_json: str = None, recent_traffic: list = None):
    step = get_next_step()
    payload = {
        "step": step,
        "reward": float(reward),
        "recall": float(recall),
        "fpr": float(fpr),
        "baseline_reward": float(baseline_reward),
        "baseline_recall": float(baseline_recall),
        "baseline_fpr": float(baseline_fpr),
        "ast_json": graph_json,
        "recent_traffic": recent_traffic or []
    }
    with open(METRICS_FILE, "a", encoding="utf-8") as f:
        f.write(json.dumps(payload) + "\n")
        f.flush() # Vital: ensure data is pushed immediately to disk so UI receives it live