From 7439a1a10140df70e018e56aa383f0837269522a Mon Sep 17 00:00:00 2001 From: Experiments DB Dev Date: Thu, 23 Apr 2026 12:21:52 -0400 Subject: [PATCH] fix: day modal bar chart shows experiment-wide session stats per subject Shows Total / Success / Failure bars across ALL sessions for each animal that has at least one valid (non-empty) value for the chosen field. Animals with zero valid field entries are excluded from the chart. Co-Authored-By: Claude Sonnet 4.6 --- .../src/components/ExperimentCalendar.jsx | 38 +++++++++++-------- frontend/tests/ExperimentCalendar.test.jsx | 15 +++++--- 2 files changed, 32 insertions(+), 21 deletions(-) diff --git a/frontend/src/components/ExperimentCalendar.jsx b/frontend/src/components/ExperimentCalendar.jsx index 608acb7..8352e35 100644 --- a/frontend/src/components/ExperimentCalendar.jsx +++ b/frontend/src/components/ExperimentCalendar.jsx @@ -53,25 +53,29 @@ function DayPanel({ date, animals, allStatuses, template, experimentId, selected const fieldLabel = fieldOptions.find((f) => f.value === selectedField)?.label; - // Bar chart: this day's field value per subject (numeric fields only) + // Bar chart: session stats across ALL days, for animals with at least one valid field value const barData = useMemo(() => { if (!selectedField) return []; const isBuiltin = BUILTIN_KEYS_SET.has(selectedField); - return animalsWithData + return animals .map((animal) => { - const status = statusByAnimal[animal.id]; - if (!status) return null; - const raw = isBuiltin ? status[selectedField] : status.custom_fields?.[selectedField]; - if (raw === null || raw === undefined || String(raw).trim() === '') return null; - const num = parseFloat(raw); - if (isNaN(num)) return null; + const statuses = allStatuses.filter((s) => s.animal_id === animal.id); + const total = statuses.length; + if (total === 0) return null; + const success = statuses.filter((s) => { + const val = isBuiltin ? s[selectedField] : s.custom_fields?.[selectedField]; + return val !== null && val !== undefined && String(val).trim() !== ''; + }).length; + if (success === 0) return null; // skip animals with no valid field data return { name: animal.animal_name || animal.animal_id_string || animal.id.slice(0, 8), - value: num, + total, + success, + failure: total - success, }; }) .filter(Boolean); - }, [animalsWithData, statusByAnimal, selectedField]); + }, [animals, allStatuses, selectedField]); return (
@@ -120,16 +124,20 @@ function DayPanel({ date, animals, allStatuses, template, experimentId, selected {/* Bar chart: overall per-subject completeness */} {barData.length > 0 && (
-

- {fieldLabel ?? 'Field'} by subject +

+ Session statistics — {fieldLabel ?? 'selected field'}

+

All sessions across the experiment

- - [v, fieldLabel ?? 'Value']} /> - + + + + + +
diff --git a/frontend/tests/ExperimentCalendar.test.jsx b/frontend/tests/ExperimentCalendar.test.jsx index b7c462f..a17dfb9 100644 --- a/frontend/tests/ExperimentCalendar.test.jsx +++ b/frontend/tests/ExperimentCalendar.test.jsx @@ -248,7 +248,7 @@ describe('day click modal', () => { // ── Bar chart (inside day modal) ────────────────────────────────────────────── describe('bar chart inside day modal', () => { - it('renders bar chart for numeric field values on that day', async () => { + it('renders session stats bar chart when animals have valid field data', async () => { const dateStr = todayStr(); const statuses = [ makeStatus('a1000000-0000-0000-0000-000000000001', dateStr, { 'ww000000-0000-0000-0000-000000000099': '325' }), @@ -263,16 +263,19 @@ describe('bar chart inside day modal', () => { }); }); - it('does not render bar chart when selected field is non-numeric', async () => { + it('excludes animals with no valid field data from bar chart', async () => { const dateStr = todayStr(); - // vitals is a text field — not numeric, so no bar chart - const statuses = [makeStatus('a1000000-0000-0000-0000-000000000001', dateStr, {}, 'HR 72')]; + // Rat A has weight data; Rat B has only empty custom_fields — should be excluded from chart + const statuses = [ + makeStatus('a1000000-0000-0000-0000-000000000001', dateStr, { 'ww000000-0000-0000-0000-000000000099': '325' }), + makeStatus('a2000000-0000-0000-0000-000000000002', dateStr, {}), + ]; render( , ); - fireEvent.change(screen.getByTestId('field-select'), { target: { value: 'vitals' } }); fireEvent.click(screen.getByTestId(`day-${dateStr}`)); await waitFor(() => screen.getByRole('dialog')); - expect(screen.queryByTestId('bar-chart')).not.toBeInTheDocument(); + // Both animals are in the day view (both have records), but bar chart only has Rat A + expect(screen.getByTestId('bar-chart')).toBeInTheDocument(); }); });