/** * Copyright (c) 2025 Elara AI Pty Ltd * Dual-licensed under AGPL-3.0 and commercial license. See LICENSE for details. */ /** * The Plan's renderer-side derivations (§4.2 — the Table idiom): the IR * DECLARES rollups, aggregates, subtotals, summaries and folds, and the numbers * are computed here over the decoded rows (split out of `model.ts`, #815). * * @packageDocumentation */ import { type ValueTypeOf } from "@elaraai/east"; import { Plan } from "@elaraai/e3-ui/internal"; import { type TickFormatOpt } from "@elaraai/east-ui-components"; import { type PlanAxisKind, type PlanInstantValue } from "./instant.js"; import { type PlanWords } from "./words.js"; import { type PlanPeriod } from "./fold.js"; import type { PlanRowIndex } from "./model.js"; import type { RowKey } from "./plan-state.js"; import type { ChartKindValue } from "./rows/chart-geometry.js"; type RunValue = ValueTypeOf; type HeatCellsValue = ValueTypeOf; type HeatCellValue = ValueTypeOf; type TableCellValue = ValueTypeOf; type TableSeriesValue = ValueTypeOf; /** * A table row's AGGREGABLE positions — the ones a parent subtotals. * * `rollup: true` NARROWS: flag a position and only the flagged ones roll up, * which is how a row says "the actual is the number, the Δ beside it is * commentary". Flag nothing and EVERY position rolls up, so a subtotal mirrors * the shape of the rows it totals — a parent over `act`/`Δ` children shows an * act subtotal beside a Δ subtotal rather than silently dropping one. * * (It used to return one position's cells unconditionally — the unflagged case * fell back to `series[0]`, so a multi-value row's second position vanished * into a parent that looked complete.) */ export declare function tableRollupSeries(series: readonly TableSeriesValue[]): readonly TableSeriesValue[]; /** One derived rollup band (`×k · qty`, pessimistic state). */ export interface DerivedBand { from: PlanInstantValue; /** The band's end — on an ordinal axis the LAST bucket covered (inclusive). */ to: PlanInstantValue; /** Peak concurrency inside the band. */ count: number; /** The members' quantities summed unit by unit, as a caption (`"208 k sheets · 12 h"`, in the * canvas's locale — #820, #824) — absent unless every member carries a quantity. */ quantity: string | undefined; /** The least-certain member's lifecycle state. */ state: RunValue["state"]; } /** * Derive a rollup parent's bands from its subtree's runs. * * @param runs - The subtree's runs * @param rollup - The declared mode * @param ordinal - The ordinal axis's value → index map, when the axis is ordinal * @param w - The canvas's words — how a total prints (#820); English in `en-US` by default * @returns The bands, in start order */ export declare function deriveBands(runs: readonly RunValue[], rollup: "union" | "byStatus" | "sum", ordinal?: ReadonlyMap, w?: PlanWords): DerivedBand[]; /** * Derive per-bucket aggregated heat cells (mean / max / sum; no-data skipped), * each labelled with its value — through `format` when one is given, else the * canvas's plain number. * * @param cells - The children's cells — already folded to the period, so each * child contributes at most one value per bucket * @param mode - The declared aggregate * @param ordinal - The ordinal axis's value → index map, when the axis is ordinal * @param w - The canvas's words (#820); English in `en-US` by default * @param period - The canvas's period — cells group by the bucket that holds * them; without one, by instant * @param format - How a derived value prints * @returns One cell per bucket, in axis order */ export declare function deriveHeatCells(cells: readonly HeatCellValue[], mode: "mean" | "max" | "sum", ordinal?: ReadonlyMap, w?: PlanWords, period?: PlanPeriod, format?: TickFormatOpt): HeatCellValue[]; /** * Derive per-bucket table subtotal cells (the Table #317 vocabulary) — raw * values only; text and tone are renderer-derived through the row's shared * `TickFormatType` format. * * @remarks * A bucket none of the cells holds a value in has no subtotal, whatever the * mode (#1436): a `count` there is no value, never 0, as e3 keeps none. * * @param cells - The children's cells, already folded to the period * @param mode - The declared aggregate; `count` counts the values here, each a leaf's (a parent's `count` over rows of * its own counts their leaves: {@link deriveTableSeries}) * @param ordinal - The ordinal axis's value → index map, when the axis is ordinal * @param period - The canvas's period — cells group by the bucket that holds them * @returns One cell per bucket, in axis order */ export declare function deriveTableCells(cells: readonly TableCellValue[], mode: "sum" | "mean" | "min" | "max" | "count", ordinal?: ReadonlyMap, period?: PlanPeriod): TableCellValue[]; /** A count in one bucket, at the instant the bucket's cell sits at (#1436). */ interface Tally { readonly at: PlanInstantValue; readonly n: number; } /** * One position of a table row as a parent above it reads it (#1436): what it * draws, and how many leaf rows beneath it draw a value in each bucket. * * @remarks * A `count` subtotal counts the leaf rows beneath that draw a value — a row * drawing its own values counts one, a parent its own count — whatever the * subtotals between them count by, as e3 keeps its `count` rollup (#1434): * `TableAggregateType`'s leaf rows beneath. So each position carries its * leaves beside its numbers. */ export interface TablePosition { /** What the position draws: its own cells folded to the period, or its children's subtotal. */ readonly series: TableSeriesValue; /** How many leaf rows beneath draw a value, bucket by bucket — one in each bucket a position of its own draws a value in. */ readonly leaves: readonly Tally[]; } /** * Derive a parent's subtotal SERIES — position by position. * * Position `i` of the parent aggregates position `i` of every child that has * one, and inherits that position's declarations (format / tone / strong / * rollup / fold) from the first child carrying it, so the subtotal is styled * like the numbers it totals rather than as anonymous plain text. * * @remarks * `sum`, `mean` (a mean of its children's, a child parent's own a mean), * `min` and `max` aggregate what each child draws. `count` is the leaf rows * beneath that draw a value in the bucket, at any depth: a child drawing its * own values counts one, a parent child its own leaves (#1436). * * @param positions - Each child's aggregable positions (see {@link tableRollupSeries}), folded * @param mode - The declared aggregate * @param ordinal - The ordinal axis's value → index map, when the axis is ordinal * @param period - The canvas's period * @returns One derived position per position, with the leaves beneath it */ export declare function deriveTableSeries(positions: ReadonlyArray, mode: "sum" | "mean" | "min" | "max" | "count", ordinal?: ReadonlyMap, period?: PlanPeriod): TablePosition[]; /** A heat scale as plain numbers — `undefined` where nothing is declared. */ export interface HeatScale { min: number | undefined; max: number | undefined; warnAt: number | undefined; } /** * Why a row cannot be drawn where it is (#811) — it renders in place as a * diagnostic row carrying this, and derives nothing. * * - `axis` — the row's instants ride another arm than the axis speaks: * `found` is the arm they ride (the first one found), `expected` the axis's. * - `duplicate` — the row repeats an id an earlier row carries (#822): `of` is * that row's key. Ids are unique by construction except where hand-built rows * repeat a key, and a repeat is shown, never dropped. */ export type PlanRowDiagnostic = { kind: "axis"; found: PlanAxisKind; expected: PlanAxisKind; } | { kind: "duplicate"; of: RowKey; }; /** * One bucket's subtotal of a parent, as e3 keeps it (#1438): the instant its * bucket starts at, and the value. */ export interface PlanParentSubtotal { readonly at: PlanInstantValue; readonly value: number; } /** * Each parent's subtotals as e3 keeps them, by the row's key (#1438): what a * Plan over paged rows read beside their nested series index reads for a * parent, at the period's buckets — one series a collection its children * read, in the order they read them: a heat parent's and a group's strip * aggregated as declared, a table parent's one a position. A parent given * them draws them in place of deriving them from its children, which a window * holds folded, off screen or not at all — and never folds them again, each * being one bucket's already. */ export type PlanParentSubtotals = ReadonlyMap; /** * The per-value derived numbers, computed once per decoded root and period. * * Each `…` map says what a row DRAWS where that is not its own declaration — * a parent's derived values, or its own folded to the period (#824). A row * absent from a map draws what it declares, as it is. */ export interface PlanDerived { /** Rollup bands by span-parent row key. */ bands: ReadonlyMap; /** The heat arm each heat row draws: a declared-aggregate parent's derived * cells — on its declared scale, else the one its members share — or a * row's own cells folded to the period. */ heatArms: ReadonlyMap; /** The value series each table row draws: a parent's subtotal positions — * the same shape its members have — or a row's own series folded. */ tableSeries: ReadonlyMap; /** The chart each chart row draws — its line, area and column layers folded. */ charts: ReadonlyMap; /** The strip each group band draws collapsed: its declared aggregate over * its members' drawn heat cells (on the scale they share), or its declared * cells folded. Absent for a plain band, or declared cells as they are. */ groupStrips: ReadonlyMap; /** Direct-member count by group row key — the `"8 rows"` gutter meta. * Derived here like every other aggregate: the IR declares no count, so * the meta is always the members the group has (#568). */ groupMembers: ReadonlyMap; /** The rows that render as diagnostic rows, by key (#811) — excluded * from every band, aggregate, subtotal and strip above. */ diagnostics: ReadonlyMap; } /** * Derive every declared rollup / aggregate / summary / fold over the decoded * rows. * * @remarks * The walk is an explicit POST-ORDER traversal from the roots: a declared * parent whose children are themselves declared parents aggregates their * DERIVED cells, so nesting composes to arbitrary depth — and it is correct * for ANY container order. The stream puts a parent before its subtree, but * not always right before it (#822: an entry's children follow all of its * `views` rows), so nothing here reads position. (It once walked the flat * array in reverse, which was only right while that array happened to be * depth-first; feeding a bottom-up aggregation the wrong order yields wrong * numbers, not an error — #568.) * * Each row's values fold to the period FIRST (#824), so a parent aggregates * what its children draw. * * Rows outside the tree — a `parent` naming a key that does not exist — are * unreachable from the roots and derive nothing, exactly as they render * nothing (`visibleRows` walks the same tree). * * With the axis kind given, a row whose instants ride another arm is a * DIAGNOSTIC row (#811): it is recorded in `diagnostics`, derives nothing of * its own, and contributes nothing to any parent's band, aggregate, subtotal * or strip — it still counts as a member, since it still renders. * * @param index - The row-tree index * @param ordinal - The ordinal axis's value → index map (orders ordinal cells; omit on other axes) * @param axisKind - The axis kind; omit to diagnose nothing * @param w - The canvas's words — how a derived number prints (#820); English in * `en-US` by default (a height measure prints nothing it reads) * @param period - The canvas's period (`PlanScale.period`); omit to fold nothing * @param parents - Each parent's subtotals as e3 keeps them, by its key (#1438): a parent given them — a heat parent, a * group's strip, a table parent's positions — draws them in place of its children's; omit to derive every parent * from its children * @returns Every derived number, keyed by row */ export declare function derivePlan(index: PlanRowIndex, ordinal?: ReadonlyMap, axisKind?: PlanAxisKind, w?: PlanWords, period?: PlanPeriod, parents?: PlanParentSubtotals): PlanDerived; /** * A re-derivation with each row's entries kept by IDENTITY where their * content did not move. * * @remarks * {@link derivePlan} rebuilds every map from scratch — on a data change, on * every paged window landing, since a landing re-indexes the resident rows, * and on a resolution change, which re-folds. A row reads only its own * entries, so a row memo can skip exactly when those entries are the objects * it rendered with. This hands back the old object wherever the new one says * the same thing, which is what lets a landing render the rows whose numbers * it moved and none of the others. * * @param prev - The previous derivation, if any * @param next - The new derivation * @returns `next`, its unchanged entries replaced by `prev`'s */ export declare function stableDerived(prev: PlanDerived | undefined, next: PlanDerived): PlanDerived; export {}; //# sourceMappingURL=derive.d.ts.map