Indicators: the history chart and the KPI card read the total when a measure publishes more groups

This commit is contained in:
2026-11-13 21:15:47 -06:00
parent b42428b068
commit 4ffd3797b6
5 changed files with 33 additions and 6 deletions
@@ -1,4 +1,4 @@
import { IndicadorDatos, PlotlyFigure, Tema } from './indicadores.models';
import { filasDelGrupo, IndicadorDatos, PlotlyFigure, Tema } from './indicadores.models';
import { c2Color, c2Theme, C2_PALETTE } from './indicadores-c2-palette';
import { sortedPeriods } from './indicadores-periodos';
import { M28_COMPLETO, M28_EN_PROCESO } from './indicadores-kpi';
@@ -67,8 +67,9 @@ function banda(periodos: string[], bajo: (number | null)[], alto: (number | null
}
// Separate rows from what is missing or suppressed; a period is judged by its only row.
function serie(data: IndicadorDatos, periodos: string[], valor: (row: Record<string, unknown>) => boolean) {
const filas = new Map(data.filas.map(row => [row['periodo'] as string, row]));
if (filas.size !== data.filas.length) throw new Error('Filas ambiguas');
const delTotal = filasDelGrupo(data, 'TOTAL');
const filas = new Map(delTotal.map(row => [row['periodo'] as string, row]));
if (filas.size !== delTotal.length) throw new Error('Filas ambiguas');
const suprimidos: string[] = [], sinDato: string[] = [];
const validas = periodos.map(periodo => {
const row = filas.get(periodo);
@@ -36,6 +36,16 @@ describe('Figuras de histórico', () => {
expect(suprimidos).toEqual(['feb 2024']);
expect(figure.layout['shapes']).toEqual([expect.objectContaining({ x0: .5, x1: 1.5 })]);
});
it('C1 con grupos adicionales dibuja la línea del total', () => {
// Pins down: a measure that publishes one group per alteration next to its total (TEA's EEG result) gets its
// history from the total's row of each period. Fails if the other groups' rows count as a second row of the
// period («Filas ambiguas»), or if two rows of the total stop being refused.
const grupos = { TOTAL: 'Total', ritmo_posterior: 'Ritmo posterior' };
const filas = base.periodos.flatMap(p => [{ ...c1(p, 40), grupo: 'TOTAL' }, { ...c1(p, 10), grupo: 'ritmo_posterior' }]);
const data: IndicadorDatos = { ...base, contrato: 'C1', grupos, filas };
expect(traza(data, 1).y).toEqual([40, 40, 40]);
expect(() => historicoFigura({ ...data, filas: [...filas, { ...c1('2024-01', 5), grupo: 'TOTAL' }] }, 'claro')).toThrow('Filas ambiguas');
});
it('C1 es una línea que no cruza huecos y su banda tampoco', () => {
const data: IndicadorDatos = { ...base, contrato: 'C1', filas: [c1('2024-01', 40), c1('2024-02', null, 1), c1('2024-03', 60)] };
const { suprimidos, figure } = historicoFigura(data, 'claro');
@@ -21,6 +21,14 @@ describe.each(['tea', 'cacu', 'cama'] as const)('KPI %s', domain => {
expect(result.value).toBe('33.4'); expect(result.info).toContain('2 de 6');
expect(run(1, [{ ...total, grupo: c1Group, valor: 0, numerador: 0, denominador: 6 }]).value).toBe('0.0');
});
it('C1 con grupos adicionales publicados lee la fila de su grupo', () => {
// Pins down: the rows of the other groups a measure publishes (one per alteration) don't make the card
// unconsolidable; a row of a group nobody published still does. Fails if those rows are mixed into the total.
const rows = [{ ...total, grupo: c1Group, valor: 50, numerador: 3, denominador: 6 }, { ...total, grupo: 'ritmo_posterior', valor: 10, numerador: 1, denominador: 6 }];
const d = { ...data(domain, 1, rows), grupos: { [c1Group]: 'Total', ritmo_posterior: 'Ritmo posterior' } };
expect(kpiResult(d, definitions[domain][1], domain, applied).value).toBe('50.0');
expect(kpiResult({ ...d, grupos: { [c1Group]: 'Total' } }, definitions[domain][1], domain, applied).state).toBe('Selección no consolidable');
});
it('C3r conserva mediana cero y Q1/Q3, sin reglas provisionales en la tarjeta', () => {
const result = run(2, [{ ...total, mediana: 0, n: 5, q1: 0, q3: 7, unidad: 'dias' }]);
expect(result.value).toBe(0);
@@ -1,4 +1,4 @@
import { Dominio, IndicadorDatos } from './indicadores.models';
import { Dominio, filasDelGrupo, IndicadorDatos } from './indicadores.models';
import { GlobalApplied } from './indicadores-memory';
import { percentNumber } from './indicadores-format';
@@ -72,9 +72,9 @@ export function kpiResult(data: IndicadorDatos, definition: KpiDefinition, domai
if (data.periodos.length !== 1 || new Set(data.filas.map(row => row['periodo'])).size > 1) return unscoped;
const period = data.periodos[0];
if (applied.selection.mode === 'explicit' && (applied.selection.periodos.length !== 1 || applied.selection.periodos[0] !== period)) return unscoped;
const rows = data.filas;
if (!rows.length) return { state: 'Sin filas utilizables' };
const group = definition.contrato === 'C1' ? C1_GROUP[domain] : 'TOTAL';
const rows = definition.contrato === 'C1' ? filasDelGrupo(data, group) : data.filas;
if (!rows.length) return { state: 'Sin filas utilizables' };
if (rows.some(row => row['periodo'] !== period || row['grupo'] !== group || row['estrato'] != null)) return unscoped;
if (definition.contrato !== 'C2' && rows.length !== 1) return unscoped;
if (rows.some(row => finite(row['suprimido']) && row['suprimido'] !== 0)) return { state: 'Dato oculto por tamaño' };
@@ -63,6 +63,14 @@ export interface PlotlyFigure {
export type Tema = 'claro' | 'oscuro';
export interface FigureSelection { dominio?: Dominio; tipoPeriodo?: TipoPeriodo; indicador: string; figura: string; tema: Tema; periodos?: string[]; }
// A measure may publish more groups next to `group` (TEA's EEG result: one per alteration). The rows without those
// of the other published groups; a row of a group nobody published stays, for the caller to refuse.
export function filasDelGrupo(data: IndicadorDatos, group: string): Record<string, unknown>[] {
const published = data['grupos'];
const others = new Set(Object.keys(published && typeof published === 'object' ? published : {}).filter(id => id !== group));
return others.size ? data.filas.filter(row => !others.has(row['grupo'] as string)) : data.filas;
}
export function parsePeriodos(value: string, tipo: TipoPeriodo = 'semestre'): string[] | null {
if (tipo !== 'semestre' && tipo !== 'mes') return null;
const parts = value.split(',').map(part => part.trim());