Spaces:
Sleeping
Sleeping
Create export.py
Browse files- core/export.py +55 -0
core/export.py
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import io
|
| 2 |
+
import json
|
| 3 |
+
import zipfile
|
| 4 |
+
import pandas as pd
|
| 5 |
+
from typing import Tuple
|
| 6 |
+
from .sim import World
|
| 7 |
+
from .kpi import compute_kpis
|
| 8 |
+
|
| 9 |
+
def export_run(w: World) -> Tuple[bytes, str]:
|
| 10 |
+
"""
|
| 11 |
+
Returns (zip_bytes, filename)
|
| 12 |
+
Contents:
|
| 13 |
+
- events.jsonl
|
| 14 |
+
- metrics.csv
|
| 15 |
+
- run_summary.json
|
| 16 |
+
"""
|
| 17 |
+
# events
|
| 18 |
+
events_jsonl = w.events.to_jsonl().encode("utf-8")
|
| 19 |
+
|
| 20 |
+
# metrics (single-row snapshot; you can also store per-tick KPI series later)
|
| 21 |
+
kpi = compute_kpis(w)
|
| 22 |
+
df = pd.DataFrame([kpi])
|
| 23 |
+
metrics_csv = df.to_csv(index=False).encode("utf-8")
|
| 24 |
+
|
| 25 |
+
# summary
|
| 26 |
+
summary = {
|
| 27 |
+
"run_id": w.run_id,
|
| 28 |
+
"seed": w.seed,
|
| 29 |
+
"tick": w.step,
|
| 30 |
+
"minutes_per_tick": w.config.minutes_per_tick,
|
| 31 |
+
"ledger": {
|
| 32 |
+
"spend_usd": w.ledger.spend_usd,
|
| 33 |
+
"llm_calls": w.ledger.llm_calls,
|
| 34 |
+
"tool_calls": w.ledger.tool_calls,
|
| 35 |
+
"tokens_prompt": w.ledger.tokens_prompt,
|
| 36 |
+
"tokens_completion": w.ledger.tokens_completion,
|
| 37 |
+
},
|
| 38 |
+
"kpi": kpi,
|
| 39 |
+
"alerts": w.ledger.alerts[-10:],
|
| 40 |
+
"tasks_total": len(w.tasks.tasks),
|
| 41 |
+
"tasks_done": len(w.tasks.done_tasks()),
|
| 42 |
+
"tasks_overdue": len(w.tasks.overdue_tasks(w.step)),
|
| 43 |
+
}
|
| 44 |
+
summary_json = json.dumps(summary, indent=2).encode("utf-8")
|
| 45 |
+
|
| 46 |
+
# zip
|
| 47 |
+
buf = io.BytesIO()
|
| 48 |
+
with zipfile.ZipFile(buf, mode="w", compression=zipfile.ZIP_DEFLATED) as z:
|
| 49 |
+
z.writestr("events.jsonl", events_jsonl)
|
| 50 |
+
z.writestr("metrics.csv", metrics_csv)
|
| 51 |
+
z.writestr("run_summary.json", summary_json)
|
| 52 |
+
|
| 53 |
+
buf.seek(0)
|
| 54 |
+
fname = f"zen_orchestrator_export__{w.run_id}.zip"
|
| 55 |
+
return buf.read(), fname
|