@malloy-publisher/sdk 0.2.7 → 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.
- package/README.md +180 -204
- package/dist/DocumentStorageProvider-BeBD0uWc.cjs.js +134 -0
- package/dist/DocumentStorageProvider-tGvodI8f.es.js +24268 -0
- package/dist/RenderedResult-CPi6uQWq.cjs.js +115 -0
- package/dist/RenderedResult-mDE-4u8S.es.js +542 -0
- package/dist/{ServerProvider-xXW1HJOJ.cjs.js → ServerProvider-CH71GsoL.cjs.js} +1 -1
- package/dist/{ServerProvider-ksC5Su_x.es.js → ServerProvider-X2Ma0HRs.es.js} +324 -359
- package/dist/builder/index.cjs.js +105 -0
- package/dist/builder/index.d.ts +2 -0
- package/dist/builder/index.es.js +9561 -0
- package/dist/builder-entry.d.ts +8 -0
- 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 +23 -16
- package/dist/components/Dashboard/DashboardGrid.d.ts +67 -0
- package/dist/components/Dashboard/DashboardTile.d.ts +6 -1
- package/dist/components/Dashboard/ExploreDialog.d.ts +18 -0
- package/dist/components/Dashboard/RowsDialog.d.ts +47 -0
- package/dist/components/Dashboard/TileCard.d.ts +44 -0
- package/dist/components/Dashboard/index.d.ts +2 -0
- package/dist/components/Dashboard/promoteMeasureRow.d.ts +40 -0
- package/dist/components/Dashboard/telemetry.d.ts +43 -0
- package/dist/components/DashboardBuilder/AddTileDialog.d.ts +35 -0
- package/dist/components/DashboardBuilder/BuilderToolbar.d.ts +29 -0
- package/dist/components/DashboardBuilder/DashboardBuilder.d.ts +112 -0
- package/dist/components/DashboardBuilder/DashboardEditor.d.ts +32 -0
- package/dist/components/DashboardBuilder/DiffDialog.d.ts +15 -0
- package/dist/components/DashboardBuilder/DrillDialog.d.ts +15 -0
- package/dist/components/DashboardBuilder/FilterDialog.d.ts +54 -0
- package/dist/components/DashboardBuilder/FilterStrip.d.ts +17 -0
- package/dist/components/DashboardBuilder/SettingsPopover.d.ts +20 -0
- package/dist/components/DashboardBuilder/TileFrame.d.ts +42 -0
- package/dist/components/DashboardBuilder/TileMenu.d.ts +24 -0
- package/dist/components/DashboardBuilder/catalog.d.ts +62 -0
- package/dist/components/DashboardBuilder/controls.d.ts +157 -0
- package/dist/components/DashboardBuilder/diff.d.ts +35 -0
- package/dist/components/DashboardBuilder/document.d.ts +218 -0
- package/dist/components/DashboardBuilder/layout.d.ts +71 -0
- package/dist/components/DashboardBuilder/malloyText.d.ts +45 -0
- package/dist/components/DashboardBuilder/preview.d.ts +60 -0
- package/dist/components/DashboardBuilder/readDocument.d.ts +106 -0
- package/dist/components/DashboardBuilder/sortable.d.ts +51 -0
- package/dist/components/DashboardBuilder/spliceDocument.d.ts +44 -0
- package/dist/components/DashboardBuilder/testing/fixtures.d.ts +10 -0
- package/dist/components/DashboardBuilder/useBuilderShortcuts.d.ts +22 -0
- package/dist/components/DashboardBuilder/useDashboardEditor.d.ts +63 -0
- package/dist/components/DashboardBuilder/useDraft.d.ts +13 -0
- package/dist/components/DashboardBuilder/useFilterForm.d.ts +65 -0
- package/dist/components/DashboardBuilder/useTileReorder.d.ts +22 -0
- package/dist/components/DashboardBuilder/useTileResize.d.ts +29 -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 +25 -0
- package/dist/components/DocumentStorage/index.d.ts +3 -0
- package/dist/components/FloatingIconButton.d.ts +16 -0
- package/dist/components/Model/index.d.ts +0 -1
- package/dist/components/Prose.d.ts +46 -0
- package/dist/components/RenderedResult/RenderedResult.d.ts +12 -0
- package/dist/components/RenderedResult/ResultContainer.d.ts +7 -1
- package/dist/components/RenderedResult/ResultPanel.d.ts +30 -0
- package/dist/components/RenderedResult/index.d.ts +2 -0
- package/dist/components/RenderedResult/resultSizing.d.ts +183 -0
- package/dist/components/drill/index.d.ts +2 -1
- package/dist/components/drill/resolveDrill.d.ts +22 -0
- package/dist/components/drill/useDrill.d.ts +11 -2
- 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 +3 -3
- package/dist/components/styles.d.ts +3 -7
- package/dist/core-8NVBCLZh.cjs.js +148 -0
- package/dist/{core-BY7vB5gN.es.js → core-B8oMW4sm.es.js} +3540 -2335
- package/dist/hooks/index.d.ts +3 -1
- package/dist/hooks/useDocumentControls.d.ts +70 -0
- package/dist/hooks/useQueryResult.d.ts +40 -0
- package/dist/hooks/useSuggestOptions.d.ts +28 -12
- package/dist/index.cjs.js +34 -1
- package/dist/index.es.js +21487 -79
- package/dist/sdk.css +1 -1
- package/dist/styles.css +0 -1
- package/dist/utils/malloyLiteral.d.ts +11 -0
- package/package.json +25 -14
- package/src/builder-entry.ts +55 -0
- package/src/components/ApiErrorDisplay.tsx +1 -1
- package/src/components/Connections/EditConnectionDialog.tsx +96 -86
- package/src/components/Dashboard/Dashboard.spec.tsx +7 -10
- package/src/components/Dashboard/Dashboard.tsx +184 -229
- package/src/components/Dashboard/DashboardGrid.tsx +124 -0
- package/src/components/Dashboard/DashboardTile.spec.tsx +30 -6
- package/src/components/Dashboard/DashboardTile.tsx +67 -101
- package/src/components/Dashboard/ExploreDialog.tsx +84 -0
- package/src/components/Dashboard/RowsDialog.spec.ts +42 -0
- package/src/components/Dashboard/RowsDialog.tsx +234 -0
- package/src/components/Dashboard/TileCard.tsx +120 -0
- package/src/components/Dashboard/index.ts +2 -0
- package/src/components/Dashboard/promoteMeasureRow.spec.ts +207 -0
- package/src/components/Dashboard/promoteMeasureRow.ts +160 -0
- package/src/components/Dashboard/telemetry.ts +44 -0
- package/src/components/Dashboard/tileGridColumn.spec.ts +1 -1
- package/src/components/DashboardBuilder/AddTileDialog.tsx +249 -0
- package/src/components/DashboardBuilder/BuilderToolbar.tsx +270 -0
- package/src/components/DashboardBuilder/DashboardBuilder.spec.tsx +858 -0
- package/src/components/DashboardBuilder/DashboardBuilder.tsx +653 -0
- package/src/components/DashboardBuilder/DashboardEditor.spec.tsx +211 -0
- package/src/components/DashboardBuilder/DashboardEditor.tsx +493 -0
- package/src/components/DashboardBuilder/DiffDialog.tsx +129 -0
- package/src/components/DashboardBuilder/DrillDialog.tsx +281 -0
- package/src/components/DashboardBuilder/FilterDialog.tsx +581 -0
- package/src/components/DashboardBuilder/FilterStrip.tsx +125 -0
- package/src/components/DashboardBuilder/SettingsPopover.tsx +168 -0
- package/src/components/DashboardBuilder/TileFrame.tsx +380 -0
- package/src/components/DashboardBuilder/TileMenu.tsx +188 -0
- package/src/components/DashboardBuilder/catalog.spec.ts +215 -0
- package/src/components/DashboardBuilder/catalog.ts +225 -0
- package/src/components/DashboardBuilder/controls.spec.ts +243 -0
- package/src/components/DashboardBuilder/controls.ts +398 -0
- package/src/components/DashboardBuilder/diff.spec.ts +44 -0
- package/src/components/DashboardBuilder/diff.ts +93 -0
- package/src/components/DashboardBuilder/document.ts +209 -0
- package/src/components/DashboardBuilder/keepRowStructure.spec.ts +84 -0
- package/src/components/DashboardBuilder/layout.ts +143 -0
- package/src/components/DashboardBuilder/malloyText.spec.ts +84 -0
- package/src/components/DashboardBuilder/malloyText.ts +151 -0
- package/src/components/DashboardBuilder/moveIntoGap.spec.ts +90 -0
- package/src/components/DashboardBuilder/preview.spec.ts +149 -0
- package/src/components/DashboardBuilder/preview.ts +172 -0
- package/src/components/DashboardBuilder/readDocument.spec.ts +375 -0
- package/src/components/DashboardBuilder/readDocument.ts +467 -0
- package/src/components/DashboardBuilder/sortable.tsx +151 -0
- package/src/components/DashboardBuilder/spliceDocument.spec.ts +1108 -0
- package/src/components/DashboardBuilder/spliceDocument.ts +1098 -0
- package/src/components/DashboardBuilder/testing/fixtures.ts +48 -0
- package/src/components/DashboardBuilder/useBuilderShortcuts.ts +88 -0
- package/src/components/DashboardBuilder/useDashboardEditor.spec.ts +255 -0
- package/src/components/DashboardBuilder/useDashboardEditor.ts +193 -0
- package/src/components/DashboardBuilder/useDraft.ts +43 -0
- package/src/components/DashboardBuilder/useFilterForm.spec.tsx +144 -0
- package/src/components/DashboardBuilder/useFilterForm.ts +352 -0
- package/src/components/DashboardBuilder/useTileReorder.spec.tsx +97 -0
- package/src/components/DashboardBuilder/useTileReorder.ts +115 -0
- package/src/components/DashboardBuilder/useTileResize.spec.tsx +121 -0
- package/src/components/DashboardBuilder/useTileResize.ts +110 -0
- package/src/components/DashboardBuilder/withGaps.spec.ts +96 -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 +58 -0
- package/src/components/DocumentStorage/index.ts +16 -0
- package/src/components/Environment/About.tsx +4 -36
- package/src/components/FloatingIconButton.tsx +41 -0
- package/src/components/Model/Model.tsx +6 -23
- package/src/components/Model/ModelCell.tsx +34 -72
- package/src/components/Model/index.ts +0 -1
- package/src/components/Notebook/Notebook.tsx +27 -156
- package/src/components/Notebook/NotebookCell.tsx +37 -191
- package/src/components/Prose.spec.tsx +85 -0
- package/src/components/Prose.tsx +179 -0
- package/src/components/QueryResult/QueryResult.tsx +15 -43
- package/src/components/RenderedResult/RenderedResult.tsx +86 -76
- package/src/components/RenderedResult/ResultContainer.tsx +25 -36
- package/src/components/RenderedResult/ResultPanel.spec.tsx +49 -0
- package/src/components/RenderedResult/ResultPanel.tsx +73 -0
- package/src/components/RenderedResult/index.ts +16 -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 +6 -0
- package/src/components/drill/resolveDrill.ts +22 -1
- package/src/components/drill/useDrill.spec.tsx +94 -0
- package/src/components/drill/useDrill.tsx +52 -12
- 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/GivensPanel.tsx +1 -1
- 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 +11 -3
- package/src/components/styles.ts +0 -37
- package/src/hooks/index.ts +13 -1
- package/src/hooks/useDocumentControls.ts +170 -0
- package/src/hooks/useGivensState.spec.ts +158 -0
- package/src/hooks/useGivensState.ts +114 -23
- package/src/hooks/useQueryResult.ts +82 -0
- package/src/hooks/useSuggestOptions.dom.spec.ts +60 -1
- package/src/hooks/useSuggestOptions.ts +94 -51
- package/src/utils/malloyLiteral.spec.ts +43 -0
- package/src/utils/malloyLiteral.ts +76 -0
- package/vite.config.ts +5 -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-CWKmWEg2.cjs.js +0 -148
- package/dist/index-B0WyZAUA.es.js +0 -66412
- package/dist/index-kxVD1RjY.cjs.js +0 -356
- 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
|
@@ -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
|
+
}
|
|
@@ -0,0 +1,207 @@
|
|
|
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
|
+
/**
|
|
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
|
+
|
|
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
|
+
|
|
168
|
+
it("accepts a single record cell as well as a one-element array", () => {
|
|
169
|
+
const row = measureRow();
|
|
170
|
+
expect(isMeasureRow(row)).toBe(true);
|
|
171
|
+
expect(
|
|
172
|
+
isMeasureRow({ ...row, data: row.data.array_value[0] as never }),
|
|
173
|
+
).toBe(true);
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
it("refuses an empty schema", () => {
|
|
177
|
+
expect(
|
|
178
|
+
isMeasureRow({ schema: { fields: [] }, data: measureRow().data }),
|
|
179
|
+
).toBe(false);
|
|
180
|
+
});
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
describe("carriesRenderTag", () => {
|
|
184
|
+
it("reads a tag anywhere in a property list", () => {
|
|
185
|
+
expect(
|
|
186
|
+
carriesRenderTag(['# label="Sales" bar_chart { x=month }\n']),
|
|
187
|
+
).toBe(true);
|
|
188
|
+
expect(carriesRenderTag(["# bar_chart.stack\n"])).toBe(true);
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
it("ignores documentation lines and formatting-only tags", () => {
|
|
192
|
+
expect(carriesRenderTag(['#" A table of sales\n'])).toBe(false);
|
|
193
|
+
expect(carriesRenderTag(["#(doc) list of orders\n"])).toBe(false);
|
|
194
|
+
expect(carriesRenderTag(["# currency colspan=2\n"])).toBe(false);
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
it("matches whole names only", () => {
|
|
198
|
+
expect(carriesRenderTag(["# bar_charts\n"])).toBe(false);
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
// A tag name inside a label reads as a tag. The only effect is a withheld
|
|
202
|
+
// promotion, which is the safe direction, and the spec pins that it is so.
|
|
203
|
+
it("errs toward not promoting when a quoted value names a tag", () => {
|
|
204
|
+
expect(carriesRenderTag(['# label="table"\n'])).toBe(false);
|
|
205
|
+
expect(carriesRenderTag(["# label=table\n"])).toBe(true);
|
|
206
|
+
});
|
|
207
|
+
});
|
|
@@ -0,0 +1,160 @@
|
|
|
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 ResultField {
|
|
54
|
+
kind?: string;
|
|
55
|
+
annotations?: Array<{ value?: string }>;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
interface ResultShape {
|
|
59
|
+
schema?: { fields?: ResultField[] };
|
|
60
|
+
data?: { kind?: string; array_value?: unknown[] };
|
|
61
|
+
annotations?: Array<{ value?: string }>;
|
|
62
|
+
}
|
|
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
|
+
|
|
90
|
+
/**
|
|
91
|
+
* True when the result is one row consisting solely of measures: the shape a
|
|
92
|
+
* `view: kpis is { aggregate: … }` tile produces.
|
|
93
|
+
*/
|
|
94
|
+
export function isMeasureRow(result: ResultShape): boolean {
|
|
95
|
+
const fields = result.schema?.fields;
|
|
96
|
+
if (!Array.isArray(fields) || fields.length === 0) return false;
|
|
97
|
+
if (!fields.every(isMeasureField)) return false;
|
|
98
|
+
const data = result.data;
|
|
99
|
+
if (!data) return false;
|
|
100
|
+
if (data.kind === "record_cell") return true;
|
|
101
|
+
return (
|
|
102
|
+
data.kind === "array_cell" &&
|
|
103
|
+
Array.isArray(data.array_value) &&
|
|
104
|
+
data.array_value.length === 1
|
|
105
|
+
);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* True when any annotation on the result names a render tag.
|
|
110
|
+
*
|
|
111
|
+
* A `# ` line is a MOTLY tag list: space-separated properties, each a bare name
|
|
112
|
+
* or `name=value`, with `{ … }` sub-properties and `.` paths. Quoted values are
|
|
113
|
+
* dropped before tokenizing so a space inside a label does not split it; the
|
|
114
|
+
* remaining tokens are compared whole, so `bar_chart` matches and `bar_charts`
|
|
115
|
+
* does not. `#(…)` and `#"` lines are documentation, never render tags.
|
|
116
|
+
*/
|
|
117
|
+
export function carriesRenderTag(annotations: readonly string[]): boolean {
|
|
118
|
+
for (const line of annotations) {
|
|
119
|
+
const text = line.trim();
|
|
120
|
+
if (
|
|
121
|
+
!text.startsWith("#") ||
|
|
122
|
+
text.startsWith("#(") ||
|
|
123
|
+
text.startsWith('#"')
|
|
124
|
+
)
|
|
125
|
+
continue;
|
|
126
|
+
const body = text.slice(1).replace(/"(?:[^"\\]|\\.)*"/g, '""');
|
|
127
|
+
for (const token of body.split(/[\s{}=.,[\]]+/)) {
|
|
128
|
+
if (RENDER_TAGS.has(token)) return true;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
return false;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* The result with `# big_value` prepended when {@link isMeasureRow} and no render
|
|
136
|
+
* tag is already present; otherwise the input string untouched. A result that
|
|
137
|
+
* does not parse as JSON is returned as is, for the renderer to report.
|
|
138
|
+
*/
|
|
139
|
+
export function promoteMeasureRowToKpis(result: string): string {
|
|
140
|
+
let parsed: ResultShape;
|
|
141
|
+
try {
|
|
142
|
+
parsed = JSON.parse(result) as ResultShape;
|
|
143
|
+
} catch {
|
|
144
|
+
return result;
|
|
145
|
+
}
|
|
146
|
+
if (!parsed || typeof parsed !== "object" || !isMeasureRow(parsed)) {
|
|
147
|
+
return result;
|
|
148
|
+
}
|
|
149
|
+
const annotations = Array.isArray(parsed.annotations)
|
|
150
|
+
? parsed.annotations
|
|
151
|
+
: [];
|
|
152
|
+
const lines = annotations
|
|
153
|
+
.map((a) => a?.value)
|
|
154
|
+
.filter((v): v is string => typeof v === "string");
|
|
155
|
+
if (carriesRenderTag(lines)) return result;
|
|
156
|
+
return JSON.stringify({
|
|
157
|
+
...parsed,
|
|
158
|
+
annotations: [BIG_VALUE_ANNOTATION, ...annotations],
|
|
159
|
+
});
|
|
160
|
+
}
|
|
@@ -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 "./
|
|
5
|
+
import { tileGridColumn } from "./DashboardGrid";
|
|
6
6
|
|
|
7
7
|
describe("tileGridColumn", () => {
|
|
8
8
|
it("spans one column when the view carries no colspan", () => {
|