@malloy-publisher/sdk 0.2.6 → 0.3.0
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 +172 -204
- package/dist/ServerProvider-CGnLjzag.cjs.js +1 -0
- package/dist/{ServerProvider-ksC5Su_x.es.js → ServerProvider-DSK9rfSY.es.js} +245 -245
- package/dist/client/api.d.ts +13 -1
- package/dist/client/index.cjs.js +1 -1
- package/dist/client/index.es.js +1 -1
- package/dist/components/ApiErrorDisplay.d.ts +1 -2
- package/dist/components/Dashboard/Dashboard.d.ts +4 -2
- package/dist/components/Dashboard/promoteMeasureRow.d.ts +36 -0
- package/dist/components/DocumentStorage/BrowserDocumentStorage.d.ts +14 -0
- package/dist/components/DocumentStorage/DocumentStorage.d.ts +58 -0
- package/dist/components/DocumentStorage/DocumentStorageProvider.d.ts +18 -0
- package/dist/components/DocumentStorage/index.d.ts +3 -0
- package/dist/components/Model/index.d.ts +0 -1
- package/dist/components/RenderedResult/RenderedResult.d.ts +12 -0
- package/dist/components/RenderedResult/ResultContainer.d.ts +7 -1
- package/dist/components/RenderedResult/index.d.ts +1 -0
- package/dist/components/RenderedResult/resultSizing.d.ts +183 -0
- package/dist/components/drill/index.d.ts +1 -0
- package/dist/components/drill/useDrillSelf.d.ts +46 -0
- package/dist/components/given/filterValue.d.ts +63 -0
- package/dist/components/given/index.d.ts +1 -0
- package/dist/components/given/useGivenUrlParams.d.ts +39 -0
- package/dist/components/index.d.ts +2 -3
- package/dist/components/styles.d.ts +3 -3
- package/dist/core-8NVBCLZh.cjs.js +148 -0
- package/dist/{core-xdZbLgaF.es.js → core-B8oMW4sm.es.js} +3540 -2335
- package/dist/hooks/index.d.ts +1 -1
- package/dist/hooks/useSuggestOptions.d.ts +28 -12
- package/dist/index.cjs.js +281 -1
- package/dist/index.es.js +45803 -79
- package/dist/sdk.css +1 -1
- package/dist/styles.css +0 -1
- package/package.json +2 -6
- package/src/components/ApiErrorDisplay.tsx +1 -1
- package/src/components/Connections/EditConnectionDialog.tsx +96 -86
- package/src/components/Dashboard/Dashboard.tsx +60 -95
- package/src/components/Dashboard/DashboardTile.tsx +25 -2
- package/src/components/Dashboard/promoteMeasureRow.spec.ts +137 -0
- package/src/components/Dashboard/promoteMeasureRow.ts +129 -0
- package/src/components/DocumentStorage/BrowserDocumentStorage.spec.ts +81 -0
- package/src/components/DocumentStorage/BrowserDocumentStorage.ts +105 -0
- package/src/components/DocumentStorage/DocumentStorage.ts +73 -0
- package/src/components/DocumentStorage/DocumentStorageProvider.tsx +46 -0
- package/src/components/DocumentStorage/index.ts +15 -0
- package/src/components/Environment/Connections.tsx +15 -15
- package/src/components/Model/ModelCell.tsx +2 -1
- package/src/components/Model/index.ts +0 -1
- package/src/components/Notebook/Notebook.tsx +10 -76
- package/src/components/Notebook/NotebookCell.tsx +2 -8
- package/src/components/RenderedResult/RenderedResult.tsx +86 -76
- package/src/components/RenderedResult/ResultContainer.tsx +21 -24
- package/src/components/RenderedResult/index.ts +15 -0
- package/src/components/RenderedResult/resultSizing.spec.ts +249 -0
- package/src/components/RenderedResult/resultSizing.ts +308 -0
- package/src/components/ResultsDialog.tsx +2 -1
- package/src/components/drill/index.ts +5 -0
- package/src/components/drill/useDrillSelf.spec.tsx +64 -0
- package/src/components/drill/useDrillSelf.ts +110 -0
- package/src/components/given/GivenInput.spec.tsx +196 -2
- package/src/components/given/GivenInput.tsx +300 -25
- package/src/components/given/filterValue.spec.ts +112 -0
- package/src/components/given/filterValue.ts +188 -0
- package/src/components/given/index.ts +4 -0
- package/src/components/given/useGivenUrlParams.spec.tsx +74 -0
- package/src/components/given/useGivenUrlParams.ts +87 -0
- package/src/components/index.ts +5 -3
- package/src/hooks/index.ts +0 -1
- package/src/hooks/useGivensState.spec.ts +158 -0
- package/src/hooks/useGivensState.ts +114 -23
- package/src/hooks/useSuggestOptions.dom.spec.ts +60 -1
- package/src/hooks/useSuggestOptions.ts +94 -51
- package/vite.config.ts +0 -1
- package/dist/ServerProvider-xXW1HJOJ.cjs.js +0 -1
- package/dist/components/AnalyzePackageButton.d.ts +0 -6
- package/dist/components/Workbook/BrowserWorkbookStorage.d.ts +0 -9
- package/dist/components/Workbook/EditableMalloyCell.d.ts +0 -11
- package/dist/components/Workbook/ModelPicker.d.ts +0 -11
- package/dist/components/Workbook/MutableCell.d.ts +0 -17
- package/dist/components/Workbook/Workbook.d.ts +0 -7
- package/dist/components/Workbook/WorkbookList.d.ts +0 -7
- package/dist/components/Workbook/WorkbookManager.d.ts +0 -84
- package/dist/components/Workbook/WorkbookStorage.d.ts +0 -17
- package/dist/components/Workbook/WorkbookStorageProvider.d.ts +0 -12
- package/dist/components/Workbook/index.d.ts +0 -7
- package/dist/core-RvrkY1Ss.cjs.js +0 -148
- package/dist/index-B3scIDRd.cjs.js +0 -356
- package/dist/index-Bda7HuZc.es.js +0 -66409
- package/dist/markdown-editor.css +0 -341
- package/src/components/AnalyzePackageButton.tsx +0 -319
- package/src/components/Workbook/BrowserWorkbookStorage.ts +0 -64
- package/src/components/Workbook/EditableMalloyCell.tsx +0 -49
- package/src/components/Workbook/ModelPicker.tsx +0 -134
- package/src/components/Workbook/MutableCell.tsx +0 -458
- package/src/components/Workbook/Workbook.tsx +0 -501
- package/src/components/Workbook/WorkbookList.tsx +0 -110
- package/src/components/Workbook/WorkbookManager.ts +0 -214
- package/src/components/Workbook/WorkbookStorage.ts +0 -35
- package/src/components/Workbook/WorkbookStorageProvider.tsx +0 -40
- package/src/components/Workbook/index.ts +0 -10
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
// SPDX-License-Identifier: MIT
|
|
3
3
|
|
|
4
4
|
import { Alert, Box, Stack, Typography } from "@mui/material";
|
|
5
|
+
import Markdown from "markdown-to-jsx";
|
|
5
6
|
import { useCallback, useMemo } from "react";
|
|
6
7
|
import type { DashboardManifest } from "../../client";
|
|
7
8
|
import { useGivensState } from "../../hooks/useGivensState";
|
|
@@ -9,9 +10,10 @@ import { useQueryWithApiError } from "../../hooks/useQueryWithApiError";
|
|
|
9
10
|
import { useSuggestOptions } from "../../hooks/useSuggestOptions";
|
|
10
11
|
import { parseResourceUri } from "../../utils/formatting";
|
|
11
12
|
import { ApiErrorDisplay } from "../ApiErrorDisplay";
|
|
12
|
-
import {
|
|
13
|
+
import { useDrill, useDrillSelf, type DrillNavigation } from "../drill";
|
|
13
14
|
import { GivensPanel } from "../given";
|
|
14
15
|
import { Loading } from "../Loading";
|
|
16
|
+
import { TILE_MAX_HEIGHT } from "../RenderedResult/resultSizing";
|
|
15
17
|
import { useServer } from "../ServerProvider";
|
|
16
18
|
import { DashboardTile } from "./DashboardTile";
|
|
17
19
|
|
|
@@ -49,37 +51,15 @@ export interface DashboardProps {
|
|
|
49
51
|
onNavigate?: (target: DrillNavigation, event?: MouseEvent) => void;
|
|
50
52
|
/**
|
|
51
53
|
* Height cap for a result panel. Left unset, each form gets the cap that
|
|
52
|
-
* suits its shape
|
|
53
|
-
*
|
|
54
|
+
* suits its shape: {@link TILE_MAX_HEIGHT} per tile for the composite form,
|
|
55
|
+
* and no cap at all for the single-query form, where the one result IS the
|
|
56
|
+
* dashboard and a cap would clip the page rather than tidy it. Set it to
|
|
57
|
+
* hold a dashboard to a fixed box, as an embedding host might.
|
|
54
58
|
*/
|
|
55
59
|
height?: number;
|
|
56
60
|
maxResultSize?: number;
|
|
57
61
|
}
|
|
58
62
|
|
|
59
|
-
/**
|
|
60
|
-
* Per-tile cap for the composite form. A tile is one panel among several, so
|
|
61
|
-
* capping them keeps the grid even instead of letting one long table set the
|
|
62
|
-
* height of its whole row.
|
|
63
|
-
*/
|
|
64
|
-
const TILE_HEIGHT = 400;
|
|
65
|
-
|
|
66
|
-
/**
|
|
67
|
-
* Cap for the single-query form, where the one result *is* the dashboard and a
|
|
68
|
-
* cap would clip the page rather than tidy it. High enough to be no cap in
|
|
69
|
-
* practice, and still a guard against a pathological result.
|
|
70
|
-
*
|
|
71
|
-
* A result that REPORTS its own height, which a `# dashboard` grid does, renders
|
|
72
|
-
* at that height and the page scrolls, which is what a reader expects. A result
|
|
73
|
-
* that sizes to its CONTAINER instead, which a bare `# bar_chart` does, has no
|
|
74
|
-
* height to report and keeps whatever first-paint height it was handed, so it
|
|
75
|
-
* stretches: measured 1992px for a two-row bar chart against 227px for the same
|
|
76
|
-
* query under a grid tag. `INITIAL_RENDER_HEIGHT` bounds that near 2000 rather
|
|
77
|
-
* than removing it, and lowering the bound is NOT the fix, because the same seed
|
|
78
|
-
* sizes a notebook's chart cells (measured 700px there). Telling the two kinds of
|
|
79
|
-
* result apart needs something the renderer does not expose here.
|
|
80
|
-
*/
|
|
81
|
-
const WHOLE_PAGE_HEIGHT = 20000;
|
|
82
|
-
|
|
83
63
|
/** Grid width when the dashboard declares no `# dashboard { columns=N }`. */
|
|
84
64
|
const DEFAULT_COLUMNS = 2;
|
|
85
65
|
|
|
@@ -224,71 +204,17 @@ export function Dashboard({
|
|
|
224
204
|
manifest?.path,
|
|
225
205
|
specs,
|
|
226
206
|
versionId,
|
|
207
|
+
// So a suggest over a gated or scoped source carries the givens it needs.
|
|
208
|
+
{ values: applied, declaredTypes },
|
|
227
209
|
);
|
|
228
210
|
|
|
229
|
-
//
|
|
230
|
-
//
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
// First declaration wins, so a model with `REGION` and `region` keeps
|
|
237
|
-
// the one it declared first rather than silently flipping.
|
|
238
|
-
if (!byFold.has(name.toLowerCase()))
|
|
239
|
-
byFold.set(name.toLowerCase(), name);
|
|
240
|
-
}
|
|
241
|
-
return byFold;
|
|
242
|
-
}, [declaredTypes]);
|
|
243
|
-
|
|
244
|
-
/** The declared given a drill tag's name refers to, or undefined. */
|
|
245
|
-
const resolveGiven = useCallback(
|
|
246
|
-
(given: string) =>
|
|
247
|
-
declaredTypes.has(given)
|
|
248
|
-
? given
|
|
249
|
-
: givenNamesByFold.get(given.toLowerCase()),
|
|
250
|
-
[declaredTypes, givenNamesByFold],
|
|
251
|
-
);
|
|
252
|
-
|
|
253
|
-
// `to=self` filters in place, which only works for a given this dashboard
|
|
254
|
-
// actually surfaces: sending one it cannot bind would fail every tile's
|
|
255
|
-
// query. Asked here rather than checked after the click, so a cell that
|
|
256
|
-
// cannot be honoured is never painted as clickable in the first place.
|
|
257
|
-
const canSelf = useCallback(
|
|
258
|
-
(given: string) => resolveGiven(given) !== undefined,
|
|
259
|
-
[resolveGiven],
|
|
260
|
-
);
|
|
261
|
-
|
|
262
|
-
const onSelf = useCallback(
|
|
263
|
-
(given: string, rawValue: unknown) => {
|
|
264
|
-
const declared = resolveGiven(given);
|
|
265
|
-
if (declared === undefined) return;
|
|
266
|
-
// Encoded against the DECLARED type, which is knowable here and is not
|
|
267
|
-
// knowable at the click: `useDrill` hands over the raw cell value for
|
|
268
|
-
// exactly this reason. Passing it straight to `setGiven` skipped the
|
|
269
|
-
// encoder, so a clicked date reached a `number` given as epoch
|
|
270
|
-
// milliseconds and a filter value went unescaped. Set under the name
|
|
271
|
-
// the MODEL declares, so the value reaches the URL and the request
|
|
272
|
-
// under the one name the server knows.
|
|
273
|
-
const declaredType = declaredTypes.get(declared);
|
|
274
|
-
const value = encodeDrillValue(rawValue, declaredType);
|
|
275
|
-
if (value === undefined) {
|
|
276
|
-
// Say so, the way the notebook does. `canSelf` only checks that the
|
|
277
|
-
// NAME resolves, so a `given=` naming a type the clicked value
|
|
278
|
-
// cannot become still paints the whole column as clickable and then
|
|
279
|
-
// drops every click. That is an authoring mistake with no other
|
|
280
|
-
// symptom, and this is the likelier surface for it, because the
|
|
281
|
-
// givens are the dashboard's own.
|
|
282
|
-
console.warn(
|
|
283
|
-
`Drill declined: ${JSON.stringify(rawValue)} cannot be a value for given "${declared}"` +
|
|
284
|
-
(declaredType ? ` of type ${declaredType}` : ""),
|
|
285
|
-
);
|
|
286
|
-
return;
|
|
287
|
-
}
|
|
288
|
-
setGiven(declared, value);
|
|
289
|
-
},
|
|
290
|
-
[declaredTypes, resolveGiven, setGiven],
|
|
291
|
-
);
|
|
211
|
+
// `to=self` filters in place. Which givens a tag may set, and setting one
|
|
212
|
+
// from a clicked cell, is the same on both surfaces, so it is shared.
|
|
213
|
+
const { canSelf, onSelf } = useDrillSelf({
|
|
214
|
+
declaredTypes,
|
|
215
|
+
setGiven,
|
|
216
|
+
documentName: dashboard,
|
|
217
|
+
});
|
|
292
218
|
|
|
293
219
|
const { drill, drillMenu } = useDrill({
|
|
294
220
|
onNavigate,
|
|
@@ -377,7 +303,7 @@ export function Dashboard({
|
|
|
377
303
|
queryName={manifest.query}
|
|
378
304
|
givens={applied}
|
|
379
305
|
declaredTypes={declaredTypes}
|
|
380
|
-
height={height
|
|
306
|
+
height={height}
|
|
381
307
|
maxResultSize={maxResultSize}
|
|
382
308
|
drill={drill}
|
|
383
309
|
/>
|
|
@@ -422,7 +348,7 @@ export function Dashboard({
|
|
|
422
348
|
givens={applied}
|
|
423
349
|
declaredTypes={declaredTypes}
|
|
424
350
|
givenNames={tile.givenNames}
|
|
425
|
-
height={height ??
|
|
351
|
+
height={height ?? TILE_MAX_HEIGHT}
|
|
426
352
|
maxResultSize={maxResultSize}
|
|
427
353
|
drill={drill}
|
|
428
354
|
/>
|
|
@@ -440,6 +366,20 @@ export function Dashboard({
|
|
|
440
366
|
);
|
|
441
367
|
}
|
|
442
368
|
|
|
369
|
+
/**
|
|
370
|
+
* The dashboard's prose header: its title, and the description as MARKDOWN.
|
|
371
|
+
*
|
|
372
|
+
* A description comes from the file's model-level doc comment (`##"` lines),
|
|
373
|
+
* and Malloy carries a whole block of them through with its newlines and blank
|
|
374
|
+
* lines intact — measured: `'## Why this page exists\nRevenue is up but margin
|
|
375
|
+
* is flat.\n\nThe tile below says where it went.'` reaches the manifest exactly
|
|
376
|
+
* like that. Rendering it as a plain `Typography` collapsed all of that onto
|
|
377
|
+
* one unstyled line, so a dashboard could already carry a narrative header and
|
|
378
|
+
* was throwing it away at the last step.
|
|
379
|
+
*
|
|
380
|
+
* Markdown here and not in a tile: prose BETWEEN tiles needs a tile kind the
|
|
381
|
+
* format cannot express yet. This is the half that needs nothing new.
|
|
382
|
+
*/
|
|
443
383
|
function DashboardHeader({ manifest }: { manifest: DashboardManifest }) {
|
|
444
384
|
return (
|
|
445
385
|
<Box>
|
|
@@ -447,9 +387,34 @@ function DashboardHeader({ manifest }: { manifest: DashboardManifest }) {
|
|
|
447
387
|
{manifest.title ?? manifest.name}
|
|
448
388
|
</Typography>
|
|
449
389
|
{manifest.description && (
|
|
450
|
-
<
|
|
451
|
-
{
|
|
452
|
-
|
|
390
|
+
<Box
|
|
391
|
+
sx={{
|
|
392
|
+
color: "text.secondary",
|
|
393
|
+
typography: "body2",
|
|
394
|
+
// The block starts flush under the title and ends flush
|
|
395
|
+
// against the controls, so a one-line description sits
|
|
396
|
+
// exactly where the old `Typography` put it and a longer one
|
|
397
|
+
// grows downward rather than pushing the title around.
|
|
398
|
+
"& > :first-of-type": { mt: 0 },
|
|
399
|
+
"& > :last-child": { mb: 0 },
|
|
400
|
+
// Headings in a description are section labels within the
|
|
401
|
+
// page, not competitors to its title, so they stay at body
|
|
402
|
+
// weight and size rather than MUI's h1..h6 scale.
|
|
403
|
+
"& h1, & h2, & h3, & h4, & h5, & h6": {
|
|
404
|
+
fontSize: "inherit",
|
|
405
|
+
fontWeight: 600,
|
|
406
|
+
m: "0.5em 0 0.25em",
|
|
407
|
+
},
|
|
408
|
+
"& p": { m: "0.5em 0" },
|
|
409
|
+
"& ul, & ol": { m: "0.5em 0", pl: 3 },
|
|
410
|
+
"& code": {
|
|
411
|
+
fontFamily: "monospace",
|
|
412
|
+
fontSize: "0.9em",
|
|
413
|
+
},
|
|
414
|
+
}}
|
|
415
|
+
>
|
|
416
|
+
<Markdown>{manifest.description}</Markdown>
|
|
417
|
+
</Box>
|
|
453
418
|
)}
|
|
454
419
|
</Box>
|
|
455
420
|
);
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
// SPDX-License-Identifier: MIT
|
|
3
3
|
|
|
4
4
|
import { Box, Paper, Typography } from "@mui/material";
|
|
5
|
+
import { useMemo } from "react";
|
|
5
6
|
import { DASHBOARD_CARD_PADDING_PX } from "../../theme/buildTableCssVars";
|
|
6
7
|
import { usePublisherTheme } from "../../theme/ThemeContext";
|
|
7
8
|
import { useQueryWithApiError } from "../../hooks/useQueryWithApiError";
|
|
@@ -13,6 +14,7 @@ import { givensToRequest } from "../given/paramCodec";
|
|
|
13
14
|
import { Loading } from "../Loading";
|
|
14
15
|
import ResultContainer from "../RenderedResult/ResultContainer";
|
|
15
16
|
import { useServer } from "../ServerProvider";
|
|
17
|
+
import { promoteMeasureRowToKpis } from "./promoteMeasureRow";
|
|
16
18
|
|
|
17
19
|
export interface DashboardTileProps {
|
|
18
20
|
environmentName: string;
|
|
@@ -124,6 +126,18 @@ export function DashboardTile({
|
|
|
124
126
|
...CHART_RESULT_QUERY_OPTIONS,
|
|
125
127
|
});
|
|
126
128
|
|
|
129
|
+
// A composite tile that is one row of measures draws as KPI cards, the way
|
|
130
|
+
// Malloyyo splices the same tile into its grid, rather than as a one-row
|
|
131
|
+
// table. Composite only: the single-query form is one result the renderer
|
|
132
|
+
// lays out from the query's own tags, and its aggregates are already tiles.
|
|
133
|
+
// Memoized on the result string so a large result is not re-parsed on every
|
|
134
|
+
// render of the tile around it.
|
|
135
|
+
const result = useMemo(() => {
|
|
136
|
+
const raw = data?.data.result;
|
|
137
|
+
if (raw === undefined || tile === undefined) return raw;
|
|
138
|
+
return promoteMeasureRowToKpis(raw);
|
|
139
|
+
}, [data, tile]);
|
|
140
|
+
|
|
127
141
|
return (
|
|
128
142
|
<Paper
|
|
129
143
|
elevation={0}
|
|
@@ -138,9 +152,18 @@ export function DashboardTile({
|
|
|
138
152
|
// honours by dropping background, border, radius and most padding on
|
|
139
153
|
// its own `.dashboard-item`. Same here, so the tag reads the same on
|
|
140
154
|
// both forms.
|
|
155
|
+
//
|
|
156
|
+
// The background is `theme.tile`, the same value the renderer card
|
|
157
|
+
// paints, and NOT MUI's Paper default. Leaving it unset was the last
|
|
158
|
+
// piece of the two cards' geometry that did not agree: measured on
|
|
159
|
+
// the `grid`/`tiled` fixture pair, the renderer card came out
|
|
160
|
+
// `#f5fafc` from the theme and this one plain white, so on a theme
|
|
161
|
+
// whose page is also white the composite tiles lost the tint that
|
|
162
|
+
// separates a card from the page while the single-query form kept
|
|
163
|
+
// it. Radius, padding, border, shadow and gap already matched.
|
|
141
164
|
border: borderless ? "none" : theme.border,
|
|
142
165
|
borderRadius: borderless ? 0 : 1,
|
|
143
|
-
background: borderless ? "none" :
|
|
166
|
+
background: borderless ? "none" : theme.tile,
|
|
144
167
|
overflow: "hidden",
|
|
145
168
|
minHeight: 120,
|
|
146
169
|
p: borderless ? "12px 0" : `${DASHBOARD_CARD_PADDING_PX}px`,
|
|
@@ -179,7 +202,7 @@ export function DashboardTile({
|
|
|
179
202
|
{!isSuccess && !isError && <Loading text="Running…" />}
|
|
180
203
|
{isSuccess && (
|
|
181
204
|
<ResultContainer
|
|
182
|
-
result={
|
|
205
|
+
result={result}
|
|
183
206
|
maxHeight={height}
|
|
184
207
|
maxResultSize={maxResultSize}
|
|
185
208
|
renderLogs={data.data.renderLogs}
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
// Copyright (c) Credible Data Inc.
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
|
|
4
|
+
import { describe, expect, it } from "bun:test";
|
|
5
|
+
import {
|
|
6
|
+
carriesRenderTag,
|
|
7
|
+
isMeasureRow,
|
|
8
|
+
promoteMeasureRowToKpis,
|
|
9
|
+
} from "./promoteMeasureRow";
|
|
10
|
+
|
|
11
|
+
/** One row of two measures, in the interfaces format a tile result arrives in. */
|
|
12
|
+
const measureRow = (annotations: string[] = []) => ({
|
|
13
|
+
schema: {
|
|
14
|
+
fields: [
|
|
15
|
+
{
|
|
16
|
+
kind: "measure",
|
|
17
|
+
name: "total_sales",
|
|
18
|
+
type: { kind: "number_type" },
|
|
19
|
+
},
|
|
20
|
+
{
|
|
21
|
+
kind: "measure",
|
|
22
|
+
name: "order_count",
|
|
23
|
+
type: { kind: "number_type" },
|
|
24
|
+
},
|
|
25
|
+
],
|
|
26
|
+
},
|
|
27
|
+
data: {
|
|
28
|
+
kind: "array_cell",
|
|
29
|
+
array_value: [
|
|
30
|
+
{
|
|
31
|
+
kind: "record_cell",
|
|
32
|
+
record_value: [
|
|
33
|
+
{ kind: "number_cell", number_value: 1200 },
|
|
34
|
+
{ kind: "number_cell", number_value: 42 },
|
|
35
|
+
],
|
|
36
|
+
},
|
|
37
|
+
],
|
|
38
|
+
},
|
|
39
|
+
annotations: annotations.map((value) => ({ value })),
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
const annotationsOf = (json: string): string[] =>
|
|
43
|
+
(JSON.parse(json).annotations as Array<{ value: string }>).map(
|
|
44
|
+
(a) => a.value,
|
|
45
|
+
);
|
|
46
|
+
|
|
47
|
+
describe("promoteMeasureRowToKpis", () => {
|
|
48
|
+
it("prepends # big_value to a bare row of measures", () => {
|
|
49
|
+
const out = promoteMeasureRowToKpis(JSON.stringify(measureRow()));
|
|
50
|
+
expect(annotationsOf(out)).toEqual(["# big_value\n"]);
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
it("keeps the author's other annotations behind it", () => {
|
|
54
|
+
const out = promoteMeasureRowToKpis(
|
|
55
|
+
JSON.stringify(measureRow(['# label="Key figures"\n'])),
|
|
56
|
+
);
|
|
57
|
+
expect(annotationsOf(out)).toEqual([
|
|
58
|
+
"# big_value\n",
|
|
59
|
+
'# label="Key figures"\n',
|
|
60
|
+
]);
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
// The author's choice wins, and `# table` is the opt-out: the same result
|
|
64
|
+
// with an explicit rendering is returned byte-identical.
|
|
65
|
+
it.each(["# big_value\n", "# table\n", '# label="x" bar_chart\n'])(
|
|
66
|
+
"leaves a result alone that already carries %j",
|
|
67
|
+
(tag) => {
|
|
68
|
+
const input = JSON.stringify(measureRow([tag]));
|
|
69
|
+
expect(promoteMeasureRowToKpis(input)).toBe(input);
|
|
70
|
+
},
|
|
71
|
+
);
|
|
72
|
+
|
|
73
|
+
it("leaves a result with a dimension alone", () => {
|
|
74
|
+
const row = measureRow();
|
|
75
|
+
row.schema.fields.push({
|
|
76
|
+
kind: "dimension",
|
|
77
|
+
name: "state",
|
|
78
|
+
type: { kind: "string_type" },
|
|
79
|
+
});
|
|
80
|
+
const input = JSON.stringify(row);
|
|
81
|
+
expect(promoteMeasureRowToKpis(input)).toBe(input);
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
it("leaves several rows of measures alone", () => {
|
|
85
|
+
const row = measureRow();
|
|
86
|
+
row.data.array_value.push(row.data.array_value[0]);
|
|
87
|
+
const input = JSON.stringify(row);
|
|
88
|
+
expect(promoteMeasureRowToKpis(input)).toBe(input);
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
it("returns anything it cannot parse untouched, for the renderer to report", () => {
|
|
92
|
+
expect(promoteMeasureRowToKpis("not json")).toBe("not json");
|
|
93
|
+
expect(promoteMeasureRowToKpis("null")).toBe("null");
|
|
94
|
+
});
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
describe("isMeasureRow", () => {
|
|
98
|
+
it("accepts a single record cell as well as a one-element array", () => {
|
|
99
|
+
const row = measureRow();
|
|
100
|
+
expect(isMeasureRow(row)).toBe(true);
|
|
101
|
+
expect(
|
|
102
|
+
isMeasureRow({ ...row, data: row.data.array_value[0] as never }),
|
|
103
|
+
).toBe(true);
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
it("refuses an empty schema", () => {
|
|
107
|
+
expect(
|
|
108
|
+
isMeasureRow({ schema: { fields: [] }, data: measureRow().data }),
|
|
109
|
+
).toBe(false);
|
|
110
|
+
});
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
describe("carriesRenderTag", () => {
|
|
114
|
+
it("reads a tag anywhere in a property list", () => {
|
|
115
|
+
expect(
|
|
116
|
+
carriesRenderTag(['# label="Sales" bar_chart { x=month }\n']),
|
|
117
|
+
).toBe(true);
|
|
118
|
+
expect(carriesRenderTag(["# bar_chart.stack\n"])).toBe(true);
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
it("ignores documentation lines and formatting-only tags", () => {
|
|
122
|
+
expect(carriesRenderTag(['#" A table of sales\n'])).toBe(false);
|
|
123
|
+
expect(carriesRenderTag(["#(doc) list of orders\n"])).toBe(false);
|
|
124
|
+
expect(carriesRenderTag(["# currency colspan=2\n"])).toBe(false);
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
it("matches whole names only", () => {
|
|
128
|
+
expect(carriesRenderTag(["# bar_charts\n"])).toBe(false);
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
// A tag name inside a label reads as a tag. The only effect is a withheld
|
|
132
|
+
// promotion, which is the safe direction, and the spec pins that it is so.
|
|
133
|
+
it("errs toward not promoting when a quoted value names a tag", () => {
|
|
134
|
+
expect(carriesRenderTag(['# label="table"\n'])).toBe(false);
|
|
135
|
+
expect(carriesRenderTag(["# label=table\n"])).toBe(true);
|
|
136
|
+
});
|
|
137
|
+
});
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
// Copyright (c) Credible Data Inc.
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Promote a composite tile whose result is one row of measures to KPI tiles.
|
|
6
|
+
*
|
|
7
|
+
* Malloyyo does this for every composite tile: a tile that comes back as a
|
|
8
|
+
* single row with nothing but measures is spliced into the grid as big-value
|
|
9
|
+
* cards, the way top-level `aggregate:` items of a `# dashboard` query render.
|
|
10
|
+
* Publisher runs each tile through the renderer on its own, so the same result
|
|
11
|
+
* drew as a one-row table unless the view carried `# big_value` itself. A repo
|
|
12
|
+
* written for Malloyyo therefore looked different here for no reason an author
|
|
13
|
+
* could see in the file.
|
|
14
|
+
*
|
|
15
|
+
* The promotion is the renderer's own tag, prepended to the result's
|
|
16
|
+
* annotations, so the KPI tiles are exactly what `# big_value` on the view would
|
|
17
|
+
* have produced. It steps aside whenever the author already said how the result
|
|
18
|
+
* should draw: any render tag on the result leaves it alone, `# table` included,
|
|
19
|
+
* which is how an author opts out.
|
|
20
|
+
*
|
|
21
|
+
* Textual rather than through `@malloydata/malloy-tag`, which the SDK loads
|
|
22
|
+
* lazily so a host may bundle without it; a synchronous check in the render
|
|
23
|
+
* path cannot wait on that import. The cost is that a tag NAME inside a quoted
|
|
24
|
+
* string reads as a tag, which only ever withholds the promotion.
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* The renderer's top-level render tags: any one of these on the result means
|
|
29
|
+
* the author chose a rendering, and this module has nothing to add.
|
|
30
|
+
*/
|
|
31
|
+
const RENDER_TAGS = new Set([
|
|
32
|
+
"bar_chart",
|
|
33
|
+
"line_chart",
|
|
34
|
+
"scatter_chart",
|
|
35
|
+
"shape_map",
|
|
36
|
+
"segment_map",
|
|
37
|
+
"point_map",
|
|
38
|
+
"big_value",
|
|
39
|
+
"sparkline",
|
|
40
|
+
"list",
|
|
41
|
+
"list_detail",
|
|
42
|
+
"dashboard",
|
|
43
|
+
"transpose",
|
|
44
|
+
"table",
|
|
45
|
+
"json",
|
|
46
|
+
"image",
|
|
47
|
+
"link",
|
|
48
|
+
"text",
|
|
49
|
+
]);
|
|
50
|
+
|
|
51
|
+
const BIG_VALUE_ANNOTATION = { value: "# big_value\n" };
|
|
52
|
+
|
|
53
|
+
interface ResultShape {
|
|
54
|
+
schema?: { fields?: Array<{ kind?: string }> };
|
|
55
|
+
data?: { kind?: string; array_value?: unknown[] };
|
|
56
|
+
annotations?: Array<{ value?: string }>;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* True when the result is one row consisting solely of measures: the shape a
|
|
61
|
+
* `view: kpis is { aggregate: … }` tile produces.
|
|
62
|
+
*/
|
|
63
|
+
export function isMeasureRow(result: ResultShape): boolean {
|
|
64
|
+
const fields = result.schema?.fields;
|
|
65
|
+
if (!Array.isArray(fields) || fields.length === 0) return false;
|
|
66
|
+
if (!fields.every((field) => field?.kind === "measure")) return false;
|
|
67
|
+
const data = result.data;
|
|
68
|
+
if (!data) return false;
|
|
69
|
+
if (data.kind === "record_cell") return true;
|
|
70
|
+
return (
|
|
71
|
+
data.kind === "array_cell" &&
|
|
72
|
+
Array.isArray(data.array_value) &&
|
|
73
|
+
data.array_value.length === 1
|
|
74
|
+
);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* True when any annotation on the result names a render tag.
|
|
79
|
+
*
|
|
80
|
+
* A `# ` line is a MOTLY tag list: space-separated properties, each a bare name
|
|
81
|
+
* or `name=value`, with `{ … }` sub-properties and `.` paths. Quoted values are
|
|
82
|
+
* dropped before tokenizing so a space inside a label does not split it; the
|
|
83
|
+
* remaining tokens are compared whole, so `bar_chart` matches and `bar_charts`
|
|
84
|
+
* does not. `#(…)` and `#"` lines are documentation, never render tags.
|
|
85
|
+
*/
|
|
86
|
+
export function carriesRenderTag(annotations: readonly string[]): boolean {
|
|
87
|
+
for (const line of annotations) {
|
|
88
|
+
const text = line.trim();
|
|
89
|
+
if (
|
|
90
|
+
!text.startsWith("#") ||
|
|
91
|
+
text.startsWith("#(") ||
|
|
92
|
+
text.startsWith('#"')
|
|
93
|
+
)
|
|
94
|
+
continue;
|
|
95
|
+
const body = text.slice(1).replace(/"(?:[^"\\]|\\.)*"/g, '""');
|
|
96
|
+
for (const token of body.split(/[\s{}=.,[\]]+/)) {
|
|
97
|
+
if (RENDER_TAGS.has(token)) return true;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
return false;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* The result with `# big_value` prepended when {@link isMeasureRow} and no render
|
|
105
|
+
* tag is already present; otherwise the input string untouched. A result that
|
|
106
|
+
* does not parse as JSON is returned as is, for the renderer to report.
|
|
107
|
+
*/
|
|
108
|
+
export function promoteMeasureRowToKpis(result: string): string {
|
|
109
|
+
let parsed: ResultShape;
|
|
110
|
+
try {
|
|
111
|
+
parsed = JSON.parse(result) as ResultShape;
|
|
112
|
+
} catch {
|
|
113
|
+
return result;
|
|
114
|
+
}
|
|
115
|
+
if (!parsed || typeof parsed !== "object" || !isMeasureRow(parsed)) {
|
|
116
|
+
return result;
|
|
117
|
+
}
|
|
118
|
+
const annotations = Array.isArray(parsed.annotations)
|
|
119
|
+
? parsed.annotations
|
|
120
|
+
: [];
|
|
121
|
+
const lines = annotations
|
|
122
|
+
.map((a) => a?.value)
|
|
123
|
+
.filter((v): v is string => typeof v === "string");
|
|
124
|
+
if (carriesRenderTag(lines)) return result;
|
|
125
|
+
return JSON.stringify({
|
|
126
|
+
...parsed,
|
|
127
|
+
annotations: [BIG_VALUE_ANNOTATION, ...annotations],
|
|
128
|
+
});
|
|
129
|
+
}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
// Copyright (c) Credible Data Inc.
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
|
|
4
|
+
import { beforeEach, describe, expect, it } from "bun:test";
|
|
5
|
+
import { BrowserDocumentStorage } from "./BrowserDocumentStorage";
|
|
6
|
+
import type { DocumentLocator, Workspace } from "./DocumentStorage";
|
|
7
|
+
|
|
8
|
+
const local: Workspace = { name: "Local", writeable: true, description: "" };
|
|
9
|
+
const dashboard: DocumentLocator = {
|
|
10
|
+
workspace: "Local",
|
|
11
|
+
type: "dashboard",
|
|
12
|
+
path: "dashboards/overview.malloy",
|
|
13
|
+
};
|
|
14
|
+
const notebook: DocumentLocator = {
|
|
15
|
+
workspace: "Local",
|
|
16
|
+
type: "notebook",
|
|
17
|
+
path: "tour.malloynb",
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
describe("BrowserDocumentStorage", () => {
|
|
21
|
+
beforeEach(() => localStorage.clear());
|
|
22
|
+
|
|
23
|
+
it("round-trips a document", async () => {
|
|
24
|
+
const store = new BrowserDocumentStorage();
|
|
25
|
+
await store.saveDocument(dashboard, "## artifact { tiles=[] }");
|
|
26
|
+
expect(await store.getDocument(dashboard)).toBe(
|
|
27
|
+
"## artifact { tiles=[] }",
|
|
28
|
+
);
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it("lists its own documents, by kind, and nothing else in localStorage", async () => {
|
|
32
|
+
const store = new BrowserDocumentStorage();
|
|
33
|
+
localStorage.setItem("publisher:themeMode", "dark");
|
|
34
|
+
await store.saveDocument(dashboard, "a");
|
|
35
|
+
await store.saveDocument(notebook, "b");
|
|
36
|
+
expect(await store.listDocuments(local)).toEqual(
|
|
37
|
+
expect.arrayContaining([dashboard, notebook]),
|
|
38
|
+
);
|
|
39
|
+
expect(await store.listDocuments(local)).toHaveLength(2);
|
|
40
|
+
expect(await store.listDocuments(local, "notebook")).toEqual([notebook]);
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it("keeps a path containing the separator intact", async () => {
|
|
44
|
+
const store = new BrowserDocumentStorage();
|
|
45
|
+
const odd = { ...dashboard, path: "dashboards/a:b.malloy" };
|
|
46
|
+
await store.saveDocument(odd, "x");
|
|
47
|
+
expect(await store.listDocuments(local, "dashboard")).toEqual([odd]);
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
it("rejects a read, delete or move of a document that is not there", async () => {
|
|
51
|
+
const store = new BrowserDocumentStorage();
|
|
52
|
+
await expect(store.getDocument(dashboard)).rejects.toThrow(
|
|
53
|
+
/No dashboard/,
|
|
54
|
+
);
|
|
55
|
+
await expect(store.deleteDocument(dashboard)).rejects.toThrow();
|
|
56
|
+
await expect(store.moveDocument(dashboard, notebook)).rejects.toThrow();
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
it("moves a document, content intact, and forgets the old address", async () => {
|
|
60
|
+
const store = new BrowserDocumentStorage();
|
|
61
|
+
await store.saveDocument(dashboard, "content");
|
|
62
|
+
const renamed = { ...dashboard, path: "dashboards/sales.malloy" };
|
|
63
|
+
await store.moveDocument(dashboard, renamed);
|
|
64
|
+
expect(await store.getDocument(renamed)).toBe("content");
|
|
65
|
+
await expect(store.getDocument(dashboard)).rejects.toThrow();
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
it("has one writeable workspace and lists nothing for any other", async () => {
|
|
69
|
+
const store = new BrowserDocumentStorage();
|
|
70
|
+
expect(await store.listWorkspaces(true)).toEqual([
|
|
71
|
+
expect.objectContaining({ name: "Local", writeable: true }),
|
|
72
|
+
]);
|
|
73
|
+
expect(
|
|
74
|
+
await store.listDocuments({
|
|
75
|
+
name: "Shared",
|
|
76
|
+
writeable: false,
|
|
77
|
+
description: "",
|
|
78
|
+
}),
|
|
79
|
+
).toEqual([]);
|
|
80
|
+
});
|
|
81
|
+
});
|