feat: Revamp KPI board configuration and validation
- Enhanced the KPI board widget to support tile configuration, allowing users to select and order tiles. - Updated validation logic to ensure proper handling of tile IDs and configuration fields. - Removed legacy chart_days support, transitioning to a fixed analysis window for KPI metrics. - Improved the DashboardLabPage to integrate the new KpiBoardConfigEditor for managing tile selections. - Bumped app_dashboard version to 1.5.0 to reflect these significant changes.
This commit is contained in:
1 parent
096d896166
commit
c0c512e942
10 files changed
+390
-91
No files matched your search
@@ -7,12 +7,16 @@ from __future__ import annotations
|
||||
|
||||
import json
|
||||
import math
|
||||
import re
|
||||
from typing import Any
|
||||
|
||||
MAX_WIDGET_CONFIG_JSON_BYTES = 1024
|
||||
MAX_WIDGET_CONFIG_JSON_BYTES = 3072
|
||||
|
||||
WIDGETS_ALLOWING_CONFIG: frozenset[str] = frozenset({"body_overview", "activity_overview", "kpi_board"})
|
||||
|
||||
_KPI_TILE_FIXED: frozenset[str] = frozenset({"body_fat", "avg_kcal"})
|
||||
_KPI_REF_TILE_RE = re.compile(r"^ref:[a-z0-9_]{1,64}$")
|
||||
|
||||
|
||||
def _config_json_size_bytes(config: dict[str, Any]) -> int:
|
||||
return len(json.dumps(config, sort_keys=True, ensure_ascii=False).encode("utf-8"))
|
||||
@@ -36,11 +40,59 @@ def validate_widget_entry_config(widget_id: str, raw: Any) -> dict[str, Any]:
|
||||
if widget_id == "activity_overview":
|
||||
return _validate_chart_days_only(raw, label="activity_overview")
|
||||
if widget_id == "kpi_board":
|
||||
return _validate_chart_days_only(raw, label="kpi_board")
|
||||
return _validate_kpi_board_config(raw)
|
||||
|
||||
raise ValueError(f"Widget {widget_id}: keine Konfiguration unterstützt")
|
||||
|
||||
|
||||
def _kpi_tile_id_valid(tid: str) -> bool:
|
||||
if tid in _KPI_TILE_FIXED:
|
||||
return True
|
||||
return bool(_KPI_REF_TILE_RE.match(tid))
|
||||
|
||||
|
||||
def _normalize_kpi_tile_entry(item: Any) -> str:
|
||||
if isinstance(item, str):
|
||||
tid = item.strip()
|
||||
elif isinstance(item, dict) and "id" in item:
|
||||
tid = str(item["id"]).strip()
|
||||
else:
|
||||
raise ValueError("kpi_board: jedes tiles-Element braucht eine id (String oder Objekt mit id)")
|
||||
if not tid:
|
||||
raise ValueError("kpi_board: leere Kachel-id")
|
||||
if not _kpi_tile_id_valid(tid):
|
||||
raise ValueError(f"kpi_board: ungültige Kachel-id {tid!r} (z. B. body_fat, avg_kcal, ref:typ_key)")
|
||||
return tid
|
||||
|
||||
|
||||
def _validate_kpi_board_config(raw: dict[str, Any]) -> dict[str, Any]:
|
||||
if not raw:
|
||||
return {}
|
||||
# Legacy nur chart_days → entfallen, automatische Kachelwahl
|
||||
if set(raw.keys()) <= frozenset({"chart_days"}):
|
||||
return {}
|
||||
allowed = frozenset({"tiles"})
|
||||
unknown = set(raw) - allowed
|
||||
if unknown:
|
||||
raise ValueError(f"kpi_board: unbekannte config-Felder: {sorted(unknown)}")
|
||||
tiles_raw = raw.get("tiles")
|
||||
if tiles_raw is None:
|
||||
return {}
|
||||
if not isinstance(tiles_raw, list):
|
||||
raise ValueError("kpi_board: tiles muss eine Liste sein")
|
||||
if len(tiles_raw) > 9:
|
||||
raise ValueError("kpi_board: maximal 9 Kacheln")
|
||||
seen: set[str] = set()
|
||||
out: list[dict[str, str]] = []
|
||||
for item in tiles_raw:
|
||||
tid = _normalize_kpi_tile_entry(item)
|
||||
if tid in seen:
|
||||
raise ValueError(f"kpi_board: doppelte Kachel-id {tid}")
|
||||
seen.add(tid)
|
||||
out.append({"id": tid})
|
||||
return {"tiles": out}
|
||||
|
||||
|
||||
def _parse_chart_days(v: Any, label: str) -> int:
|
||||
if isinstance(v, bool):
|
||||
raise ValueError(f"{label}: chart_days muss ganze Zahl sein")
|
||||
|
||||
@@ -30,11 +30,25 @@ def test_activity_chart_days():
|
||||
validate_widget_entry_config("activity_overview", {"chart_days": 5})
|
||||
|
||||
|
||||
def test_kpi_board_chart_days():
|
||||
def test_kpi_board_tiles():
|
||||
assert validate_widget_entry_config("kpi_board", {}) == {}
|
||||
assert validate_widget_entry_config("kpi_board", {"chart_days": 14}) == {"chart_days": 14}
|
||||
assert validate_widget_entry_config("kpi_board", {"tiles": []}) == {"tiles": []}
|
||||
assert validate_widget_entry_config(
|
||||
"kpi_board",
|
||||
{"tiles": [{"id": "body_fat"}, {"id": "avg_kcal"}, {"id": "ref:hr_max"}]},
|
||||
) == {"tiles": [{"id": "body_fat"}, {"id": "avg_kcal"}, {"id": "ref:hr_max"}]}
|
||||
with pytest.raises(ValueError):
|
||||
validate_widget_entry_config("kpi_board", {"chart_days": 5})
|
||||
validate_widget_entry_config("kpi_board", {"tiles": [{"id": "unknown"}]})
|
||||
with pytest.raises(ValueError):
|
||||
validate_widget_entry_config("kpi_board", {"tiles": [{"id": "body_fat"}, {"id": "body_fat"}]})
|
||||
with pytest.raises(ValueError):
|
||||
validate_widget_entry_config("kpi_board", {"extra": 1})
|
||||
|
||||
|
||||
def test_kpi_board_legacy_chart_days_dropped():
|
||||
"""Nur chart_days (Alt-Layouts) → automatische Kachelwahl, kein Ø-Kal-Fenster mehr."""
|
||||
assert validate_widget_entry_config("kpi_board", {"chart_days": 14}) == {}
|
||||
assert validate_widget_entry_config("kpi_board", {"chart_days": 5}) == {}
|
||||
|
||||
|
||||
def test_welcome_still_rejects_config():
|
||||
|
||||
+1
-1
@@ -30,7 +30,7 @@ MODULE_VERSIONS = {
|
||||
"importdata": "1.0.0",
|
||||
"membership": "2.1.0",
|
||||
"workflow": "0.6.0", # Phase 4: End Node Template Engine
|
||||
"app_dashboard": "1.4.0", # kpi_board.config.chart_days (Ø-Kalorien Fenster)
|
||||
"app_dashboard": "1.5.0", # kpi_board: Kachelwahl tiles statt chart_days
|
||||
}
|
||||
|
||||
CHANGELOG = [
|
||||
|
||||
@@ -30,7 +30,7 @@ WIDGET_CATALOG: list[WidgetCatalogEntry] = [
|
||||
{
|
||||
"id": "kpi_board",
|
||||
"title": "KPI-Kacheln",
|
||||
"description": "Referenzwerte, KF%, Ø-Kalorien (optional: chart_days 7–90 für Ernährungsfenster)",
|
||||
"description": "Referenzwerte, KF%, Ø-Kalorien — optional Kacheln & Reihenfolge (config.tiles, max. 9)",
|
||||
},
|
||||
{
|
||||
"id": "body_overview",
|
||||
|
||||
Reference in new issue
Block a user