derive-dashboards 0.1.0 → 0.1.1
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 +4 -3
- package/dist/adapter/sql-adapter.mjs +4 -4
- package/dist/body.mjs +11 -1
- package/dist/cache.d.mts +1 -1
- package/dist/cli/check.mjs +14 -1
- package/dist/cli/import/dbt-schema.mjs +2 -2
- package/dist/cli/import/index.mjs +2 -2
- package/dist/core/conformance/cases.d.mts +3 -0
- package/dist/core/conformance/cases.mjs +20 -4
- package/dist/core/conformance/check.mjs +6 -3
- package/dist/core/conformance/drill-cases.mjs +120 -0
- package/dist/core/document/view.d.mts +26 -7
- package/dist/core/document/view.mjs +42 -11
- package/dist/core/engine/index.d.mts +14 -2
- package/dist/core/engine/index.mjs +12 -1
- package/dist/core/engine/members.mjs +1 -1
- package/dist/core/engine/postgres.mjs +1 -1
- package/dist/core/engine/record-layout.d.mts +11 -0
- package/dist/core/engine/record-layout.mjs +7 -7
- package/dist/core/engine/records.d.mts +3 -2
- package/dist/core/engine/records.mjs +2 -1
- package/dist/core/engine/segmented.mjs +5 -2
- package/dist/core/engine/sql-records.mjs +2 -1
- package/dist/core/engine/statement.mjs +37 -3
- package/dist/core/engine/types.d.mts +3 -0
- package/dist/core/engine/version.d.mts +1 -1
- package/dist/core/engine/version.mjs +1 -1
- package/dist/core/error/codes.d.mts +31 -0
- package/dist/core/error/codes.mjs +28 -1
- package/dist/core/index.d.mts +9 -5
- package/dist/core/index.mjs +5 -3
- package/dist/core/model/catalog/check.mjs +1 -1
- package/dist/core/model/hints.d.mts +36 -0
- package/dist/core/model/member.d.mts +4 -2
- package/dist/core/model/member.mjs +16 -3
- package/dist/core/model/types.d.mts +8 -6
- package/dist/core/model/validate/drill.mjs +70 -0
- package/dist/core/model/validate/index.mjs +3 -0
- package/dist/core/model/validate/shape.mjs +150 -0
- package/dist/core/model/validate/source.mjs +5 -3
- package/dist/core/query/alias.mjs +7 -1
- package/dist/core/query/drill/compose.d.mts +22 -0
- package/dist/core/query/drill/compose.mjs +85 -0
- package/dist/core/query/drill/fields.d.mts +21 -0
- package/dist/core/query/drill/fields.mjs +91 -0
- package/dist/core/query/drill/internal.d.mts +11 -0
- package/dist/core/query/drill/items.d.mts +29 -0
- package/dist/core/query/drill/items.mjs +98 -0
- package/dist/core/query/drill/line.d.mts +31 -0
- package/dist/core/query/drill/line.mjs +183 -0
- package/dist/core/query/drill/links.d.mts +33 -0
- package/dist/core/query/drill/links.mjs +31 -0
- package/dist/core/query/drill/operand.mjs +7 -0
- package/dist/core/query/drill/scopes.d.mts +22 -0
- package/dist/core/query/drill/scopes.mjs +58 -0
- package/dist/core/query/drill/selection.d.mts +37 -0
- package/dist/core/query/drill/selection.mjs +144 -0
- package/dist/core/query/drill/steps.d.mts +14 -0
- package/dist/core/query/drill/steps.mjs +35 -0
- package/dist/core/query/drill/synthetic-expr.mjs +85 -0
- package/dist/core/query/drill/synthetic.d.mts +22 -0
- package/dist/core/query/drill/synthetic.mjs +165 -0
- package/dist/core/query/drill/time.d.mts +11 -0
- package/dist/core/query/drill/time.mjs +71 -0
- package/dist/core/query/drill/types.d.mts +155 -0
- package/dist/core/query/drill/types.mjs +4 -0
- package/dist/core/query/drill/views.d.mts +62 -0
- package/dist/core/query/drill/views.mjs +257 -0
- package/dist/core/query/segments.d.mts +12 -2
- package/dist/core/query/segments.mjs +3 -1
- package/dist/core/result/columns.mjs +17 -4
- package/dist/core/result/types.d.mts +5 -1
- package/dist/core/schema/answer-parts.mjs +117 -0
- package/dist/core/schema/answers.d.mts +2108 -128
- package/dist/core/schema/answers.mjs +10 -113
- package/dist/core/schema/commands.d.mts +2 -2
- package/dist/core/schema/document.d.mts +1 -1
- package/dist/core/schema/drill-answers.mjs +179 -0
- package/dist/core/schema/drill.d.mts +260 -0
- package/dist/core/schema/drill.mjs +107 -0
- package/dist/core/schema/index.d.mts +4 -2
- package/dist/core/schema/index.mjs +4 -2
- package/dist/core/schema/picks.d.mts +35 -0
- package/dist/core/schema/picks.mjs +23 -0
- package/dist/core/schema/query.d.mts +1 -1
- package/dist/core/schema/tools.mjs +1 -1
- package/dist/core/schema/view.d.mts +0 -35
- package/dist/core/schema/view.mjs +19 -24
- package/dist/core/store/schema.d.mts +5 -0
- package/dist/core/store/schema.mjs +10 -0
- package/dist/core/types/api.d.mts +15 -1
- package/dist/core/types/api.mjs +1 -1
- package/dist/core/types/drill-api.d.mts +210 -0
- package/dist/core/types/options.d.mts +21 -0
- package/dist/core/types/viewer.d.mts +5 -0
- package/dist/cursor-seal.d.mts +8 -0
- package/dist/cursor-seal.mjs +98 -0
- package/dist/dashboard-id.mjs +1 -1
- package/dist/drill-access.mjs +93 -0
- package/dist/drill-change.mjs +76 -0
- package/dist/drill-crumbs.mjs +113 -0
- package/dist/drill-distinct.mjs +130 -0
- package/dist/drill-explain.mjs +64 -0
- package/dist/drill-identity.mjs +35 -0
- package/dist/drill-item-id.mjs +35 -0
- package/dist/drill-items.mjs +132 -0
- package/dist/drill-open.mjs +169 -0
- package/dist/drill-seal.mjs +170 -0
- package/dist/drill-secret.mjs +14 -0
- package/dist/drill-share.mjs +53 -0
- package/dist/drill-summary.mjs +158 -0
- package/dist/drill-targets.mjs +53 -0
- package/dist/drill-totals.mjs +8 -0
- package/dist/drill-views.mjs +175 -0
- package/dist/drill.mjs +157 -0
- package/dist/editing.mjs +2 -2
- package/dist/endpoints/dashboards.mjs +11 -3
- package/dist/endpoints/drill.mjs +55 -0
- package/dist/endpoints/index.d.mts +233 -2
- package/dist/endpoints/index.mjs +5 -1
- package/dist/endpoints/query.mjs +26 -4
- package/dist/execute.d.mts +19 -1
- package/dist/execute.mjs +15 -17
- package/dist/index.d.mts +3 -1
- package/dist/index.mjs +29 -2
- package/dist/mcp/drill.mjs +61 -0
- package/dist/mcp/format.mjs +27 -3
- package/dist/mcp/index.d.mts +15 -4
- package/dist/mcp/index.mjs +13 -7
- package/dist/mcp/query.d.mts +7 -2
- package/dist/mcp/query.mjs +7 -3
- package/dist/node-postgres.d.mts +8 -4
- package/dist/node-postgres.mjs +29 -6
- package/dist/owner.mjs +3 -3
- package/dist/postgres-connection.d.mts +8 -2
- package/dist/postgres-connection.mjs +2 -1
- package/dist/react/Dashboard.d.mts +10 -5
- package/dist/react/Dashboard.mjs +51 -23
- package/dist/react/DashboardList.mjs +30 -2
- package/dist/react/Discard.mjs +7 -2
- package/dist/react/Grid.mjs +12 -5
- package/dist/react/Header.mjs +9 -0
- package/dist/react/Parts.mjs +6 -2
- package/dist/react/ReviewPanel.mjs +13 -2
- package/dist/react/RowBody.d.mts +19 -0
- package/dist/react/RowBody.mjs +55 -0
- package/dist/react/Tile.d.mts +1 -1
- package/dist/react/Tile.mjs +54 -30
- package/dist/react/builder/state.d.mts +1 -1
- package/dist/react/client.d.mts +2 -0
- package/dist/react/client.mjs +1 -0
- package/dist/react/context.d.mts +15 -1
- package/dist/react/context.mjs +19 -4
- package/dist/react/drill/Bars.mjs +179 -0
- package/dist/react/drill/Controls.mjs +173 -0
- package/dist/react/drill/DashboardTile.d.mts +18 -0
- package/dist/react/drill/DashboardTile.mjs +107 -0
- package/dist/react/drill/DrillPanel.d.mts +31 -0
- package/dist/react/drill/DrillPanel.mjs +104 -0
- package/dist/react/drill/DrillTabs.mjs +52 -0
- package/dist/react/drill/Header.mjs +131 -0
- package/dist/react/drill/Items.d.mts +22 -0
- package/dist/react/drill/Items.mjs +164 -0
- package/dist/react/drill/ItemsFoot.mjs +60 -0
- package/dist/react/drill/Notes.mjs +58 -0
- package/dist/react/drill/PanelBody.mjs +74 -0
- package/dist/react/drill/PanelStates.mjs +37 -0
- package/dist/react/drill/board.d.mts +18 -0
- package/dist/react/drill/board.mjs +91 -0
- package/dist/react/drill/counts.mjs +14 -0
- package/dist/react/drill/host.mjs +26 -0
- package/dist/react/drill/item-parts.d.mts +5 -0
- package/dist/react/drill/item-parts.mjs +76 -0
- package/dist/react/drill/item-type.d.mts +26 -0
- package/dist/react/drill/item-type.mjs +7 -0
- package/dist/react/drill/lazy.d.mts +6 -0
- package/dist/react/drill/lazy.mjs +17 -0
- package/dist/react/drill/pages.mjs +48 -0
- package/dist/react/drill/panel-focus.mjs +51 -0
- package/dist/react/drill/props.d.mts +25 -0
- package/dist/react/drill/props.mjs +72 -0
- package/dist/react/drill/state.d.mts +18 -0
- package/dist/react/drill/state.mjs +68 -0
- package/dist/react/drill/useDrill.d.mts +46 -0
- package/dist/react/drill/useDrill.mjs +194 -0
- package/dist/react/drill/words.d.mts +98 -0
- package/dist/react/drill/words.mjs +77 -0
- package/dist/react/english.mjs +9 -1
- package/dist/react/format.d.mts +3 -3
- package/dist/react/format.mjs +34 -12
- package/dist/react/index.d.mts +27 -4
- package/dist/react/index.mjs +10 -3
- package/dist/react/messages.d.mts +14 -1
- package/dist/react/renderers/index.d.mts +10 -2
- package/dist/react/renderers/index.mjs +54 -14
- package/dist/react/renderers/list.d.mts +22 -0
- package/dist/react/renderers/list.mjs +118 -0
- package/dist/react/review-words.mjs +1 -1
- package/dist/react/review.mjs +2 -2
- package/dist/react/scope.mjs +2 -7
- package/dist/react/series.d.mts +37 -0
- package/dist/react/series.mjs +95 -0
- package/dist/react/theme.css +1823 -1296
- package/dist/react/theme.css.d.ts +1 -0
- package/dist/react/tile-store.mjs +15 -3
- package/dist/react/useBoard.d.mts +5 -0
- package/dist/react/useBoard.mjs +4 -1
- package/dist/react/useDashboard.d.mts +1 -0
- package/dist/react/useDashboard.mjs +2 -1
- package/dist/react/useTiles.mjs +50 -15
- package/dist/react/view.d.mts +9 -0
- package/dist/react/view.mjs +22 -10
- package/dist/reading.d.mts +2 -0
- package/dist/reading.mjs +9 -1
- package/dist/recharts/charts.d.mts +0 -1
- package/dist/recharts/charts.mjs +128 -34
- package/dist/recharts/index.d.mts +2 -2
- package/dist/recharts/index.mjs +2 -2
- package/dist/recharts/marks.mjs +83 -0
- package/dist/recharts/series.d.mts +2 -22
- package/dist/recharts/series.mjs +1 -64
- package/dist/records-read.mjs +44 -0
- package/dist/store/apply.mjs +5 -5
- package/dist/store/commands.mjs +4 -4
- package/dist/store/discard.mjs +7 -7
- package/dist/store/drafts.mjs +5 -5
- package/dist/store/editor.mjs +1 -1
- package/dist/store/publish.mjs +12 -12
- package/dist/store/reader.mjs +41 -22
- package/dist/store/review.mjs +16 -16
- package/dist/store/rows.mjs +20 -20
- package/dist/store/undo.mjs +3 -3
- package/dist/store/unpublished.mjs +4 -4
- package/dist/store/versions.mjs +14 -14
- package/dist/testing/adapter-suite.mjs +4 -4
- package/dist/tiles.mjs +37 -3
- package/package.json +8 -10
- package/dist/react/Records.d.mts +0 -12
- package/dist/react/Records.mjs +0 -97
- package/src/adapter/sql-adapter.ts +0 -95
- package/src/adapter/statement.ts +0 -185
- package/src/authorize.ts +0 -73
- package/src/body.ts +0 -169
- package/src/bounded.ts +0 -43
- package/src/cache.ts +0 -192
- package/src/checks.ts +0 -170
- package/src/custom-source.ts +0 -59
- package/src/dashboard-id.ts +0 -36
- package/src/dashboards.ts +0 -132
- package/src/editing.ts +0 -148
- package/src/endpoints/access.ts +0 -41
- package/src/endpoints/dashboards.ts +0 -92
- package/src/endpoints/drafts.ts +0 -138
- package/src/endpoints/index.ts +0 -21
- package/src/endpoints/meta.ts +0 -137
- package/src/endpoints/query.ts +0 -86
- package/src/endpoints/read.ts +0 -92
- package/src/endpoints/review.ts +0 -89
- package/src/endpoints/values.ts +0 -62
- package/src/endpoints/versions.ts +0 -104
- package/src/execute.ts +0 -207
- package/src/handler.ts +0 -232
- package/src/http.ts +0 -66
- package/src/index.ts +0 -163
- package/src/introspect.ts +0 -205
- package/src/mcp/api.ts +0 -59
- package/src/mcp/format.ts +0 -67
- package/src/mcp/index.ts +0 -151
- package/src/mcp/query.ts +0 -153
- package/src/mcp/resolve.ts +0 -114
- package/src/node-postgres.ts +0 -81
- package/src/owner.ts +0 -36
- package/src/postgres-connection.ts +0 -143
- package/src/postgres-store.ts +0 -82
- package/src/principal.ts +0 -39
- package/src/reading.ts +0 -56
- package/src/request-rows.ts +0 -23
- package/src/request.ts +0 -22
- package/src/storage.ts +0 -63
- package/src/store/apply.ts +0 -129
- package/src/store/commands.ts +0 -75
- package/src/store/conflicts.ts +0 -98
- package/src/store/create-tables.ts +0 -104
- package/src/store/discard.ts +0 -42
- package/src/store/drafts.ts +0 -133
- package/src/store/editor.ts +0 -28
- package/src/store/log.ts +0 -28
- package/src/store/publish.ts +0 -199
- package/src/store/publisher.ts +0 -21
- package/src/store/reader.ts +0 -139
- package/src/store/review.ts +0 -239
- package/src/store/rows.ts +0 -250
- package/src/store/tenant.ts +0 -28
- package/src/store/undo.ts +0 -56
- package/src/store/unpublished.ts +0 -33
- package/src/store/versions.ts +0 -106
- package/src/store/writer.ts +0 -19
- package/src/testing/adapter-suite.ts +0 -239
- package/src/testing/index.ts +0 -2
- package/src/tile-doc.ts +0 -46
- package/src/tiles.ts +0 -120
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
import { DRILL_REASONS } from "../../error/codes.mjs";
|
|
2
|
+
import { parseMemberId, resolveMeasure } from "../../model/member.mjs";
|
|
3
|
+
import { memberLabel } from "../../result/columns.mjs";
|
|
4
|
+
import { canonical } from "../../value/canonical.mjs";
|
|
5
|
+
import { aliasPart, lineAlias } from "../alias.mjs";
|
|
6
|
+
import { operandColumn } from "./operand.mjs";
|
|
7
|
+
import { flagName, setName, valueName } from "./synthetic.mjs";
|
|
8
|
+
// src/query/drill/line.ts
|
|
9
|
+
/** The line a drill reads: the one clicked, or the tile's only measure. */
|
|
10
|
+
function drillMeasure(set, drill) {
|
|
11
|
+
if (drill.measure !== void 0) return lineAlias(drill.measure);
|
|
12
|
+
return set.measures.length === 1 && (set.calc ?? []).length === 0 ? "m1" : void 0;
|
|
13
|
+
}
|
|
14
|
+
/** The line a drill reads, as a measure of the query; none for a formula or an unknown line. */
|
|
15
|
+
function lineMeasure(model, set, drill) {
|
|
16
|
+
const line = drillMeasure(set, drill);
|
|
17
|
+
if (line === void 0) return void 0;
|
|
18
|
+
const part = aliasPart(line);
|
|
19
|
+
if (part.kind !== "measure") return void 0;
|
|
20
|
+
const entry = set.measures[part.index];
|
|
21
|
+
if (entry === void 0) return void 0;
|
|
22
|
+
const def = resolveMeasure(model, set.source, entry.measure);
|
|
23
|
+
const name = parseMemberId(entry.measure)?.name;
|
|
24
|
+
return def === void 0 || name === void 0 ? void 0 : {
|
|
25
|
+
entry,
|
|
26
|
+
def,
|
|
27
|
+
name
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
/** The measure whose rows a line reads, with its name: a ratio's are its denominator's. */
|
|
31
|
+
function rowsMeasure(model, source, line) {
|
|
32
|
+
const { def } = line;
|
|
33
|
+
if (def.kind !== "ratio") return line;
|
|
34
|
+
const denominator = model.sources[source]?.measures[def.denominator];
|
|
35
|
+
return denominator === void 0 ? void 0 : {
|
|
36
|
+
def: denominator,
|
|
37
|
+
name: def.denominator
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
/** A field of the source by its name, when the model (with a drill's own fields) has it. */
|
|
41
|
+
const field = (model, source, name) => model.sources[source]?.dimensions[name] === void 0 ? void 0 : `${source}.${name}`;
|
|
42
|
+
/** The column a measure reads row by row: a sum's, an average's, a distinct count's. */
|
|
43
|
+
/**
|
|
44
|
+
* The field a measure adds up or ranks, to show: a number field the model shows on its column,
|
|
45
|
+
* else the drill's own field of that column (`dd_value_...`, exact as stored). None when the
|
|
46
|
+
* column is kept back (a sensitive or hidden field reads it).
|
|
47
|
+
*/
|
|
48
|
+
function valueField(model, source, m) {
|
|
49
|
+
const { def } = m;
|
|
50
|
+
if (!("of" in def) || typeof def.of !== "string") return void 0;
|
|
51
|
+
return shownOn(model, source, def.of, true) ?? field(model, source, valueName(m.name));
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* The predicate field "the measure's operand is set" (`dd_set_...`), apart from whether the
|
|
55
|
+
* operand may be shown: the rows an average or a distinct count reads are those, whoever asks.
|
|
56
|
+
*/
|
|
57
|
+
const operandSet = (model, source, name) => field(model, source, setName(name));
|
|
58
|
+
/** A field the model shows on a column (a number one, when asked), never time, sensitive or hidden. */
|
|
59
|
+
function shownOn(model, source, column, numberOnly) {
|
|
60
|
+
const dims = model.sources[source]?.dimensions ?? {};
|
|
61
|
+
const shown = Object.entries(dims).find(([name, d]) => !name.startsWith("dd_") && "column" in d && d.column === column && d.kind !== "time" && (!numberOnly || d.kind === "number") && d.sensitive !== true && d.hidden !== true);
|
|
62
|
+
return shown === void 0 ? void 0 : `${source}.${shown[0]}`;
|
|
63
|
+
}
|
|
64
|
+
const off = (code) => ({
|
|
65
|
+
ok: false,
|
|
66
|
+
code,
|
|
67
|
+
reason: DRILL_REASONS[code]
|
|
68
|
+
});
|
|
69
|
+
const ON = { ok: true };
|
|
70
|
+
const NON_NULL = Object.freeze({
|
|
71
|
+
avg: true,
|
|
72
|
+
min: true,
|
|
73
|
+
max: true,
|
|
74
|
+
percentile: true
|
|
75
|
+
});
|
|
76
|
+
const counts = (def) => def.kind === "count" && def.distinct === void 0;
|
|
77
|
+
/** A ratio's semantics: its denominator's rows, each marked when it is also in the numerator. */
|
|
78
|
+
function ratioSemantics(model, source, line) {
|
|
79
|
+
const { def } = line;
|
|
80
|
+
const base = {
|
|
81
|
+
semantics: "ratio",
|
|
82
|
+
additive: false,
|
|
83
|
+
counted: false
|
|
84
|
+
};
|
|
85
|
+
if (def.kind !== "ratio") return {
|
|
86
|
+
...base,
|
|
87
|
+
items: ON
|
|
88
|
+
};
|
|
89
|
+
const measures = model.sources[source]?.measures;
|
|
90
|
+
const numerator = measures?.[def.numerator];
|
|
91
|
+
if (!rateOfRows(numerator, measures?.[def.denominator])) return {
|
|
92
|
+
...base,
|
|
93
|
+
items: off("RATIO_NOT_ROWS")
|
|
94
|
+
};
|
|
95
|
+
const label = memberLabel(def.numerator, numerator?.label);
|
|
96
|
+
const flag = field(model, source, flagName(line.name));
|
|
97
|
+
return {
|
|
98
|
+
...base,
|
|
99
|
+
items: ON,
|
|
100
|
+
numerator: {
|
|
101
|
+
label,
|
|
102
|
+
...flag === void 0 ? {} : { flag }
|
|
103
|
+
}
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
/** A count of rows: no field of its own (a sum, a distinct count) to read per row. */
|
|
107
|
+
const rowCount = (m) => m?.kind === "count" && m.distinct === void 0;
|
|
108
|
+
/**
|
|
109
|
+
* Whether a ratio is a rate of rows (won deals of closed deals): both lines count rows and every
|
|
110
|
+
* numerator row is a denominator row (the denominator's condition is none, or one the numerator's
|
|
111
|
+
* holds too). Only then does listing the denominator's rows, the numerator's marked, explain it.
|
|
112
|
+
*/
|
|
113
|
+
function rateOfRows(numerator, denominator) {
|
|
114
|
+
if (!rowCount(numerator) || !rowCount(denominator)) return false;
|
|
115
|
+
const below = denominator.where;
|
|
116
|
+
if (below === void 0) return true;
|
|
117
|
+
const top = numerator.where;
|
|
118
|
+
return (top === void 0 ? [] : "all" in top ? top.all : [top]).some((p) => canonical(p) === canonical(below));
|
|
119
|
+
}
|
|
120
|
+
/** What each kind of measure's drill shows (Rules, section 2). */
|
|
121
|
+
function lineSemantics(model, set, drill) {
|
|
122
|
+
const line = lineMeasure(model, set, drill);
|
|
123
|
+
if (line === void 0) return {
|
|
124
|
+
semantics: "expression",
|
|
125
|
+
items: off("FORMULA_ITEMS"),
|
|
126
|
+
additive: false,
|
|
127
|
+
counted: false
|
|
128
|
+
};
|
|
129
|
+
const { def } = line;
|
|
130
|
+
const additive = def.merge === "sum";
|
|
131
|
+
if ("partitionBy" in def && def.partitionBy !== void 0) return {
|
|
132
|
+
semantics: "rows",
|
|
133
|
+
items: off("SNAPSHOT"),
|
|
134
|
+
additive: false,
|
|
135
|
+
counted: false
|
|
136
|
+
};
|
|
137
|
+
if (def.kind === "ratio") return ratioSemantics(model, set.source, line);
|
|
138
|
+
if (def.kind === "count" && def.distinct !== void 0) return distinctSemantics(model, set.source, def.distinct);
|
|
139
|
+
return {
|
|
140
|
+
...valueSemantics(model, set.source, line),
|
|
141
|
+
additive,
|
|
142
|
+
counted: counts(def)
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
/** A distinct count's semantics: its distinct values, by a field the model shows or its own. */
|
|
146
|
+
function distinctSemantics(model, source, column) {
|
|
147
|
+
const shown = shownOn(model, source, column, false);
|
|
148
|
+
const own = model.sources[source]?.dimensions;
|
|
149
|
+
const dd = Object.entries(own ?? {}).find(([n, d]) => n.startsWith("dd_distinct_") && "column" in d && d.column === column)?.[0];
|
|
150
|
+
const value = shown ?? (dd === void 0 ? void 0 : `${source}.${dd}`);
|
|
151
|
+
const base = {
|
|
152
|
+
semantics: "distinct",
|
|
153
|
+
additive: false,
|
|
154
|
+
counted: false
|
|
155
|
+
};
|
|
156
|
+
return value === void 0 ? {
|
|
157
|
+
...base,
|
|
158
|
+
items: off("DISTINCT_HIDDEN")
|
|
159
|
+
} : {
|
|
160
|
+
...base,
|
|
161
|
+
items: ON,
|
|
162
|
+
value
|
|
163
|
+
};
|
|
164
|
+
}
|
|
165
|
+
/** A count's, sum's or ranked measure's semantics: its rows, with the field it reads. */
|
|
166
|
+
function valueSemantics(model, source, line) {
|
|
167
|
+
const { def } = line;
|
|
168
|
+
const value = valueField(model, source, line);
|
|
169
|
+
const nonNull = Object.hasOwn(NON_NULL, def.kind);
|
|
170
|
+
const own = value === `${source}.${valueName(line.name)}`;
|
|
171
|
+
const hidden = operandColumn(def) !== void 0 && value === void 0;
|
|
172
|
+
return {
|
|
173
|
+
semantics: nonNull ? "non-null" : "rows",
|
|
174
|
+
items: hidden ? off("OPERAND_HIDDEN") : ON,
|
|
175
|
+
...value === void 0 ? {} : { value },
|
|
176
|
+
...own ? { stored: storedOf(def) } : {},
|
|
177
|
+
...nonNull && value !== void 0 ? { ranked: true } : {},
|
|
178
|
+
...def.kind === "percentile" ? { percentile: def.p } : {}
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
/** How a measure's own column is stored: its scale, when it keeps money in minor units. */
|
|
182
|
+
const storedOf = (def) => "numeric" in def && def.numeric !== void 0 ? { numeric: def.numeric } : {};
|
|
183
|
+
export { drillMeasure, lineMeasure, lineSemantics, operandSet, rowsMeasure };
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { Primitive } from "../../types/primitives.mjs";
|
|
2
|
+
/**
|
|
3
|
+
* Where each source's items open in the host's app, by source: a path or URL template, such as
|
|
4
|
+
* `{ order: "/orders/{id}" }`. `{id}` is the item's id: its key's value for a key of one
|
|
5
|
+
* column, its values as JSON for a compound key. `{key.<column>}` is one value of its key. Each
|
|
6
|
+
* is encoded for a URL. An item whose key is hidden (`records.key: "hide"`, or a key column a
|
|
7
|
+
* sensitive field reads) carries no key and gets no link. Plain data, so a server component,
|
|
8
|
+
* the React UI and an agent share it. Typed by source from the model with `ItemLinksFor<M>`.
|
|
9
|
+
*/
|
|
10
|
+
export type ItemLinks = Readonly<Record<string, string>>;
|
|
11
|
+
/** The source names of a model. */
|
|
12
|
+
type SourcesOf<M> = M extends {
|
|
13
|
+
sources: infer S;
|
|
14
|
+
} ? keyof S & string : string;
|
|
15
|
+
/**
|
|
16
|
+
* A link template per source of a model: `L`, the map as written, may name no other source, so
|
|
17
|
+
* a misspelled one does not compile.
|
|
18
|
+
*/
|
|
19
|
+
export type ItemLinksFor<M, L = unknown> = Readonly<Partial<Record<SourcesOf<M>, string>>> & { readonly [K in Exclude<keyof L, SourcesOf<M>>]: never; };
|
|
20
|
+
/** The parts a link template may name: `{id}` and `{key.<column>}`. */
|
|
21
|
+
export declare const LINK_PART: RegExp;
|
|
22
|
+
/**
|
|
23
|
+
* What is wrong with a link template for a source with this key, in words; none when it is right.
|
|
24
|
+
* A template starts with "/" or a scheme, and names only {id} and {key.<column>} of that key.
|
|
25
|
+
*/
|
|
26
|
+
export declare function linkTemplateIssues(template: string, key: readonly string[]): string[];
|
|
27
|
+
/** The link to one item, or undefined when its source has no template, its key is hidden, or a part is missing. */
|
|
28
|
+
export declare function itemLink(links: ItemLinks | undefined, item: {
|
|
29
|
+
source: string;
|
|
30
|
+
id: string;
|
|
31
|
+
key?: Readonly<Record<string, Primitive>>;
|
|
32
|
+
}): string | undefined;
|
|
33
|
+
export {};
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
// src/query/drill/links.ts
|
|
2
|
+
/** The parts a link template may name: `{id}` and `{key.<column>}`. */
|
|
3
|
+
const LINK_PART = /\{(id|key\.[^{}]+)\}/gu;
|
|
4
|
+
/**
|
|
5
|
+
* What is wrong with a link template for a source with this key, in words; none when it is right.
|
|
6
|
+
* A template starts with "/" or a scheme, and names only {id} and {key.<column>} of that key.
|
|
7
|
+
*/
|
|
8
|
+
function linkTemplateIssues(template, key) {
|
|
9
|
+
const issues = [];
|
|
10
|
+
if (!/^(?:\/|[a-z][a-z\d+.-]*:)/iu.test(template)) issues.push(`"${template}": a link starts with "/" or a scheme ("https://").`);
|
|
11
|
+
if (/^\s*(?:javascript|vbscript|data):/iu.test(template)) issues.push(`"${template}": a link cannot run script or carry inline data.`);
|
|
12
|
+
for (const [, part = ""] of template.matchAll(/\{([^{}]*)\}/gu)) {
|
|
13
|
+
const column = part.startsWith("key.") ? part.slice(4) : void 0;
|
|
14
|
+
if (part !== "id" && column === void 0) issues.push(`names {${part}}: a template names {id} or {key.<column>} only.`);
|
|
15
|
+
if (column !== void 0 && !key.includes(column)) issues.push(`names {${part}}, but "${column}" is not a column of the source's key (${key.join(", ")}).`);
|
|
16
|
+
}
|
|
17
|
+
return issues;
|
|
18
|
+
}
|
|
19
|
+
/** The link to one item, or undefined when its source has no template, its key is hidden, or a part is missing. */
|
|
20
|
+
function itemLink(links, item) {
|
|
21
|
+
const template = links?.[item.source];
|
|
22
|
+
if (template === void 0 || item.key === void 0) return void 0;
|
|
23
|
+
let missing = false;
|
|
24
|
+
const link = template.replaceAll(LINK_PART, (_all, part) => {
|
|
25
|
+
const value = part === "id" ? item.id : item.key?.[part.slice(4)];
|
|
26
|
+
if (value === void 0 || value === null) missing = true;
|
|
27
|
+
return encodeURIComponent(String(value ?? ""));
|
|
28
|
+
});
|
|
29
|
+
return missing ? void 0 : link;
|
|
30
|
+
}
|
|
31
|
+
export { LINK_PART, itemLink, linkTemplateIssues };
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
// src/query/drill/operand.ts
|
|
2
|
+
/** The column a measure reads row by row: a sum's or average's, a distinct count's; none for a count of rows or a ratio. */
|
|
3
|
+
function operandColumn(def) {
|
|
4
|
+
if ("of" in def && typeof def.of === "string") return def.of;
|
|
5
|
+
return def.kind === "count" ? def.distinct : void 0;
|
|
6
|
+
}
|
|
7
|
+
export { operandColumn };
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { Cell, RefOf, WireQuery } from "../types.mjs";
|
|
2
|
+
import { DrillStep } from "./types.mjs";
|
|
3
|
+
/** Where a part of a formula is read: the drill's own rows, or a scope around them. */
|
|
4
|
+
export type Scope = "row" | RefOf;
|
|
5
|
+
/** The scopes a line reads, through the calcs it reads in turn. */
|
|
6
|
+
export declare function lineScopes(query: WireQuery, id: string, seen?: Set<string>): Set<RefOf>;
|
|
7
|
+
/** The steps a scope keeps: every step for the row and the period before, the time steps for a bucket, none for the total. */
|
|
8
|
+
export declare function scopeSteps(steps: readonly DrillStep[], scope: Scope): DrillStep[];
|
|
9
|
+
/** A query's measures alone: what one scope reads. */
|
|
10
|
+
export declare function measuresOnly(query: WireQuery): WireQuery;
|
|
11
|
+
/**
|
|
12
|
+
* Every line of the tile on a drill, from each scope's measures (in the tile's measure order):
|
|
13
|
+
* its measures as read on the drill's rows, its calcs worked out from the scopes they read.
|
|
14
|
+
*/
|
|
15
|
+
export declare function scopedLines(query: WireQuery, cells: {
|
|
16
|
+
row: readonly Cell[];
|
|
17
|
+
} & { [K in RefOf]?: readonly Cell[]; }): Map<string, Cell>;
|
|
18
|
+
/** A formula's parts, each once: the line it reads and the scope it reads it in. */
|
|
19
|
+
export declare function formulaParts(query: WireQuery, id: string): {
|
|
20
|
+
entry: string;
|
|
21
|
+
scope: Scope;
|
|
22
|
+
}[];
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { valueRow } from "../../result/calc.mjs";
|
|
2
|
+
import { formulaRefs } from "../formulas.mjs";
|
|
3
|
+
// src/query/drill/scopes.ts
|
|
4
|
+
const calcOf = (query, id) => (query.calc ?? []).find((c) => c.id === id);
|
|
5
|
+
/** The scopes a line reads, through the calcs it reads in turn. */
|
|
6
|
+
function lineScopes(query, id, seen = /* @__PURE__ */ new Set()) {
|
|
7
|
+
const calc = calcOf(query, id);
|
|
8
|
+
if (calc === void 0 || seen.has(id)) return /* @__PURE__ */ new Set();
|
|
9
|
+
seen.add(id);
|
|
10
|
+
const scopes = /* @__PURE__ */ new Set();
|
|
11
|
+
for (const ref of formulaRefs(calc.formula)) {
|
|
12
|
+
if (ref.of !== void 0) scopes.add(ref.of);
|
|
13
|
+
for (const s of lineScopes(query, ref.ref, seen)) scopes.add(s);
|
|
14
|
+
}
|
|
15
|
+
return scopes;
|
|
16
|
+
}
|
|
17
|
+
/** The calcs that read around their row, themselves or through another calc. */
|
|
18
|
+
function aroundCalcs(query) {
|
|
19
|
+
return new Set((query.calc ?? []).filter((c) => lineScopes(query, c.id).size > 0).map((c) => c.id));
|
|
20
|
+
}
|
|
21
|
+
/** The steps a scope keeps: every step for the row and the period before, the time steps for a bucket, none for the total. */
|
|
22
|
+
function scopeSteps(steps, scope) {
|
|
23
|
+
if (scope === "total") return [];
|
|
24
|
+
if (scope === "bucket") return steps.filter((s) => "bucket" in s);
|
|
25
|
+
return [...steps];
|
|
26
|
+
}
|
|
27
|
+
/** A query's measures alone: what one scope reads. */
|
|
28
|
+
function measuresOnly(query) {
|
|
29
|
+
const { calc: _calc, ...rest } = query;
|
|
30
|
+
return rest;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Every line of the tile on a drill, from each scope's measures (in the tile's measure order):
|
|
34
|
+
* its measures as read on the drill's rows, its calcs worked out from the scopes they read.
|
|
35
|
+
*/
|
|
36
|
+
function scopedLines(query, cells) {
|
|
37
|
+
const { row, ...refs } = cells;
|
|
38
|
+
const values = valueRow(query, row, refs);
|
|
39
|
+
const ids = [...query.measures.map((m) => m.id), ...(query.calc ?? []).map((c) => c.id)];
|
|
40
|
+
return new Map(ids.map((id, i) => [id, values[i] ?? null]));
|
|
41
|
+
}
|
|
42
|
+
/** A formula's parts, each once: the line it reads and the scope it reads it in. */
|
|
43
|
+
function formulaParts(query, id) {
|
|
44
|
+
const calc = calcOf(query, id);
|
|
45
|
+
if (calc === void 0) return [];
|
|
46
|
+
const parts = formulaRefs(calc.formula).map((r) => ({
|
|
47
|
+
entry: r.ref,
|
|
48
|
+
scope: r.of ?? "row"
|
|
49
|
+
}));
|
|
50
|
+
const seen = /* @__PURE__ */ new Set();
|
|
51
|
+
return parts.filter((p) => {
|
|
52
|
+
const key = `${p.entry} ${p.scope}`;
|
|
53
|
+
if (seen.has(key)) return false;
|
|
54
|
+
seen.add(key);
|
|
55
|
+
return true;
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
export { aroundCalcs, formulaParts, lineScopes, measuresOnly, scopeSteps, scopedLines };
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { DrillReason } from "../../error/codes.mjs";
|
|
2
|
+
import { ResultCell, ResultColumn, ResultRows } from "../../result/types.mjs";
|
|
3
|
+
import { Span } from "../../time/range.mjs";
|
|
4
|
+
import { Calendar } from "../types.mjs";
|
|
5
|
+
import { Cuts, Selection } from "./types.mjs";
|
|
6
|
+
/** An answer as it was drawn: its columns and rows, its resolved range and its calendar. */
|
|
7
|
+
export type Drawn = {
|
|
8
|
+
columns: readonly ResultColumn[];
|
|
9
|
+
rows: ResultRows;
|
|
10
|
+
span?: Span;
|
|
11
|
+
calendar: Calendar;
|
|
12
|
+
/** The query has a limit of its own with Other: an all-Other row is that limit's Other. */
|
|
13
|
+
limited?: boolean;
|
|
14
|
+
/** What each dimension's own limit kept, from the engine (`Ran.cuts`). */
|
|
15
|
+
cuts?: Cuts;
|
|
16
|
+
/** The answer was cut at the row cap: no kept set read from it is whole. */
|
|
17
|
+
truncated?: boolean;
|
|
18
|
+
/** The search the answer was read under, which its Other keeps. */
|
|
19
|
+
search?: {
|
|
20
|
+
member: string;
|
|
21
|
+
text: string;
|
|
22
|
+
};
|
|
23
|
+
};
|
|
24
|
+
/** A selection, or why a mark does not open. */
|
|
25
|
+
export type Picked = {
|
|
26
|
+
selection: Selection;
|
|
27
|
+
} | {
|
|
28
|
+
refused: DrillReason;
|
|
29
|
+
};
|
|
30
|
+
/**
|
|
31
|
+
* What a click on one row of an answer selects: its time bucket's exact window, the values of
|
|
32
|
+
* the fields it groups by (a total's folded fields left out, so a total selects its row, its
|
|
33
|
+
* column or the whole tile), and Other with the exact groups its limit kept. Refused, with the
|
|
34
|
+
* reason, for a row that cannot drill: a field the answer does not name, or an Other whose
|
|
35
|
+
* kept set is too large or not whole.
|
|
36
|
+
*/
|
|
37
|
+
export declare function rowSelection(drawn: Drawn, row: readonly ResultCell[]): Picked;
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
import { isMark } from "../../result/marks.mjs";
|
|
2
|
+
import { nextBucketStart } from "../../time/bucket.mjs";
|
|
3
|
+
import { formatTextTime, parseDate, parseTextTime } from "../../time/instant.mjs";
|
|
4
|
+
import { startOfDate } from "../../time/zone.mjs";
|
|
5
|
+
// src/query/drill/selection.ts
|
|
6
|
+
/** The instant a bucket's label starts at: a local date in the zone, or an hour's instant. */
|
|
7
|
+
function labelStart(label, grain, calendar) {
|
|
8
|
+
if (grain === "hour") return parseTextTime(label);
|
|
9
|
+
const date = parseDate(label);
|
|
10
|
+
return date === void 0 ? void 0 : startOfDate(date, calendar.timezone);
|
|
11
|
+
}
|
|
12
|
+
/** A time point's step: its bucket's exact window from the engine's calendar, cut to the range. */
|
|
13
|
+
function bucketStep(drawn, column, cell) {
|
|
14
|
+
if (column.member === void 0 || column.grain === void 0 || typeof cell !== "string") return void 0;
|
|
15
|
+
const start = labelStart(cell, column.grain, drawn.calendar);
|
|
16
|
+
if (start === void 0) return void 0;
|
|
17
|
+
const end = nextBucketStart(start, column.grain, drawn.calendar);
|
|
18
|
+
const from = Math.max(start, drawn.span?.from ?? start);
|
|
19
|
+
const to = Math.min(end, drawn.span?.to ?? end);
|
|
20
|
+
return { bucket: {
|
|
21
|
+
member: column.member,
|
|
22
|
+
from: formatTextTime(from),
|
|
23
|
+
to: formatTextTime(to),
|
|
24
|
+
grain: column.grain
|
|
25
|
+
} };
|
|
26
|
+
}
|
|
27
|
+
/** The group values and Other marks of a row's dimension cells; undefined when one cannot drill. */
|
|
28
|
+
function groupParts(columns, row) {
|
|
29
|
+
const parts = {
|
|
30
|
+
group: [],
|
|
31
|
+
others: []
|
|
32
|
+
};
|
|
33
|
+
for (const [i, column] of columns.entries()) {
|
|
34
|
+
const cell = row[i] ?? null;
|
|
35
|
+
if (!column.id.startsWith("d")) continue;
|
|
36
|
+
if (column.member === void 0) return void 0;
|
|
37
|
+
if (isMark(cell)) {
|
|
38
|
+
if (cell.mark === "other") parts.others.push(i);
|
|
39
|
+
} else if (typeof cell === "object" && cell !== null) return void 0;
|
|
40
|
+
else parts.group.push({
|
|
41
|
+
member: column.member,
|
|
42
|
+
value: cell
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
return parts;
|
|
46
|
+
}
|
|
47
|
+
/** A row's plain values at some columns; undefined when one is a mark or an exact number. */
|
|
48
|
+
function plainAt(row, at) {
|
|
49
|
+
const cells = at.map((i) => row[i] ?? null);
|
|
50
|
+
return cells.every((c) => typeof c !== "object" || c === null) ? cells : void 0;
|
|
51
|
+
}
|
|
52
|
+
/** The longest a kept set may be as text, so a sealed selection stays inside the path's cap. */
|
|
53
|
+
const MAX_KEPT_TEXT = 4e3;
|
|
54
|
+
/** An Other step from its kept tuples; refused past `MAX_KEPT_GROUPS` or the text cap. */
|
|
55
|
+
function keptStep(members, kept) {
|
|
56
|
+
if (kept.length > 50) return { refused: "OTHER_TOO_LARGE" };
|
|
57
|
+
if (JSON.stringify(kept).length > MAX_KEPT_TEXT) return { refused: "OTHER_TOO_LARGE" };
|
|
58
|
+
return { steps: [{ other: {
|
|
59
|
+
members,
|
|
60
|
+
kept
|
|
61
|
+
} }] };
|
|
62
|
+
}
|
|
63
|
+
/** A dimension's own limit's Other: the rows outside every value the engine kept for it. */
|
|
64
|
+
function segmentStep(drawn, at) {
|
|
65
|
+
const column = drawn.columns[at];
|
|
66
|
+
const index = drawn.columns.slice(0, at).filter((c) => c.id.startsWith("d")).length;
|
|
67
|
+
const kept = drawn.cuts?.segments.find(([i]) => i === index)?.[1];
|
|
68
|
+
if (column?.member === void 0 || kept === void 0) return { refused: "OTHER_CUT" };
|
|
69
|
+
const values = kept.flatMap((v) => typeof v === "object" && v !== null ? [] : [[v]]);
|
|
70
|
+
if (values.length !== kept.length) return { refused: "OTHER_CUT" };
|
|
71
|
+
return keptStep([column.member], values);
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* The query's own limit's Other: the rows in none of the groups it kept. Its rows are every
|
|
75
|
+
* group it kept, so the kept set is read from them, but only when that is the whole set: not
|
|
76
|
+
* past the row cap, and not when a kept group is itself a dimension's Other (two cuts).
|
|
77
|
+
*/
|
|
78
|
+
function queryOtherStep(drawn, at) {
|
|
79
|
+
if (drawn.truncated === true) return { refused: "OTHER_CUT" };
|
|
80
|
+
const kept = /* @__PURE__ */ new Map();
|
|
81
|
+
for (const row of drawn.rows) {
|
|
82
|
+
if (at.every((i) => isMark(row[i] ?? null))) continue;
|
|
83
|
+
const values = plainAt(row, at);
|
|
84
|
+
if (values === void 0) return { refused: "OTHER_CUT" };
|
|
85
|
+
kept.set(JSON.stringify(values), values);
|
|
86
|
+
}
|
|
87
|
+
const members = at.map((i) => drawn.columns[i]?.member);
|
|
88
|
+
if (members.some((m) => m === void 0)) return { refused: "OTHER_CUT" };
|
|
89
|
+
return keptStep(members.filter((m) => m !== void 0), [...kept.values()]);
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* An Other row's steps: the query limit's Other when every dimension is Other and the query has
|
|
93
|
+
* a limit of its own, else each dimension's own Other; under a search, the search too, so the
|
|
94
|
+
* Other of a searched Breakdown stays the searched rows. Refused when a kept set is not whole.
|
|
95
|
+
*/
|
|
96
|
+
function otherSteps(drawn, others, dims) {
|
|
97
|
+
if (drawn.truncated === true) return { refused: "OTHER_CUT" };
|
|
98
|
+
const search = drawn.search === void 0 ? [] : [{ search: drawn.search }];
|
|
99
|
+
const parts = drawn.limited === true && others.length === dims ? [queryOtherStep(drawn, others)] : others.map((i) => segmentStep(drawn, i));
|
|
100
|
+
const refused = parts.find((p) => "refused" in p);
|
|
101
|
+
if (refused !== void 0) return refused;
|
|
102
|
+
return { steps: [...search, ...parts.flatMap((p) => "steps" in p ? p.steps : [])] };
|
|
103
|
+
}
|
|
104
|
+
/** The row's own value of each line, by column id: what was clicked. */
|
|
105
|
+
function clickedOf(columns, row) {
|
|
106
|
+
const clicked = {};
|
|
107
|
+
for (const [i, column] of columns.entries()) {
|
|
108
|
+
const cell = row[i] ?? null;
|
|
109
|
+
if ((column.id.startsWith("m") || column.id.startsWith("c")) && !isMark(cell)) clicked[column.id] = cell;
|
|
110
|
+
}
|
|
111
|
+
return clicked;
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* What a click on one row of an answer selects: its time bucket's exact window, the values of
|
|
115
|
+
* the fields it groups by (a total's folded fields left out, so a total selects its row, its
|
|
116
|
+
* column or the whole tile), and Other with the exact groups its limit kept. Refused, with the
|
|
117
|
+
* reason, for a row that cannot drill: a field the answer does not name, or an Other whose
|
|
118
|
+
* kept set is too large or not whole.
|
|
119
|
+
*/
|
|
120
|
+
function rowSelection(drawn, row) {
|
|
121
|
+
const time = timeSteps(drawn, row);
|
|
122
|
+
const parts = groupParts(drawn.columns, row);
|
|
123
|
+
if (time === void 0 || parts === void 0) return { refused: "OTHER_CUT" };
|
|
124
|
+
const dims = drawn.columns.filter((c) => c.id.startsWith("d")).length;
|
|
125
|
+
const other = parts.others.length === 0 ? { steps: [] } : otherSteps(drawn, parts.others, dims);
|
|
126
|
+
if ("refused" in other) return other;
|
|
127
|
+
return { selection: {
|
|
128
|
+
steps: [
|
|
129
|
+
...time,
|
|
130
|
+
...parts.group.length === 0 ? [] : [{ group: parts.group }],
|
|
131
|
+
...other.steps
|
|
132
|
+
],
|
|
133
|
+
clicked: clickedOf(drawn.columns, row)
|
|
134
|
+
} };
|
|
135
|
+
}
|
|
136
|
+
/** A row's time step, if the answer has a bucket column; undefined when its label does not read. */
|
|
137
|
+
function timeSteps(drawn, row) {
|
|
138
|
+
const at = drawn.columns.findIndex((c) => c.id === "t");
|
|
139
|
+
const bucket = drawn.columns[at];
|
|
140
|
+
if (bucket === void 0) return [];
|
|
141
|
+
const step = bucketStep(drawn, bucket, row[at] ?? null);
|
|
142
|
+
return step === void 0 ? void 0 : [step];
|
|
143
|
+
}
|
|
144
|
+
export { rowSelection };
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { DrillStep, Selection } from "./types.mjs";
|
|
2
|
+
/** The fields a step names. */
|
|
3
|
+
export declare function stepMembers(step: DrillStep): readonly string[];
|
|
4
|
+
/** The fields a path fixes to one value, and those it cuts an Other on (Breakdown offers those again). */
|
|
5
|
+
export declare function pathFields(steps: readonly DrillStep[]): {
|
|
6
|
+
fixed: string[];
|
|
7
|
+
other: string[];
|
|
8
|
+
};
|
|
9
|
+
/**
|
|
10
|
+
* Which selections of a path stay, by index: a click on a field the path already fixes replaces
|
|
11
|
+
* that step and drops the ones after it, so a path never fixes a field twice. Under Other the
|
|
12
|
+
* same field may be clicked again: it narrows to one of the values Other left out.
|
|
13
|
+
*/
|
|
14
|
+
export declare function keptPath(path: readonly Selection[]): number[];
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
// src/query/drill/steps.ts
|
|
2
|
+
/** The fields a step names. */
|
|
3
|
+
function stepMembers(step) {
|
|
4
|
+
if ("group" in step) return step.group.map((g) => g.member);
|
|
5
|
+
if ("search" in step) return [step.search.member];
|
|
6
|
+
return "other" in step ? step.other.members : [step.bucket.member];
|
|
7
|
+
}
|
|
8
|
+
/** The fields a selection fixes: its group values and time window, not its Other or search. */
|
|
9
|
+
const fixed = (selection) => new Set(selection.steps.filter((s) => !("other" in s) && !("search" in s)).flatMap((s) => stepMembers(s)));
|
|
10
|
+
/** The fields a path fixes to one value, and those it cuts an Other on (Breakdown offers those again). */
|
|
11
|
+
function pathFields(steps) {
|
|
12
|
+
return {
|
|
13
|
+
fixed: steps.flatMap((s) => "group" in s || "bucket" in s ? stepMembers(s) : []),
|
|
14
|
+
other: steps.flatMap((s) => "other" in s ? s.other.members : [])
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Which selections of a path stay, by index: a click on a field the path already fixes replaces
|
|
19
|
+
* that step and drops the ones after it, so a path never fixes a field twice. Under Other the
|
|
20
|
+
* same field may be clicked again: it narrows to one of the values Other left out.
|
|
21
|
+
*/
|
|
22
|
+
function keptPath(path) {
|
|
23
|
+
const kept = [];
|
|
24
|
+
for (const [i, selection] of path.entries()) {
|
|
25
|
+
const fields = fixed(selection);
|
|
26
|
+
const clash = kept.findIndex((k) => {
|
|
27
|
+
const earlier = path[k];
|
|
28
|
+
return earlier !== void 0 && [...fixed(earlier)].some((m) => fields.has(m));
|
|
29
|
+
});
|
|
30
|
+
if (clash >= 0) kept.splice(clash);
|
|
31
|
+
kept.push(i);
|
|
32
|
+
}
|
|
33
|
+
return kept;
|
|
34
|
+
}
|
|
35
|
+
export { keptPath, pathFields, stepMembers };
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { parseMemberId } from "../../model/member.mjs";
|
|
2
|
+
import { fold } from "../../value/casefold.mjs";
|
|
3
|
+
import { operandColumn } from "./operand.mjs";
|
|
4
|
+
// src/query/drill/synthetic-expr.ts
|
|
5
|
+
/**
|
|
6
|
+
* An own dimension's leaf as a condition on its column. A field that compares without case
|
|
7
|
+
* compares folded, its column and its values both, as a filter on it does.
|
|
8
|
+
*/
|
|
9
|
+
function leafCondition(def, source, f) {
|
|
10
|
+
if (!("member" in f) || !("values" in f) || !("op" in f)) return void 0;
|
|
11
|
+
const dim = ownColumn(def, source, f.member);
|
|
12
|
+
if (dim === void 0) return void 0;
|
|
13
|
+
if (dim.kind !== "string" || dim.collation !== "ci") return {
|
|
14
|
+
left: { col: dim.column },
|
|
15
|
+
op: f.op,
|
|
16
|
+
values: f.values
|
|
17
|
+
};
|
|
18
|
+
const values = f.values.map((v) => typeof v === "string" ? fold(v) : v);
|
|
19
|
+
return {
|
|
20
|
+
left: {
|
|
21
|
+
fn: "lower_fold",
|
|
22
|
+
args: [{ col: dim.column }]
|
|
23
|
+
},
|
|
24
|
+
op: f.op,
|
|
25
|
+
values
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
/** A member's own dimension when it reads a column of the source, not through a join or a clock. */
|
|
29
|
+
function ownColumn(def, source, member) {
|
|
30
|
+
const ref = parseMemberId(member);
|
|
31
|
+
const dim = ref?.source === source && ref.join === void 0 ? def.dimensions[ref.name] : void 0;
|
|
32
|
+
return dim !== void 0 && "column" in dim && dim.kind !== "time" ? dim : void 0;
|
|
33
|
+
}
|
|
34
|
+
const TRUE = { lit: true };
|
|
35
|
+
const FALSE = { lit: false };
|
|
36
|
+
/** `case when <test> then <yes> else <no> end`. */
|
|
37
|
+
const branch = (when, yes, no) => ({
|
|
38
|
+
case: [{
|
|
39
|
+
when,
|
|
40
|
+
then: yes
|
|
41
|
+
}],
|
|
42
|
+
else: no
|
|
43
|
+
});
|
|
44
|
+
/**
|
|
45
|
+
* A filter as a true-or-false expression, when it is leaves on own fields joined by all, any and
|
|
46
|
+
* not; undefined for anything else (a join, `some`, a viewer value).
|
|
47
|
+
*/
|
|
48
|
+
function filterExpr(def, source, f) {
|
|
49
|
+
if ("not" in f) {
|
|
50
|
+
const inner = filterExpr(def, source, f.not);
|
|
51
|
+
return inner === void 0 ? void 0 : branch(isTrue(inner), FALSE, TRUE);
|
|
52
|
+
}
|
|
53
|
+
if ("all" in f) return joined(def, source, f.all, true);
|
|
54
|
+
if ("any" in f) return joined(def, source, f.any, false);
|
|
55
|
+
const condition = leafCondition(def, source, f);
|
|
56
|
+
return condition === void 0 ? void 0 : branch(condition, TRUE, FALSE);
|
|
57
|
+
}
|
|
58
|
+
const isTrue = (left) => ({
|
|
59
|
+
left,
|
|
60
|
+
op: "equals",
|
|
61
|
+
values: [true]
|
|
62
|
+
});
|
|
63
|
+
/** Filters joined by all (`all`) or any, as one expression; undefined when one cannot be. */
|
|
64
|
+
function joined(def, source, list, all) {
|
|
65
|
+
const parts = list.flatMap((p) => filterExpr(def, source, p) ?? []);
|
|
66
|
+
if (parts.length !== list.length) return;
|
|
67
|
+
return parts.reduceRight((rest, part) => branch(isTrue(part), all ? rest : TRUE, all ? FALSE : rest), all ? TRUE : FALSE);
|
|
68
|
+
}
|
|
69
|
+
/** A ratio's numerator's `where`, as a true-or-false expression, when it has one that can be. */
|
|
70
|
+
function numeratorExpr(def, source, numerator) {
|
|
71
|
+
const top = def.measures[numerator];
|
|
72
|
+
if (top === void 0) return void 0;
|
|
73
|
+
const where = "where" in top ? top.where : void 0;
|
|
74
|
+
const holds = where === void 0 ? TRUE : filterExpr(def, source, where);
|
|
75
|
+
if (holds === void 0) return void 0;
|
|
76
|
+
const operand = operandColumn(top);
|
|
77
|
+
if (operand === void 0) return holds;
|
|
78
|
+
const set = branch({
|
|
79
|
+
left: { col: operand },
|
|
80
|
+
op: "set",
|
|
81
|
+
values: []
|
|
82
|
+
}, TRUE, FALSE);
|
|
83
|
+
return branch(isTrue(holds), set, FALSE);
|
|
84
|
+
}
|
|
85
|
+
export { FALSE, TRUE, branch, numeratorExpr };
|