@malloy-publisher/sdk 0.3.0 → 0.3.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.
Files changed (156) hide show
  1. package/README.md +8 -0
  2. package/dist/DocumentStorageProvider-BeBD0uWc.cjs.js +134 -0
  3. package/dist/DocumentStorageProvider-tGvodI8f.es.js +24268 -0
  4. package/dist/RenderedResult-CPi6uQWq.cjs.js +115 -0
  5. package/dist/RenderedResult-mDE-4u8S.es.js +542 -0
  6. package/dist/ServerProvider-CH71GsoL.cjs.js +1 -0
  7. package/dist/{ServerProvider-DSK9rfSY.es.js → ServerProvider-X2Ma0HRs.es.js} +486 -521
  8. package/dist/builder/index.cjs.js +105 -0
  9. package/dist/builder/index.d.ts +2 -0
  10. package/dist/builder/index.es.js +9561 -0
  11. package/dist/builder-entry.d.ts +8 -0
  12. package/dist/client/index.cjs.js +1 -1
  13. package/dist/client/index.es.js +1 -1
  14. package/dist/components/Dashboard/Dashboard.d.ts +19 -14
  15. package/dist/components/Dashboard/DashboardGrid.d.ts +67 -0
  16. package/dist/components/Dashboard/DashboardTile.d.ts +6 -1
  17. package/dist/components/Dashboard/ExploreDialog.d.ts +18 -0
  18. package/dist/components/Dashboard/RowsDialog.d.ts +47 -0
  19. package/dist/components/Dashboard/TileCard.d.ts +44 -0
  20. package/dist/components/Dashboard/index.d.ts +2 -0
  21. package/dist/components/Dashboard/promoteMeasureRow.d.ts +7 -3
  22. package/dist/components/Dashboard/telemetry.d.ts +43 -0
  23. package/dist/components/DashboardBuilder/AddTileDialog.d.ts +35 -0
  24. package/dist/components/DashboardBuilder/BuilderToolbar.d.ts +29 -0
  25. package/dist/components/DashboardBuilder/DashboardBuilder.d.ts +112 -0
  26. package/dist/components/DashboardBuilder/DashboardEditor.d.ts +32 -0
  27. package/dist/components/DashboardBuilder/DiffDialog.d.ts +15 -0
  28. package/dist/components/DashboardBuilder/DrillDialog.d.ts +15 -0
  29. package/dist/components/DashboardBuilder/FilterDialog.d.ts +54 -0
  30. package/dist/components/DashboardBuilder/FilterStrip.d.ts +17 -0
  31. package/dist/components/DashboardBuilder/SettingsPopover.d.ts +20 -0
  32. package/dist/components/DashboardBuilder/TileFrame.d.ts +42 -0
  33. package/dist/components/DashboardBuilder/TileMenu.d.ts +24 -0
  34. package/dist/components/DashboardBuilder/catalog.d.ts +62 -0
  35. package/dist/components/DashboardBuilder/controls.d.ts +157 -0
  36. package/dist/components/DashboardBuilder/diff.d.ts +35 -0
  37. package/dist/components/DashboardBuilder/document.d.ts +218 -0
  38. package/dist/components/DashboardBuilder/layout.d.ts +71 -0
  39. package/dist/components/DashboardBuilder/malloyText.d.ts +45 -0
  40. package/dist/components/DashboardBuilder/preview.d.ts +60 -0
  41. package/dist/components/DashboardBuilder/readDocument.d.ts +106 -0
  42. package/dist/components/DashboardBuilder/sortable.d.ts +51 -0
  43. package/dist/components/DashboardBuilder/spliceDocument.d.ts +44 -0
  44. package/dist/components/DashboardBuilder/testing/fixtures.d.ts +10 -0
  45. package/dist/components/DashboardBuilder/useBuilderShortcuts.d.ts +22 -0
  46. package/dist/components/DashboardBuilder/useDashboardEditor.d.ts +63 -0
  47. package/dist/components/DashboardBuilder/useDraft.d.ts +13 -0
  48. package/dist/components/DashboardBuilder/useFilterForm.d.ts +65 -0
  49. package/dist/components/DashboardBuilder/useTileReorder.d.ts +22 -0
  50. package/dist/components/DashboardBuilder/useTileResize.d.ts +29 -0
  51. package/dist/components/DocumentStorage/DocumentStorageProvider.d.ts +7 -0
  52. package/dist/components/DocumentStorage/index.d.ts +1 -1
  53. package/dist/components/FloatingIconButton.d.ts +16 -0
  54. package/dist/components/Prose.d.ts +46 -0
  55. package/dist/components/RenderedResult/ResultPanel.d.ts +30 -0
  56. package/dist/components/RenderedResult/index.d.ts +1 -0
  57. package/dist/components/drill/index.d.ts +1 -1
  58. package/dist/components/drill/resolveDrill.d.ts +22 -0
  59. package/dist/components/drill/useDrill.d.ts +11 -2
  60. package/dist/components/index.d.ts +1 -0
  61. package/dist/components/styles.d.ts +0 -4
  62. package/dist/hooks/index.d.ts +2 -0
  63. package/dist/hooks/useDocumentControls.d.ts +70 -0
  64. package/dist/hooks/useQueryResult.d.ts +40 -0
  65. package/dist/index.cjs.js +23 -270
  66. package/dist/index.es.js +16977 -41293
  67. package/dist/utils/malloyLiteral.d.ts +11 -0
  68. package/package.json +24 -9
  69. package/src/builder-entry.ts +55 -0
  70. package/src/components/Dashboard/Dashboard.spec.tsx +7 -10
  71. package/src/components/Dashboard/Dashboard.tsx +165 -175
  72. package/src/components/Dashboard/DashboardGrid.tsx +124 -0
  73. package/src/components/Dashboard/DashboardTile.spec.tsx +30 -6
  74. package/src/components/Dashboard/DashboardTile.tsx +66 -123
  75. package/src/components/Dashboard/ExploreDialog.tsx +84 -0
  76. package/src/components/Dashboard/RowsDialog.spec.ts +42 -0
  77. package/src/components/Dashboard/RowsDialog.tsx +234 -0
  78. package/src/components/Dashboard/TileCard.tsx +120 -0
  79. package/src/components/Dashboard/index.ts +2 -0
  80. package/src/components/Dashboard/promoteMeasureRow.spec.ts +70 -0
  81. package/src/components/Dashboard/promoteMeasureRow.ts +33 -2
  82. package/src/components/Dashboard/telemetry.ts +44 -0
  83. package/src/components/Dashboard/tileGridColumn.spec.ts +1 -1
  84. package/src/components/DashboardBuilder/AddTileDialog.tsx +249 -0
  85. package/src/components/DashboardBuilder/BuilderToolbar.tsx +270 -0
  86. package/src/components/DashboardBuilder/DashboardBuilder.spec.tsx +858 -0
  87. package/src/components/DashboardBuilder/DashboardBuilder.tsx +653 -0
  88. package/src/components/DashboardBuilder/DashboardEditor.spec.tsx +211 -0
  89. package/src/components/DashboardBuilder/DashboardEditor.tsx +493 -0
  90. package/src/components/DashboardBuilder/DiffDialog.tsx +129 -0
  91. package/src/components/DashboardBuilder/DrillDialog.tsx +281 -0
  92. package/src/components/DashboardBuilder/FilterDialog.tsx +581 -0
  93. package/src/components/DashboardBuilder/FilterStrip.tsx +125 -0
  94. package/src/components/DashboardBuilder/SettingsPopover.tsx +168 -0
  95. package/src/components/DashboardBuilder/TileFrame.tsx +380 -0
  96. package/src/components/DashboardBuilder/TileMenu.tsx +188 -0
  97. package/src/components/DashboardBuilder/catalog.spec.ts +215 -0
  98. package/src/components/DashboardBuilder/catalog.ts +225 -0
  99. package/src/components/DashboardBuilder/controls.spec.ts +243 -0
  100. package/src/components/DashboardBuilder/controls.ts +398 -0
  101. package/src/components/DashboardBuilder/diff.spec.ts +44 -0
  102. package/src/components/DashboardBuilder/diff.ts +93 -0
  103. package/src/components/DashboardBuilder/document.ts +209 -0
  104. package/src/components/DashboardBuilder/keepRowStructure.spec.ts +84 -0
  105. package/src/components/DashboardBuilder/layout.ts +143 -0
  106. package/src/components/DashboardBuilder/malloyText.spec.ts +84 -0
  107. package/src/components/DashboardBuilder/malloyText.ts +151 -0
  108. package/src/components/DashboardBuilder/moveIntoGap.spec.ts +90 -0
  109. package/src/components/DashboardBuilder/preview.spec.ts +149 -0
  110. package/src/components/DashboardBuilder/preview.ts +172 -0
  111. package/src/components/DashboardBuilder/readDocument.spec.ts +375 -0
  112. package/src/components/DashboardBuilder/readDocument.ts +467 -0
  113. package/src/components/DashboardBuilder/sortable.tsx +151 -0
  114. package/src/components/DashboardBuilder/spliceDocument.spec.ts +1108 -0
  115. package/src/components/DashboardBuilder/spliceDocument.ts +1098 -0
  116. package/src/components/DashboardBuilder/testing/fixtures.ts +48 -0
  117. package/src/components/DashboardBuilder/useBuilderShortcuts.ts +88 -0
  118. package/src/components/DashboardBuilder/useDashboardEditor.spec.ts +255 -0
  119. package/src/components/DashboardBuilder/useDashboardEditor.ts +193 -0
  120. package/src/components/DashboardBuilder/useDraft.ts +43 -0
  121. package/src/components/DashboardBuilder/useFilterForm.spec.tsx +144 -0
  122. package/src/components/DashboardBuilder/useFilterForm.ts +352 -0
  123. package/src/components/DashboardBuilder/useTileReorder.spec.tsx +97 -0
  124. package/src/components/DashboardBuilder/useTileReorder.ts +115 -0
  125. package/src/components/DashboardBuilder/useTileResize.spec.tsx +121 -0
  126. package/src/components/DashboardBuilder/useTileResize.ts +110 -0
  127. package/src/components/DashboardBuilder/withGaps.spec.ts +96 -0
  128. package/src/components/DocumentStorage/DocumentStorageProvider.tsx +12 -0
  129. package/src/components/DocumentStorage/index.ts +1 -0
  130. package/src/components/Environment/About.tsx +4 -36
  131. package/src/components/FloatingIconButton.tsx +41 -0
  132. package/src/components/Model/Model.tsx +6 -23
  133. package/src/components/Model/ModelCell.tsx +33 -72
  134. package/src/components/Notebook/Notebook.tsx +25 -88
  135. package/src/components/Notebook/NotebookCell.tsx +35 -183
  136. package/src/components/Prose.spec.tsx +85 -0
  137. package/src/components/Prose.tsx +179 -0
  138. package/src/components/QueryResult/QueryResult.tsx +15 -43
  139. package/src/components/RenderedResult/ResultContainer.tsx +4 -12
  140. package/src/components/RenderedResult/ResultPanel.spec.tsx +49 -0
  141. package/src/components/RenderedResult/ResultPanel.tsx +73 -0
  142. package/src/components/RenderedResult/index.ts +1 -0
  143. package/src/components/drill/index.ts +1 -0
  144. package/src/components/drill/resolveDrill.ts +22 -1
  145. package/src/components/drill/useDrill.spec.tsx +94 -0
  146. package/src/components/drill/useDrill.tsx +52 -12
  147. package/src/components/given/GivensPanel.tsx +1 -1
  148. package/src/components/index.ts +6 -0
  149. package/src/components/styles.ts +0 -37
  150. package/src/hooks/index.ts +13 -0
  151. package/src/hooks/useDocumentControls.ts +170 -0
  152. package/src/hooks/useQueryResult.ts +82 -0
  153. package/src/utils/malloyLiteral.spec.ts +43 -0
  154. package/src/utils/malloyLiteral.ts +76 -0
  155. package/vite.config.ts +5 -0
  156. package/dist/ServerProvider-CGnLjzag.cjs.js +0 -1
