From a0e4a47522b694a55698971832dc5140a693ee28 Mon Sep 17 00:00:00 2001 From: Nyan404 Date: Mon, 21 Sep 2026 22:43:04 +0300 Subject: [PATCH] fix(admin): backfill legacy inspection scores --- app/belki/history.py | 28 ++++++++++++++++++++++++++++ tests/test_history.py | 18 ++++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/app/belki/history.py b/app/belki/history.py index 24eb7ff..56c4ef5 100644 --- a/app/belki/history.py +++ b/app/belki/history.py @@ -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], diff --git a/tests/test_history.py b/tests/test_history.py index 4106a41..da14d23 100644 --- a/tests/test_history.py +++ b/tests/test_history.py @@ -50,6 +50,24 @@ class CalculationHistoryTests(unittest.TestCase): rows = store.list_calculations(sort_by="kitten_id", descending=False) self.assertEqual([row["input"]["kitten_id"] for row in rows], [2, "12", "not-an-id"]) + def test_admin_history_backfills_legacy_inspection_breakdown(self) -> None: + with tempfile.TemporaryDirectory() as directory: + store = CalculationStore(sqlite_path=Path(directory) / "history.sqlite3") + store.save( + actor_id=99, + source="vk", + payload={ + "mode": "inspection", "character_name": "Белка", "character_id": 1, + "place": "дупло", "inspections": 17, + "resources": [["паутина", 1], ["мох", 1], ["мох", 2]], + }, + response={"mode": "inspection", "message": "старый отчёт", "result": {"points": 53}}, + ) + result = store.list_calculations()[0]["result"] + self.assertEqual(result["inspection_points"], 34) + self.assertEqual(result["points"], 50) + self.assertEqual(result["resource_points"][0]["points"], 7) + if __name__ == "__main__": unittest.main()