@sprigr/apps-reporting-core 0.0.5 → 0.0.6
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 +1 -1
- package/dist/index.js +66 -20
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -78,7 +78,7 @@ Modes `total`, `period`, `variance`, `period_variance`, `transaction`, `stream`,
|
|
|
78
78
|
- **Transaction** lists the fact rows: the date as text, every dimension code of the stream, the stream's extra `columns`, and each measure as that row holds it (a count is 1).
|
|
79
79
|
- **Stream** puts measures from more than one stream side by side over the whole period, such as invoiced beside ordered. It reads the view's own stream and every other stream its measures belong to. Each stream is grouped on its own under the same drill path, filters and keyword, then the results are folded to one row per member, so a formula can read sums from two streams. A member is listed when any of those streams has rows for it. Every stream read must carry each dimension the view uses. `streams` in the result lists them.
|
|
80
80
|
- **A total view with a measure of another stream** compiles exactly as stream mode does, so a view saved as a total of stock beside sales needs no change of mode.
|
|
81
|
-
- **A period or variance view with a measure of another stream** is compiled the same way: each stream is grouped on its own, over its own date column, then the results are folded to one row per member. A period view gets one column per period for every measure,
|
|
81
|
+
- **A period, variance or period variance view with a measure of another stream** is compiled the same way: each stream is grouped on its own, over its own date column, then the results are folded to one row per member. A period view gets one column per period for every measure, a variance its current, earlier, variance and variance percentage columns, and a period variance those four for every period beside the same period of the comparison window, all named as in a view of one stream. A formula is worked out for each period from the folded sums, with that period's own days. A snapshot measure reads the latest day of each period, or of each window, in its own stream. Every stream read must carry each dimension the view uses, `streams` in the result lists them, and `compileRollupView` reads a rollup of each. The statement is always one column per period: the long shape is refused, and so is a view too wide for the route. A custom view reads one through its points, below.
|
|
82
82
|
- **Matrix** groups by two dimensions: `group_by` gives the rows and `across` the columns. The result is long, one row per cell, and the grid pivots it. `__row` and `__col` say where a cell goes (0 is a total), `__total = 1` marks a column total and `__across_total = 1` a row total. Cells, row totals, column totals and the grand total come from one pass, each worked out from its own sums. Rows are sorted, paged and filtered on a measure by their row totals. `across.limit` keeps the first columns (12 unless given), picked by the first measure's column total or by code. Row totals and column totals are always returned, the column totals because they name the columns; the grand total comes with `options.total_row`. A page holds fewer rows than in other modes, since each row costs one result row per column kept and the column totals take one each; a `limit` above that is cut down, and `limit` in the result says what a page holds.
|
|
83
83
|
- **Custom** builds each column on its own. `custom_columns` lists them and `measures` is not read. A column is one of three things. A data point: `point` names a measure, and may give its own `period` (the view's period when left out) and a `where` rule on one dimension, such as sales of one department last June. A comparison: `point` and `against`, shown as the difference or, with `show: 'percent'`, as a fraction of the second point; a blank counts as 0 and the percentage is blank when there is nothing to compare against, as in a variance. An expression: a formula over the column's own `points`, named by the letters the formula uses, with a `format` (`number` unless given). `hidden` keeps a column out of the result; it can still be sorted on. `sort.by` takes `code`, `property.<key>` or `column.<key>`, and the first shown column, descending, when left out. Every distinct data point is read once, in one pass over the rows of every window the columns use. Result columns are named `c__<key>`. `windows` in the result lists those windows and `window` runs from the earliest to the latest. The drill path, dimension filters, keyword, properties, the total, sum and average rows and hiding zero rows work as in total mode, the last three over the shown columns. A snapshot measure reads the latest day of its own point's window. A point may read a measure of another stream, such as stock on hand beside sales: each stream is then grouped in its own stage, over only the windows its points use, under the same drill path, filters and keyword, and the stages are folded to one row per member as in stream mode. The view's own stream is always read, every stream read must carry each dimension the view uses, and `streams` in the result lists them. A formula measure is read on the stream of the first measure it names, so one that mixes streams still refuses with `measure_not_on_stream`; put each stream's figure in its own point and combine them in an expression column. Nested rows are refused across streams.
|
|
84
84
|
- **A period view that is too wide** for one column per period (the route takes 24,000 characters of SQL) is returned long instead: the matrix result with the periods as its columns, `shape: 'long'`, and `across` holding the period's number from the oldest. A period no row falls in has no cell. A total is worked out over the whole window, so `DAYS()` and a snapshot measure read the window there and the period on a cell. `sort.by` can still name one period of a shown measure. `periodShape` in the context forces `wide` or `long`; left out, the compiler goes long only when it must. A page holds fewer rows (153 with 12 periods), and `limit` in the result says how many.
|
package/dist/index.js
CHANGED
|
@@ -1191,12 +1191,11 @@ function compileMode(model, view, ctx) {
|
|
|
1191
1191
|
if (view.mode === "stream") return compileStreams(model, view, ctx);
|
|
1192
1192
|
if (view.mode === "matrix") return compileMatrix(model, view, ctx);
|
|
1193
1193
|
if (view.mode === "custom") return compileCustom(model, view, ctx);
|
|
1194
|
-
|
|
1195
|
-
const total = view.mode === "total";
|
|
1194
|
+
const direct = view.mode === "total" || view.mode === "period_variance";
|
|
1196
1195
|
try {
|
|
1197
|
-
return
|
|
1196
|
+
return direct ? compileGrouped(model, view, ctx) : compileOneStream(model, view, ctx);
|
|
1198
1197
|
} catch (err) {
|
|
1199
|
-
if (!(err instanceof ReportingError) || err.reason !== "measure_not_on_stream" || !
|
|
1198
|
+
if (!(err instanceof ReportingError) || err.reason !== "measure_not_on_stream" || !direct && ctx.periodShape === "long") throw err;
|
|
1200
1199
|
return compileStreams(model, view, ctx);
|
|
1201
1200
|
}
|
|
1202
1201
|
}
|
|
@@ -1596,19 +1595,26 @@ function comparedWith(model, view, ctx, window) {
|
|
|
1596
1595
|
previous: whole("previous", compare === "previous_year" ? "Previous year" : compare === "previous_period" ? "Previous period" : "Comparison", comparison)
|
|
1597
1596
|
};
|
|
1598
1597
|
}
|
|
1599
|
-
function factAcross(stream, range, buckets, comparison, latest) {
|
|
1598
|
+
function factAcross(stream, range, buckets, comparison, latest, earlier = []) {
|
|
1600
1599
|
if (buckets.length === 0 && !comparison) return factOver(stream, range, latest);
|
|
1601
1600
|
const table = quoteIdent(stream.table);
|
|
1602
1601
|
const date = quoteIdent(stream.date);
|
|
1603
1602
|
const inWindow = (w) => `${date} >= ${sqlDate(w.from)} AND ${date} < ${sqlDate(w.to)}`;
|
|
1604
1603
|
if (!comparison) {
|
|
1605
|
-
const
|
|
1606
|
-
const columns2 = `, ${
|
|
1607
|
-
return latest ? withLatestDays(table, date,
|
|
1604
|
+
const numbered2 = perBucket(date, buckets, (_, i) => i + 1);
|
|
1605
|
+
const columns2 = `, ${numbered2} AS "__b"`;
|
|
1606
|
+
return latest ? withLatestDays(table, date, numbered2, columns2, inWindow(range)) : `(SELECT *${columns2} FROM ${table} WHERE ${inWindow(range)}) AS ${FACT}`;
|
|
1608
1607
|
}
|
|
1609
|
-
const
|
|
1608
|
+
const byPeriod = earlier.length > 0;
|
|
1609
|
+
const numbered = (w, parts2) => `CASE WHEN ${inWindow(w)} THEN ${perBucket(date, parts2, (_, i) => i + 1)} END`;
|
|
1610
|
+
const periods = byPeriod ? `, ${numbered(range, buckets)} AS "__b", ${numbered(comparison, earlier)} AS "__pb"` : "";
|
|
1611
|
+
const columns = `${periods}, (${inWindow(range)}) AS "__cur", (${inWindow(comparison)}) AS "__prev"`;
|
|
1610
1612
|
const where = `(${inWindow(range)}) OR (${inWindow(comparison)})`;
|
|
1611
|
-
|
|
1613
|
+
const periodOf = (parts2) => perBucket(date, parts2, (_, i) => i + 1);
|
|
1614
|
+
return latest ? withLatestDayOfEach(table, date, columns, where, [
|
|
1615
|
+
{ where: inWindow(range), flag: "__lastc", ...byPeriod ? { periods: { by: periodOf(buckets), flag: "__last" } } : {} },
|
|
1616
|
+
{ where: inWindow(comparison), flag: "__lastp", ...byPeriod ? { periods: { by: periodOf(earlier), flag: "__plast" } } : {} }
|
|
1617
|
+
]) : `(SELECT *${columns} FROM ${table} WHERE ${where}) AS ${FACT}`;
|
|
1612
1618
|
}
|
|
1613
1619
|
function perBucket(date, buckets, value) {
|
|
1614
1620
|
return `CASE ${buckets.map((b, i) => `WHEN ${date} < ${sqlDate(b.to)} THEN ${value(b, i)}`).join(" ")} END`;
|
|
@@ -2214,15 +2220,24 @@ function compileStreams(model, view, ctx) {
|
|
|
2214
2220
|
const unseen = lead.filterMeasures.filter((key) => !view.measures.includes(key)).map((key) => measureByKey.get(key) ?? fail("unknown_measure", `no measure "${key}"`));
|
|
2215
2221
|
const period = resolvePeriod(view.period, model.periods, ctx.today, { periodToDate: options.period_to_date === true });
|
|
2216
2222
|
const range = period.window;
|
|
2217
|
-
const
|
|
2218
|
-
const
|
|
2223
|
+
const periodVariance = view.mode === "period_variance";
|
|
2224
|
+
const buckets = view.mode === "period" || periodVariance ? period.buckets : [];
|
|
2225
|
+
const two = view.mode === "variance" || periodVariance ? comparedWith(model, view, ctx, range) : null;
|
|
2226
|
+
const earlierBuckets = periodVariance && two ? splitWindow(two.comparison, period.grain, model.periods) : [];
|
|
2227
|
+
if (periodVariance && earlierBuckets.length !== buckets.length) {
|
|
2228
|
+
fail("option_unsupported", `the period splits into ${buckets.length} columns and the comparison into ${earlierBuckets.length}, so they cannot be set side by side`);
|
|
2229
|
+
}
|
|
2219
2230
|
const whole = two ? { prefix: "a", days: two.current.days, filter: `${FACT}."__cur"`, last: `${FACT}."__lastc"` } : { prefix: "a", days: daysBetween(range.from, range.to), filter: null, last: `${FACT}."__lastw"` };
|
|
2220
2231
|
const earlier = two ? { prefix: "ap", days: two.previous.days, filter: `${FACT}."__prev"`, last: `${FACT}."__lastp"` } : null;
|
|
2221
|
-
const perPeriod = buckets.map((bucket, i) =>
|
|
2222
|
-
|
|
2223
|
-
|
|
2224
|
-
|
|
2225
|
-
|
|
2232
|
+
const perPeriod = buckets.map((bucket, i) => {
|
|
2233
|
+
const back = earlierBuckets[i];
|
|
2234
|
+
return {
|
|
2235
|
+
bucket,
|
|
2236
|
+
part: { prefix: `a${i + 1}`, days: bucket.days, filter: `${FACT}."__b" = ${i + 1}`, last: `${FACT}."__b" = ${i + 1} AND ${FACT}."__last"` },
|
|
2237
|
+
index: i + 1,
|
|
2238
|
+
before: back ? { bucket: back, part: { prefix: `p${i + 1}`, days: back.days, filter: `${FACT}."__pb" = ${i + 1}`, last: `${FACT}."__pb" = ${i + 1} AND ${FACT}."__plast"` } } : null
|
|
2239
|
+
};
|
|
2240
|
+
});
|
|
2226
2241
|
const needed = /* @__PURE__ */ new Map();
|
|
2227
2242
|
const parsed = /* @__PURE__ */ new Map();
|
|
2228
2243
|
const exprOf = (key, part, depth) => {
|
|
@@ -2266,8 +2281,39 @@ function compileStreams(model, view, ctx) {
|
|
|
2266
2281
|
const addMeasure = (measure, seen) => {
|
|
2267
2282
|
const about = { measure: measure.key, format: measure.format };
|
|
2268
2283
|
if (seen) {
|
|
2269
|
-
display.forEach(({ bucket, part, index }) => {
|
|
2270
|
-
|
|
2284
|
+
display.forEach(({ bucket, part, index, before }) => {
|
|
2285
|
+
const name = `b${index}__${measure.key}`;
|
|
2286
|
+
put(
|
|
2287
|
+
select,
|
|
2288
|
+
name,
|
|
2289
|
+
figure(measure.key, part),
|
|
2290
|
+
true,
|
|
2291
|
+
{ ...about, label: `${measure.label} ${bucket.label}`, period: bucket, ...before ? { part: "current", bucket: index } : {} }
|
|
2292
|
+
);
|
|
2293
|
+
if (!before) return;
|
|
2294
|
+
const earlierName = `pb${index}__${measure.key}`;
|
|
2295
|
+
const group = { ...about, bucket: index };
|
|
2296
|
+
put(
|
|
2297
|
+
select,
|
|
2298
|
+
earlierName,
|
|
2299
|
+
figure(measure.key, before.part),
|
|
2300
|
+
true,
|
|
2301
|
+
{ ...group, label: `${measure.label} ${before.bucket.label}`, period: before.bucket, part: "previous" }
|
|
2302
|
+
);
|
|
2303
|
+
put(
|
|
2304
|
+
derived,
|
|
2305
|
+
`bv${index}__${measure.key}`,
|
|
2306
|
+
`COALESCE(${quoteIdent(name)}, 0) - COALESCE(${quoteIdent(earlierName)}, 0)`,
|
|
2307
|
+
true,
|
|
2308
|
+
{ ...group, label: `${measure.label} ${bucket.label} Variance`, period: bucket, part: "variance" }
|
|
2309
|
+
);
|
|
2310
|
+
put(
|
|
2311
|
+
derived,
|
|
2312
|
+
`bvp${index}__${measure.key}`,
|
|
2313
|
+
`(COALESCE(${quoteIdent(name)}, 0) - ${quoteIdent(earlierName)}) / NULLIF(abs(${quoteIdent(earlierName)}), 0)`,
|
|
2314
|
+
true,
|
|
2315
|
+
{ ...group, format: "percent", label: `${measure.label} ${bucket.label} Variance %`, period: bucket, part: "variance_pct" }
|
|
2316
|
+
);
|
|
2271
2317
|
});
|
|
2272
2318
|
}
|
|
2273
2319
|
const wholeName = `m__${measure.key}`;
|
|
@@ -2334,7 +2380,7 @@ function compileStreams(model, view, ctx) {
|
|
|
2334
2380
|
return {
|
|
2335
2381
|
select: part,
|
|
2336
2382
|
// A snapshot measure reads its own stream's latest day in each set of dates.
|
|
2337
|
-
from: factAcross(stream, range, buckets, two ? two.comparison : null, mine.some(({ measure }) => measure.snapshot)),
|
|
2383
|
+
from: factAcross(stream, range, buckets, two ? two.comparison : null, mine.some(({ measure }) => measure.snapshot), earlierBuckets),
|
|
2338
2384
|
joins: [...scope.joins.values()],
|
|
2339
2385
|
where: scope.where,
|
|
2340
2386
|
groupBy: totalRow ? `GROUP BY GROUPING SETS ((${codeExpr}), ())` : `GROUP BY ${codeExpr}`
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sprigr/apps-reporting-core",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.6",
|
|
4
4
|
"description": "Pure reporting core: a model of dimensions and measures, a formula parser, a period engine, and a compiler from a saved view to one DuckDB statement.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|