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 json
|
||||||
import os
|
import os
|
||||||
import sqlite3
|
import sqlite3
|
||||||
|
from dataclasses import asdict
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import Any
|
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:
|
class CalculationStore:
|
||||||
"""
|
"""
|
||||||
@@ -148,6 +175,7 @@ class CalculationStore:
|
|||||||
for row in rows:
|
for row in rows:
|
||||||
input_data = row[4] if isinstance(row[4], dict) else json.loads(row[4])
|
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 = 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({
|
records.append({
|
||||||
"id": row[0], "actor_id": row[1], "source": row[2], "mode": row[3],
|
"id": row[0], "actor_id": row[1], "source": row[2], "mode": row[3],
|
||||||
"input": input_data, "result": result_data, "message": row[6],
|
"input": input_data, "result": result_data, "message": row[6],
|
||||||
|
|||||||
@@ -50,6 +50,24 @@ class CalculationHistoryTests(unittest.TestCase):
|
|||||||
rows = store.list_calculations(sort_by="kitten_id", descending=False)
|
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"])
|
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__":
|
if __name__ == "__main__":
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|||||||
Reference in new issue
Block a user