File size: 2,989 Bytes
eef4eed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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, RULESET_ID


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 public_report(result: dict[str, Any]) -> dict[str, Any]:
    kpis = result["kpis"]
    return {
        "product": PRODUCT,
        "code_version": CODE_VERSION,
        "ruleset": RULESET_ID,
        "run_id": result["run_id"],
        "scenario_id": result.get("scenario_id"),
        "input_hash": result.get("input_hash"),
        "verdict": result["verdict"].value if hasattr(result["verdict"], "value") else result["verdict"],
        "kpis": kpis.model_dump() if hasattr(kpis, "model_dump") else kpis,
        "comparison": result.get("comparison") or [],
        "finding_count": len(result.get("finding_rows") or []),
        "engine_profile": result.get("engine_profile"),
        "policy_action": result.get("policy_action"),
        "limits": (
            "Detector-scope redaction only. This report does not claim that every sensitive "
            "value was removed. Scanned PDFs and production identity data are out of scope."
        ),
    }


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 = {
        "documents": folder / f"{run_id}_documents.csv",
        "findings": folder / f"{run_id}_findings.csv",
        "spans": folder / f"{run_id}_spans.csv",
        "comparison": folder / f"{run_id}_comparison.csv",
        "report": folder / f"{run_id}_report.json",
        "redacted": folder / f"{run_id}_redacted.txt",
    }
    pd.DataFrame(result.get("doc_rows") or []).to_csv(files["documents"], index=False)
    pd.DataFrame(result.get("finding_rows") or []).to_csv(files["findings"], index=False)
    pd.DataFrame(result.get("span_rows") or []).to_csv(files["spans"], index=False)
    pd.DataFrame(result.get("comparison") or []).to_csv(files["comparison"], index=False)
    files["report"].write_text(
        json.dumps(public_report(result), indent=2, ensure_ascii=False, default=str),
        encoding="utf-8",
        newline="\n",
    )
    files["redacted"].write_text(result.get("redacted_text") or "", encoding="utf-8", newline="\n")
    exports = result.get("exports") or {}
    for doc_id, payload in exports.items():
        if payload.get("pdf"):
            pdf_path = folder / f"{run_id}_{doc_id}.pdf"
            pdf_path.write_bytes(payload["pdf"])
            files[f"pdf_{doc_id}"] = pdf_path
        text_path = folder / f"{run_id}_{doc_id}.txt"
        text_path.write_text(payload.get("text") or "", encoding="utf-8", newline="\n")
        files[f"text_{doc_id}"] = text_path
    return {key: str(path) for key, path in files.items()}