@malloy-publisher/sdk 0.10.1 → 0.10.3
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/dist/{RenderedResult-vf6tu8tf.es.js → RenderedResult-B_sW7neX.es.js} +329 -266
- package/dist/RenderedResult-BlYdT4lz.cjs.js +179 -0
- package/dist/{ServerProvider-B6RLAbgV.es.js → ServerProvider-BN399NH6.es.js} +4 -4
- package/dist/builder/index.cjs.js +1 -1
- package/dist/builder/index.es.js +3 -3
- package/dist/client/api.d.ts +6 -6
- package/dist/client/index.es.js +1 -1
- package/dist/components/Dashboard/DashboardView.d.ts +10 -3
- package/dist/components/Dashboard/DownloadRowsCsv.d.ts +10 -0
- package/dist/components/Dashboard/RowsDialog.d.ts +63 -21
- package/dist/components/Dashboard/index.d.ts +2 -0
- package/dist/components/Dashboard/rowsCsv.d.ts +26 -0
- package/dist/components/RenderedResult/ResultContainer.d.ts +2 -3
- package/dist/components/RenderedResult/resultSizing.d.ts +2 -0
- package/dist/components/Report/Report.d.ts +8 -1
- package/dist/components/drill/drillClauses.d.ts +34 -0
- package/dist/components/drill/drillClickRelay.d.ts +36 -0
- package/dist/components/drill/resolveDrill.d.ts +29 -0
- package/dist/components/drill/rowsTarget.d.ts +114 -0
- package/dist/components/drill/useDrill.d.ts +24 -12
- package/dist/index.cjs.js +13 -11
- package/dist/index.es.js +7331 -6886
- package/dist/{useCompiledDocument-C995sQ3j.cjs.js → useCompiledDocument-CyPOSStA.cjs.js} +31 -31
- package/dist/{useCompiledDocument-CLbTFtsJ.es.js → useCompiledDocument-xJRD5DRP.es.js} +1588 -1575
- package/dist/utils/malloyString.d.ts +2 -1
- package/package.json +1 -1
- package/src/components/Dashboard/DashboardView.tsx +54 -28
- package/src/components/Dashboard/DownloadRowsCsv.tsx +152 -0
- package/src/components/Dashboard/RowsDialog.actions.spec.tsx +250 -0
- package/src/components/Dashboard/RowsDialog.drill.compile.spec.ts +672 -0
- package/src/components/Dashboard/RowsDialog.spec.ts +38 -11
- package/src/components/Dashboard/RowsDialog.tsx +194 -40
- package/src/components/Dashboard/index.ts +2 -0
- package/src/components/Dashboard/rowsCsv.spec.ts +105 -0
- package/src/components/Dashboard/rowsCsv.ts +107 -0
- package/src/components/RenderedResult/RenderedResult.tsx +44 -7
- package/src/components/RenderedResult/ResultContainer.tsx +2 -2
- package/src/components/RenderedResult/resultSizing.ts +3 -0
- package/src/components/Report/Report.tsx +9 -0
- package/src/components/drill/drillClauses.ts +201 -0
- package/src/components/drill/drillClickRelay.spec.ts +122 -0
- package/src/components/drill/drillClickRelay.ts +106 -0
- package/src/components/drill/resolveDrill.ts +29 -0
- package/src/components/drill/rowsTarget.ts +245 -0
- package/src/components/drill/useDrill.spec.tsx +311 -15
- package/src/components/drill/useDrill.tsx +206 -67
- package/src/components/legacy/malloynb/NotebookCell.tsx +0 -7
- package/src/utils/malloyString.ts +21 -2
- package/dist/RenderedResult-B5f4qOjp.cjs.js +0 -171
|
@@ -4,7 +4,6 @@
|
|
|
4
4
|
import { Box } from "@mui/material";
|
|
5
5
|
import React, {
|
|
6
6
|
Suspense,
|
|
7
|
-
useCallback,
|
|
8
7
|
useEffect,
|
|
9
8
|
useLayoutEffect,
|
|
10
9
|
useMemo,
|
|
@@ -22,6 +21,7 @@ import {
|
|
|
22
21
|
contentNodeDepth,
|
|
23
22
|
measureContentHeight,
|
|
24
23
|
remeasuresAfterReady,
|
|
24
|
+
RESULT_SCROLLER_CLASS,
|
|
25
25
|
resultSizing,
|
|
26
26
|
type ResultSizing,
|
|
27
27
|
TIGHT_ATTR,
|
|
@@ -39,7 +39,11 @@ import {
|
|
|
39
39
|
markDrillableCells,
|
|
40
40
|
type DrillMetadataSource,
|
|
41
41
|
} from "../drill/markDrillableCells";
|
|
42
|
-
import
|
|
42
|
+
import {
|
|
43
|
+
drillClickRelay,
|
|
44
|
+
reportRowsWithoutFilters,
|
|
45
|
+
type RendererDrill,
|
|
46
|
+
} from "../drill/drillClickRelay";
|
|
43
47
|
import { loadMalloyTag } from "../ReportBuilder/loadMalloy";
|
|
44
48
|
import type { DrillBinding } from "../drill/useDrill";
|
|
45
49
|
|
|
@@ -120,6 +124,7 @@ const FILL_ATTR = "data-publisher-fill";
|
|
|
120
124
|
const createRenderer = async (
|
|
121
125
|
theme: ResolvedTheme,
|
|
122
126
|
onClick?: DrillBinding["onClick"],
|
|
127
|
+
onDrill?: (data: RendererDrill) => void,
|
|
123
128
|
): Promise<MalloyVizHandle> => {
|
|
124
129
|
if (typeof window === "undefined") {
|
|
125
130
|
throw new Error("MalloyRenderer can only be used in browser environment");
|
|
@@ -128,6 +133,9 @@ const createRenderer = async (
|
|
|
128
133
|
const { MalloyRenderer } = await loadMalloyRenderer();
|
|
129
134
|
const renderer = new MalloyRenderer({
|
|
130
135
|
onClick,
|
|
136
|
+
// A table cell's row reaches the host only through the renderer's drill,
|
|
137
|
+
// which it reports only with `enableDrill` set; see `drillClickRelay`.
|
|
138
|
+
...(onDrill ? { onDrill, tableConfig: { enableDrill: true } } : {}),
|
|
131
139
|
vegaConfigOverride: buildVegaThemeOverride(theme),
|
|
132
140
|
// Pass the explicit theme so table chrome and dashboard tiles
|
|
133
141
|
// pick up the operator's colours directly. Without this the
|
|
@@ -391,6 +399,14 @@ div.malloy-render .malloy-dashboard .dashboard-row-header {
|
|
|
391
399
|
color: var(--publisher-drill-link) !important;
|
|
392
400
|
text-decoration: underline;
|
|
393
401
|
}
|
|
402
|
+
/* The renderer's own drill, on so a click can report its row, paints the
|
|
403
|
+
whole hovered row as a link. Which cells click is the marking above. */
|
|
404
|
+
.malloy-render .malloy-table .td.highlight {
|
|
405
|
+
color: inherit;
|
|
406
|
+
}
|
|
407
|
+
.malloy-render .malloy-table .td.highlight:not(.${DRILL_CELL_CLASS}) {
|
|
408
|
+
cursor: inherit;
|
|
409
|
+
}
|
|
394
410
|
`;
|
|
395
411
|
|
|
396
412
|
/**
|
|
@@ -431,9 +447,11 @@ function RenderedResultInner({
|
|
|
431
447
|
// one safely.
|
|
432
448
|
const drillRef = useRef(drill);
|
|
433
449
|
drillRef.current = drill;
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
450
|
+
// The click and the row the renderer reports for it, joined; see the relay.
|
|
451
|
+
const relay = useMemo(
|
|
452
|
+
() => drillClickRelay((payload) => drillRef.current?.onClick(payload)),
|
|
453
|
+
[],
|
|
454
|
+
);
|
|
437
455
|
// Whether any drill is wired at all, which is what the effect reacts to: a
|
|
438
456
|
// surface that gains or loses the capability has to (re)mark its cells, but
|
|
439
457
|
// a new inline binding with the same capability must not rebuild anything.
|
|
@@ -548,10 +566,22 @@ function RenderedResultInner({
|
|
|
548
566
|
// A filled table root is as tall as its box, which would read back as its content.
|
|
549
567
|
const stretched = element.hasAttribute(FILL_ATTR);
|
|
550
568
|
if (stretched) element.removeAttribute(FILL_ATTR);
|
|
569
|
+
// Measured as if the tile's scroller shows no scrollbar. Its scrollbar
|
|
570
|
+
// narrows the result, a narrower table can wrap its header onto a
|
|
571
|
+
// second line, and the taller height reported back is what decides
|
|
572
|
+
// whether the scroller needs that scrollbar. Measured at whichever
|
|
573
|
+
// width is on screen, a table at the threshold reports two heights
|
|
574
|
+
// in turn and its tile grows and shrinks on every frame.
|
|
575
|
+
const scroller = element.closest<HTMLElement>(
|
|
576
|
+
`.${RESULT_SCROLLER_CLASS}`,
|
|
577
|
+
);
|
|
578
|
+
const scrollerOverflow = scroller?.style.overflowY;
|
|
579
|
+
if (scroller) scroller.style.overflowY = "hidden";
|
|
551
580
|
const renderedHeight = measureContentHeight(
|
|
552
581
|
root,
|
|
553
582
|
contentNodeDepth(renderAs),
|
|
554
583
|
);
|
|
584
|
+
if (scroller) scroller.style.overflowY = scrollerOverflow ?? "";
|
|
555
585
|
if (stretched) element.setAttribute(FILL_ATTR, "");
|
|
556
586
|
|
|
557
587
|
if (renderedHeight > 0) {
|
|
@@ -591,7 +621,11 @@ function RenderedResultInner({
|
|
|
591
621
|
if (!isCurrent()) return;
|
|
592
622
|
|
|
593
623
|
try {
|
|
594
|
-
viz = await createRenderer(
|
|
624
|
+
viz = await createRenderer(
|
|
625
|
+
effectiveTheme,
|
|
626
|
+
relay.onClick,
|
|
627
|
+
drillable ? relay.onDrill : undefined,
|
|
628
|
+
);
|
|
595
629
|
} catch (error) {
|
|
596
630
|
console.error("Failed to create renderer:", error);
|
|
597
631
|
return;
|
|
@@ -692,6 +726,9 @@ function RenderedResultInner({
|
|
|
692
726
|
try {
|
|
693
727
|
const metadata = viz.getMetadata();
|
|
694
728
|
const root = metadata?.getRootField();
|
|
729
|
+
// A row's clauses are wanted only for its dimensions; see
|
|
730
|
+
// `reportRowsWithoutFilters`.
|
|
731
|
+
if (drillable) reportRowsWithoutFilters(root);
|
|
695
732
|
renderAs = root?.renderAs();
|
|
696
733
|
// The plugin's own `sizingStrategy`, which decides this outright
|
|
697
734
|
// where there is one. Read from the PLUGIN; the field entry's
|
|
@@ -971,7 +1008,7 @@ function RenderedResultInner({
|
|
|
971
1008
|
};
|
|
972
1009
|
}, [
|
|
973
1010
|
result,
|
|
974
|
-
|
|
1011
|
+
relay,
|
|
975
1012
|
drillable,
|
|
976
1013
|
onSizeChange,
|
|
977
1014
|
onSizing,
|
|
@@ -19,14 +19,14 @@ import { Loading, LOADING_COPY } from "../Loading";
|
|
|
19
19
|
import { warmMalloyRenderer } from "./loadRenderer";
|
|
20
20
|
import { summarizeRenderLogs } from "./renderLogs";
|
|
21
21
|
import {
|
|
22
|
+
RESULT_SCROLLER_CLASS,
|
|
22
23
|
resolveResultHeight,
|
|
23
24
|
resultCap,
|
|
24
25
|
type ResultSizing,
|
|
25
26
|
TIGHT_ATTR,
|
|
26
27
|
} from "./resultSizing";
|
|
27
28
|
|
|
28
|
-
|
|
29
|
-
export const RESULT_SCROLLER_CLASS = "publisher-result-scroller";
|
|
29
|
+
export { RESULT_SCROLLER_CLASS };
|
|
30
30
|
|
|
31
31
|
const RenderedResult = lazy(() => import("../RenderedResult/RenderedResult"));
|
|
32
32
|
|
|
@@ -377,3 +377,6 @@ export function resolveResultHeight({
|
|
|
377
377
|
* (`ResultContainer` sets it); the renderer overrides in `RenderedResult` read it.
|
|
378
378
|
*/
|
|
379
379
|
export const TIGHT_ATTR = "data-publisher-tight";
|
|
380
|
+
|
|
381
|
+
/** The box a dashboard tile's result scrolls in when it is taller than its cap. */
|
|
382
|
+
export const RESULT_SCROLLER_CLASS = "publisher-result-scroller";
|
|
@@ -9,6 +9,7 @@ import { parseResourceUri } from "../../utils/formatting";
|
|
|
9
9
|
import { ApiErrorDisplay } from "../ApiErrorDisplay";
|
|
10
10
|
import type { NavigationClick } from "../click_helper";
|
|
11
11
|
import { DashboardView } from "../Dashboard/DashboardView";
|
|
12
|
+
import type { RowsActions } from "../Dashboard/RowsDialog";
|
|
12
13
|
import type { TileChrome } from "../Dashboard/TileCard";
|
|
13
14
|
import type { DrillNavigation } from "../drill";
|
|
14
15
|
import { Loading, LOADING_COPY } from "../Loading";
|
|
@@ -89,6 +90,12 @@ export interface ReportProps {
|
|
|
89
90
|
* the end of the title's line rather than on a row of their own above it.
|
|
90
91
|
*/
|
|
91
92
|
actions?: ReactNode;
|
|
93
|
+
/**
|
|
94
|
+
* The host's actions for the rows behind a clicked value (a download),
|
|
95
|
+
* drawn in the rows dialog's title row. Omitted, the dialog offers its own
|
|
96
|
+
* Download CSV; return `null` for none.
|
|
97
|
+
*/
|
|
98
|
+
rowsActions?: RowsActions;
|
|
92
99
|
}
|
|
93
100
|
|
|
94
101
|
/**
|
|
@@ -112,6 +119,7 @@ export function Report({
|
|
|
112
119
|
chrome,
|
|
113
120
|
manifestStaleTime,
|
|
114
121
|
actions,
|
|
122
|
+
rowsActions,
|
|
115
123
|
}: ReportProps) {
|
|
116
124
|
const parsed = parseResourceUri(resourceUri);
|
|
117
125
|
const { apiClients } = useServer();
|
|
@@ -240,6 +248,7 @@ export function Report({
|
|
|
240
248
|
maxResultSize={maxResultSize}
|
|
241
249
|
chrome={chrome}
|
|
242
250
|
{...(actions !== undefined ? { actions } : {})}
|
|
251
|
+
{...(rowsActions !== undefined ? { rowsActions } : {})}
|
|
243
252
|
/>
|
|
244
253
|
);
|
|
245
254
|
return (
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
// Copyright (c) Credible Data Inc.
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
|
|
4
|
+
import type * as Malloy from "@malloydata/malloy-interfaces";
|
|
5
|
+
import { escapeMalloyString } from "../../utils/malloyString";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* The clicked row of a table, as the renderer reports it (its `onDrill`
|
|
9
|
+
* `DrillData.stableDrillClauses`): each dimension's expression and value,
|
|
10
|
+
* enclosing rows included. The click itself carries the clicked field alone.
|
|
11
|
+
*
|
|
12
|
+
* The rows query reads each dimension's value from here and writes it itself
|
|
13
|
+
* (`literalText`), rather than taking the renderer's own `DrillData.query`,
|
|
14
|
+
* which Malloy 0.0.435 cannot run as written: it puts a string between double
|
|
15
|
+
* quotes without escaping it, it reads every hour as `00`, and it writes
|
|
16
|
+
* quarter, week and minute literals (`@2026-Q1`) a filter refuses. What a
|
|
17
|
+
* filter does accept for every truncation is the truncated value itself at
|
|
18
|
+
* full precision, so that is what is written: a date as its day, a timestamp
|
|
19
|
+
* to the second, or to its fraction when it has one.
|
|
20
|
+
*
|
|
21
|
+
* Remove these workarounds, and take `DrillData.query` as written, once a
|
|
22
|
+
* Malloy release fixes the renderer's string quoting, hour digits and
|
|
23
|
+
* truncated literals.
|
|
24
|
+
*/
|
|
25
|
+
export type DrillClause = Malloy.DrillOperation;
|
|
26
|
+
|
|
27
|
+
/** One clause as the title reads it: `region` is `West`. */
|
|
28
|
+
export interface DrillClauseLabel {
|
|
29
|
+
field: string;
|
|
30
|
+
label: string;
|
|
31
|
+
/** The clause's field expression, to find the result field it names. */
|
|
32
|
+
expression?: Malloy.Expression;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** The clauses as the dialog title names them, each value as the table showed it. */
|
|
36
|
+
export function drillClauseLabels(
|
|
37
|
+
clauses: readonly DrillClause[],
|
|
38
|
+
timezone?: string,
|
|
39
|
+
): DrillClauseLabel[] {
|
|
40
|
+
return clauses.flatMap(({ filter }) => {
|
|
41
|
+
if (
|
|
42
|
+
filter.kind !== "literal_equality" ||
|
|
43
|
+
filter.expression.kind !== "field_reference"
|
|
44
|
+
)
|
|
45
|
+
return [];
|
|
46
|
+
const label = literalLabel(filter.value, timezone);
|
|
47
|
+
return label === undefined
|
|
48
|
+
? []
|
|
49
|
+
: [
|
|
50
|
+
{
|
|
51
|
+
field: filter.expression.name,
|
|
52
|
+
label,
|
|
53
|
+
expression: filter.expression,
|
|
54
|
+
},
|
|
55
|
+
];
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* A literal as a filter's right-hand side, a timestamp as its wall clock in
|
|
61
|
+
* `timezone`. Undefined for a null, which is written `is null` instead.
|
|
62
|
+
*/
|
|
63
|
+
export function literalText(
|
|
64
|
+
value: Malloy.LiteralValue,
|
|
65
|
+
timezone?: string,
|
|
66
|
+
): string | undefined {
|
|
67
|
+
switch (value.kind) {
|
|
68
|
+
case "string_literal":
|
|
69
|
+
return `'${escapeMalloyString(value.string_value)}'`;
|
|
70
|
+
case "number_literal":
|
|
71
|
+
// The renderer's own digits when it has them: a bigint above 2^53
|
|
72
|
+
// read as a JavaScript number names a different row.
|
|
73
|
+
if (value.string_value !== undefined)
|
|
74
|
+
return /^-?\d+(\.\d+)?$/.test(value.string_value)
|
|
75
|
+
? value.string_value
|
|
76
|
+
: undefined;
|
|
77
|
+
return Number.isFinite(value.number_value)
|
|
78
|
+
? String(value.number_value)
|
|
79
|
+
: undefined;
|
|
80
|
+
case "boolean_literal":
|
|
81
|
+
return String(value.boolean_value);
|
|
82
|
+
case "date_literal": {
|
|
83
|
+
const day = dateDigits(value.date_value);
|
|
84
|
+
return day && `@${day[0]}-${day[1]}-${day[2]}`;
|
|
85
|
+
}
|
|
86
|
+
case "timestamp_literal": {
|
|
87
|
+
const t = wallClock(value.timestamp_value, timezone);
|
|
88
|
+
// With its fraction: `=` against whole seconds matches nothing for
|
|
89
|
+
// a raw timestamp at `.250`.
|
|
90
|
+
return (
|
|
91
|
+
t &&
|
|
92
|
+
`@${t[0]}-${t[1]}-${t[2]} ${t[3]}:${t[4]}:${t[5]}${fraction(value.timestamp_value)}`
|
|
93
|
+
);
|
|
94
|
+
}
|
|
95
|
+
default:
|
|
96
|
+
return undefined;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** A value as the table printed it: a quarter as `2026-Q1`, an hour as `2026-02-03 23`. */
|
|
101
|
+
function literalLabel(
|
|
102
|
+
value: Malloy.LiteralValue,
|
|
103
|
+
timezone?: string,
|
|
104
|
+
): string | undefined {
|
|
105
|
+
switch (value.kind) {
|
|
106
|
+
case "string_literal":
|
|
107
|
+
return value.string_value;
|
|
108
|
+
case "null_literal":
|
|
109
|
+
return "null";
|
|
110
|
+
case "date_literal": {
|
|
111
|
+
const day = dateDigits(value.date_value);
|
|
112
|
+
return (
|
|
113
|
+
day &&
|
|
114
|
+
granularLabel(
|
|
115
|
+
[...day, "00", "00", "00"],
|
|
116
|
+
value.granularity ?? "day",
|
|
117
|
+
)
|
|
118
|
+
);
|
|
119
|
+
}
|
|
120
|
+
case "timestamp_literal": {
|
|
121
|
+
const t = wallClock(value.timestamp_value, timezone);
|
|
122
|
+
return (
|
|
123
|
+
t &&
|
|
124
|
+
granularLabel(t, value.granularity ?? "second") +
|
|
125
|
+
(value.granularity === undefined
|
|
126
|
+
? fraction(value.timestamp_value)
|
|
127
|
+
: "")
|
|
128
|
+
);
|
|
129
|
+
}
|
|
130
|
+
default:
|
|
131
|
+
return literalText(value);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
type WallClock = [string, string, string, string, string, string];
|
|
136
|
+
|
|
137
|
+
/** A date's digits: a date has no zone, so they are read as written. */
|
|
138
|
+
function dateDigits(text: string): [string, string, string] | undefined {
|
|
139
|
+
const day = /^(\d{4})-(\d{2})-(\d{2})/.exec(text);
|
|
140
|
+
return day ? [day[1], day[2], day[3]] : undefined;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* The fraction of a second an ISO instant carries, as `.250`, or empty. A zone
|
|
145
|
+
* moves whole minutes, so the digits are read as written.
|
|
146
|
+
*/
|
|
147
|
+
function fraction(iso: string): string {
|
|
148
|
+
const digits = /:\d{2}\.(\d+)/.exec(iso)?.[1]?.replace(/0+$/, "");
|
|
149
|
+
return digits ? `.${digits}` : "";
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/** An instant's wall clock in `timezone`, UTC when unset. */
|
|
153
|
+
function wallClock(iso: string, timezone?: string): WallClock | undefined {
|
|
154
|
+
const instant = new Date(iso);
|
|
155
|
+
if (Number.isNaN(instant.getTime())) return undefined;
|
|
156
|
+
let parts: Intl.DateTimeFormatPart[];
|
|
157
|
+
try {
|
|
158
|
+
parts = new Intl.DateTimeFormat("en-US", {
|
|
159
|
+
timeZone: timezone ?? "UTC",
|
|
160
|
+
hourCycle: "h23",
|
|
161
|
+
year: "numeric",
|
|
162
|
+
month: "2-digit",
|
|
163
|
+
day: "2-digit",
|
|
164
|
+
hour: "2-digit",
|
|
165
|
+
minute: "2-digit",
|
|
166
|
+
second: "2-digit",
|
|
167
|
+
}).formatToParts(instant);
|
|
168
|
+
} catch {
|
|
169
|
+
return undefined;
|
|
170
|
+
}
|
|
171
|
+
const part = (type: string) =>
|
|
172
|
+
parts.find((p) => p.type === type)?.value ?? "";
|
|
173
|
+
return [
|
|
174
|
+
part("year").padStart(4, "0"),
|
|
175
|
+
part("month"),
|
|
176
|
+
part("day"),
|
|
177
|
+
part("hour"),
|
|
178
|
+
part("minute"),
|
|
179
|
+
part("second"),
|
|
180
|
+
];
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
function granularLabel([y, mo, d, h, mi, s]: WallClock, granularity: string) {
|
|
184
|
+
switch (granularity) {
|
|
185
|
+
case "year":
|
|
186
|
+
return y;
|
|
187
|
+
case "quarter":
|
|
188
|
+
return `${y}-Q${Math.floor((Number(mo) - 1) / 3) + 1}`;
|
|
189
|
+
case "month":
|
|
190
|
+
return `${y}-${mo}`;
|
|
191
|
+
case "week":
|
|
192
|
+
case "day":
|
|
193
|
+
return `${y}-${mo}-${d}`;
|
|
194
|
+
case "hour":
|
|
195
|
+
return `${y}-${mo}-${d} ${h}`;
|
|
196
|
+
case "minute":
|
|
197
|
+
return `${y}-${mo}-${d} ${h}:${mi}`;
|
|
198
|
+
default:
|
|
199
|
+
return `${y}-${mo}-${d} ${h}:${mi}:${s}`;
|
|
200
|
+
}
|
|
201
|
+
}
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
// Copyright (c) Credible Data Inc.
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
|
|
4
|
+
import { describe, expect, it } from "bun:test";
|
|
5
|
+
import type { DrillClause } from "./drillClauses";
|
|
6
|
+
import { drillClickRelay, reportRowsWithoutFilters } from "./drillClickRelay";
|
|
7
|
+
import type { DrillClickPayload } from "./resolveDrill";
|
|
8
|
+
|
|
9
|
+
const REGION: DrillClause = {
|
|
10
|
+
filter: {
|
|
11
|
+
kind: "literal_equality",
|
|
12
|
+
expression: { kind: "field_reference", name: "region", path: ["v"] },
|
|
13
|
+
value: { kind: "string_literal", string_value: "West" },
|
|
14
|
+
},
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
function click(timezone?: () => string | undefined): DrillClickPayload {
|
|
18
|
+
return {
|
|
19
|
+
field: { name: "region", getEffectiveQueryTimezone: timezone },
|
|
20
|
+
value: "West",
|
|
21
|
+
} as unknown as DrillClickPayload;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** The relay, and every payload it forwarded once the held click is released. */
|
|
25
|
+
function relayed() {
|
|
26
|
+
const forwarded: DrillClickPayload[] = [];
|
|
27
|
+
const relay = drillClickRelay((payload) => forwarded.push(payload));
|
|
28
|
+
const settle = () => new Promise<void>((resolve) => queueMicrotask(resolve));
|
|
29
|
+
return { relay, forwarded, settle };
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
describe("drillClickRelay", () => {
|
|
33
|
+
it("puts the row the renderer reports in the same dispatch on the click", async () => {
|
|
34
|
+
const { relay, forwarded, settle } = relayed();
|
|
35
|
+
relay.onClick(click());
|
|
36
|
+
relay.onDrill({ stableDrillClauses: [REGION] });
|
|
37
|
+
expect(forwarded).toHaveLength(0);
|
|
38
|
+
await settle();
|
|
39
|
+
expect(forwarded).toHaveLength(1);
|
|
40
|
+
expect(forwarded[0].drillClauses).toEqual([REGION]);
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it("forwards a click the renderer reports no row for as it came", async () => {
|
|
44
|
+
const { relay, forwarded, settle } = relayed();
|
|
45
|
+
relay.onClick(click());
|
|
46
|
+
await settle();
|
|
47
|
+
expect(forwarded).toHaveLength(1);
|
|
48
|
+
expect(forwarded[0].drillClauses).toBeUndefined();
|
|
49
|
+
expect(forwarded[0].value).toBe("West");
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
it("leaves the clauses off when the row has no stable form", async () => {
|
|
53
|
+
const { relay, forwarded, settle } = relayed();
|
|
54
|
+
relay.onClick(click());
|
|
55
|
+
relay.onDrill({ stableDrillClauses: undefined });
|
|
56
|
+
await settle();
|
|
57
|
+
expect(forwarded[0].drillClauses).toBeUndefined();
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it("never puts one click's row on another click", async () => {
|
|
61
|
+
const { relay, forwarded, settle } = relayed();
|
|
62
|
+
// A row with no click held, then a click that gets no row.
|
|
63
|
+
relay.onDrill({ stableDrillClauses: [REGION] });
|
|
64
|
+
relay.onClick(click());
|
|
65
|
+
await settle();
|
|
66
|
+
// A row that arrives after its click went out.
|
|
67
|
+
relay.onDrill({ stableDrillClauses: [REGION] });
|
|
68
|
+
relay.onClick(click());
|
|
69
|
+
await settle();
|
|
70
|
+
expect(forwarded.map((p) => p.drillClauses)).toEqual([
|
|
71
|
+
undefined,
|
|
72
|
+
undefined,
|
|
73
|
+
]);
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it("reads the clicked field's query timezone, and survives a field that throws", async () => {
|
|
77
|
+
const { relay, forwarded, settle } = relayed();
|
|
78
|
+
relay.onClick(click(() => "Asia/Tokyo"));
|
|
79
|
+
relay.onClick(
|
|
80
|
+
click(() => {
|
|
81
|
+
throw new Error("no query");
|
|
82
|
+
}),
|
|
83
|
+
);
|
|
84
|
+
await settle();
|
|
85
|
+
expect(forwarded.map((p) => p.timezone)).toEqual([
|
|
86
|
+
"Asia/Tokyo",
|
|
87
|
+
undefined,
|
|
88
|
+
]);
|
|
89
|
+
});
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
describe("reportRowsWithoutFilters", () => {
|
|
93
|
+
it("has every repeated level, nested ones included, report no stable filters", () => {
|
|
94
|
+
const level = (fields: object[]) => ({
|
|
95
|
+
fields,
|
|
96
|
+
isArray: () => true,
|
|
97
|
+
get stableDrillFilters(): unknown {
|
|
98
|
+
return undefined;
|
|
99
|
+
},
|
|
100
|
+
});
|
|
101
|
+
const nested = level([{ name: "hour", isArray: () => false }]);
|
|
102
|
+
const root = level([{ name: "region", isArray: () => false }, nested]);
|
|
103
|
+
reportRowsWithoutFilters(root);
|
|
104
|
+
expect(root.stableDrillFilters).toEqual([]);
|
|
105
|
+
expect(nested.stableDrillFilters).toEqual([]);
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
it("leaves alone a field that is not a repeated level, and one that throws", () => {
|
|
109
|
+
const basic = { name: "region", isArray: () => false };
|
|
110
|
+
const broken = {
|
|
111
|
+
isArray: () => {
|
|
112
|
+
throw new Error("no type");
|
|
113
|
+
},
|
|
114
|
+
};
|
|
115
|
+
reportRowsWithoutFilters({
|
|
116
|
+
fields: [basic, broken],
|
|
117
|
+
isArray: () => true,
|
|
118
|
+
});
|
|
119
|
+
expect("stableDrillFilters" in basic).toBe(false);
|
|
120
|
+
expect("stableDrillFilters" in broken).toBe(false);
|
|
121
|
+
});
|
|
122
|
+
});
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
// Copyright (c) Credible Data Inc.
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
|
|
4
|
+
import type { DrillClause } from "./drillClauses";
|
|
5
|
+
import type { DrillClickPayload } from "./resolveDrill";
|
|
6
|
+
|
|
7
|
+
/** As much of the renderer's `DrillData` as the relay reads. */
|
|
8
|
+
export interface RendererDrill {
|
|
9
|
+
stableDrillClauses: DrillClause[] | undefined;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Joins the renderer's two reports of one table-cell click into one payload.
|
|
14
|
+
*
|
|
15
|
+
* `onClick` names the clicked field and value; only `onDrill` knows the row,
|
|
16
|
+
* and the renderer calls it only with `tableConfig.enableDrill` set. For a
|
|
17
|
+
* table cell both run inside the same event dispatch, `onClick` first (the
|
|
18
|
+
* cell's handler, then its row-cell wrapper's), so a click is held for one
|
|
19
|
+
* microtask and the row lands on it before `forward` sees it. A click with no
|
|
20
|
+
* row, such as a dashboard item, goes out as it came.
|
|
21
|
+
*/
|
|
22
|
+
export function drillClickRelay(
|
|
23
|
+
forward: (payload: DrillClickPayload) => void,
|
|
24
|
+
): {
|
|
25
|
+
onClick: (payload: DrillClickPayload) => void;
|
|
26
|
+
onDrill: (data: RendererDrill) => void;
|
|
27
|
+
} {
|
|
28
|
+
let held: DrillClickPayload | undefined;
|
|
29
|
+
return {
|
|
30
|
+
onClick: (payload) => {
|
|
31
|
+
const click: DrillClickPayload = {
|
|
32
|
+
...payload,
|
|
33
|
+
...timezoneOf(payload),
|
|
34
|
+
};
|
|
35
|
+
held = click;
|
|
36
|
+
queueMicrotask(() => {
|
|
37
|
+
if (held === click) held = undefined;
|
|
38
|
+
forward(click);
|
|
39
|
+
});
|
|
40
|
+
},
|
|
41
|
+
onDrill: (data) => {
|
|
42
|
+
const click = held;
|
|
43
|
+
held = undefined;
|
|
44
|
+
// Undefined when a clause has no stable form: the click then stands
|
|
45
|
+
// on its own value rather than on part of a row.
|
|
46
|
+
if (click && data.stableDrillClauses !== undefined)
|
|
47
|
+
click.drillClauses = data.stableDrillClauses;
|
|
48
|
+
},
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* The query timezone the clicked field was computed in, which a truncated
|
|
54
|
+
* timestamp in the row's clauses is spelled in. The renderer's field answers
|
|
55
|
+
* it; the narrowed payload type does not declare it.
|
|
56
|
+
*/
|
|
57
|
+
function timezoneOf(payload: DrillClickPayload): { timezone?: string } {
|
|
58
|
+
const field = payload.field as
|
|
59
|
+
| { getEffectiveQueryTimezone?: () => string | undefined }
|
|
60
|
+
| undefined;
|
|
61
|
+
try {
|
|
62
|
+
const timezone = field?.getEffectiveQueryTimezone?.();
|
|
63
|
+
return timezone ? { timezone } : {};
|
|
64
|
+
} catch {
|
|
65
|
+
return {};
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** A renderer result field, as much of it as {@link reportRowsWithoutFilters} reads. */
|
|
70
|
+
interface ResultLevel {
|
|
71
|
+
fields?: ResultLevel[];
|
|
72
|
+
isArray?: () => boolean;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Lets the renderer report a clicked row's dimensions even where a level's
|
|
77
|
+
* filter has no stable form.
|
|
78
|
+
*
|
|
79
|
+
* The renderer builds a row's clauses (`stableDrillClauses`) from each level's
|
|
80
|
+
* filters and then its dimensions, and gives up on the whole row when one
|
|
81
|
+
* filter has no stable form, which is any filter that reads a given
|
|
82
|
+
* (`where: region ~ $REGION`, what a report's filter control writes). The rows
|
|
83
|
+
* query takes every filter from the result's metadata as code instead
|
|
84
|
+
* (`rowsTarget.ts`), so the filters are not wanted from the clauses at all.
|
|
85
|
+
* Reporting none per level leaves the clauses as the row's dimensions alone,
|
|
86
|
+
* which never depend on a filter's form.
|
|
87
|
+
*
|
|
88
|
+
* Per result, on its own field objects; the renderer's classes are untouched.
|
|
89
|
+
*/
|
|
90
|
+
export function reportRowsWithoutFilters(root: object | undefined): void {
|
|
91
|
+
const visit = (level: ResultLevel) => {
|
|
92
|
+
let isArray = false;
|
|
93
|
+
try {
|
|
94
|
+
isArray = level.isArray?.() === true;
|
|
95
|
+
} catch {
|
|
96
|
+
isArray = false;
|
|
97
|
+
}
|
|
98
|
+
if (isArray && "stableDrillFilters" in level)
|
|
99
|
+
Object.defineProperty(level, "stableDrillFilters", {
|
|
100
|
+
configurable: true,
|
|
101
|
+
get: () => [],
|
|
102
|
+
});
|
|
103
|
+
for (const child of level.fields ?? []) visit(child);
|
|
104
|
+
};
|
|
105
|
+
if (root) visit(root as ResultLevel);
|
|
106
|
+
}
|
|
@@ -27,6 +27,8 @@ import {
|
|
|
27
27
|
filterInnerType,
|
|
28
28
|
isFilterType,
|
|
29
29
|
} from "../given/filterValue";
|
|
30
|
+
import type { DrillClause } from "./drillClauses";
|
|
31
|
+
import type { RowsDimension, RowsTarget } from "./rowsTarget";
|
|
30
32
|
|
|
31
33
|
/**
|
|
32
34
|
* The parsed-tag surface this module reads, structurally typed.
|
|
@@ -40,6 +42,8 @@ export interface DrillTagReader {
|
|
|
40
42
|
tag(...at: string[]): DrillTagReader | undefined;
|
|
41
43
|
text(...at: string[]): string | undefined;
|
|
42
44
|
textArray(...at: string[]): string[] | undefined;
|
|
45
|
+
array?(...at: string[]): DrillTagReader[] | undefined;
|
|
46
|
+
has?(...at: string[]): boolean;
|
|
43
47
|
}
|
|
44
48
|
|
|
45
49
|
/** The clicked field, as much of it as drill reads. */
|
|
@@ -55,6 +59,8 @@ export interface DrillField {
|
|
|
55
59
|
* carry the method; absent, the field is taken at its word.
|
|
56
60
|
*/
|
|
57
61
|
wasDimension?: () => boolean;
|
|
62
|
+
/** The column header: the field's `# label`, else its name. */
|
|
63
|
+
getLabel?: () => string;
|
|
58
64
|
}
|
|
59
65
|
|
|
60
66
|
/**
|
|
@@ -79,6 +85,19 @@ export interface DrillClickPayload {
|
|
|
79
85
|
* this value" runs against is read from here.
|
|
80
86
|
*/
|
|
81
87
|
context?: string;
|
|
88
|
+
/**
|
|
89
|
+
* Every dimension of the clicked row, enclosing rows included, as the
|
|
90
|
+
* renderer's `onDrill` reports them. Set by `RenderedResult` for a table
|
|
91
|
+
* cell; absent for a click the renderer reports no row for.
|
|
92
|
+
*/
|
|
93
|
+
drillClauses?: readonly DrillClause[];
|
|
94
|
+
/** The query timezone, which a truncated timestamp in a clause is read in. */
|
|
95
|
+
timezone?: string;
|
|
96
|
+
/**
|
|
97
|
+
* Set by a host that shows no rows for this click's tile (a restricted
|
|
98
|
+
* one). Tag drills still apply.
|
|
99
|
+
*/
|
|
100
|
+
noRows?: boolean;
|
|
82
101
|
}
|
|
83
102
|
|
|
84
103
|
/**
|
|
@@ -93,6 +112,16 @@ export interface DrillRowsRequest {
|
|
|
93
112
|
label: string;
|
|
94
113
|
/** {@link DrillClickPayload.context}, when the host set one. */
|
|
95
114
|
context?: string;
|
|
115
|
+
/** {@link DrillClickPayload.drillClauses}: the whole row, not just `field`. */
|
|
116
|
+
drillClauses?: readonly DrillClause[];
|
|
117
|
+
/** {@link DrillClickPayload.timezone}. */
|
|
118
|
+
timezone?: string;
|
|
119
|
+
/** The source and filters the rows run with, from the result's metadata. */
|
|
120
|
+
target?: RowsTarget;
|
|
121
|
+
/** The row's dimensions as code with their values, from `drillClauses`. */
|
|
122
|
+
dimensions?: readonly RowsDimension[];
|
|
123
|
+
/** The clicked field's code, for a click that came with no row. */
|
|
124
|
+
code?: string;
|
|
96
125
|
}
|
|
97
126
|
|
|
98
127
|
/** The literal `to=self`: filter the current view instead of leaving it. */
|