@sprigr/apps-reporting-core 0.0.14 → 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 CHANGED
@@ -87,7 +87,7 @@ Modes `total`, `period`, `variance`, `period_variance`, `transaction`, `stream`,
87
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
@@ -398,12 +398,15 @@ interface ViewMeasureRule {
398
398
  * that member over `period` (the view's period when absent), compared with `value`. Such as the products
399
399
  * that sold more than 100 last year, whatever the view then groups by. The test reads every row of the
400
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.
401
403
  */
402
404
  interface ViewMemberRule {
403
405
  dimension: string;
404
406
  measure: string;
405
407
  cmp: string;
406
- value: number;
408
+ value?: number;
409
+ against?: string;
407
410
  period?: PeriodSpec;
408
411
  }
409
412
  interface ViewFilterGroup {
package/dist/index.js CHANGED
@@ -1454,8 +1454,11 @@ function memberRuleStreams(model, view) {
1454
1454
  const walk = (rule) => {
1455
1455
  if (isGroup(rule)) (Array.isArray(rule.rules) ? rule.rules : []).forEach(walk);
1456
1456
  else if (isMemberRule(rule)) {
1457
- const key = memberRuleStream(model, rule.measure, view.stream);
1458
- if (!found.includes(key)) found.push(key);
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
+ }
1459
1462
  }
1460
1463
  };
1461
1464
  if (view.filters) walk(view.filters);
@@ -1611,25 +1614,44 @@ function scopeOf(model, view, ctx, withMembers = false, splitFocus = false) {
1611
1614
  if (dim.via && parent?.table) return { table: parent.table, column: dim.via.column };
1612
1615
  return fail2("dimension_has_no_table", `"${dim.key}" has no table to list its members from`);
1613
1616
  };
1614
- const memberLeaf = (rule, outer) => {
1615
- if (!MEASURE_CMPS.includes(rule.cmp)) fail2("measure_filter_unsupported", `"${String(rule.cmp)}" is not a comparison`);
1616
- if (!measureByKey.has(rule.measure)) fail2("unknown_measure", `no measure "${String(rule.measure)}"`);
1617
- const on = model.streams.find((s) => s.key === memberRuleStream(model, rule.measure, stream.key)) ?? stream;
1618
- 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;
1619
1620
  const own = /* @__PURE__ */ new Map();
1620
1621
  const member = codeOf(rule.dimension, 0, own, on);
1621
1622
  const uses = /* @__PURE__ */ new Set();
1622
1623
  const reads = { stream: on, measureByKey, parsed: /* @__PURE__ */ new Map(), countAll: "COUNT(*)", days: daysBetween(window.from, window.to), uses };
1623
- const figure = measureOver(reads, rule.measure, null);
1624
- const value = sqlNumber(rule.value);
1624
+ const figure = measureOver(reads, key, null);
1625
1625
  const from = [`FROM ${factOver(on, window, uses.has("snapshot"))}`, ...own.values()].join(" ");
1626
- const measure = measureByKey.get(rule.measure);
1627
- const blankIsZero = measure !== void 0 && !isFormulaMeasure(measure) && !measure.snapshot && (measure.agg === "sum" || measure.agg === "count") && zeroPasses(rule.cmp, rule.value);
1628
- if (!blankIsZero) return `CAST(${outer} AS VARCHAR) IN (SELECT CAST(${member} AS VARCHAR) ${from} GROUP BY ${member} HAVING ${figure} ${rule.cmp} ${value})`;
1629
- const source = memberSource(dimension(rule.dimension));
1630
- const listed = `"__mb".${quoteIdent(source.column)}`;
1631
- const figures = `SELECT CAST(${member} AS VARCHAR) AS "__m", ${figure} AS "__v" ${from} GROUP BY ${member}`;
1632
- return `CAST(${outer} AS VARCHAR) IN (SELECT CAST(${listed} AS VARCHAR) FROM ${quoteIdent(source.table)} AS "__mb" LEFT JOIN (${figures}) AS "__r" ON "__r"."__m" = CAST(${listed} AS VARCHAR) WHERE COALESCE("__r"."__v", 0) ${rule.cmp} ${value})`;
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}`);
1633
1655
  };
1634
1656
  const dimensionLeafWith = (resolve) => (rule) => {
1635
1657
  if (isMemberRule(rule)) return memberLeaf(rule, resolve(rule.dimension, "code"));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sprigr/apps-reporting-core",
3
- "version": "0.0.14",
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",