feat(export): generalized buildMatrix over assignable dimensions
This commit is contained in:
@@ -148,53 +148,57 @@ function dateKey(date) {
|
|||||||
return String(date).slice(0, 10); // ISO datetime or date → YYYY-MM-DD
|
return String(date).slice(0, 10); // ISO datetime or date → YYYY-MM-DD
|
||||||
}
|
}
|
||||||
|
|
||||||
// Pivot statuses into { columns, rows }.
|
// Pivot statuses into a 2-D matrix per the chosen row/column dimensions.
|
||||||
// columns: [{ id, header }] — all animals, ordered by name, headers
|
// The leftover (pinned) dimension is fixed to pinnedMember. Every cell resolves
|
||||||
// disambiguated with animal_id_string on duplicate names.
|
// a (dateKey, animalId, param) triple through the status index.
|
||||||
// rows: [{ date, values }] — one per date that has ≥1 value; values are
|
export function buildMatrix(config, ctx) {
|
||||||
// aligned to columns, blank cells are '' (empty string).
|
const { rowDim, colDim, pinnedMember, groupField } = config;
|
||||||
export function buildMatrix(statuses, animals, param) {
|
const pinnedDim = otherDimension(rowDim, colDim);
|
||||||
const sortedAnimals = [...(animals ?? [])].sort((a, b) =>
|
const index = buildStatusIndex(ctx.statuses);
|
||||||
String(a.animal_name ?? '').localeCompare(String(b.animal_name ?? '')),
|
const full = { ...ctx, groupField };
|
||||||
);
|
const rowMembers = listDimensionMembers(rowDim, full);
|
||||||
|
const colMembers = listDimensionMembers(colDim, full);
|
||||||
|
|
||||||
const nameCounts = new Map();
|
const cellValue = (rowM, colM) => {
|
||||||
for (const a of sortedAnimals) {
|
const byDim = { [rowDim]: rowM, [colDim]: colM, [pinnedDim]: pinnedMember };
|
||||||
const n = a.animal_name ?? '';
|
const dk = byDim.date?.dateKey ?? null;
|
||||||
nameCounts.set(n, (nameCounts.get(n) ?? 0) + 1);
|
const animalId = byDim.subject?.animalId ?? null;
|
||||||
}
|
const param = byDim.parameter?.param ?? null;
|
||||||
const columns = sortedAnimals.map((a) => {
|
if (dk === null || animalId === null || animalId === undefined || !param) return '';
|
||||||
const name = a.animal_name ?? '';
|
const status = index.get(dk)?.get(animalId);
|
||||||
const header = nameCounts.get(name) > 1 ? `${name} (${a.animal_id_string ?? ''})` : name;
|
if (!status) return '';
|
||||||
return { id: a.id, header };
|
const v = extractValue(status, param);
|
||||||
});
|
|
||||||
const colIndex = new Map(columns.map((c, i) => [c.id, i]));
|
|
||||||
|
|
||||||
// cells: dateKey -> Map(animalId -> value); first status per (date, animal) wins.
|
|
||||||
const cells = new Map();
|
|
||||||
const datesWithData = new Set();
|
|
||||||
const ordered = [...(statuses ?? [])].sort((a, b) => dateKey(a.date).localeCompare(dateKey(b.date)));
|
|
||||||
for (const s of ordered) {
|
|
||||||
if (!colIndex.has(s.animal_id)) continue;
|
|
||||||
const dk = dateKey(s.date);
|
|
||||||
if (!cells.has(dk)) cells.set(dk, new Map());
|
|
||||||
const byAnimal = cells.get(dk);
|
|
||||||
if (byAnimal.has(s.animal_id)) continue; // first wins
|
|
||||||
const v = extractValue(s, param);
|
|
||||||
byAnimal.set(s.animal_id, v);
|
|
||||||
if (hasValue(v)) datesWithData.add(dk);
|
|
||||||
}
|
|
||||||
|
|
||||||
const rows = [...datesWithData].sort().map((dk) => {
|
|
||||||
const byAnimal = cells.get(dk);
|
|
||||||
const values = columns.map((c) => {
|
|
||||||
const v = byAnimal.has(c.id) ? byAnimal.get(c.id) : null;
|
|
||||||
return hasValue(v) ? v : '';
|
return hasValue(v) ? v : '';
|
||||||
});
|
};
|
||||||
return { date: dk, values };
|
|
||||||
});
|
|
||||||
|
|
||||||
return { columns, rows };
|
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) {
|
function escapeCsvCell(value) {
|
||||||
|
|||||||
@@ -73,56 +73,68 @@ describe('listExportableParams', () => {
|
|||||||
|
|
||||||
import { buildMatrix } from '../src/lib/dataExport';
|
import { buildMatrix } from '../src/lib/dataExport';
|
||||||
|
|
||||||
const animals = [
|
const gAnimals = [
|
||||||
{ id: 'a2', animal_name: 'Beta', animal_id_string: 'R-002' },
|
{ id: 'a2', animal_name: 'Beta', animal_id_string: 'R-002', subject_info: { grp: 'Drug' } },
|
||||||
{ id: 'a1', animal_name: 'Alpha', animal_id_string: 'R-001' },
|
{ id: 'a1', animal_name: 'Alpha', animal_id_string: 'R-001', subject_info: { grp: 'Control' } },
|
||||||
];
|
];
|
||||||
const totalParam = { kind: 'total' };
|
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', () => {
|
describe('buildMatrix', () => {
|
||||||
it('orders columns by animal_name and rows by date ascending', () => {
|
it('default preset: rows=date, cols=subject, pinned=parameter (legacy layout)', () => {
|
||||||
const statuses = [
|
const m = buildMatrix({ rowDim: 'date', colDim: 'subject', pinnedMember: totalMember, groupField: '__none__' }, ctx);
|
||||||
{ animal_id: 'a1', date: '2026-07-02T00:00:00.000Z', analysis_summary: { total: 5 } },
|
expect(m.corner).toBe('Date');
|
||||||
{ animal_id: 'a2', date: '2026-07-01T00:00:00.000Z', analysis_summary: { total: 9 } },
|
expect(m.context).toEqual({ label: 'Parameter', value: 'Total attempts' });
|
||||||
];
|
expect(m.columns.map((c) => c.label)).toEqual(['Alpha', 'Beta']);
|
||||||
const m = buildMatrix(statuses, animals, totalParam);
|
expect(m.rows.map((r) => r.member.label)).toEqual(['2026-07-01', '2026-07-02']);
|
||||||
expect(m.columns.map((c) => c.header)).toEqual(['Alpha', 'Beta']);
|
expect(m.rows[0].values).toEqual([5, 9]);
|
||||||
expect(m.rows.map((r) => r.date)).toEqual(['2026-07-01', '2026-07-02']);
|
expect(m.rows[1].values).toEqual([7, '']); // Beta blank on 07-02
|
||||||
// Row for 07-01: Alpha blank, Beta 9
|
expect(m.groupAxis).toBeNull();
|
||||||
expect(m.rows[0].values).toEqual(['', 9]);
|
|
||||||
// Row for 07-02: Alpha 5, Beta blank
|
|
||||||
expect(m.rows[1].values).toEqual([5, '']);
|
|
||||||
});
|
});
|
||||||
it('includes only dates that have at least one value (0 counts as a value)', () => {
|
|
||||||
|
it('drops blank date rows but keeps all subject columns', () => {
|
||||||
const statuses = [
|
const statuses = [
|
||||||
{ animal_id: 'a1', date: '2026-07-01T00:00:00.000Z', analysis_summary: { total: 0 } },
|
{ 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
|
{ animal_id: 'a1', date: '2026-07-03T00:00:00.000Z', analysis_summary: null }, // no value
|
||||||
];
|
];
|
||||||
const m = buildMatrix(statuses, animals, totalParam);
|
const m = buildMatrix({ rowDim: 'date', colDim: 'subject', pinnedMember: totalMember, groupField: '__none__' },
|
||||||
expect(m.rows.map((r) => r.date)).toEqual(['2026-07-01']);
|
{ 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, '']);
|
expect(m.rows[0].values).toEqual([0, '']);
|
||||||
});
|
});
|
||||||
it('disambiguates duplicate animal names with the id string', () => {
|
|
||||||
const dupAnimals = [
|
it('subject as columns sets groupAxis=col and clusters subjects', () => {
|
||||||
{ id: 'a1', animal_name: 'Rat', animal_id_string: 'R-001' },
|
const m = buildMatrix({ rowDim: 'date', colDim: 'subject', pinnedMember: totalMember, groupField: 'grp' }, ctx);
|
||||||
{ id: 'a2', animal_name: 'Rat', animal_id_string: 'R-002' },
|
expect(m.groupAxis).toBe('col');
|
||||||
];
|
expect(m.columns.map((c) => [c.group, c.label])).toEqual([['Control', 'Alpha'], ['Drug', 'Beta']]);
|
||||||
const statuses = [{ animal_id: 'a1', date: '2026-07-01T00:00:00.000Z', analysis_summary: { total: 1 } }];
|
|
||||||
const m = buildMatrix(statuses, dupAnimals, totalParam);
|
|
||||||
expect(m.columns.map((c) => c.header)).toEqual(['Rat (R-001)', 'Rat (R-002)']);
|
|
||||||
});
|
});
|
||||||
it('keeps the first status when a subject has duplicates on one date', () => {
|
|
||||||
const statuses = [
|
it('subject as rows sets groupAxis=row; pinned date; cols=parameter', () => {
|
||||||
{ animal_id: 'a1', date: '2026-07-01T00:00:00.000Z', analysis_summary: { total: 7 } },
|
const dateMember = { id: '2026-07-01', label: '2026-07-01', dateKey: '2026-07-01' };
|
||||||
{ animal_id: 'a1', date: '2026-07-01T00:00:00.000Z', analysis_summary: { total: 99 } },
|
const m = buildMatrix({ rowDim: 'subject', colDim: 'parameter', pinnedMember: dateMember, groupField: 'grp' },
|
||||||
];
|
{ statuses: gStatuses, animals: gAnimals, dailyTemplate: [] });
|
||||||
const m = buildMatrix(statuses, animals, totalParam);
|
expect(m.corner).toBe('Subject');
|
||||||
expect(m.rows[0].values).toEqual([7, '']);
|
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('returns empty rows when no status has a value', () => {
|
|
||||||
const m = buildMatrix([{ animal_id: 'a1', date: '2026-07-01T00:00:00.000Z', analysis_summary: null }], animals, totalParam);
|
it('drops blank parameter columns (params always dropped when empty)', () => {
|
||||||
expect(m.rows).toEqual([]);
|
// dailyTemplate adds a custom field with no data anywhere -> its column drops
|
||||||
expect(m.columns.length).toBe(2);
|
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');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user