@sprigr/apps-reporting-core 0.0.13 → 0.0.14

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
@@ -84,7 +84,7 @@ 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, or `{ period }` for a window of its own, which may overlap the period or come after it) in one pass. Each measure gets four columns: current, previous, variance and variance percentage (a fraction).
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
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.
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
- /** The same dates a year earlier, the period just before, or a period of its own. */
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;
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) {
@@ -1840,10 +1841,26 @@ function withLatestDayOfEach(table, date, columns, where, windows) {
1840
1841
  });
1841
1842
  return `(SELECT *${columns}, ${flags.join(", ")} FROM ${table} WHERE ${where}) AS ${FACT}`;
1842
1843
  }
1844
+ function shiftedWindow(window, shift) {
1845
+ const s = typeof shift === "object" && shift !== null ? shift : {};
1846
+ const days = s.days;
1847
+ if (typeof days !== "number" || !Number.isInteger(days) || Math.abs(days) > MAX_SHIFT_DAYS) {
1848
+ fail2("option_unsupported", `a shifted comparison needs "days", a whole number of days within ${MAX_SHIFT_DAYS} either way`);
1849
+ }
1850
+ const length = s.length_days ?? daysBetween(window.from, window.to);
1851
+ if (typeof length !== "number" || !Number.isInteger(length) || length < 1 || length > MAX_SHIFT_DAYS) {
1852
+ fail2("option_unsupported", `"length_days" is a whole number of days from 1 to ${MAX_SHIFT_DAYS}`);
1853
+ }
1854
+ const from = addDays(window.from, days);
1855
+ return { from, to: addDays(from, length) };
1856
+ }
1843
1857
  function comparedWith(model, view, ctx, window) {
1844
1858
  const compare = view.compare ?? "previous_year";
1845
1859
  let comparison;
1846
- if (typeof compare === "object" && compare !== null) {
1860
+ if (typeof compare === "object" && compare !== null && "shift" in compare) {
1861
+ if ("period" in compare) fail2("option_unsupported", "a comparison takes a period or a shift, not both");
1862
+ comparison = shiftedWindow(window, compare.shift);
1863
+ } else if (typeof compare === "object" && compare !== null) {
1847
1864
  if (typeof compare.period !== "object" || compare.period === null) fail2("option_unsupported", "a comparison given as an object needs a period");
1848
1865
  comparison = resolvePeriod(compare.period, model.periods, ctx.today, { periodToDate: view.options?.period_to_date === true }).window;
1849
1866
  } else {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sprigr/apps-reporting-core",
3
- "version": "0.0.13",
3
+ "version": "0.0.14",
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",