@@ -0,0 +1,234 @@
1
+ // Copyright (c) Credible Data Inc.
2
+ // SPDX-License-Identifier: MIT
3
+
4
+ import CloseIcon from "@mui/icons-material/Close";
5
+ import {
6
+ Box,
7
+ Button,
8
+ Dialog,
9
+ DialogActions,
10
+ DialogContent,
11
+ DialogTitle,
12
+ IconButton,
13
+ Typography,
14
+ } from "@mui/material";
15
+ import { useEffect, useRef } from "react";
16
+ import { useQueryResult } from "../../hooks/useQueryResult";
17
+ import { malloyLiteral } from "../../utils/malloyLiteral";
18
+ import { isIdentifier, tileSteps } from "../DashboardBuilder/malloyText";
19
+ import { usePublisherTheme } from "../../theme/ThemeContext";
20
+ import { ResultPanel } from "../RenderedResult/ResultPanel";
21
+ import { now } from "./telemetry";
22
+
23
+ /**
24
+ * The rows behind a value: "show the rows" on a cell, as one query.
25
+ *
26
+ * A grouped value is an aggregate over rows of the tile's source, and Malloy's
27
+ * `drill:` names exactly those rows: `drill: <view>.<field> = <value>` applies
28
+ * the view's own `where:` — the tile's control bindings included — and the
29
+ * clicked value's, so what comes back is the rows the number was made of.
30
+ * Measured: `where: name = …` alone failed on a view that spelled the column
31
+ * `name is products.name`, because the output name is not a source field;
32
+ * `drill:` resolves it through the view, which is where the name lives.
33
+ */
34
+ export const ROW_LIMIT = 200;
35
+
36
+ export interface RowsRequest {
37
+ /** The source the rows are of: the tile expression's first step. */
38
+ source: string;
39
+ /** The view the value was grouped in: the expression's last step. */
40
+ view: string;
41
+ field: string;
42
+ rawValue: unknown;
43
+ label: string;
44
+ }
45
+
46
+ /** The query the dialog runs, or undefined when the value cannot be spelled. */
47
+ export function rowsQuery(request: RowsRequest): string | undefined {
48
+ const literal = malloyLiteral(request.rawValue);
49
+ if (literal === undefined) return undefined;
50
+ return `run: ${request.source} -> { drill: ${request.view}.${request.field} = ${literal}; select: *; limit: ${ROW_LIMIT} }`;
51
+ }
52
+
53
+ /**
54
+ * `overview -> revenue_trend` → its source and view; undefined for anything
55
+ * else — an inline stage has no view to drill through, a refined one carries
56
+ * filters `drill:` would not, and a longer pipeline has no one view its value
57
+ * came from.
58
+ */
59
+ export function stepsOf(
60
+ tileExpression: string | undefined,
61
+ ): { source: string; view: string } | undefined {
62
+ const steps = tileSteps(tileExpression);
63
+ if (
64
+ !steps ||
65
+ steps.refinement !== undefined ||
66
+ !isIdentifier(steps.source) ||
67
+ !isIdentifier(steps.view)
68
+ )
69
+ return undefined;
70
+ return { source: steps.source, view: steps.view };
71
+ }
72
+
73
+ /** The source alone, for opening the explorer on it. */
74
+ export const sourceOf = (tileExpression: string | undefined) =>
75
+ stepsOf(tileExpression)?.source;
76
+
77
+ export function RowsDialog({
78
+ request,
79
+ environmentName,
80
+ packageName,
81
+ versionId,
82
+ modelPath,
83
+ givens,
84
+ onClose,
85
+ onDone,
86
+ }: {
87
+ request: RowsRequest | undefined;
88
+ environmentName: string;
89
+ packageName: string;
90
+ versionId?: string;
91
+ modelPath: string;
92
+ /** The applied control row, which the source's own `where:` may read. */
93
+ givens: Record<string, unknown>;
94
+ onClose: () => void;
95
+ /** The query's outcome and how long it took, once per request. */
96
+ onDone?: (ok: boolean, durationMs: number) => void;
97
+ }) {
98
+ const { theme } = usePublisherTheme();
99
+ const query = request ? rowsQuery(request) : undefined;
100
+
101
+ return (
102
+ <Dialog
103
+ open={request !== undefined}
104
+ onClose={onClose}
105
+ maxWidth="lg"
106
+ fullWidth
107
+ >
108
+ <DialogTitle
109
+ sx={{
110
+ display: "flex",
111
+ alignItems: "flex-start",
112
+ justifyContent: "space-between",
113
+ pb: 0.5,
114
+ }}
115
+ >
116
+ <Box>
117
+ {request ? (
118
+ <>
119
+ Rows where{" "}
120
+ <Box component="code" sx={{ fontSize: "0.9em" }}>
121
+ {request.field}
122
+ </Box>{" "}
123
+ is {request.label}
124
+ </>
125
+ ) : (
126
+ "Rows"
127
+ )}
128
+ <Typography
129
+ component="div"
130
+ variant="caption"
131
+ sx={{ color: theme.tileTitle, mt: 0.25 }}
132
+ >
133
+ {request
134
+ ? `The first ${ROW_LIMIT} rows of ${request.source} behind this value, with the tile's own filters and the controls applied.`
135
+ : ""}
136
+ </Typography>
137
+ </Box>
138
+ <IconButton
139
+ onClick={onClose}
140
+ aria-label="Close"
141
+ size="small"
142
+ sx={{ color: "text.secondary", ml: 1 }}
143
+ >
144
+ <CloseIcon fontSize="small" />
145
+ </IconButton>
146
+ </DialogTitle>
147
+ <DialogContent dividers sx={{ p: 0, minHeight: 240 }}>
148
+ {query === undefined && request && (
149
+ <Typography variant="body2" sx={{ p: 2 }}>
150
+ This value cannot be written as a filter, so its rows cannot
151
+ be fetched.
152
+ </Typography>
153
+ )}
154
+ {query !== undefined && (
155
+ <RowsResult
156
+ query={query}
157
+ environmentName={environmentName}
158
+ packageName={packageName}
159
+ {...(versionId === undefined ? {} : { versionId })}
160
+ modelPath={modelPath}
161
+ givens={givens}
162
+ {...(onDone ? { onDone } : {})}
163
+ />
164
+ )}
165
+ </DialogContent>
166
+ <DialogActions sx={{ px: 3, py: 1.5 }}>
167
+ {query !== undefined && (
168
+ <Typography
169
+ variant="caption"
170
+ sx={{
171
+ mr: "auto",
172
+ color: theme.tileTitle,
173
+ fontFamily: "ui-monospace, monospace",
174
+ overflow: "hidden",
175
+ textOverflow: "ellipsis",
176
+ whiteSpace: "nowrap",
177
+ }}
178
+ title={query}
179
+ >
180
+ {query}
181
+ </Typography>
182
+ )}
183
+ <Button onClick={onClose}>Close</Button>
184
+ </DialogActions>
185
+ </Dialog>
186
+ );
187
+ }
188
+
189
+ /**
190
+ * The rows themselves. Its own component so the query exists only while a
191
+ * request does: a closed dialog holds nothing in the cache.
192
+ */
193
+ function RowsResult({
194
+ query,
195
+ environmentName,
196
+ packageName,
197
+ versionId,
198
+ modelPath,
199
+ givens,
200
+ onDone,
201
+ }: {
202
+ query: string;
203
+ environmentName: string;
204
+ packageName: string;
205
+ versionId?: string;
206
+ modelPath: string;
207
+ givens: Record<string, unknown>;
208
+ onDone?: (ok: boolean, durationMs: number) => void;
209
+ }) {
210
+ const state = useQueryResult({
211
+ environmentName,
212
+ packageName,
213
+ modelPath,
214
+ ...(versionId === undefined ? {} : { versionId }),
215
+ query,
216
+ givens,
217
+ });
218
+ const { isSuccess, isError } = state;
219
+ // Reported once per request, when the query settles.
220
+ const started = useRef<number | undefined>(undefined);
221
+ useEffect(() => {
222
+ started.current = now();
223
+ }, [query]);
224
+ useEffect(() => {
225
+ if (started.current === undefined || (!isSuccess && !isError)) return;
226
+ onDone?.(isSuccess, now() - started.current);
227
+ started.current = undefined;
228
+ }, [isSuccess, isError, onDone]);
229
+ return (
230
+ <Box sx={{ p: 1 }}>
231
+ <ResultPanel state={state} context={query} />
232
+ </Box>
233
+ );
234
+ }
@@ -0,0 +1,120 @@
1
+ // Copyright (c) Credible Data Inc.
2
+ // SPDX-License-Identifier: MIT
3
+
4
+ import {
5
+ Box,
6
+ Paper,
7
+ Typography,
8
+ type SxProps,
9
+ type Theme,
10
+ } from "@mui/material";
11
+ import type { ReactNode } from "react";
12
+ import { DASHBOARD_CARD_PADDING_PX } from "../../theme/buildTableCssVars";
13
+ import { usePublisherTheme } from "../../theme/ThemeContext";
14
+
15
+ /**
16
+ * The card a dashboard tile sits in, and nothing else — so the builder, which
17
+ * draws a stand-in tile when it has no server to run one, draws the same card
18
+ * the reader does rather than restating its geometry.
19
+ *
20
+ * The instance theme's border, not MUI's `divider`: the renderer card's edge is
21
+ * this same value, and a card that agrees with the theme everywhere except its
22
+ * outline still reads as a different card. Radius stays on the host's
23
+ * `shape.borderRadius`, which the renderer card is pointed at too. The
24
+ * background is `theme.tile`, the value the renderer card paints — left unset,
25
+ * on a theme whose page is also white the composite tiles lost the tint that
26
+ * separates a card from the page while the single-query form kept it.
27
+ *
28
+ * `borderless` is `# borderless` on the view: the result with no card, which
29
+ * the renderer honours by dropping background, border, radius and most padding
30
+ * on its own `.dashboard-item`. Same here, so the tag reads the same on both
31
+ * forms.
32
+ *
33
+ * `overflow: hidden` and `minWidth: 0` are load-bearing for RESIZING, not
34
+ * tidiness: this card is a grid item, and a grid item that clips gets a
35
+ * minimum width of zero instead of its content's, which is what lets it
36
+ * narrow below the chart it holds so the renderer redraws the chart to fit.
37
+ * See the `minWidth: 0` note in `DashboardGrid` for the rest of that chain.
38
+ */
39
+ export function TileCard({
40
+ borderless = false,
41
+ sx,
42
+ children,
43
+ }: {
44
+ borderless?: boolean;
45
+ sx?: SxProps<Theme>;
46
+ children: ReactNode;
47
+ }) {
48
+ const { theme } = usePublisherTheme();
49
+ return (
50
+ <Paper
51
+ elevation={0}
52
+ sx={[
53
+ {
54
+ border: borderless ? "none" : theme.border,
55
+ borderRadius: borderless ? 0 : 1,
56
+ background: borderless ? "none" : theme.tile,
57
+ overflow: "hidden",
58
+ minWidth: 0,
59
+ minHeight: 120,
60
+ p: borderless ? "12px 0" : `${DASHBOARD_CARD_PADDING_PX}px`,
61
+ },
62
+ ...(Array.isArray(sx) ? sx : [sx]),
63
+ ]}
64
+ >
65
+ {children}
66
+ </Paper>
67
+ );
68
+ }
69
+
70
+ /**
71
+ * A tile's heading: its `# label`, and its `# subtitle` under it. In the
72
+ * instance theme's title colour and face, so a tile is titled the same
73
+ * whichever surface draws it.
74
+ */
75
+ export function TileHeading({
76
+ title,
77
+ subtitle,
78
+ tooltip,
79
+ action,
80
+ }: {
81
+ title: string;
82
+ subtitle?: string;
83
+ /** What actually ran, kept reachable without being the heading. */
84
+ tooltip?: string;
85
+ /** A control on the heading's row, hard right — the tile's Explore button. */
86
+ action?: ReactNode;
87
+ }) {
88
+ const { theme } = usePublisherTheme();
89
+ return (
90
+ <Box sx={{ pb: 1.5, display: "flex", alignItems: "flex-start", gap: 1 }}>
91
+ <Box sx={{ minWidth: 0, flex: 1 }}>
92
+ <Typography
93
+ variant="subtitle2"
94
+ sx={{
95
+ fontWeight: 500,
96
+ color: theme.tileTitle,
97
+ fontFamily: theme.font.family,
98
+ }}
99
+ title={tooltip}
100
+ >
101
+ {title}
102
+ </Typography>
103
+ {subtitle !== undefined && (
104
+ <Typography
105
+ variant="caption"
106
+ sx={{
107
+ display: "block",
108
+ color: theme.tileTitle,
109
+ fontFamily: theme.font.family,
110
+ opacity: 0.8,
111
+ }}
112
+ >
113
+ {subtitle}
114
+ </Typography>
115
+ )}
116
+ </Box>
117
+ {action}
118
+ </Box>
119
+ );
120
+ }
@@ -3,3 +3,5 @@
3
3
 
