feat: add nutrition_history_viz widget and enhance configuration handling
- Introduced the `nutrition_history_viz` widget to the dashboard, allowing users to visualize nutrition history data. - Updated widget configuration to include `nutrition_history_viz` in the allowed widgets and added validation for its configuration. - Enhanced the widget catalog with details for the new `nutrition_history_viz` entry. - Implemented default values and validation logic for the widget's configuration, ensuring proper handling of user inputs. - Added tests to ensure proper validation of the `nutrition_history_viz` widget configuration. - Bumped application version to reflect the addition of the new widget.
This commit is contained in:
1 parent
20f195aca1
commit
db5557e4aa
12 files changed
+993
-509
No files matched your search
@@ -15,6 +15,7 @@ MAX_WIDGET_CONFIG_JSON_BYTES = 3072
|
||||
WIDGETS_ALLOWING_CONFIG: frozenset[str] = frozenset({
|
||||
"body_overview",
|
||||
"body_history_viz",
|
||||
"nutrition_history_viz",
|
||||
"activity_overview",
|
||||
"kpi_board",
|
||||
"quick_capture",
|
||||
@@ -59,6 +60,34 @@ _BODY_HISTORY_VIZ_DEFAULTS: dict[str, Any] = {
|
||||
"show_circumference_lines_chart": False,
|
||||
}
|
||||
|
||||
_NUTRITION_HISTORY_VIZ_BOOL_KEYS: frozenset[str] = frozenset({
|
||||
"show_goals_strip",
|
||||
"show_intro_blurb",
|
||||
"show_kpis",
|
||||
"show_kcal_vs_weight",
|
||||
"show_calorie_balance_chart",
|
||||
"show_protein_lean_chart",
|
||||
"show_heuristics",
|
||||
"show_macro_daily_bars",
|
||||
"show_macro_distribution_pair",
|
||||
"show_energy_protein_charts",
|
||||
})
|
||||
|
||||
_NUTRITION_HISTORY_VIZ_DEFAULTS: dict[str, Any] = {
|
||||
"chart_days": 30,
|
||||
"show_goals_strip": False,
|
||||
"show_intro_blurb": False,
|
||||
"show_kpis": True,
|
||||
"kpi_detail": "compact",
|
||||
"show_kcal_vs_weight": True,
|
||||
"show_calorie_balance_chart": False,
|
||||
"show_protein_lean_chart": False,
|
||||
"show_heuristics": False,
|
||||
"show_macro_daily_bars": True,
|
||||
"show_macro_distribution_pair": True,
|
||||
"show_energy_protein_charts": False,
|
||||
}
|
||||
|
||||
|
||||
def _config_json_size_bytes(config: dict[str, Any]) -> int:
|
||||
return len(json.dumps(config, sort_keys=True, ensure_ascii=False).encode("utf-8"))
|
||||
@@ -80,12 +109,16 @@ def validate_widget_entry_config(widget_id: str, raw: Any) -> dict[str, Any]:
|
||||
if not raw:
|
||||
if widget_id == "body_history_viz":
|
||||
return _validate_body_history_viz_config({})
|
||||
if widget_id == "nutrition_history_viz":
|
||||
return _validate_nutrition_history_viz_config({})
|
||||
return {}
|
||||
|
||||
if widget_id == "body_overview":
|
||||
return _validate_chart_days_only(raw, label="body_overview")
|
||||
if widget_id == "body_history_viz":
|
||||
return _validate_body_history_viz_config(raw)
|
||||
if widget_id == "nutrition_history_viz":
|
||||
return _validate_nutrition_history_viz_config(raw)
|
||||
if widget_id == "activity_overview":
|
||||
return _validate_chart_days_only(raw, label="activity_overview")
|
||||
if widget_id == "kpi_board":
|
||||
@@ -222,6 +255,46 @@ def _validate_body_history_viz_config(raw: dict[str, Any]) -> dict[str, Any]:
|
||||
return out
|
||||
|
||||
|
||||
def _validate_nutrition_history_viz_config(raw: dict[str, Any]) -> dict[str, Any]:
|
||||
label = "nutrition_history_viz"
|
||||
allowed = _NUTRITION_HISTORY_VIZ_BOOL_KEYS | frozenset({"chart_days", "kpi_detail"})
|
||||
unknown = set(raw) - allowed
|
||||
if unknown:
|
||||
raise ValueError(f"{label}: unbekannte config-Felder: {sorted(unknown)}")
|
||||
out: dict[str, Any] = dict(_NUTRITION_HISTORY_VIZ_DEFAULTS)
|
||||
for k in _NUTRITION_HISTORY_VIZ_BOOL_KEYS:
|
||||
if k not in raw:
|
||||
continue
|
||||
v = raw[k]
|
||||
if not isinstance(v, bool):
|
||||
raise ValueError(f"{label}: {k} muss boolean sein")
|
||||
out[k] = v
|
||||
if "kpi_detail" in raw:
|
||||
kd = raw["kpi_detail"]
|
||||
if kd not in ("compact", "full"):
|
||||
raise ValueError(f"{label}: kpi_detail muss 'compact' oder 'full' sein")
|
||||
out["kpi_detail"] = kd
|
||||
if "chart_days" in raw:
|
||||
v = _parse_chart_days(raw["chart_days"], label)
|
||||
if v < 7 or v > 90:
|
||||
raise ValueError(f"{label}: chart_days muss zwischen 7 und 90 liegen")
|
||||
out["chart_days"] = v
|
||||
if not out["show_kpis"] and not any(
|
||||
out[k]
|
||||
for k in (
|
||||
"show_kcal_vs_weight",
|
||||
"show_calorie_balance_chart",
|
||||
"show_protein_lean_chart",
|
||||
"show_heuristics",
|
||||
"show_macro_daily_bars",
|
||||
"show_macro_distribution_pair",
|
||||
"show_energy_protein_charts",
|
||||
)
|
||||
):
|
||||
raise ValueError(f"{label}: mindestens KPIs oder ein Chart-Bereich muss sichtbar sein")
|
||||
return out
|
||||
|
||||
|
||||
def _validate_chart_days_only(raw: dict[str, Any], *, label: str) -> dict[str, Any]:
|
||||
allowed = frozenset({"chart_days"})
|
||||
unknown = set(raw) - allowed
|
||||
|
||||
@@ -44,6 +44,37 @@ def test_body_history_viz_unknown_key():
|
||||
validate_widget_entry_config("body_history_viz", {"evil": True})
|
||||
|
||||
|
||||
def test_nutrition_history_viz_empty_expands_defaults():
|
||||
d = validate_widget_entry_config("nutrition_history_viz", {})
|
||||
assert d["chart_days"] == 30
|
||||
assert d["show_kpis"] is True
|
||||
assert d["show_kcal_vs_weight"] is True
|
||||
assert d["kpi_detail"] == "compact"
|
||||
assert d["show_calorie_balance_chart"] is False
|
||||
assert d["show_energy_protein_charts"] is False
|
||||
|
||||
|
||||
def test_nutrition_history_viz_chart_days_and_merge():
|
||||
d = validate_widget_entry_config("nutrition_history_viz", {"chart_days": 45})
|
||||
assert d["chart_days"] == 45
|
||||
assert d["show_goals_strip"] is False
|
||||
with pytest.raises(ValueError):
|
||||
validate_widget_entry_config("nutrition_history_viz", {"chart_days": 5})
|
||||
|
||||
|
||||
def test_nutrition_history_viz_requires_visible_block():
|
||||
with pytest.raises(ValueError):
|
||||
validate_widget_entry_config(
|
||||
"nutrition_history_viz",
|
||||
{"show_kpis": False, "show_kcal_vs_weight": False, "show_macro_daily_bars": False, "show_macro_distribution_pair": False},
|
||||
)
|
||||
|
||||
|
||||
def test_nutrition_history_viz_unknown_key():
|
||||
with pytest.raises(ValueError):
|
||||
validate_widget_entry_config("nutrition_history_viz", {"evil": True})
|
||||
|
||||
|
||||
def test_welcome_config_rejected_unknown_key():
|
||||
with pytest.raises(ValueError):
|
||||
validate_widget_entry_config("welcome", {"x": 1})
|
||||
|
||||
+1
-1
@@ -30,7 +30,7 @@ MODULE_VERSIONS = {
|
||||
"importdata": "1.0.0",
|
||||
"membership": "2.1.0",
|
||||
"workflow": "0.7.0", # Part 3: Inline Prompts (reference + inline mode)
|
||||
"app_dashboard": "1.13.0", # body_history_viz: Sichtbarkeits-Config + Defaults schlank
|
||||
"app_dashboard": "1.14.0", # nutrition_history_viz: Verlauf-Bundle-Widget + Config wie Körper
|
||||
"csv_import": "0.3.2", # Import-Fehler: enrich_row_error / freundlichere 500-Hinweise
|
||||
"admin_csv_templates": "0.3.0", # POST /validate + Speichern nur bei valid (422 + warnings in Response)
|
||||
}
|
||||
|
||||
@@ -100,6 +100,12 @@ WIDGET_CATALOG: list[WidgetCatalogEntry] = [
|
||||
"description": "Phase-0c NutritionCharts (optional chart_days 7–90, Default 30); Feature nutrition_entries",
|
||||
"requires_feature": "nutrition_entries",
|
||||
},
|
||||
{
|
||||
"id": "nutrition_history_viz",
|
||||
"title": "Ernährung (Verlauf-Bundle)",
|
||||
"description": "Layer-2b nutrition-history-viz: schlanker Standard; Blöcke per show_* / kpi_detail; chart_days 7–90; Feature nutrition_entries",
|
||||
"requires_feature": "nutrition_entries",
|
||||
},
|
||||
{
|
||||
"id": "recovery_charts_panel",
|
||||
"title": "Erholung — Charts R1–R5",
|
||||
|
||||
Reference in new issue
Block a user