Download demo/src/reporting.py from AriaAICompany/cloudtrail-lab: direct link, hf CLI and curl.
- Browser
- Download file 3.51 kB
-
https://huggingface.co/datasets/AriaAICompany/cloudtrail-lab/resolve/main/demo/src/reporting.py
- Command line
-
hf download hf://datasets/AriaAICompany/cloudtrail-lab/demo/src/reporting.py
-
curl -L -o reporting.py https://huggingface.co/datasets/AriaAICompany/cloudtrail-lab/resolve/main/demo/src/reporting.py
3.51 kB
| from __future__ import annotations | |
| import hashlib | |
| import json | |
| from pathlib import Path | |
| from typing import Any | |
| import pandas as pd | |
| from src.domain import CODE_VERSION, PRODUCT, SCHEMA_ID, SNAPSHOT_ID | |
| from src.schemas import ChangeFinding | |
| def input_hash(payload: dict[str, Any]) -> str: | |
| raw = json.dumps(payload, sort_keys=True, ensure_ascii=False, default=str) | |
| return hashlib.sha256(raw.encode("utf-8")).hexdigest()[:16] | |
| def findings_frame(rows: list[ChangeFinding]) -> pd.DataFrame: | |
| records = [] | |
| for item in rows: | |
| data = item.model_dump() | |
| data["observed"] = " | ".join(item.observed) | |
| data["inferred"] = " | ".join(item.inferred) | |
| data["gold_event_ids"] = ",".join(item.gold_event_ids) | |
| data["evidence_event_ids"] = ",".join(item.evidence_event_ids) | |
| data["methods_fired"] = ",".join(item.methods_fired) | |
| data["tag"] = item.tag.value | |
| data["alert_class"] = item.alert_class.value | |
| data["severity"] = item.severity.value | |
| data["confidence"] = item.confidence.value | |
| data["workflow"] = item.workflow.value | |
| data["approval_status"] = item.approval_status.value | |
| data["before_state"] = item.before_state.value | |
| data["after_state"] = item.after_state.value | |
| records.append(data) | |
| return pd.DataFrame(records) | |
| def report_payload(result: dict[str, Any]) -> dict[str, Any]: | |
| return { | |
| "product": PRODUCT, | |
| "code_version": CODE_VERSION, | |
| "schema_id": SCHEMA_ID, | |
| "snapshot_id": SNAPSHOT_ID, | |
| "run_id": result["run_id"], | |
| "verdict": result["verdict"].value if hasattr(result["verdict"], "value") else result["verdict"], | |
| "kpis": result["kpis"].model_dump() if hasattr(result["kpis"], "model_dump") else result["kpis"], | |
| "comparison": [row.model_dump() if hasattr(row, "model_dump") else row for row in result.get("comparison") or []], | |
| "calibration": [row.model_dump() if hasattr(row, "model_dump") else row for row in result.get("calibration") or []], | |
| "input_hash": result["run_id"].split("-")[-1], | |
| "live": True, | |
| "disclaimer": ( | |
| "A finding is a review ranking, not proof of current cloud exposure. " | |
| "Before/after is shown only when both events exist in this export. " | |
| "This demo never requests cloud credentials and never changes configuration. " | |
| "Severity is not approval status. Maintenance windows stay context." | |
| ), | |
| } | |
| def write_downloads(result: dict[str, Any], folder: Path) -> dict[str, str]: | |
| folder.mkdir(parents=True, exist_ok=True) | |
| run_id = result["run_id"] | |
| files = { | |
| "findings": folder / f"{run_id}_findings.csv", | |
| "comparison": folder / f"{run_id}_comparison.csv", | |
| "calibration": folder / f"{run_id}_calibration.csv", | |
| "report": folder / f"{run_id}_report.json", | |
| } | |
| findings_frame(result["findings"]).to_csv(files["findings"], index=False) | |
| pd.DataFrame([row.model_dump() if hasattr(row, "model_dump") else row for row in result.get("comparison") or []]).to_csv( | |
| files["comparison"], index=False | |
| ) | |
| pd.DataFrame([row.model_dump() if hasattr(row, "model_dump") else row for row in result.get("calibration") or []]).to_csv( | |
| files["calibration"], index=False | |
| ) | |
| files["report"].write_text(json.dumps(result["report"], indent=2, ensure_ascii=False, default=str), encoding="utf-8") | |
| return {key: str(path) for key, path in files.items()} | |