4
4
  export { Dashboard, type DashboardProps } from "./Dashboard";
5
5
  export { DashboardTile, type DashboardTileProps } from "./DashboardTile";
6
+ export type { DashboardEvent, DashboardEventHandler } from "./telemetry";
7
+ export { TileCard, TileHeading } from "./TileCard";
@@ -94,7 +94,77 @@ describe("promoteMeasureRowToKpis", () => {
94
94
  });
95
95
  });
96
96
 
97
+ /**
98
+ * The row as the SERVER actually sends it. Every output field is a
99
+ * `dimension`; an aggregate is marked by the `calculation` token in its
100
+ * internal annotation. Captured from `order_items -> key_figures` on the
101
+ * storefront package, trimmed to what the promotion reads.
102
+ */
103
+ const serverMeasureRow = () => ({
104
+ schema: {
105
+ fields: [
106
+ {
107
+ kind: "dimension",
108
+ name: "total_sales",
109
+ type: { kind: "number_type", subtype: "decimal" },
110
+ annotations: [
111
+ { value: "#(doc) Total revenue\n" },
112
+ { value: "# currency\n" },
113
+ { value: '# label="Revenue"\n' },
114
+ {
115
+ value: '#(malloy) reference_id = "276f77be" calculation drill_expression { kind = field_reference name = total_sales }\n',
116
+ },
117
+ ],
118
+ },
119
+ {
120
+ kind: "dimension",
121
+ name: "order_count",
122
+ type: { kind: "number_type", subtype: "integer" },
123
+ annotations: [
124
+ { value: '# label="Orders"\n' },
125
+ {
126
+ value: '#(malloy) reference_id = "97a73061" calculation drill_expression { kind = field_reference name = order_count }\n',
127
+ },
128
+ ],
129
+ },
130
+ ],
131
+ },
132
+ data: measureRow().data,
133
+ annotations: [
134
+ { value: "#(doc) Revenue and orders\n" },
135
+ { value: "#(malloy) source.name = order_items\n" },
136
+ ],
137
+ });
138
+
97
139
  describe("isMeasureRow", () => {
140
+ // The case that matters: real output. Until the aggregate marker was read,
141
+ // this returned false for every result the server ever produced, and the
142
+ // KPI strip drew as a one-row table whenever its view did not carry
143
+ // `# big_value` itself.
144
+ it("recognises the server's own shape, where aggregates are dimensions marked calculation", () => {
145
+ const row = serverMeasureRow();
146
+ expect(isMeasureRow(row)).toBe(true);
147
+ expect(
148
+ annotationsOf(promoteMeasureRowToKpis(JSON.stringify(row)))[0],
149
+ ).toBe("# big_value\n");
150
+ });
151
+
152
+ it("refuses a grouped result in the server's shape", () => {
153
+ const row = serverMeasureRow();
154
+ row.schema.fields.unshift({
155
+ kind: "dimension",
156
+ name: "category",
157
+ type: { kind: "string_type", subtype: "text" },
158
+ annotations: [
159
+ { value: '# label="Category"\n' },
160
+ {
161
+ value: '#(malloy) reference_id = "4d02e53c" drill_expression { kind = field_reference name = category }\n',
162
+ },
163
+ ],
164
+ });
165
+ expect(isMeasureRow(row)).toBe(false);
166
+ });
167
+
98
168
  it("accepts a single record cell as well as a one-element array", () => {
99
169
  const row = measureRow();
100
170
  expect(isMeasureRow(row)).toBe(true);
@@ -50,12 +50,43 @@ const RENDER_TAGS = new Set([
50
50
 
51
51
  const BIG_VALUE_ANNOTATION = { value: "# big_value\n" };
52
52
 
53
+ interface ResultField {
54
+ kind?: string;
55
+ annotations?: Array<{ value?: string }>;
56
+ }
57
+
53
58
  interface ResultShape {
54
- schema?: { fields?: Array<{ kind?: string }> };
59
+ schema?: { fields?: ResultField[] };
55
60
  data?: { kind?: string; array_value?: unknown[] };
56
61
  annotations?: Array<{ value?: string }>;
57
62
  }
58
63
 
64
+ /**
65
+ * Whether an output field is an aggregate.
66
+ *
67
+ * Measured against the server: every output field of a query comes back
68
+ * `kind: "dimension"` — a result's columns are its dimensions, whatever they
69
+ * were in the source — and what marks an aggregate is the `calculation` token
70
+ * in the field's internal annotation:
71
+ *
72
+ * #(malloy) reference_id = "…" calculation drill_expression { … }
73
+ *
74
+ * `kind === "measure"` is kept for a renderer that says so directly, but no
75
+ * real result has yet. Until this read the annotation the promotion never
76
+ * fired on real output, and only its own fixture — written with `"measure"` —
77
+ * ever passed it.
78
+ */
79
+ const MALLOY_CALCULATION = /^#\(malloy\)[\s\S]*\bcalculation\b/;
80
+ function isMeasureField(field: ResultField | undefined): boolean {
81
+ if (!field) return false;
82
+ if (field.kind === "measure") return true;
83
+ return (field.annotations ?? []).some(
84
+ (annotation) =>
85
+ typeof annotation?.value === "string" &&
86
+ MALLOY_CALCULATION.test(annotation.value.trim()),
87
+ );
88
+ }
89
+
59
90
  /**
60
91
  * True when the result is one row consisting solely of measures: the shape a
61
92
  * `view: kpis is { aggregate: … }` tile produces.
@@ -63,7 +94,7 @@ interface ResultShape {
63
94
  export function isMeasureRow(result: ResultShape): boolean {
64
95
  const fields = result.schema?.fields;
65
96
  if (!Array.isArray(fields) || fields.length === 0) return false;
66
- if (!fields.every((field) => field?.kind === "measure")) return false;
97
+ if (!fields.every(isMeasureField)) return false;
67
98
  const data = result.data;
68
99
  if (!data) return false;
69
100
  if (data.kind === "record_cell") return true;
@@ -0,0 +1,44 @@
1
+ // Copyright (c) Credible Data Inc.
2
+ // SPDX-License-Identifier: MIT
3
+
4
+ /**
5
+ * What a dashboard surface reports about itself, for the host to log or
6
+ * count: the operations that matter (open, save, export, the rows behind a
7
+ * value, exploring a tile), each with its outcome and how long it took.
8
+ *
9
+ * Context-free on purpose — the host that mounted the surface knows which
10
+ * environment, package and dashboard it is, and adds that when it logs.
11
+ */
12
+ export type DashboardEvent =
13
+ | {
14
+ type: "dashboard.opened";
15
+ /** The package file, or a draft the host had saved. */
16
+ from: "package" | "draft";
17
+ tiles: number;
18
+ durationMs: number;
19
+ }
20
+ | { type: "dashboard.open_refused"; reason: string }
21
+ | {
22
+ type: "dashboard.saved";
23
+ tiles: number;
24
+ /** Whether a tile was added or removed, which moves declarations. */
25
+ structural: boolean;
26
+ durationMs: number;
27
+ }
28
+ | { type: "dashboard.save_refused"; reason: string }
29
+ | { type: "dashboard.exported"; bytes: number }
30
+ | {
31
+ type: "dashboard.rows_shown";
32
+ source: string;
33
+ view: string;
34
+ field: string;
35
+ ok: boolean;
36
+ durationMs: number;
37
+ }
38
+ | { type: "dashboard.explored"; tile: string };
39
+
40
+ export type DashboardEventHandler = (event: DashboardEvent) => void;
41
+
42
+ /** A monotonic clock in milliseconds, for durations. */
43
+ export const now = (): number =>
44
+ typeof performance !== "undefined" ? performance.now() : Date.now();
@@ -2,7 +2,7 @@
2
2
  // SPDX-License-Identifier: MIT
3
3
 
4
4
  import { describe, expect, it } from "bun:test";
5
- import { tileGridColumn } from "./Dashboard";
5
+ import { tileGridColumn } from "./DashboardGrid";
6
6
 
7
7
  describe("tileGridColumn", () => {
8
8
  it("spans one column when the view carries no colspan", () => {