|
| 1 | +import json |
| 2 | +import uuid |
| 3 | +from typing import Any |
| 4 | + |
| 5 | +from sqlalchemy.engine.row import Row |
| 6 | + |
| 7 | +from policyengine_api.data import database |
| 8 | + |
| 9 | + |
| 10 | +REPORT_RUN_VERSION_FIELDS = ( |
| 11 | + "country_package_version", |
| 12 | + "policyengine_version", |
| 13 | + "data_version", |
| 14 | + "runtime_app_name", |
| 15 | + "report_cache_version", |
| 16 | + "simulation_cache_version", |
| 17 | + "requested_version_override", |
| 18 | + "resolved_dataset", |
| 19 | + "resolved_options_hash", |
| 20 | +) |
| 21 | + |
| 22 | + |
| 23 | +class ReportRunService: |
| 24 | + def _serialize_json( |
| 25 | + self, value: dict[str, Any] | list[Any] | str | None |
| 26 | + ) -> str | None: |
| 27 | + if value is None or isinstance(value, str): |
| 28 | + return value |
| 29 | + return json.dumps(value) |
| 30 | + |
| 31 | + def _parse_run_row(self, row: Row | dict | None) -> dict | None: |
| 32 | + if row is None: |
| 33 | + return None |
| 34 | + |
| 35 | + run = dict(row) |
| 36 | + if isinstance(run.get("report_spec_snapshot_json"), str): |
| 37 | + run["report_spec_snapshot_json"] = json.loads( |
| 38 | + run["report_spec_snapshot_json"] |
| 39 | + ) |
| 40 | + return run |
| 41 | + |
| 42 | + def create_report_output_run( |
| 43 | + self, |
| 44 | + report_output_id: int, |
| 45 | + status: str = "pending", |
| 46 | + trigger_type: str = "initial", |
| 47 | + output: dict[str, Any] | list[Any] | str | None = None, |
| 48 | + error_message: str | None = None, |
| 49 | + source_run_id: str | None = None, |
| 50 | + report_spec_snapshot: dict[str, Any] | str | None = None, |
| 51 | + version_manifest: dict[str, str | None] | None = None, |
| 52 | + run_id: str | None = None, |
| 53 | + ) -> dict: |
| 54 | + run_id = run_id or str(uuid.uuid4()) |
| 55 | + version_manifest = version_manifest or {} |
| 56 | + lock_clause = "" if database.local else " FOR UPDATE" |
| 57 | + |
| 58 | + def create_run_transaction(tx) -> None: |
| 59 | + parent_row: Row | None = tx.query( |
| 60 | + f"SELECT id FROM report_outputs WHERE id = ?{lock_clause}", |
| 61 | + (report_output_id,), |
| 62 | + ).fetchone() |
| 63 | + if parent_row is None: |
| 64 | + raise ValueError(f"Report output #{report_output_id} not found") |
| 65 | + |
| 66 | + run_sequence_row: Row | None = tx.query( |
| 67 | + """ |
| 68 | + SELECT COALESCE(MAX(run_sequence), 0) AS max_run_sequence |
| 69 | + FROM report_output_runs |
| 70 | + WHERE report_output_id = ? |
| 71 | + """, |
| 72 | + (report_output_id,), |
| 73 | + ).fetchone() |
| 74 | + run_sequence = ( |
| 75 | + int(run_sequence_row["max_run_sequence"]) + 1 |
| 76 | + if run_sequence_row is not None |
| 77 | + else 1 |
| 78 | + ) |
| 79 | + |
| 80 | + tx.query( |
| 81 | + f""" |
| 82 | + INSERT INTO report_output_runs ( |
| 83 | + id, report_output_id, run_sequence, status, output, error_message, |
| 84 | + trigger_type, requested_at, started_at, finished_at, source_run_id, |
| 85 | + report_spec_snapshot_json, {", ".join(REPORT_RUN_VERSION_FIELDS)} |
| 86 | + ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) |
| 87 | + """, |
| 88 | + ( |
| 89 | + run_id, |
| 90 | + report_output_id, |
| 91 | + run_sequence, |
| 92 | + status, |
| 93 | + self._serialize_json(output), |
| 94 | + error_message, |
| 95 | + trigger_type, |
| 96 | + None, |
| 97 | + None, |
| 98 | + None, |
| 99 | + source_run_id, |
| 100 | + self._serialize_json(report_spec_snapshot), |
| 101 | + *[ |
| 102 | + version_manifest.get(field) |
| 103 | + for field in REPORT_RUN_VERSION_FIELDS |
| 104 | + ], |
| 105 | + ), |
| 106 | + ) |
| 107 | + |
| 108 | + database.transaction(create_run_transaction) |
| 109 | + return self.get_report_output_run(run_id) |
| 110 | + |
| 111 | + def get_report_output_run(self, run_id: str) -> dict | None: |
| 112 | + row: Row | None = database.query( |
| 113 | + "SELECT * FROM report_output_runs WHERE id = ?", |
| 114 | + (run_id,), |
| 115 | + ).fetchone() |
| 116 | + return self._parse_run_row(row) |
| 117 | + |
| 118 | + def list_report_output_runs(self, report_output_id: int) -> list[dict]: |
| 119 | + rows = database.query( |
| 120 | + """ |
| 121 | + SELECT * FROM report_output_runs |
| 122 | + WHERE report_output_id = ? |
| 123 | + ORDER BY run_sequence ASC |
| 124 | + """, |
| 125 | + (report_output_id,), |
| 126 | + ).fetchall() |
| 127 | + return [self._parse_run_row(row) for row in rows] |
| 128 | + |
| 129 | + def get_newest_report_output_run(self, report_output_id: int) -> dict | None: |
| 130 | + row: Row | None = database.query( |
| 131 | + """ |
| 132 | + SELECT * FROM report_output_runs |
| 133 | + WHERE report_output_id = ? |
| 134 | + ORDER BY run_sequence DESC |
| 135 | + LIMIT 1 |
| 136 | + """, |
| 137 | + (report_output_id,), |
| 138 | + ).fetchone() |
| 139 | + return self._parse_run_row(row) |
| 140 | + |
| 141 | + def select_display_run(self, report_output: dict) -> dict | None: |
| 142 | + if report_output.get("active_run_id"): |
| 143 | + active_run = self.get_report_output_run(report_output["active_run_id"]) |
| 144 | + if active_run is not None: |
| 145 | + return active_run |
| 146 | + if report_output.get("latest_successful_run_id"): |
| 147 | + latest_successful_run = self.get_report_output_run( |
| 148 | + report_output["latest_successful_run_id"] |
| 149 | + ) |
| 150 | + if latest_successful_run is not None: |
| 151 | + return latest_successful_run |
| 152 | + return self.get_newest_report_output_run(report_output["id"]) |
0 commit comments