@sprigr/apps-reporting-core 0.0.13 → 0.0.15
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 +2 -2
- package/dist/index.d.ts +13 -2
- package/dist/index.js +56 -17
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -84,10 +84,10 @@ Modes `total`, `period`, `variance`, `period_variance`, `transaction`, `stream`,
|
|
|
84
84
|
|
|
85
85
|
`__total` says what a result row is: 0 a member, 1 the total, 2 the others row, 3 the average row, 4 the sum row.
|
|
86
86
|
|
|
87
|
-
- **Variance** reads the period and a comparison window (`compare`: `previous_year` for the same dates a year earlier, `previous_period` for the period just before,
|
|
87
|
+
- **Variance** reads the period and a comparison window (`compare`: `previous_year` for the same dates a year earlier, `previous_period` for the period just before, `{ period }` for a window of its own, which may overlap the period or come after it, or `{ shift: { days, length_days } }` for the period moved by `days` and lasting `length_days`, its own length when left out, so it moves with the period: `{ shift: { days: -364 } }` is the same weekdays 52 weeks earlier, and `{ shift: { days: -9, length_days: 9 } }` the 9 days just before) in one pass. Each measure gets four columns: current, previous, variance and variance percentage (a fraction).
|
|
88
88
|
- **Period variance** is a period view where every period is a variance. Each period gets four columns: its own value, the value of the period at the same place in the comparison window, the variance and the variance percentage. The same four over the whole windows follow. `compare` is read as in a variance. The comparison window is cut with the period's grain, and the view is refused when the two windows do not cut into the same number of periods. Columns are named `b<n>__`, `pb<n>__`, `bv<n>__` and `bvp<n>__` before the measure key, `<n>` counted from the oldest period; each carries `bucket: n` and its `part`. `sort.by` and a filter on a measure take what a period view and a variance take. The statement is always one column per period, and a view too wide for the route is refused.
|
|
89
89
|
- **A filter on a dimension** tests fact rows, on the code or on a property. `in` and `not_in` match whole values. `contains` and `not_contains` look for text anywhere in the value, whatever its case; a rule with several texts passes when any one is found, and a text can be up to 200 characters. A row with no value passes `not_in` and `not_contains`. Rules sit in groups joined by `and` or `or`, up to three deep, so a search over a code or a name is an `or` group of two `contains` rules. A data point of a custom view takes the same rules.
|
|
90
|
-
- **A filter on a member's own figure** names a dimension and a measure together: `{ dimension, measure, cmp, value, period? }`. It keeps the fact rows of the members whose figure passes, worked out over the rule's own `period` (the view's when it gives none), so "products that sold more than 100 last year" can filter a view of this month. It sits with the dimension rules, in `and` and `or` groups and in every mode. The figure is over all of the rows of the measure's own stream in those dates, whatever else the view filters on, so "stock on hand over 0" can filter a view of sales. That stream must reach the rule's dimension, and a formula over two streams is refused. Each stream the view reads keeps the same members, and `compileRollupView` reads a rollup of the rule's stream too. A member with no rows on that stream has a sum or count of 0, as Phocas reads it, so "sales under 100" keeps products that sold nothing. Those members are listed from the dimension's table, or from the table of the dimension it hangs off, and a rule that 0 passes is refused on a dimension with neither. A snapshot figure, a formula and the other aggregates stay blank for such a member and pass no comparison. The result lists the dates each such rule reads in `ruleWindows`, and `months` covers them. A data point of a custom view does not take one.
|
|
90
|
+
- **A filter on a member's own figure** names a dimension and a measure together: `{ dimension, measure, cmp, value, period? }`. It keeps the fact rows of the members whose figure passes, worked out over the rule's own `period` (the view's when it gives none), so "products that sold more than 100 last year" can filter a view of this month. It sits with the dimension rules, in `and` and `or` groups and in every mode. The figure is over all of the rows of the measure's own stream in those dates, whatever else the view filters on, so "stock on hand over 0" can filter a view of sales. That stream must reach the rule's dimension, and a formula over two streams is refused. Each stream the view reads keeps the same members, and `compileRollupView` reads a rollup of the rule's stream too. A member with no rows on that stream has a sum or count of 0, as Phocas reads it, so "sales under 100" keeps products that sold nothing. Those members are listed from the dimension's table, or from the table of the dimension it hangs off, and a rule that 0 passes is refused on a dimension with neither. A snapshot figure, a formula and the other aggregates stay blank for such a member and pass no comparison. With `against: <measure>` in place of `value`, the member's figure is compared with its figure of that second measure, such as products whose sales are below what was ordered. Each side is read on its own measure's stream over the rule's dates and matched on the member code, a blank reads as above (0 for a sum or count, otherwise blank and failing), and the members are listed from the dimension's table only when both sides are sums or counts and two zeros pass. `compileRollupView` reads a rollup of both streams. A rule gives `value` or `against`, not both. The result lists the dates each such rule reads in `ruleWindows`, and `months` covers them. A data point of a custom view does not take one.
|
|
91
91
|
- **A filter on a measure** drops rows after grouping. It never drops the total row, which stays the total of every member. A measure the view does not show can be filtered on. Dimension rules and measure rules combine with "and" only.
|
|
92
92
|
- **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).
|
|
93
93
|
- **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.
|
package/dist/index.d.ts
CHANGED
|
@@ -356,9 +356,17 @@ declare const MEASURE_CMPS: readonly ["=", "<>", "<", "<=", ">", ">="];
|
|
|
356
356
|
/** The four columns a measure has in variance mode. */
|
|
357
357
|
declare const VARIANCE_PARTS: readonly ["current", "previous", "variance", "variance_pct"];
|
|
358
358
|
type VariancePart = (typeof VARIANCE_PARTS)[number];
|
|
359
|
-
/**
|
|
359
|
+
/**
|
|
360
|
+
* The same dates a year earlier, the period just before, a period of its own, or the period moved by `days`
|
|
361
|
+
* and lasting `length_days` (the period's own length when left out), so it moves with the period.
|
|
362
|
+
*/
|
|
360
363
|
type ViewCompare = 'previous_year' | 'previous_period' | {
|
|
361
364
|
period: PeriodSpec;
|
|
365
|
+
} | {
|
|
366
|
+
shift: {
|
|
367
|
+
days: number;
|
|
368
|
+
length_days?: number;
|
|
369
|
+
};
|
|
362
370
|
};
|
|
363
371
|
interface ViewPathStep {
|
|
364
372
|
dimension: string;
|
|
@@ -390,12 +398,15 @@ interface ViewMeasureRule {
|
|
|
390
398
|
* that member over `period` (the view's period when absent), compared with `value`. Such as the products
|
|
391
399
|
* that sold more than 100 last year, whatever the view then groups by. The test reads every row of the
|
|
392
400
|
* view's stream in those dates, whatever else the view filters on. It sits where a dimension rule can.
|
|
401
|
+
* With `against` in place of `value`, the member's `measure` is compared with its figure of another measure,
|
|
402
|
+
* each read on its own stream over the same dates. A blank sum or count reads as 0; any other blank passes nothing.
|
|
393
403
|
*/
|
|
394
404
|
interface ViewMemberRule {
|
|
395
405
|
dimension: string;
|
|
396
406
|
measure: string;
|
|
397
407
|
cmp: string;
|
|
398
|
-
value
|
|
408
|
+
value?: number;
|
|
409
|
+
against?: string;
|
|
399
410
|
period?: PeriodSpec;
|
|
400
411
|
}
|
|
401
412
|
interface ViewFilterGroup {
|
package/dist/index.js
CHANGED
|
@@ -1275,6 +1275,7 @@ var MAX_LEVELS = 5;
|
|
|
1275
1275
|
var VIEW_MODES = ["total", "period", "variance", "period_variance", "transaction", "stream", "matrix", "custom"];
|
|
1276
1276
|
var MEASURE_CMPS = ["=", "<>", "<", "<=", ">", ">="];
|
|
1277
1277
|
var VARIANCE_PARTS = ["current", "previous", "variance", "variance_pct"];
|
|
1278
|
+
var MAX_SHIFT_DAYS = 36600;
|
|
1278
1279
|
var PART_PREFIX = { current: "m__", previous: "prev__", variance: "var__", variance_pct: "varp__" };
|
|
1279
1280
|
var FACT = "f";
|
|
1280
1281
|
function fail2(reason, detail) {
|
|
@@ -1453,8 +1454,11 @@ function memberRuleStreams(model, view) {
|
|
|
1453
1454
|
const walk = (rule) => {
|
|
1454
1455
|
if (isGroup(rule)) (Array.isArray(rule.rules) ? rule.rules : []).forEach(walk);
|
|
1455
1456
|
else if (isMemberRule(rule)) {
|
|
1456
|
-
const
|
|
1457
|
-
|
|
1457
|
+
const sides = typeof rule.against === "string" ? [rule.measure, rule.against] : [rule.measure];
|
|
1458
|
+
for (const side of sides) {
|
|
1459
|
+
const key = memberRuleStream(model, side, view.stream);
|
|
1460
|
+
if (!found.includes(key)) found.push(key);
|
|
1461
|
+
}
|
|
1458
1462
|
}
|
|
1459
1463
|
};
|
|
1460
1464
|
if (view.filters) walk(view.filters);
|
|
@@ -1610,25 +1614,44 @@ function scopeOf(model, view, ctx, withMembers = false, splitFocus = false) {
|
|
|
1610
1614
|
if (dim.via && parent?.table) return { table: parent.table, column: dim.via.column };
|
|
1611
1615
|
return fail2("dimension_has_no_table", `"${dim.key}" has no table to list its members from`);
|
|
1612
1616
|
};
|
|
1613
|
-
const
|
|
1614
|
-
if (!
|
|
1615
|
-
|
|
1616
|
-
const on = model.streams.find((s) => s.key === memberRuleStream(model, rule.measure, stream.key)) ?? stream;
|
|
1617
|
-
const window = memberWindow(model, view, ctx, rule);
|
|
1617
|
+
const memberSide = (rule, key, window) => {
|
|
1618
|
+
if (typeof key !== "string" || !measureByKey.has(key)) fail2("unknown_measure", `no measure "${String(key)}"`);
|
|
1619
|
+
const on = model.streams.find((s) => s.key === memberRuleStream(model, key, stream.key)) ?? stream;
|
|
1618
1620
|
const own = /* @__PURE__ */ new Map();
|
|
1619
1621
|
const member = codeOf(rule.dimension, 0, own, on);
|
|
1620
1622
|
const uses = /* @__PURE__ */ new Set();
|
|
1621
1623
|
const reads = { stream: on, measureByKey, parsed: /* @__PURE__ */ new Map(), countAll: "COUNT(*)", days: daysBetween(window.from, window.to), uses };
|
|
1622
|
-
const figure = measureOver(reads,
|
|
1623
|
-
const value = sqlNumber(rule.value);
|
|
1624
|
+
const figure = measureOver(reads, key, null);
|
|
1624
1625
|
const from = [`FROM ${factOver(on, window, uses.has("snapshot"))}`, ...own.values()].join(" ");
|
|
1625
|
-
const measure = measureByKey.get(
|
|
1626
|
-
const blankIsZero = measure !== void 0 && !isFormulaMeasure(measure) && !measure.snapshot && (measure.agg === "sum" || measure.agg === "count")
|
|
1627
|
-
|
|
1628
|
-
|
|
1629
|
-
|
|
1630
|
-
|
|
1631
|
-
|
|
1626
|
+
const measure = measureByKey.get(key);
|
|
1627
|
+
const blankIsZero = measure !== void 0 && !isFormulaMeasure(measure) && !measure.snapshot && (measure.agg === "sum" || measure.agg === "count");
|
|
1628
|
+
return { member, figure, from, blankIsZero, figures: `SELECT CAST(${member} AS VARCHAR) AS "__m", ${figure} AS "__v" ${from} GROUP BY ${member}` };
|
|
1629
|
+
};
|
|
1630
|
+
const memberLeaf = (rule, outer) => {
|
|
1631
|
+
if (!MEASURE_CMPS.includes(rule.cmp)) fail2("measure_filter_unsupported", `"${String(rule.cmp)}" is not a comparison`);
|
|
1632
|
+
if (rule.against !== void 0 && rule.value !== void 0) fail2("measure_filter_unsupported", "a member rule compares with a value or with another measure, not both");
|
|
1633
|
+
const window = memberWindow(model, view, ctx, rule);
|
|
1634
|
+
const left = memberSide(rule, rule.measure, window);
|
|
1635
|
+
const listedFrom = (joins2, test) => {
|
|
1636
|
+
const source = memberSource(dimension(rule.dimension));
|
|
1637
|
+
const listed = `"__mb".${quoteIdent(source.column)}`;
|
|
1638
|
+
return `CAST(${outer} AS VARCHAR) IN (SELECT CAST(${listed} AS VARCHAR) FROM ${quoteIdent(source.table)} AS "__mb" ${joins2(`CAST(${listed} AS VARCHAR)`)} WHERE ${test})`;
|
|
1639
|
+
};
|
|
1640
|
+
if (rule.against !== void 0) {
|
|
1641
|
+
const right = memberSide(rule, rule.against, window);
|
|
1642
|
+
const a = left.blankIsZero ? 'COALESCE("__a"."__v", 0)' : '"__a"."__v"';
|
|
1643
|
+
const b = right.blankIsZero ? 'COALESCE("__b"."__v", 0)' : '"__b"."__v"';
|
|
1644
|
+
const test = `${a} ${rule.cmp} ${b}`;
|
|
1645
|
+
if (left.blankIsZero && right.blankIsZero && zeroPasses(rule.cmp, 0)) {
|
|
1646
|
+
return listedFrom((m) => `LEFT JOIN (${left.figures}) AS "__a" ON "__a"."__m" = ${m} LEFT JOIN (${right.figures}) AS "__b" ON "__b"."__m" = ${m}`, test);
|
|
1647
|
+
}
|
|
1648
|
+
return `CAST(${outer} AS VARCHAR) IN (SELECT COALESCE("__a"."__m", "__b"."__m") FROM (${left.figures}) AS "__a" FULL OUTER JOIN (${right.figures}) AS "__b" ON "__b"."__m" = "__a"."__m" WHERE ${test})`;
|
|
1649
|
+
}
|
|
1650
|
+
const value = sqlNumber(rule.value);
|
|
1651
|
+
if (!(left.blankIsZero && zeroPasses(rule.cmp, rule.value))) {
|
|
1652
|
+
return `CAST(${outer} AS VARCHAR) IN (SELECT CAST(${left.member} AS VARCHAR) ${left.from} GROUP BY ${left.member} HAVING ${left.figure} ${rule.cmp} ${value})`;
|
|
1653
|
+
}
|
|
1654
|
+
return listedFrom((m) => `LEFT JOIN (${left.figures}) AS "__r" ON "__r"."__m" = ${m}`, `COALESCE("__r"."__v", 0) ${rule.cmp} ${value}`);
|
|
1632
1655
|
};
|
|
1633
1656
|
const dimensionLeafWith = (resolve) => (rule) => {
|
|
1634
1657
|
if (isMemberRule(rule)) return memberLeaf(rule, resolve(rule.dimension, "code"));
|
|
@@ -1840,10 +1863,26 @@ function withLatestDayOfEach(table, date, columns, where, windows) {
|
|
|
1840
1863
|
});
|
|
1841
1864
|
return `(SELECT *${columns}, ${flags.join(", ")} FROM ${table} WHERE ${where}) AS ${FACT}`;
|
|
1842
1865
|
}
|
|
1866
|
+
function shiftedWindow(window, shift) {
|
|
1867
|
+
const s = typeof shift === "object" && shift !== null ? shift : {};
|
|
1868
|
+
const days = s.days;
|
|
1869
|
+
if (typeof days !== "number" || !Number.isInteger(days) || Math.abs(days) > MAX_SHIFT_DAYS) {
|
|
1870
|
+
fail2("option_unsupported", `a shifted comparison needs "days", a whole number of days within ${MAX_SHIFT_DAYS} either way`);
|
|
1871
|
+
}
|
|
1872
|
+
const length = s.length_days ?? daysBetween(window.from, window.to);
|
|
1873
|
+
if (typeof length !== "number" || !Number.isInteger(length) || length < 1 || length > MAX_SHIFT_DAYS) {
|
|
1874
|
+
fail2("option_unsupported", `"length_days" is a whole number of days from 1 to ${MAX_SHIFT_DAYS}`);
|
|
1875
|
+
}
|
|
1876
|
+
const from = addDays(window.from, days);
|
|
1877
|
+
return { from, to: addDays(from, length) };
|
|
1878
|
+
}
|
|
1843
1879
|
function comparedWith(model, view, ctx, window) {
|
|
1844
1880
|
const compare = view.compare ?? "previous_year";
|
|
1845
1881
|
let comparison;
|
|
1846
|
-
if (typeof compare === "object" && compare !== null) {
|
|
1882
|
+
if (typeof compare === "object" && compare !== null && "shift" in compare) {
|
|
1883
|
+
if ("period" in compare) fail2("option_unsupported", "a comparison takes a period or a shift, not both");
|
|
1884
|
+
comparison = shiftedWindow(window, compare.shift);
|
|
1885
|
+
} else if (typeof compare === "object" && compare !== null) {
|
|
1847
1886
|
if (typeof compare.period !== "object" || compare.period === null) fail2("option_unsupported", "a comparison given as an object needs a period");
|
|
1848
1887
|
comparison = resolvePeriod(compare.period, model.periods, ctx.today, { periodToDate: view.options?.period_to_date === true }).window;
|
|
1849
1888
|
} else {
|
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.15",
|
|
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",
|