refactor(export): remove dead assignable-axes helpers and their tests
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
// Pure helpers for exporting daily-parameter data as a CSV matrix. buildMatrix is
|
||||
// dimension-agnostic: rows and columns are assignable to Date/Subject/Parameter,
|
||||
// with the leftover dimension pinned to a single value.
|
||||
// Pure helpers for exporting daily-parameter data as a CSV matrix. The export is a
|
||||
// subject-series matrix: subjects are always columns, the chosen X coordinate
|
||||
// (Date / # Days Reach / a daily field) provides the rows, and one Data
|
||||
// parameter supplies the cell values.
|
||||
// No React, no I/O — mirrors the crossSubjectChart.js pure-helper pattern.
|
||||
|
||||
// Read a single parameter's raw value from one daily status.
|
||||
@@ -156,20 +157,6 @@ export function buildSubjectSeriesMatrix({ xField, dataParam, groupField }, { st
|
||||
};
|
||||
}
|
||||
|
||||
export const EXPORT_DIMENSIONS = [
|
||||
{ dim: 'date', label: 'Date' },
|
||||
{ dim: 'subject', label: 'Subject' },
|
||||
{ dim: 'parameter', label: 'Parameter' },
|
||||
];
|
||||
|
||||
export function dimLabel(dim) {
|
||||
return EXPORT_DIMENSIONS.find((d) => d.dim === dim)?.label ?? '';
|
||||
}
|
||||
|
||||
export function otherDimension(rowDim, colDim) {
|
||||
return EXPORT_DIMENSIONS.map((d) => d.dim).find((d) => d !== rowDim && d !== colDim) ?? null;
|
||||
}
|
||||
|
||||
// Resolve a subject's group value for a given group field id.
|
||||
// null -> no grouping (field is falsy or '__none__')
|
||||
// '—' -> grouping is on but this subject has no value
|
||||
@@ -181,29 +168,6 @@ export function subjectGroupValue(animal, groupField) {
|
||||
return v === null || v === undefined || v === '' ? '—' : v;
|
||||
}
|
||||
|
||||
export function listDateMembers(statuses) {
|
||||
const keys = new Set();
|
||||
for (const s of statuses ?? []) {
|
||||
if (s?.date) keys.add(dateKey(s.date));
|
||||
}
|
||||
return [...keys].sort().map((k) => ({ id: k, label: k, dateKey: k }));
|
||||
}
|
||||
|
||||
// dateKey -> Map(animalId -> status); first status per (date, animal) wins,
|
||||
// scanning in date order (matches the legacy buildMatrix tie-break).
|
||||
export function buildStatusIndex(statuses) {
|
||||
const index = new Map();
|
||||
const ordered = [...(statuses ?? [])].sort((a, b) => dateKey(a?.date).localeCompare(dateKey(b?.date)));
|
||||
for (const s of ordered) {
|
||||
if (!s?.date || s.animal_id === null || s.animal_id === undefined) continue;
|
||||
const dk = dateKey(s.date);
|
||||
if (!index.has(dk)) index.set(dk, new Map());
|
||||
const byAnimal = index.get(dk);
|
||||
if (!byAnimal.has(s.animal_id)) byAnimal.set(s.animal_id, s);
|
||||
}
|
||||
return index;
|
||||
}
|
||||
|
||||
export function listSubjectMembers(animals, groupField) {
|
||||
const list = [...(animals ?? [])];
|
||||
const nameCounts = new Map();
|
||||
@@ -227,19 +191,6 @@ export function listSubjectMembers(animals, groupField) {
|
||||
return members;
|
||||
}
|
||||
|
||||
export function listParameterMembers(dailyTemplate, statuses) {
|
||||
return listExportableParams(dailyTemplate, statuses).map((p) => ({
|
||||
id: p.id, label: p.label, param: p, group: p.group,
|
||||
}));
|
||||
}
|
||||
|
||||
export function listDimensionMembers(dim, { statuses, animals, dailyTemplate, groupField }) {
|
||||
if (dim === 'date') return listDateMembers(statuses);
|
||||
if (dim === 'subject') return listSubjectMembers(animals, groupField);
|
||||
if (dim === 'parameter') return listParameterMembers(dailyTemplate, statuses);
|
||||
return [];
|
||||
}
|
||||
|
||||
// A cell has a value unless it is null, undefined, or the empty string.
|
||||
// (0 and false are real values.)
|
||||
function hasValue(v) {
|
||||
@@ -250,59 +201,6 @@ function dateKey(date) {
|
||||
return String(date).slice(0, 10); // ISO datetime or date → YYYY-MM-DD
|
||||
}
|
||||
|
||||
// Pivot statuses into a 2-D matrix per the chosen row/column dimensions.
|
||||
// The leftover (pinned) dimension is fixed to pinnedMember. Every cell resolves
|
||||
// a (dateKey, animalId, param) triple through the status index.
|
||||
export function buildMatrix(config, ctx) {
|
||||
const { rowDim, colDim, pinnedMember, groupField } = config;
|
||||
const pinnedDim = otherDimension(rowDim, colDim);
|
||||
const index = buildStatusIndex(ctx.statuses);
|
||||
const full = { ...ctx, groupField };
|
||||
const rowMembers = listDimensionMembers(rowDim, full);
|
||||
const colMembers = listDimensionMembers(colDim, full);
|
||||
|
||||
const cellValue = (rowM, colM) => {
|
||||
const byDim = { [rowDim]: rowM, [colDim]: colM, [pinnedDim]: pinnedMember };
|
||||
const dk = byDim.date?.dateKey ?? null;
|
||||
const animalId = byDim.subject?.animalId ?? null;
|
||||
const param = byDim.parameter?.param ?? null;
|
||||
if (dk === null || animalId === null || animalId === undefined || !param) return '';
|
||||
const status = index.get(dk)?.get(animalId);
|
||||
if (!status) return '';
|
||||
const v = extractValue(status, param);
|
||||
return hasValue(v) ? v : '';
|
||||
};
|
||||
|
||||
let rows = rowMembers.map((rowM) => ({
|
||||
member: rowM,
|
||||
values: colMembers.map((colM) => cellValue(rowM, colM)),
|
||||
}));
|
||||
|
||||
// Empty-member rule: keep all subjects; drop entirely-blank date/parameter rows & columns.
|
||||
if (rowDim !== 'subject') rows = rows.filter((r) => r.values.some((v) => v !== ''));
|
||||
|
||||
let keptCols = colMembers.map((_, i) => i);
|
||||
if (colDim !== 'subject') keptCols = keptCols.filter((i) => rows.some((r) => r.values[i] !== ''));
|
||||
const columns = keptCols.map((i) => colMembers[i]);
|
||||
rows = rows.map((r) => ({ member: r.member, values: keptCols.map((i) => r.values[i]) }));
|
||||
|
||||
const grouped = !!groupField && groupField !== '__none__';
|
||||
const groupAxis = grouped
|
||||
? (rowDim === 'subject' ? 'row' : colDim === 'subject' ? 'col' : null)
|
||||
: null;
|
||||
|
||||
return {
|
||||
rowDim,
|
||||
colDim,
|
||||
pinnedDim,
|
||||
corner: dimLabel(rowDim),
|
||||
context: { label: dimLabel(pinnedDim), value: pinnedMember?.label ?? '' },
|
||||
groupAxis,
|
||||
columns,
|
||||
rows,
|
||||
};
|
||||
}
|
||||
|
||||
function escapeCsvCell(value) {
|
||||
const s = value === null || value === undefined ? '' : String(value);
|
||||
return /[",\n]/.test(s) ? `"${s.replace(/"/g, '""')}"` : s;
|
||||
|
||||
@@ -71,92 +71,6 @@ describe('listExportableParams', () => {
|
||||
});
|
||||
});
|
||||
|
||||
import { buildMatrix } from '../src/lib/dataExport';
|
||||
|
||||
const gAnimals = [
|
||||
{ id: 'a2', animal_name: 'Beta', animal_id_string: 'R-002', subject_info: { grp: 'Drug' } },
|
||||
{ id: 'a1', animal_name: 'Alpha', animal_id_string: 'R-001', subject_info: { grp: 'Control' } },
|
||||
];
|
||||
const gStatuses = [
|
||||
{ animal_id: 'a1', date: '2026-07-01T00:00:00.000Z', analysis_summary: { total: 5, success_rate: 0.5 } },
|
||||
{ animal_id: 'a2', date: '2026-07-01T00:00:00.000Z', analysis_summary: { total: 9, success_rate: 0.9 } },
|
||||
{ animal_id: 'a1', date: '2026-07-02T00:00:00.000Z', analysis_summary: { total: 7, success_rate: 0.7 } },
|
||||
];
|
||||
const ctx = { statuses: gStatuses, animals: gAnimals, dailyTemplate: [] };
|
||||
const totalMember = { id: '__total__', label: 'Total attempts', param: { kind: 'total' }, group: 'metrics' };
|
||||
|
||||
describe('buildMatrix', () => {
|
||||
it('default preset: rows=date, cols=subject, pinned=parameter (legacy layout)', () => {
|
||||
const m = buildMatrix({ rowDim: 'date', colDim: 'subject', pinnedMember: totalMember, groupField: '__none__' }, ctx);
|
||||
expect(m.corner).toBe('Date');
|
||||
expect(m.context).toEqual({ label: 'Parameter', value: 'Total attempts' });
|
||||
expect(m.columns.map((c) => c.label)).toEqual(['Alpha', 'Beta']);
|
||||
expect(m.rows.map((r) => r.member.label)).toEqual(['2026-07-01', '2026-07-02']);
|
||||
expect(m.rows[0].values).toEqual([5, 9]);
|
||||
expect(m.rows[1].values).toEqual([7, '']); // Beta blank on 07-02
|
||||
expect(m.groupAxis).toBeNull();
|
||||
});
|
||||
|
||||
it('drops blank date rows but keeps all subject columns', () => {
|
||||
const statuses = [
|
||||
{ animal_id: 'a1', date: '2026-07-01T00:00:00.000Z', analysis_summary: { total: 0 } }, // 0 is a value
|
||||
{ animal_id: 'a1', date: '2026-07-03T00:00:00.000Z', analysis_summary: null }, // no value
|
||||
];
|
||||
const m = buildMatrix({ rowDim: 'date', colDim: 'subject', pinnedMember: totalMember, groupField: '__none__' },
|
||||
{ statuses, animals: gAnimals, dailyTemplate: [] });
|
||||
expect(m.rows.map((r) => r.member.label)).toEqual(['2026-07-01']);
|
||||
expect(m.columns.length).toBe(2);
|
||||
expect(m.rows[0].values).toEqual([0, '']);
|
||||
});
|
||||
|
||||
it('subject as columns sets groupAxis=col and clusters subjects', () => {
|
||||
const m = buildMatrix({ rowDim: 'date', colDim: 'subject', pinnedMember: totalMember, groupField: 'grp' }, ctx);
|
||||
expect(m.groupAxis).toBe('col');
|
||||
expect(m.columns.map((c) => [c.group, c.label])).toEqual([['Control', 'Alpha'], ['Drug', 'Beta']]);
|
||||
});
|
||||
|
||||
it('subject as rows sets groupAxis=row; pinned date; cols=parameter', () => {
|
||||
const dateMember = { id: '2026-07-01', label: '2026-07-01', dateKey: '2026-07-01' };
|
||||
const m = buildMatrix({ rowDim: 'subject', colDim: 'parameter', pinnedMember: dateMember, groupField: 'grp' },
|
||||
{ statuses: gStatuses, animals: gAnimals, dailyTemplate: [] });
|
||||
expect(m.corner).toBe('Subject');
|
||||
expect(m.context).toEqual({ label: 'Date', value: '2026-07-01' });
|
||||
expect(m.groupAxis).toBe('row');
|
||||
expect(m.rows.map((r) => [r.member.group, r.member.label])).toEqual([['Control', 'Alpha'], ['Drug', 'Beta']]);
|
||||
// columns are parameters (Total attempts, Success rate); Alpha on 07-01: total 5, rate 0.5
|
||||
const totalCol = m.columns.findIndex((c) => c.label === 'Total attempts');
|
||||
expect(m.rows[0].values[totalCol]).toBe(5);
|
||||
});
|
||||
|
||||
it('drops blank parameter columns (params always dropped when empty)', () => {
|
||||
// dailyTemplate adds a custom field with no data anywhere -> its column drops
|
||||
const dailyTemplate = [{ fieldId: 'c-x', key: 'x', label: 'X', builtin: false, active: true }];
|
||||
const dateMember = { id: '2026-07-01', label: '2026-07-01', dateKey: '2026-07-01' };
|
||||
const m = buildMatrix({ rowDim: 'subject', colDim: 'parameter', pinnedMember: dateMember, groupField: '__none__' },
|
||||
{ statuses: gStatuses, animals: gAnimals, dailyTemplate });
|
||||
expect(m.columns.map((c) => c.label)).not.toContain('X');
|
||||
});
|
||||
|
||||
it('neither axis is Subject: drops a blank date row and a blank parameter column together', () => {
|
||||
// Subject is pinned (rowDim=date, colDim=parameter). Both drop-blank filters engage:
|
||||
// 07-02 has no data at all (row dropped), and the custom field X never has data (column dropped).
|
||||
const dailyTemplate = [{ fieldId: 'c-x', key: 'x', label: 'X', builtin: false, active: true }];
|
||||
const statuses = [
|
||||
{ animal_id: 'a1', date: '2026-07-01T00:00:00.000Z', analysis_summary: { total: 5, success_rate: 0.5 } },
|
||||
{ animal_id: 'a1', date: '2026-07-02T00:00:00.000Z', analysis_summary: null },
|
||||
];
|
||||
const subjectMember = { id: 'a1', label: 'Alpha', animalId: 'a1' };
|
||||
const m = buildMatrix(
|
||||
{ rowDim: 'date', colDim: 'parameter', pinnedMember: subjectMember, groupField: '__none__' },
|
||||
{ statuses, animals: gAnimals, dailyTemplate },
|
||||
);
|
||||
expect(m.groupAxis).toBeNull();
|
||||
expect(m.rows.map((r) => r.member.label)).toEqual(['2026-07-01']);
|
||||
expect(m.columns.map((c) => c.label)).not.toContain('X');
|
||||
expect(m.columns.map((c) => c.label)).toEqual(['Total attempts', 'Success rate']);
|
||||
});
|
||||
});
|
||||
|
||||
import { toCSV, csvFilename } from '../src/lib/dataExport';
|
||||
|
||||
describe('toCSV', () => {
|
||||
@@ -225,23 +139,7 @@ describe('csvFilename', () => {
|
||||
});
|
||||
});
|
||||
|
||||
import {
|
||||
EXPORT_DIMENSIONS, dimLabel, otherDimension, subjectGroupValue,
|
||||
listDateMembers, buildStatusIndex,
|
||||
} from '../src/lib/dataExport';
|
||||
|
||||
describe('dimension primitives', () => {
|
||||
it('exposes the three dimensions with labels', () => {
|
||||
expect(EXPORT_DIMENSIONS.map((d) => d.dim)).toEqual(['date', 'subject', 'parameter']);
|
||||
expect(dimLabel('date')).toBe('Date');
|
||||
expect(dimLabel('subject')).toBe('Subject');
|
||||
expect(dimLabel('parameter')).toBe('Parameter');
|
||||
});
|
||||
it('otherDimension returns the leftover', () => {
|
||||
expect(otherDimension('date', 'subject')).toBe('parameter');
|
||||
expect(otherDimension('parameter', 'date')).toBe('subject');
|
||||
});
|
||||
});
|
||||
import { subjectGroupValue } from '../src/lib/dataExport';
|
||||
|
||||
describe('subjectGroupValue', () => {
|
||||
const animal = { animal_name: 'Alpha', animal_id_string: 'R-001', subject_info: { sex: 'M', cohort: '' } };
|
||||
@@ -260,37 +158,7 @@ describe('subjectGroupValue', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('listDateMembers', () => {
|
||||
it('returns sorted unique dateKeys with id/label', () => {
|
||||
const statuses = [
|
||||
{ date: '2026-07-02T00:00:00.000Z' },
|
||||
{ date: '2026-07-01T12:00:00.000Z' },
|
||||
{ date: '2026-07-02T09:00:00.000Z' },
|
||||
];
|
||||
expect(listDateMembers(statuses)).toEqual([
|
||||
{ id: '2026-07-01', label: '2026-07-01', dateKey: '2026-07-01' },
|
||||
{ id: '2026-07-02', label: '2026-07-02', dateKey: '2026-07-02' },
|
||||
]);
|
||||
});
|
||||
it('tolerates empty input', () => {
|
||||
expect(listDateMembers(undefined)).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('buildStatusIndex', () => {
|
||||
it('indexes by dateKey then animalId, first status wins', () => {
|
||||
const statuses = [
|
||||
{ animal_id: 'a1', date: '2026-07-01T00:00:00.000Z', analysis_summary: { total: 7 } },
|
||||
{ animal_id: 'a1', date: '2026-07-01T06:00:00.000Z', analysis_summary: { total: 99 } },
|
||||
{ animal_id: 'a2', date: '2026-07-01T00:00:00.000Z', analysis_summary: { total: 3 } },
|
||||
];
|
||||
const idx = buildStatusIndex(statuses);
|
||||
expect(idx.get('2026-07-01').get('a1').analysis_summary.total).toBe(7);
|
||||
expect(idx.get('2026-07-01').get('a2').analysis_summary.total).toBe(3);
|
||||
});
|
||||
});
|
||||
|
||||
import { listSubjectMembers, listParameterMembers, listDimensionMembers } from '../src/lib/dataExport';
|
||||
import { listSubjectMembers } from '../src/lib/dataExport';
|
||||
|
||||
describe('listSubjectMembers', () => {
|
||||
const animals = [
|
||||
@@ -319,23 +187,6 @@ describe('listSubjectMembers', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('listParameterMembers / listDimensionMembers', () => {
|
||||
const dailyTemplate = [{ fieldId: 'b-vitals', key: 'vitals', label: 'Vitals', builtin: true, active: true }];
|
||||
const statuses = [{ date: '2026-07-01T00:00:00.000Z', animal_id: 'a1', analysis_summary: { total: 1 } }];
|
||||
const animals = [{ id: 'a1', animal_name: 'Alpha', animal_id_string: 'R-001' }];
|
||||
it('wraps params with id/label/param/group', () => {
|
||||
const m = listParameterMembers(dailyTemplate, statuses);
|
||||
expect(m[0]).toMatchObject({ id: '__total__', label: 'Total attempts', group: 'metrics' });
|
||||
expect(m[0].param).toMatchObject({ kind: 'total' });
|
||||
});
|
||||
it('dispatches by dimension', () => {
|
||||
const ctx = { statuses, animals, dailyTemplate, groupField: '__none__' };
|
||||
expect(listDimensionMembers('date', ctx).map((d) => d.id)).toEqual(['2026-07-01']);
|
||||
expect(listDimensionMembers('subject', ctx).map((d) => d.id)).toEqual(['a1']);
|
||||
expect(listDimensionMembers('parameter', ctx)[0].id).toBe('__total__');
|
||||
});
|
||||
});
|
||||
|
||||
import { numericFieldVal, listSampleMembers, buildSampleIndex } from '../src/lib/dataExport';
|
||||
|
||||
const sDaily = [{ fieldId: 'c-w', key: 'w', label: 'Weight', builtin: false, active: true }];
|
||||
|
||||
Reference in New Issue
Block a user