/** * Arithmetic over catalogue rows, so a caller can ask a question the upstream * API has no parameter for. * * The other side sorts by five things and filters by price and quota. Every * other price question a customer actually asks is arithmetic over fields the * API returns but cannot order by: which package is cheapest per gigabyte, which * discount is the largest, what a gigabyte costs per day. There is no * `price_per_gb` parameter upstream and there never will be, so the computation * belongs here rather than in the caller: an agent asked for "the biggest * discount" should not have to fetch 300 rows and do sums in its head. * * Three rules govern this file. * * 1. **A value that cannot be computed is null, and says why.** `originalPrice` * is 0 on 298 of 315 packages, meaning "no discount" — so `originalPrice - * price` is not a small discount, it is a negative number that would sort the * whole catalogue by a field that does not apply. Every metric reports how * many rows it could not compute and the reason census, the way the price * summary already reports `excludedNotGb`. * * 2. **No silent coercion.** Arithmetic requires numbers: a null, a string or a * boolean operand makes the result null rather than 0 or NaN. Comparison * against null is identity, not ordering. `&&` and `||` operate on booleans * and short-circuit, which is what makes a guard like * `quotaUnit == "GB" && quotaValue > 0 ? price / quotaValue : null` behave. * * 3. **A caller's expression is data, not code.** This is a parser, not `eval`: * identifiers resolve against published fields and declared metrics only, and * anything else is refused with the list of names that would have worked. */ /** The value domain. No arrays, no objects: a metric is a number, a string, a boolean or null. */ export type ComputedValue = number | string | boolean | null; /** A row to compute over: a package record, already parsed. */ export type ComputeRow = Record; export interface MetricSpec { /** Identifier the result is reported under, and the name `sort_by` accepts. */ name: string; /** Arithmetic over fields, metrics and the built-ins. */ expr: string; } export interface CompiledMetric { name: string; expr: string; ast: Node; } export interface MetricStats { name: string; expr: string; /** Which type the results are, or `mixed` when they are not all one type. */ kind: 'number' | 'boolean' | 'string' | 'null' | 'mixed'; /** Rows where the metric produced a value. */ computed: number; /** Rows where it could not, with the reason in `why`. */ notComputable: number; /** Statistics over the numeric results only; null when there are none. */ min: number | null; p25: number | null; median: number | null; mean: number | null; p75: number | null; max: number | null; /** Why rows were skipped, most common first: `{ "no discount (originalPrice is 0)": 290 }`. */ why: Record; } export interface ComputedRows { /** One entry per row, in row order: `{ price_per_gb: 2200, saving: null }`. */ values: Record[]; stats: MetricStats[]; } /** Raised for an expression that cannot be compiled. The message names what would work. */ export declare class ComputeError extends Error { constructor(message: string); } export type Node = { kind: 'number'; value: number; } | { kind: 'string'; value: string; } | { kind: 'boolean'; value: boolean; } | { kind: 'null'; } | { kind: 'field'; name: string; } | { kind: 'unary'; op: '-' | '!'; operand: Node; } | { kind: 'binary'; op: string; left: Node; right: Node; } | { kind: 'logical'; op: '&&' | '||'; left: Node; right: Node; } | { kind: 'ternary'; test: Node; then: Node; otherwise: Node; } | { kind: 'call'; name: string; args: Node[]; }; export declare function parseExpression(expr: string): Node; /** Every identifier an expression reads, for scope checking and reporting. */ export declare function identifiersIn(node: Node, found?: Set): Set; interface EvalResult { value: ComputedValue; /** Set only when the value is null: what stopped it. */ why: string | null; } export declare function evaluate(node: Node, scope: (name: string) => EvalResult): EvalResult; /** * Metrics every caller can sort by without declaring anything. * * Each one is written in the same language a caller's expression is, so the * library is compiled and evaluated by the code path it documents rather than * beside it. `price_per_gb` is the definition the price summary already uses — * GB packages only, rounded to whole rupiah — kept identical on purpose: two * definitions of "per gigabyte" in one server would be two answers. */ export declare const PREDEFINED_METRICS: readonly MetricSpec[]; export declare function isPredefinedMetric(name: string): boolean; export declare function predefinedMetric(name: string): MetricSpec | undefined; /** * Compile caller metrics, refusing anything that could not be computed later. * * Compilation is where a typo is cheap to find: an unknown name is refused here * with the list of names that would have worked, instead of quietly producing * null for every row. */ export declare function compileMetrics(specs: readonly MetricSpec[], fields: Iterable): CompiledMetric[]; /** Compile one metric by name: a caller's, or one from the library. */ export declare function compileMetricByName(name: string, specs: readonly MetricSpec[], fields: Iterable): CompiledMetric; /** * Compile the library against the real field list. * * Called by the offline suite, so a library metric that reads a field the * catalogue does not publish fails in CI rather than for a caller. This checks * identifiers only: the name-collision rules below are for caller metrics, and * the library is made of the names they reserve. */ export declare function validatePredefinedMetrics(fields: Iterable): CompiledMetric[]; /** Compute every metric for every row, with per-metric statistics and the reason census. */ export declare function computeRows(rows: readonly T[], metrics: readonly CompiledMetric[]): ComputedRows; /** * Order rows by a metric. * * Rows the metric cannot be computed for sort last in both directions: a missing * discount is not the smallest discount, and putting those rows first on an * ascending sort would answer "biggest discount" with the 290 packages that have * none. */ export declare function sortRowsByMetric(rows: readonly T[], metrics: readonly CompiledMetric[], name: string, direction: 'asc' | 'desc'): T[]; export {};