credit-scoring-api-v2 / monitoring /export_prod_data.py
perachon's picture
fix & améliorations
0ad7b2b
raw
history blame contribute delete
972 Bytes
import json
import pandas as pd
from pathlib import Path
import os
LOG_FILE = Path("logs/api_requests.jsonl")
OUT_FILE = Path("monitoring/prod_data.csv")
MODE = os.getenv("PROD_MODE", "recent_half") # all | recent_half
records = []
with open(LOG_FILE, "r", encoding="utf-8") as f:
for line in f:
record = json.loads(line)
if record.get("event") == "prediction":
records.append({
"probability_default": record["output"]["probability_default"]
})
df = pd.DataFrame(records)
if df.empty:
raise ValueError("Aucune donnée de production trouvée (event=prediction dans les logs).")
# Par défaut: on prend la moitié la plus récente comme 'current' pour le drift
mode = MODE
if mode == "recent_half" and len(df) > 1:
split_index = max(1, len(df) // 2)
df = df.iloc[split_index:].reset_index(drop=True)
df.to_csv(OUT_FILE, index=False)
print(f"✅ Données de production exportées : {OUT_FILE}")