fix(admin): backfill legacy inspection scores
This commit is contained in:
1 parent
6235368baf
commit
a0e4a47522
2 files changed
+46
No files matched your search
@@ -9,9 +9,36 @@ from __future__ import annotations
|
||||
import json
|
||||
import os
|
||||
import sqlite3
|
||||
from dataclasses import asdict
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
from .domain import InspectionReport, calculate_inspection
|
||||
|
||||
|
||||
def _restore_inspection_breakdown(input_data: dict[str, Any], result_data: Any) -> Any:
|
||||
"""Backfill score details for inspection rows written before the breakdown existed."""
|
||||
if input_data.get("mode") != "inspection" or not isinstance(result_data, dict):
|
||||
return result_data
|
||||
if "inspection_points" in result_data and "resource_points" in result_data:
|
||||
return result_data
|
||||
try:
|
||||
resources = tuple(
|
||||
(str(item[0]), int(item[1]))
|
||||
for item in input_data.get("resources", [])
|
||||
if isinstance(item, (list, tuple)) and len(item) == 2
|
||||
)
|
||||
report = InspectionReport(
|
||||
character_name=str(input_data.get("character_name", "Персонаж")),
|
||||
character_id=int(input_data.get("character_id", 1)),
|
||||
place=str(input_data.get("place", "дупло")),
|
||||
inspections=int(input_data.get("inspections", 0)),
|
||||
resources=resources,
|
||||
)
|
||||
return asdict(calculate_inspection(report))
|
||||
except (TypeError, ValueError):
|
||||
return result_data
|
||||
|
||||
|
||||
class CalculationStore:
|
||||
"""
|
||||
@@ -148,6 +175,7 @@ class CalculationStore:
|
||||
for row in rows:
|
||||
input_data = row[4] if isinstance(row[4], dict) else json.loads(row[4])
|
||||
result_data = row[5] if isinstance(row[5], dict) or row[5] is None else json.loads(row[5])
|
||||
result_data = _restore_inspection_breakdown(input_data, result_data)
|
||||
records.append({
|
||||
"id": row[0], "actor_id": row[1], "source": row[2], "mode": row[3],
|
||||
"input": input_data, "result": result_data, "message": row[6],
|
||||
|
||||
Reference in new issue
Block a user