import { etiquetaVentana, SeguimientoTablaResponse, SeguimientoValor } from '../seguimiento-tabla.service'; /** Largest number of rows each format accepts. A query above the limit is refused, never truncated. */ export const EXPORT_LIMITS = { xlsx: 5000, pdf: 1000 } as const; export type ExportFormat = keyof typeof EXPORT_LIMITS; export type TrackingRow = Record; /** Everything one export needs: the first answer (columns, filters, window) and all the rows it announced. */ export interface TrackingSnapshot { /** How the analysis named the selection this list opens, when it does; the files carry it so they say where they come from. */ seleccion?: string; corte: string; ventana: SeguimientoTablaResponse['ventana']; filtros: SeguimientoTablaResponse['filtros']; filtros_disponibles: SeguimientoTablaResponse['filtros_disponibles']; columnas: SeguimientoTablaResponse['columnas']; items: TrackingRow[]; } /** The window as the files state it: the whole cohort up to the cut-off, or the dates and months of the window. */ export function ventanaConsultada(snapshot: Pick): string { const { meses, desde, hasta } = snapshot.ventana; return meses === 0 ? `Toda la cohorte, al ${hasta}` : `${desde} al ${hasta} (${etiquetaVentana(meses)})`; } export type TrackingExportEvent = | { kind: 'progress'; fetched: number; total: number } | { kind: 'done'; snapshot: TrackingSnapshot }; export class ExportLimitExceeded extends Error { constructor(readonly total: number, readonly limit: number) { super(`The query has ${total} rows and the limit is ${limit}`); } } /** The pages did not describe one stable list (the total changed, a page repeated or a row was lost). */ export class ExportInconsistent extends Error {}