report-studio 1.2.2 → 1.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +254 -3
- package/dist/designer-Dj330bWd.js +4690 -0
- package/dist/designer.js +1 -1
- package/dist/index.js +2 -2
- package/dist/react-designer.js +1 -1
- package/dist/react.js +1 -1
- package/dist/viewer-BewnkhfP.js +4028 -0
- package/package.json +2 -2
- package/src/designer/blank.js +0 -1
- package/src/designer/canvas.js +172 -54
- package/src/designer/designer.js +313 -12
- package/src/designer/dialog.js +609 -5
- package/src/designer/fields.js +128 -11
- package/src/designer/geometry.js +77 -4
- package/src/designer/grouping.js +556 -0
- package/src/designer/icons.js +9 -0
- package/src/designer/menu.js +50 -6
- package/src/designer/panel.js +478 -38
- package/src/designer/rulers.js +14 -6
- package/src/designer/sample-data.js +112 -31
- package/src/designer/select.js +3 -2
- package/src/designer/structure.js +126 -12
- package/src/designer/styles.css +661 -27
- package/src/designer/tokens.js +38 -11
- package/src/engine/calculate.js +487 -0
- package/src/engine/csv.js +17 -9
- package/src/engine/format.js +483 -0
- package/src/engine/group.js +350 -70
- package/src/engine/highlight.js +224 -0
- package/src/engine/index.js +8 -1
- package/src/engine/measure.js +53 -26
- package/src/engine/paginate.js +189 -92
- package/src/engine/resolve.js +117 -10
- package/src/engine/rows.js +71 -8
- package/src/engine/search.js +12 -4
- package/src/engine/tables.js +309 -11
- package/src/engine/validate.js +265 -8
- package/src/engine/xlsx-style.js +50 -4
- package/src/engine/xlsx.js +91 -21
- package/src/index.js +18 -0
- package/src/render/items.js +61 -10
- package/src/render/report.css +0 -5
- package/types/index.d.ts +96 -8
- package/types/layout.d.ts +156 -2
- package/dist/designer-DQlfKnTr.js +0 -3416
- package/dist/viewer-BSPNCNRU.js +0 -2912
package/src/engine/group.js
CHANGED
|
@@ -1,11 +1,14 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* group.js binds every table to its rows, partitions
|
|
2
|
+
* group.js binds every table to its rows, partitions each grouped one, and
|
|
3
3
|
* resolves aggregate placeholders.
|
|
4
4
|
*
|
|
5
5
|
* An aggregate has to be worked out over some set of rows, and there are only
|
|
6
6
|
* two sets a band can mean:
|
|
7
7
|
*
|
|
8
|
-
* groupHeader / groupFooter - that group's rows, and only that group's.
|
|
8
|
+
* groupHeader / groupFooter - that group's rows, and only that group's. Each
|
|
9
|
+
* grouped table has its own pair for each level it is grouped at, carried
|
|
10
|
+
* on the table as `grouping`. A region footer means the region's rows; a
|
|
11
|
+
* city footer inside it means that city's.
|
|
9
12
|
* They are templates rather than bands: one copy is drawn per group, so
|
|
10
13
|
* each copy has a different answer. Filled in per fragment by paginate.js,
|
|
11
14
|
* where which group is being drawn is known.
|
|
@@ -19,43 +22,59 @@
|
|
|
19
22
|
*/
|
|
20
23
|
|
|
21
24
|
import { ReportError } from './validate.js';
|
|
22
|
-
import { tablesOf, datasetOf,
|
|
25
|
+
import { tablesOf, datasetOf, groupingsOf, normalizeGrouping } from './tables.js';
|
|
26
|
+
import { formatValue, percentFormat, toNumber } from './format.js';
|
|
27
|
+
import { soleKey } from './resolve.js';
|
|
28
|
+
import { highlightFor } from './highlight.js';
|
|
29
|
+
import { calculatedRows } from './calculate.js';
|
|
23
30
|
|
|
24
31
|
|
|
25
32
|
const REGEX = /\{([^{}]+)\}/g;
|
|
26
33
|
|
|
27
34
|
|
|
28
35
|
export function group(resolvedJson, data) {
|
|
29
|
-
|
|
30
|
-
const bands = json.bands;
|
|
31
|
-
const tables = tablesOf(json);
|
|
32
|
-
|
|
33
|
-
if (json.groupBy != null && tables.length === 0) {
|
|
36
|
+
if (resolvedJson.groupBy != null && tablesOf(resolvedJson).length === 0) {
|
|
34
37
|
throw new ReportError(
|
|
35
|
-
`group: layout.groupBy is "${
|
|
38
|
+
`group: layout.groupBy is "${resolvedJson.groupBy}" but no band contains a table to group`
|
|
36
39
|
);
|
|
37
40
|
}
|
|
38
41
|
|
|
39
|
-
|
|
42
|
+
/**
|
|
43
|
+
* From here on grouping is said one way only - on each table - whichever
|
|
44
|
+
* way the layout file said it. normalizeGrouping also makes the copy this
|
|
45
|
+
* stage works on, so the layout handed in is left as it was.
|
|
46
|
+
*/
|
|
47
|
+
const json = normalizeGrouping(resolvedJson);
|
|
48
|
+
const bands = json.bands;
|
|
49
|
+
const tables = tablesOf(json);
|
|
50
|
+
|
|
51
|
+
const groupings = new Map(groupingsOf(json).map(g => [g.table, g]));
|
|
40
52
|
const named = namedDatasets(data);
|
|
41
53
|
|
|
42
54
|
/** the rows each table reads, bound as they are found */
|
|
43
55
|
const bound = tables.map(table => {
|
|
44
|
-
|
|
56
|
+
/**
|
|
57
|
+
* Calculated columns first: from here on a calculated value is a field
|
|
58
|
+
* of the row like any other - grouped and sorted by, totalled by
|
|
59
|
+
* {sum(amount)} in any band, formatted, highlighted and exported.
|
|
60
|
+
*/
|
|
61
|
+
const rows = calculatedRows(table, datasetFor(table, json, data));
|
|
45
62
|
named.set(datasetOf(table, json), rows);
|
|
46
63
|
return { table, rows };
|
|
47
64
|
});
|
|
48
65
|
|
|
49
66
|
for (const { table, rows } of bound) {
|
|
50
|
-
|
|
51
|
-
|
|
67
|
+
const grouping = groupings.get(table);
|
|
68
|
+
|
|
69
|
+
if (grouping) {
|
|
70
|
+
bindGroups(table, rows, grouping, datasetOf(table, json));
|
|
52
71
|
} else {
|
|
53
72
|
table.row = rows;
|
|
54
73
|
}
|
|
55
74
|
}
|
|
56
75
|
|
|
57
76
|
/** a report of nothing but text bands is legal - there is simply nothing to bind */
|
|
58
|
-
const scope = { rows: bound[0]?.rows ?? [], named };
|
|
77
|
+
const scope = { rows: bound[0]?.rows ?? [], table: bound[0]?.table ?? null, named, data };
|
|
59
78
|
|
|
60
79
|
aggregate(bands, scope);
|
|
61
80
|
return json;
|
|
@@ -63,23 +82,102 @@ export function group(resolvedJson, data) {
|
|
|
63
82
|
|
|
64
83
|
|
|
65
84
|
/**
|
|
66
|
-
* Splits
|
|
85
|
+
* Splits one grouped table's rows into groups, each carrying its own totals.
|
|
86
|
+
*
|
|
87
|
+
* The totals are worked out from this table's own group bands over this
|
|
88
|
+
* table's own rows. With one grouped table that was a distinction without a
|
|
89
|
+
* difference; with two, reading the other table's bands is how a subtotal of
|
|
90
|
+
* refunds would come to be summed over orders.
|
|
91
|
+
*
|
|
92
|
+
* A table grouped at more than one level - by region, and within each region
|
|
93
|
+
* by city - still comes out as one flat list, of its innermost groups, in the
|
|
94
|
+
* order they are drawn. Every stage after this one walks `groups` as a list,
|
|
95
|
+
* and a tree would have had each of them learn to walk it. What a flat list
|
|
96
|
+
* has to say instead is where each outer group starts and ends, so each
|
|
97
|
+
* innermost group carries:
|
|
98
|
+
*
|
|
99
|
+
* key its own group's key, as a one-level group always has
|
|
100
|
+
* path the key at every level, outermost first: ['North', 'Oslo']
|
|
101
|
+
* rows its rows
|
|
102
|
+
* aggregates its own level's totals, as a one-level group always has
|
|
103
|
+
* levels one entry per level, outermost first: { by, key, aggregates },
|
|
104
|
+
* each level's totals over that level's whole group - the
|
|
105
|
+
* region's footer sums the region, not the city it ends with
|
|
106
|
+
* opens the outermost level whose group starts with this one; it and
|
|
107
|
+
* every level inside it print their headers here
|
|
108
|
+
* closes the outermost level whose group ends with this one; footers
|
|
109
|
+
* print from the innermost out to it
|
|
110
|
+
*
|
|
111
|
+
* A one-level group opens and closes level 0, always.
|
|
112
|
+
*
|
|
67
113
|
* @param {object} table
|
|
68
114
|
* @param {object[]} rows
|
|
69
|
-
* @param {
|
|
70
|
-
* @param {object[]} bands
|
|
115
|
+
* @param {{levels: {by: string, header: object|null, footer: object|null}[]}} grouping
|
|
71
116
|
* @param {string|null} dataset the table's dataset name, which a group band's
|
|
72
117
|
* aggregate may carry as a prefix
|
|
73
118
|
*/
|
|
74
|
-
function bindGroups(table, rows,
|
|
75
|
-
const
|
|
76
|
-
const
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
119
|
+
function bindGroups(table, rows, grouping, dataset) {
|
|
120
|
+
const levels = grouping.levels;
|
|
121
|
+
const leaves = [];
|
|
122
|
+
|
|
123
|
+
/** splits `rows` by one level, and each part by the next, down to the last */
|
|
124
|
+
/** what a {percentage()} is a share of: the whole table, or the group outside */
|
|
125
|
+
const all = rows;
|
|
126
|
+
|
|
127
|
+
const split = (rows, depth, outer) => {
|
|
128
|
+
const level = levels[depth];
|
|
129
|
+
const bands = [level.header, level.footer].filter(Boolean);
|
|
130
|
+
|
|
131
|
+
for (const [key, groupRows] of sortedGroups(groupedRows(rows, level.by), level.sort)) {
|
|
132
|
+
const node = {
|
|
133
|
+
by: level.by,
|
|
134
|
+
key,
|
|
135
|
+
aggregates: groupAggregates(bands, groupRows, dataset, { all, parent: rows, table })
|
|
136
|
+
};
|
|
137
|
+
const chain = [...outer, node];
|
|
138
|
+
|
|
139
|
+
if (depth < levels.length - 1) {
|
|
140
|
+
split(groupRows, depth + 1, chain);
|
|
141
|
+
continue;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
leaves.push({
|
|
145
|
+
key,
|
|
146
|
+
path: chain.map(n => n.key),
|
|
147
|
+
rows: groupRows,
|
|
148
|
+
aggregates: node.aggregates,
|
|
149
|
+
levels: chain
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
};
|
|
153
|
+
|
|
154
|
+
split(rows, 0, []);
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Two neighbouring groups share an outer group for as long as their chains
|
|
158
|
+
* hold the same node - the same object, not merely an equal key.
|
|
159
|
+
*/
|
|
160
|
+
leaves.forEach((leaf, n) => {
|
|
161
|
+
leaf.opens = firstDifference(leaves[n - 1], leaf);
|
|
162
|
+
leaf.closes = firstDifference(leaves[n + 1], leaf);
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
table.groups = leaves;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* The outermost level at which a neighbouring group is in a different group
|
|
171
|
+
* from this one - 0 when there is no neighbour, as at either end of the table.
|
|
172
|
+
* @param {object|undefined} other
|
|
173
|
+
* @param {object} leaf
|
|
174
|
+
* @returns {number}
|
|
175
|
+
*/
|
|
176
|
+
function firstDifference(other, leaf) {
|
|
177
|
+
if (!other) return 0;
|
|
178
|
+
|
|
179
|
+
const depth = leaf.levels.findIndex((node, n) => other.levels[n] !== node);
|
|
180
|
+
return depth === -1 ? leaf.levels.length - 1 : depth;
|
|
83
181
|
}
|
|
84
182
|
|
|
85
183
|
|
|
@@ -157,7 +255,15 @@ function aggregate(bands, scope) {
|
|
|
157
255
|
* standing; re-resolving from `value` here would discard its work
|
|
158
256
|
* and leave "{authorizer}" printed literally on the report.
|
|
159
257
|
*/
|
|
160
|
-
item.text = fillAggregates(item.text ?? item.value, scope);
|
|
258
|
+
item.text = fillAggregates(item.text ?? item.value, scope, item);
|
|
259
|
+
|
|
260
|
+
/**
|
|
261
|
+
* Every value the item prints is known now - its placeholders were
|
|
262
|
+
* filled by resolve.js, its totals just here - so its highlight
|
|
263
|
+
* rules can be asked. A rule naming a field reads the data.
|
|
264
|
+
*/
|
|
265
|
+
const lit = highlightFor(item.highlight, shownValue(item), scope.data);
|
|
266
|
+
if (lit) item.highlighted = lit;
|
|
161
267
|
}
|
|
162
268
|
}
|
|
163
269
|
}
|
|
@@ -170,16 +276,41 @@ function aggregate(bands, scope) {
|
|
|
170
276
|
* as they are.
|
|
171
277
|
* @param {string} value
|
|
172
278
|
* @param {{rows: object[], named: Map<string, object[]>}} scope
|
|
279
|
+
* @param {object} item the text item, whose format the answer is printed in -
|
|
280
|
+
* and which keeps the answer as `raw` when it is the item's only placeholder
|
|
173
281
|
* @returns {string}
|
|
174
282
|
*/
|
|
175
|
-
function fillAggregates(value, scope) {
|
|
283
|
+
function fillAggregates(value, scope, item) {
|
|
284
|
+
const sole = soleKey(item.value);
|
|
285
|
+
|
|
176
286
|
return value.replace(REGEX, (match, key) => {
|
|
177
287
|
const expr = parseAggregate(key.trim());
|
|
178
288
|
if (!expr) return match;
|
|
179
289
|
|
|
180
|
-
const
|
|
181
|
-
|
|
182
|
-
|
|
290
|
+
const isShare = expr.key === PERCENTAGE;
|
|
291
|
+
let result;
|
|
292
|
+
|
|
293
|
+
if (isShare) {
|
|
294
|
+
/** outside a group, a share is of the whole: every scope is the dataset */
|
|
295
|
+
const measure = (which, fn, field) => {
|
|
296
|
+
const target = scoped(field, scope);
|
|
297
|
+
return compute({ key: fn, field: target.field }, target.rows);
|
|
298
|
+
};
|
|
299
|
+
result = percentage(expr.field, measure, () => measureField(scope.table, scope.rows));
|
|
300
|
+
} else {
|
|
301
|
+
const { rows, field } = scoped(expr.field, scope);
|
|
302
|
+
result = compute({ key: expr.key, field }, rows);
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
if (sole === key.trim()) {
|
|
306
|
+
item.raw = result;
|
|
307
|
+
if (isShare) item.rawFormat = percentFormat(item.format);
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
/** a count is a count of rows, whatever the item's format says about money */
|
|
311
|
+
if (result == null) return '';
|
|
312
|
+
if (isShare) return formatValue(result, percentFormat(item.format));
|
|
313
|
+
return formatValue(result, expr.key === 'count' ? null : item.format);
|
|
183
314
|
});
|
|
184
315
|
}
|
|
185
316
|
|
|
@@ -215,6 +346,117 @@ function scoped(field, scope) {
|
|
|
215
346
|
}
|
|
216
347
|
|
|
217
348
|
|
|
349
|
+
/** the aggregate that is a share of a total rather than a figure of its own */
|
|
350
|
+
export const PERCENTAGE = 'percentage';
|
|
351
|
+
|
|
352
|
+
|
|
353
|
+
/**
|
|
354
|
+
* {percentage(part)} or {percentage(part, of)} - a value as a share of another,
|
|
355
|
+
* as a fraction: 0.5708 prints as 57.08%.
|
|
356
|
+
*
|
|
357
|
+
* part an aggregate - sum(amount), count(), avg(price) - over the rows in
|
|
358
|
+
* scope, or a plain number: 1000
|
|
359
|
+
* of what it is a share of. Left out, it is the whole table's total: the
|
|
360
|
+
* same aggregate over every row for an aggregate part, and the sum of
|
|
361
|
+
* the table's money column for a number. Or a number - a target, such
|
|
362
|
+
* as 50000 - an aggregate over every row, or `parent`: the same over
|
|
363
|
+
* the group outside this one.
|
|
364
|
+
*
|
|
365
|
+
* Nothing is a share of zero or of nothing: that is blank, never Infinity%.
|
|
366
|
+
*
|
|
367
|
+
* @param {string} args what is inside the brackets
|
|
368
|
+
* @param {(which: 'part'|'all'|'parent', fn: string, field: string) => number|null} measure
|
|
369
|
+
* an aggregate over this group's rows, the table's or the outer group's
|
|
370
|
+
* @param {() => string|null} moneyField the field a plain number is a share of
|
|
371
|
+
* @returns {number|null}
|
|
372
|
+
*/
|
|
373
|
+
function percentage(args, measure, moneyField) {
|
|
374
|
+
const [partText, ofText] = splitArguments(args);
|
|
375
|
+
const part = term(partText);
|
|
376
|
+
if (!part || part.kind === 'parent') return null;
|
|
377
|
+
|
|
378
|
+
/** what "the whole" is made of when only the part is given, or `parent` is */
|
|
379
|
+
const whole = part.kind === 'aggregate'
|
|
380
|
+
? { fn: part.fn, field: part.field }
|
|
381
|
+
: { fn: 'sum', field: moneyField() };
|
|
382
|
+
|
|
383
|
+
const partValue = part.kind === 'number' ? part.value : measure('part', part.fn, part.field);
|
|
384
|
+
|
|
385
|
+
const of = ofText === undefined ? { kind: 'all' } : term(ofText);
|
|
386
|
+
if (!of) return null;
|
|
387
|
+
|
|
388
|
+
let ofValue = null;
|
|
389
|
+
if (of.kind === 'number') ofValue = of.value;
|
|
390
|
+
else if (of.kind === 'aggregate') ofValue = measure('all', of.fn, of.field);
|
|
391
|
+
else if (whole.fn === 'count' || whole.field) {
|
|
392
|
+
ofValue = measure(of.kind === 'parent' ? 'parent' : 'all', whole.fn, whole.field);
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
if (partValue == null || ofValue == null || ofValue === 0) return null;
|
|
396
|
+
return round(partValue / ofValue);
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
|
|
400
|
+
/** one argument of percentage(): a number, `parent`, or an aggregate */
|
|
401
|
+
function term(text) {
|
|
402
|
+
const t = (text ?? '').trim();
|
|
403
|
+
if (t === '') return null;
|
|
404
|
+
if (t === 'parent') return { kind: 'parent' };
|
|
405
|
+
|
|
406
|
+
const number = toNumber(t);
|
|
407
|
+
if (number !== null) return { kind: 'number', value: number };
|
|
408
|
+
|
|
409
|
+
const expr = parseAggregate(t);
|
|
410
|
+
if (!expr || expr.key === PERCENTAGE) return null;
|
|
411
|
+
return { kind: 'aggregate', fn: expr.key, field: expr.field };
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
|
|
415
|
+
/** "sum(amount), 50000" into its two arguments, a comma inside brackets being no split */
|
|
416
|
+
function splitArguments(args) {
|
|
417
|
+
let depth = 0;
|
|
418
|
+
|
|
419
|
+
for (let i = 0; i < args.length; i++) {
|
|
420
|
+
if (args[i] === '(') depth++;
|
|
421
|
+
else if (args[i] === ')') depth--;
|
|
422
|
+
else if (args[i] === ',' && depth === 0) return [args.slice(0, i), args.slice(i + 1)];
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
return [args, undefined];
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
|
|
429
|
+
/**
|
|
430
|
+
* The field a plain-number share is of: the table's money column - the first
|
|
431
|
+
* formatted as currency, then as a number, then the last column that holds
|
|
432
|
+
* numbers, totals being the right-hand column of most tables.
|
|
433
|
+
* @param {object|null} table
|
|
434
|
+
* @param {object[]} rows
|
|
435
|
+
* @returns {string|null}
|
|
436
|
+
*/
|
|
437
|
+
function measureField(table, rows) {
|
|
438
|
+
const columns = Array.isArray(table?.columns) ? table.columns : [];
|
|
439
|
+
const formatted = (type) => columns.find(c => c?.format?.type === type)?.field;
|
|
440
|
+
const numeric = [...columns].reverse()
|
|
441
|
+
.find(c => (rows ?? []).some(row => toNumber(row?.[c?.field]) !== null));
|
|
442
|
+
|
|
443
|
+
return formatted('currency') ?? formatted('number') ?? numeric?.field ?? null;
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
|
|
447
|
+
/**
|
|
448
|
+
* The value a highlight rule tests for a text item: what it printed, as the
|
|
449
|
+
* reader reads it - a share as its percentage, 57.08 rather than 0.5708.
|
|
450
|
+
* @param {object} item
|
|
451
|
+
* @returns {*}
|
|
452
|
+
*/
|
|
453
|
+
export function shownValue(item) {
|
|
454
|
+
return item.rawFormat?.type === 'percent' && typeof item.raw === 'number'
|
|
455
|
+
? round(item.raw * 100)
|
|
456
|
+
: item.raw;
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
|
|
218
460
|
function parseAggregate(expr) {
|
|
219
461
|
const match = expr.match(/^(\w+)\((.*?)\)$/);
|
|
220
462
|
if (!match) return null;
|
|
@@ -226,13 +468,14 @@ function parseAggregate(expr) {
|
|
|
226
468
|
|
|
227
469
|
|
|
228
470
|
/**
|
|
229
|
-
*
|
|
230
|
-
* the
|
|
231
|
-
* other, or {sum(price)} collide with {sum(qty)}.
|
|
471
|
+
* One group's aggregates, keyed on the field the expression targets and then
|
|
472
|
+
* the function, so {sum(price)} does not collide with {sum(qty)}.
|
|
232
473
|
*
|
|
233
|
-
* Both group bands are read, not just the footer. "Region A -
|
|
234
|
-
* heading, and a count that worked in the footer and printed
|
|
235
|
-
* header is the same fault this file exists to have stopped
|
|
474
|
+
* Both of the level's group bands are read, not just the footer. "Region A -
|
|
475
|
+
* 12 orders" is a heading, and a count that worked in the footer and printed
|
|
476
|
+
* blank in the header is the same fault this file exists to have stopped
|
|
477
|
+
* having. Only that level's bands are read: a city footer's {avg(price)} is
|
|
478
|
+
* not worked out for regions that never print it.
|
|
236
479
|
*
|
|
237
480
|
* Keyed on the expression's argument as written, because that is what
|
|
238
481
|
* paginate.js looks it up by - so `{sum(sales.amount)}` in a group footer is
|
|
@@ -240,43 +483,39 @@ function parseAggregate(expr) {
|
|
|
240
483
|
* band only ever means its group's rows; naming the grouped dataset is allowed
|
|
241
484
|
* because it is the same thing said in full.
|
|
242
485
|
*
|
|
243
|
-
* Shape: aggregates[
|
|
244
|
-
* @param {object[]} bands
|
|
245
|
-
* @param {object}
|
|
486
|
+
* Shape: aggregates[field] = { count, sum, avg, min, max }
|
|
487
|
+
* @param {object[]} bands the level's own group bands
|
|
488
|
+
* @param {object[]} rows the group's rows
|
|
246
489
|
* @param {string|null} name the grouped table's dataset
|
|
490
|
+
* @param {{all?: object[], parent?: object[], table?: object}} [around] the
|
|
491
|
+
* table's rows and the outer group's, for {percentage()} to divide by
|
|
247
492
|
* @returns {object}
|
|
248
493
|
*/
|
|
249
|
-
function
|
|
494
|
+
function groupAggregates(bands, rows, name, around = {}) {
|
|
250
495
|
const aggregates = {};
|
|
251
496
|
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
}
|
|
497
|
+
const within = { part: rows, all: around.all ?? rows, parent: around.parent ?? around.all ?? rows };
|
|
498
|
+
const measure = (which, fn, field) =>
|
|
499
|
+
compute({ key: fn, field: withoutScope(field, name) }, within[which]);
|
|
256
500
|
|
|
257
501
|
for (const band of bands) {
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
for (const item of band.items) {
|
|
502
|
+
for (const item of band.items ?? []) {
|
|
261
503
|
if (item.type != "text") continue;
|
|
262
504
|
|
|
263
505
|
for (const expr of parseAll(item.value)) {
|
|
264
506
|
const field = withoutScope(expr.field, name);
|
|
265
507
|
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
aggregates[key][expr.field][expr.key] =
|
|
278
|
-
compute({ key: expr.key, field }, rows);
|
|
279
|
-
}
|
|
508
|
+
aggregates[expr.field] ??= {
|
|
509
|
+
count: null,
|
|
510
|
+
sum: null,
|
|
511
|
+
avg: null,
|
|
512
|
+
min: null,
|
|
513
|
+
max: null
|
|
514
|
+
};
|
|
515
|
+
|
|
516
|
+
aggregates[expr.field][expr.key] = expr.key === PERCENTAGE
|
|
517
|
+
? percentage(expr.field, measure, () => measureField(around.table, within.all))
|
|
518
|
+
: compute({ key: expr.key, field }, rows);
|
|
280
519
|
}
|
|
281
520
|
}
|
|
282
521
|
}
|
|
@@ -304,19 +543,60 @@ function parseAll(value) {
|
|
|
304
543
|
}
|
|
305
544
|
|
|
306
545
|
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
546
|
+
/**
|
|
547
|
+
* Rows split by one field, in the order each value first appears.
|
|
548
|
+
*
|
|
549
|
+
* A Map, not an object: an object lists integer-like keys first and in
|
|
550
|
+
* ascending order whatever order they were added in, so grouping by year put
|
|
551
|
+
* 2023 before 2024 even when the data ran newest first. Keys are still the
|
|
552
|
+
* field's value as text, which is what they have always been.
|
|
553
|
+
*
|
|
554
|
+
* @param {object[]} rows
|
|
555
|
+
* @param {string} by
|
|
556
|
+
* @returns {Map<string, object[]>}
|
|
557
|
+
*/
|
|
558
|
+
function groupedRows(rows, by) {
|
|
559
|
+
const grouped = new Map();
|
|
560
|
+
|
|
561
|
+
for (const row of rows) {
|
|
562
|
+
const key = String(row?.[by]);
|
|
563
|
+
if (!grouped.has(key)) grouped.set(key, []);
|
|
564
|
+
grouped.get(key).push(row);
|
|
315
565
|
}
|
|
566
|
+
|
|
316
567
|
return grouped;
|
|
317
568
|
}
|
|
318
569
|
|
|
319
570
|
|
|
571
|
+
/** compares group keys the way a person reads them: "Item 2" before "Item 10" */
|
|
572
|
+
const KEY_ORDER = new Intl.Collator(undefined, { numeric: true, sensitivity: 'base' });
|
|
573
|
+
|
|
574
|
+
|
|
575
|
+
/**
|
|
576
|
+
* One level's groups in the order its `sort` asks for: as first seen, when it
|
|
577
|
+
* asks for none; else by key, ascending or descending.
|
|
578
|
+
*
|
|
579
|
+
* Keys are compared as numbers when both are numbers - a key is the field's
|
|
580
|
+
* value as text, and as text "9" sorts after "10". Otherwise they are compared
|
|
581
|
+
* as words with their digits read as numbers, so an ISO date sorts by date. Two
|
|
582
|
+
* keys that compare equal keep the order they were first seen in.
|
|
583
|
+
*
|
|
584
|
+
* @param {Map<string, object[]>} groups
|
|
585
|
+
* @param {'asc'|'desc'|null|undefined} sort
|
|
586
|
+
* @returns {Iterable<[string, object[]]>}
|
|
587
|
+
*/
|
|
588
|
+
function sortedGroups(groups, sort) {
|
|
589
|
+
if (sort !== 'asc' && sort !== 'desc') return groups;
|
|
590
|
+
|
|
591
|
+
const direction = sort === 'desc' ? -1 : 1;
|
|
592
|
+
const numeric = (key) => key.trim() !== '' && Number.isFinite(Number(key));
|
|
593
|
+
|
|
594
|
+
return [...groups].sort(([a], [b]) => direction * (numeric(a) && numeric(b)
|
|
595
|
+
? Number(a) - Number(b)
|
|
596
|
+
: KEY_ORDER.compare(a, b)));
|
|
597
|
+
}
|
|
598
|
+
|
|
599
|
+
|
|
320
600
|
/**
|
|
321
601
|
* Runs one aggregate over one set of rows.
|
|
322
602
|
* Non-numeric cells are skipped rather than counted as zero - a blank price
|