-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathdataset-executor.ts
More file actions
358 lines (328 loc) · 13.6 KB
/
Copy pathdataset-executor.ts
File metadata and controls
358 lines (328 loc) · 13.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.
import type {
IAnalyticsService,
AnalyticsQuery,
AnalyticsResult,
DatasetSelection,
DatasetCompareTo,
} from '@objectstack/spec/contracts';
import type { FilterCondition } from '@objectstack/spec/data';
import type { ExecutionContext } from '@objectstack/spec/kernel';
import type { CompiledDataset, DerivedMeasureSpec } from './dataset-compiler.js';
// Re-export the shared protocol shapes so existing importers keep working.
export type { DatasetSelection } from '@objectstack/spec/contracts';
/** @deprecated use DatasetCompareTo from @objectstack/spec/contracts */
export type CompareTo = DatasetCompareTo;
/**
* Dataset executor (ADR-0021 WS2).
*
* Turns a compiled dataset + a presentation's selection (dimensions, measures,
* runtime filter, compareTo) into one or more `AnalyticsQuery`s against the Cube
* runtime, then post-processes the results:
* - resolves the base measures a selection needs (including derived deps),
* - applies measure-scoped filters via supplementary grouped queries,
* - evaluates derived measures (ratio/sum/difference/product) row-by-row (Q1),
* - shifts the query for `compareTo` (previousPeriod / previousYear) and
* attaches `<measure>__compare` columns,
* - computes server-side totals (`selection.totals.groupings`, #1753) by
* re-running the selection per dimension subset, so matrix subtotals and
* the grand total use each measure's true aggregate.
*
* RLS/tenant scoping is NOT handled here — it is enforced inside the strategy
* via the StrategyContext read-scope hook (D-C). This layer is pure query
* shaping + arithmetic.
*/
/** AND two optional FilterConditions into one (MongoDB-style). */
export function combineFilters(
a?: FilterCondition,
b?: FilterCondition,
): FilterCondition | undefined {
if (a && b) return { $and: [a, b] } as FilterCondition;
return a ?? b;
}
/**
* Evaluate derived measures on each aggregated row, mutating a shallow copy.
* Division by zero (and missing operands) yields `null` rather than Infinity/NaN.
*/
export function evaluateDerivedMeasures(
rows: Record<string, unknown>[],
derived: DerivedMeasureSpec[],
): Record<string, unknown>[] {
if (derived.length === 0) return rows;
return rows.map((row) => {
const out = { ...row };
for (const d of derived) {
out[d.name] = computeDerived(d, out);
}
return out;
});
}
function num(v: unknown): number | null {
if (v == null) return null;
const n = typeof v === 'number' ? v : Number(v);
return Number.isFinite(n) ? n : null;
}
function computeDerived(d: DerivedMeasureSpec, row: Record<string, unknown>): number | null {
const vals = d.of.map((name) => num(row[name]));
if (vals.some((v) => v === null)) return null;
const nums = vals as number[];
switch (d.op) {
case 'ratio': {
if (nums.length < 2 || nums[1] === 0) return null;
return nums[0] / nums[1];
}
case 'difference':
return nums.slice(1).reduce((acc, v) => acc - v, nums[0]);
case 'sum':
return nums.reduce((acc, v) => acc + v, 0);
case 'product':
return nums.reduce((acc, v) => acc * v, 1);
default:
return null;
}
}
// ── compareTo date math (deterministic — no Date.now) ────────────────────────
function parseUTC(date: string): number {
// Accepts 'YYYY-MM-DD' (and ISO datetimes); interpreted as UTC.
const ms = Date.parse(date.length === 10 ? `${date}T00:00:00Z` : date);
if (Number.isNaN(ms)) throw new Error(`[dataset-executor] invalid date in dateRange: "${date}"`);
return ms;
}
const DAY_MS = 86_400_000;
function toISODate(ms: number): string {
return new Date(ms).toISOString().slice(0, 10);
}
function shiftYear(date: string, years: number): string {
const d = new Date(parseUTC(date));
d.setUTCFullYear(d.getUTCFullYear() + years);
return toISODate(d.getTime());
}
/** Compute the comparison window for a [start,end] range. */
export function shiftRange(range: [string, string], kind: CompareTo['kind']): [string, string] {
const [start, end] = range;
if (kind === 'previousYear') {
return [shiftYear(start, -1), shiftYear(end, -1)];
}
// previousPeriod — the equal-length window ending the day before `start`.
const startMs = parseUTC(start);
const endMs = parseUTC(end);
const lengthDays = Math.round((endMs - startMs) / DAY_MS) + 1;
const prevEndMs = startMs - DAY_MS;
const prevStartMs = prevEndMs - (lengthDays - 1) * DAY_MS;
return [toISODate(prevStartMs), toISODate(prevEndMs)];
}
export class DatasetExecutor {
constructor(private readonly service: IAnalyticsService) {}
/**
* Execute a dataset selection and return the shaped rows (+ field metadata).
*
* @param context - The request's ExecutionContext, threaded into every
* underlying `IAnalyticsService.query` so the tenant/RLS read scope is
* applied per request (ADR-0021 D-C).
*/
async execute(
compiled: CompiledDataset,
selection: DatasetSelection,
context?: ExecutionContext,
): Promise<AnalyticsResult> {
const result = await this.executeSelection(compiled, selection, context);
// Server-side totals (#1753) — re-run the selection grouped by each
// requested dimension subset, so a subtotal/grand total is the measure's
// TRUE aggregate over the underlying rows (an avg total is the average of
// all rows, not of bucket averages). Re-running the full pipeline keeps
// measure-scoped filters, derived measures, and compareTo consistent with
// the primary grid. order/limit/offset are dropped: totals cover the whole
// selection, and an order key may reference a dimension the grouping drops.
const groupings = selection.totals?.groupings;
if (groupings?.length) {
const selected = new Set(selection.dimensions ?? []);
const totals: NonNullable<AnalyticsResult['totals']> = [];
for (const grouping of groupings) {
const unknown = grouping.filter((d) => !selected.has(d));
if (unknown.length) {
throw new Error(
`[dataset-executor] totals grouping [${grouping.join(', ')}] is not a subset of the selected dimensions — unknown: ${unknown.join(', ')}.`,
);
}
const sub = await this.executeSelection(compiled, {
...selection,
dimensions: grouping,
totals: undefined,
order: undefined,
limit: undefined,
offset: undefined,
}, context);
totals.push({ dimensions: grouping, rows: sub.rows });
}
result.totals = totals;
}
return result;
}
private async executeSelection(
compiled: CompiledDataset,
selection: DatasetSelection,
context?: ExecutionContext,
): Promise<AnalyticsResult> {
const derivedByName = new Map(compiled.derived.map((d) => [d.name, d]));
const selectedDerived = selection.measures
.map((m) => derivedByName.get(m))
.filter((d): d is DerivedMeasureSpec => !!d);
// Base measures = selected non-derived + dependencies of selected derived.
const baseMeasures = new Set<string>();
for (const m of selection.measures) {
if (!derivedByName.has(m)) baseMeasures.add(m);
}
for (const d of selectedDerived) {
for (const dep of d.of) baseMeasures.add(dep);
}
// Split measures into those with a scoped filter and those without.
const unfiltered: string[] = [];
const filtered: string[] = [];
for (const m of baseMeasures) {
(compiled.measureFilters[m] ? filtered : unfiltered).push(m);
}
const baseFilter = combineFilters(compiled.filter, selection.runtimeFilter);
const dimensions = selection.dimensions ?? [];
// Primary query: all unfiltered base measures in one pass. When every base
// measure is filter-scoped, the supplementary queries below build the grid.
let result: AnalyticsResult;
if (unfiltered.length > 0 || filtered.length === 0) {
result = await this.service.query(this.buildQuery(compiled, {
measures: unfiltered,
dimensions,
where: baseFilter,
selection,
}), context);
} else {
result = { rows: [], fields: [] };
}
// Supplementary queries: one per measure-scoped filter, merged by dimension key.
for (const m of filtered) {
const mFilter = combineFilters(baseFilter, compiled.measureFilters[m]);
const sub = await this.service.query(this.buildQuery(compiled, {
measures: [m], dimensions, where: mFilter, selection,
}), context);
result.rows = mergeByDimensions(result.rows, sub.rows, dimensions, [m]);
result.fields.push({ name: m, type: 'number' });
}
// compareTo — run a shifted query over the same base measures and attach.
if (selection.compareTo) {
const compareRows = await this.runCompare(compiled, selection, [...baseMeasures], dimensions, baseFilter, context);
result.rows = mergeByDimensions(
result.rows,
compareRows,
dimensions,
[...baseMeasures].map((m) => `${m}__compare`),
);
for (const m of baseMeasures) result.fields.push({ name: `${m}__compare`, type: 'number' });
}
// Derived measures (computed from base + compare columns already present).
result.rows = evaluateDerivedMeasures(result.rows, selectedDerived);
for (const d of selectedDerived) result.fields.push({ name: d.name, type: 'number' });
return result;
}
private buildQuery(
compiled: CompiledDataset,
opts: {
measures: string[];
dimensions: string[];
where?: FilterCondition;
selection: DatasetSelection;
},
): AnalyticsQuery {
const q: AnalyticsQuery = {
cube: compiled.cube.name,
measures: opts.measures,
dimensions: opts.dimensions,
timezone: opts.selection.timezone ?? 'UTC',
};
if (opts.where) q.where = opts.where as Record<string, unknown>;
// Bucket selected date dimensions that declare an explicit `dateGranularity`
// (the dataset compiled a single-entry `granularities`). Without this a date
// dimension groups by the raw timestamp — one bucket per row, rendering epoch
// millis on trend charts. A dimension already carried by `selection.timeDimensions`
// (e.g. compareTo) keeps its entry; we never override it.
const selTimeDims = opts.selection.timeDimensions ?? [];
const selDims = new Set(selTimeDims.map((t) => t.dimension));
const explicitTimeDims: Array<{ dimension: string; granularity: string }> = [];
for (const name of opts.dimensions) {
const cd = compiled.cube.dimensions[name];
if (cd?.type === 'time' && cd.granularities?.length === 1 && !selDims.has(name)) {
explicitTimeDims.push({ dimension: name, granularity: String(cd.granularities[0]) });
}
}
const mergedTimeDims = [...selTimeDims, ...explicitTimeDims];
if (mergedTimeDims.length > 0) q.timeDimensions = mergedTimeDims as AnalyticsQuery['timeDimensions'];
if (opts.selection.order) q.order = opts.selection.order;
if (opts.selection.limit != null) q.limit = opts.selection.limit;
if (opts.selection.offset != null) q.offset = opts.selection.offset;
return q;
}
private async runCompare(
compiled: CompiledDataset,
selection: DatasetSelection,
measures: string[],
dimensions: string[],
baseFilter: FilterCondition | undefined,
context?: ExecutionContext,
): Promise<Record<string, unknown>[]> {
const cmp = selection.compareTo!;
const td = (selection.timeDimensions ?? []).find((t) => t.dimension === cmp.dimension);
if (!td || !td.dateRange) {
throw new Error(
`[dataset-executor] compareTo requires a timeDimension "${cmp.dimension}" with a dateRange.`,
);
}
const range: [string, string] = Array.isArray(td.dateRange)
? [td.dateRange[0], td.dateRange[1] ?? td.dateRange[0]]
: [td.dateRange, td.dateRange];
const shifted = shiftRange(range, cmp.kind);
const shiftedTd = (selection.timeDimensions ?? []).map((t) =>
t.dimension === cmp.dimension ? { ...t, dateRange: shifted } : t,
);
const sub = await this.service.query({
cube: compiled.cube.name,
measures,
dimensions,
where: baseFilter as Record<string, unknown> | undefined,
timeDimensions: shiftedTd,
timezone: selection.timezone ?? 'UTC',
}, context);
// Rename measure columns to `<measure>__compare` so they merge alongside primary.
return sub.rows.map((row) => {
const out: Record<string, unknown> = {};
for (const dim of dimensions) out[dim] = row[dim];
for (const m of measures) out[`${m}__compare`] = row[m];
return out;
});
}
}
/**
* Left-merge `extra` rows onto `base` rows by their dimension-key tuple,
* copying the listed value columns. Rows in `extra` with no base match are
* appended (outer-ish merge so comparison-only buckets still surface).
*/
export function mergeByDimensions(
base: Record<string, unknown>[],
extra: Record<string, unknown>[],
dimensions: string[],
valueColumns: string[],
): Record<string, unknown>[] {
const keyOf = (row: Record<string, unknown>) => dimensions.map((d) => String(row[d] ?? '')).join('');
const index = new Map<string, Record<string, unknown>>();
for (const row of base) index.set(keyOf(row), row);
for (const row of extra) {
const key = keyOf(row);
const target = index.get(key);
if (target) {
for (const c of valueColumns) target[c] = row[c];
} else {
const fresh: Record<string, unknown> = {};
for (const d of dimensions) fresh[d] = row[d];
for (const c of valueColumns) fresh[c] = row[c];
index.set(key, fresh);
base.push(fresh);
}
}
return base;
}