Insert or update a run record.
(run_id: str, data: dict)
| 127 | # ── Run CRUD ─────────────────────────────────────────────────────────────────── |
| 128 | |
| 129 | def upsert_run(run_id: str, data: dict) -> None: |
| 130 | """Insert or update a run record.""" |
| 131 | cols = [ |
| 132 | "id", "engagement_id", "operator", "target", "mode", |
| 133 | "started_at", "completed_at", "risk_score", "risk_color", |
| 134 | "total_hosts", "total_services", "total_cves", |
| 135 | "confirmed_exploitable", "already_exploited", |
| 136 | "remediations_generated", "remediations_applied", "verified_closed", |
| 137 | "errors", "report_path", |
| 138 | ] |
| 139 | vals = [ |
| 140 | run_id, |
| 141 | data.get("engagement_id", ""), |
| 142 | data.get("operator", ""), |
| 143 | data.get("target", ""), |
| 144 | data.get("mode", ""), |
| 145 | data.get("started_at", ""), |
| 146 | data.get("completed_at"), |
| 147 | data.get("risk_score", 0), |
| 148 | data.get("risk_color", "UNKNOWN"), |
| 149 | data.get("total_hosts", 0), |
| 150 | data.get("total_services", 0), |
| 151 | data.get("total_cves", 0), |
| 152 | data.get("confirmed_exploitable", 0), |
| 153 | data.get("already_exploited", 0), |
| 154 | data.get("remediations_generated", 0), |
| 155 | data.get("remediations_applied", 0), |
| 156 | data.get("verified_closed", 0), |
| 157 | json.dumps(data.get("errors", [])), |
| 158 | data.get("report_path"), |
| 159 | ] |
| 160 | placeholders = ", ".join(["?"] * len(cols)) |
| 161 | updates = ", ".join(f"{c}=excluded.{c}" for c in cols if c != "id") |
| 162 | sql = ( |
| 163 | f"INSERT INTO runs ({', '.join(cols)}) VALUES ({placeholders}) " |
| 164 | f"ON CONFLICT(id) DO UPDATE SET {updates}" |
| 165 | ) |
| 166 | with get_db() as conn: |
| 167 | conn.execute(sql, vals) |
| 168 | |
| 169 | |
| 170 | def get_run(run_id: str) -> dict | None: |
no test coverage detected