@sprigr/apps-reporting-core 0.0.11 → 0.0.12
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 +69 -22
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -73,7 +73,7 @@ Modes `total`, `period`, `variance`, `period_variance`, `transaction`, `stream`,
|
|
|
73
73
|
- **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).
|
|
74
74
|
- **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.
|
|
75
75
|
- **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.
|
|
76
|
-
- **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
|
|
76
|
+
- **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.
|
|
77
77
|
- **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.
|
|
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.
|
package/dist/index.js
CHANGED
|
@@ -1313,7 +1313,8 @@ function compileRollupView(model, view, ctx) {
|
|
|
1313
1313
|
}
|
|
1314
1314
|
let rolled = model;
|
|
1315
1315
|
const rollups = [];
|
|
1316
|
-
|
|
1316
|
+
const keys = [.../* @__PURE__ */ new Set([...plain.streams ?? [view.stream], ...memberRuleStreams(model, view)])];
|
|
1317
|
+
for (const key of keys) {
|
|
1317
1318
|
const listed = model.streams.find((s) => s.key === key)?.rollups ?? [];
|
|
1318
1319
|
const smallestFirst = [...listed].sort((a, b) => Object.keys(a.dimension_keys).length - Object.keys(b.dimension_keys).length);
|
|
1319
1320
|
const pick = smallestFirst.find((rollup) => compileView(onRollup(model, key, rollup), view, ctx).ok);
|
|
@@ -1351,6 +1352,49 @@ function memberWindows(model, view, ctx) {
|
|
|
1351
1352
|
if (view.filters) walk(view.filters);
|
|
1352
1353
|
return found;
|
|
1353
1354
|
}
|
|
1355
|
+
function measureStreams(measureByKey, key, depth = 0) {
|
|
1356
|
+
const measure = measureByKey.get(key);
|
|
1357
|
+
if (!measure) return /* @__PURE__ */ new Set();
|
|
1358
|
+
if (!isFormulaMeasure(measure)) return /* @__PURE__ */ new Set([measure.stream]);
|
|
1359
|
+
const found = /* @__PURE__ */ new Set();
|
|
1360
|
+
if (depth <= 20) formulaRefs(parseFormula(measure.formula)).forEach((ref) => measureStreams(measureByKey, ref, depth + 1).forEach((s) => found.add(s)));
|
|
1361
|
+
return found;
|
|
1362
|
+
}
|
|
1363
|
+
function memberRuleStream(model, key, fallback) {
|
|
1364
|
+
const streams = [...measureStreams(new Map(model.measures.map((m) => [m.key, m])), key)];
|
|
1365
|
+
if (streams.length > 1) fail2("measure_not_on_stream", `"${key}" reads streams ${streams.map((s) => `"${s}"`).join(" and ")}; a member rule tests its members on one`);
|
|
1366
|
+
return streams[0] ?? fallback;
|
|
1367
|
+
}
|
|
1368
|
+
function zeroPasses(cmp, value) {
|
|
1369
|
+
switch (cmp) {
|
|
1370
|
+
case "=":
|
|
1371
|
+
return value === 0;
|
|
1372
|
+
case "<>":
|
|
1373
|
+
return value !== 0;
|
|
1374
|
+
case "<":
|
|
1375
|
+
return 0 < value;
|
|
1376
|
+
case "<=":
|
|
1377
|
+
return 0 <= value;
|
|
1378
|
+
case ">":
|
|
1379
|
+
return 0 > value;
|
|
1380
|
+
case ">=":
|
|
1381
|
+
return 0 >= value;
|
|
1382
|
+
default:
|
|
1383
|
+
return false;
|
|
1384
|
+
}
|
|
1385
|
+
}
|
|
1386
|
+
function memberRuleStreams(model, view) {
|
|
1387
|
+
const found = [];
|
|
1388
|
+
const walk = (rule) => {
|
|
1389
|
+
if (isGroup(rule)) (Array.isArray(rule.rules) ? rule.rules : []).forEach(walk);
|
|
1390
|
+
else if (isMemberRule(rule)) {
|
|
1391
|
+
const key = memberRuleStream(model, rule.measure, view.stream);
|
|
1392
|
+
if (!found.includes(key)) found.push(key);
|
|
1393
|
+
}
|
|
1394
|
+
};
|
|
1395
|
+
if (view.filters) walk(view.filters);
|
|
1396
|
+
return found;
|
|
1397
|
+
}
|
|
1354
1398
|
function scopeOf(model, view, ctx, withMembers = false, splitFocus = false) {
|
|
1355
1399
|
const stream = model.streams.find((s) => s.key === view.stream) ?? fail2("unknown_stream", `no stream "${String(view.stream)}"`);
|
|
1356
1400
|
const dimByKey = new Map(model.dimensions.map((d) => [d.key, d]));
|
|
@@ -1364,14 +1408,14 @@ function scopeOf(model, view, ctx, withMembers = false, splitFocus = false) {
|
|
|
1364
1408
|
if (!into.has(a)) into.set(a, `LEFT JOIN ${quoteIdent(dim.table)} AS ${a} ON ${a}.${quoteIdent(dim.code)} = ${codeExpr2}`);
|
|
1365
1409
|
return a;
|
|
1366
1410
|
};
|
|
1367
|
-
const codeOf = (key, depth = 0, into = joins) => {
|
|
1411
|
+
const codeOf = (key, depth = 0, into = joins, on = stream) => {
|
|
1368
1412
|
if (depth > 20) fail2("unknown_dimension", `the via chain of "${key}" does not end`);
|
|
1369
1413
|
const dim = dimension(key);
|
|
1370
|
-
const direct =
|
|
1414
|
+
const direct = on.dimension_keys[key];
|
|
1371
1415
|
if (direct !== void 0) return `${FACT}.${quoteIdent(direct)}`;
|
|
1372
|
-
if (!dim.via) fail2("dimension_not_on_stream", `stream "${
|
|
1416
|
+
if (!dim.via) fail2("dimension_not_on_stream", `stream "${on.key}" has no "${key}"`);
|
|
1373
1417
|
const parent = dimension(dim.via.dimension);
|
|
1374
|
-
return `${join(parent, codeOf(parent.key, depth + 1, into), into)}.${quoteIdent(dim.via.column)}`;
|
|
1418
|
+
return `${join(parent, codeOf(parent.key, depth + 1, into, on), into)}.${quoteIdent(dim.via.column)}`;
|
|
1375
1419
|
};
|
|
1376
1420
|
const columnOf = (dim, property) => {
|
|
1377
1421
|
const column = property === "name" ? dim.name : dim.properties.find((p) => p.key === property)?.column;
|
|
@@ -1432,21 +1476,31 @@ function scopeOf(model, view, ctx, withMembers = false, splitFocus = false) {
|
|
|
1432
1476
|
if (mb && mb.reaches(step.dimension)) mb.conds.push(`CAST(${mb.codeHere(step.dimension)} AS VARCHAR) IN ${inList(values)}`);
|
|
1433
1477
|
});
|
|
1434
1478
|
const filterMeasures = [];
|
|
1479
|
+
const memberSource = (dim) => {
|
|
1480
|
+
if (dim.table && dim.code) return { table: dim.table, column: dim.code };
|
|
1481
|
+
const parent = dim.via ? dimension(dim.via.dimension) : void 0;
|
|
1482
|
+
if (dim.via && parent?.table) return { table: parent.table, column: dim.via.column };
|
|
1483
|
+
return fail2("dimension_has_no_table", `"${dim.key}" has no table to list its members from`);
|
|
1484
|
+
};
|
|
1435
1485
|
const memberLeaf = (rule, outer) => {
|
|
1436
1486
|
if (!MEASURE_CMPS.includes(rule.cmp)) fail2("measure_filter_unsupported", `"${String(rule.cmp)}" is not a comparison`);
|
|
1437
1487
|
if (!measureByKey.has(rule.measure)) fail2("unknown_measure", `no measure "${String(rule.measure)}"`);
|
|
1488
|
+
const on = model.streams.find((s) => s.key === memberRuleStream(model, rule.measure, stream.key)) ?? stream;
|
|
1438
1489
|
const window = memberWindow(model, view, ctx, rule);
|
|
1439
1490
|
const own = /* @__PURE__ */ new Map();
|
|
1440
|
-
const member = codeOf(rule.dimension, 0, own);
|
|
1491
|
+
const member = codeOf(rule.dimension, 0, own, on);
|
|
1441
1492
|
const uses = /* @__PURE__ */ new Set();
|
|
1442
|
-
const reads = { stream, measureByKey, parsed: /* @__PURE__ */ new Map(), countAll: "COUNT(*)", days: daysBetween(window.from, window.to), uses };
|
|
1493
|
+
const reads = { stream: on, measureByKey, parsed: /* @__PURE__ */ new Map(), countAll: "COUNT(*)", days: daysBetween(window.from, window.to), uses };
|
|
1443
1494
|
const figure = measureOver(reads, rule.measure, null);
|
|
1444
|
-
const
|
|
1445
|
-
|
|
1446
|
-
|
|
1447
|
-
|
|
1448
|
-
|
|
1449
|
-
|
|
1495
|
+
const value = sqlNumber(rule.value);
|
|
1496
|
+
const from = [`FROM ${factOver(on, window, uses.has("snapshot"))}`, ...own.values()].join(" ");
|
|
1497
|
+
const measure = measureByKey.get(rule.measure);
|
|
1498
|
+
const blankIsZero = measure !== void 0 && !isFormulaMeasure(measure) && !measure.snapshot && (measure.agg === "sum" || measure.agg === "count") && zeroPasses(rule.cmp, rule.value);
|
|
1499
|
+
if (!blankIsZero) return `CAST(${outer} AS VARCHAR) IN (SELECT CAST(${member} AS VARCHAR) ${from} GROUP BY ${member} HAVING ${figure} ${rule.cmp} ${value})`;
|
|
1500
|
+
const source = memberSource(dimension(rule.dimension));
|
|
1501
|
+
const listed = `"__mb".${quoteIdent(source.column)}`;
|
|
1502
|
+
const figures = `SELECT CAST(${member} AS VARCHAR) AS "__m", ${figure} AS "__v" ${from} GROUP BY ${member}`;
|
|
1503
|
+
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})`;
|
|
1450
1504
|
};
|
|
1451
1505
|
const dimensionLeafWith = (resolve) => (rule) => {
|
|
1452
1506
|
if (isMemberRule(rule)) return memberLeaf(rule, resolve(rule.dimension, "code"));
|
|
@@ -2031,15 +2085,8 @@ function compileCustom(model, view, ctx) {
|
|
|
2031
2085
|
}
|
|
2032
2086
|
return found;
|
|
2033
2087
|
};
|
|
2034
|
-
const streamsOf = (measure
|
|
2035
|
-
|
|
2036
|
-
const found = /* @__PURE__ */ new Set();
|
|
2037
|
-
if (depth <= 20) {
|
|
2038
|
-
formulaRefs(parseFormula(measure.formula)).forEach((ref) => {
|
|
2039
|
-
const named = measureByKey.get(ref);
|
|
2040
|
-
if (named) streamsOf(named, depth + 1).forEach((key) => found.add(key));
|
|
2041
|
-
});
|
|
2042
|
-
}
|
|
2088
|
+
const streamsOf = (measure) => {
|
|
2089
|
+
const found = measureStreams(measureByKey, measure.key);
|
|
2043
2090
|
return found.size > 0 ? found : /* @__PURE__ */ new Set([stream.key]);
|
|
2044
2091
|
};
|
|
2045
2092
|
const points = [];
|
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.12",
|
|
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",
|