@malloy-publisher/sdk 0.9.1 → 0.9.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{Dashboard-H1kJlDFo.es.js → DashboardTile-BFcxdv7R.es.js} +15918 -17760
- package/dist/DashboardTile-sg8LjUk8.cjs.js +137 -0
- package/dist/RenderedResult-B48wAjM8.cjs.js +141 -0
- package/dist/{RenderedResult-BRgIgxWh.es.js → RenderedResult-CBLHr32C.es.js} +193 -183
- package/dist/{ServerProvider-Boxrv6Ym.es.js → ServerProvider-DCV8BUWD.es.js} +529 -521
- package/dist/ServerProvider-HRqT3w1C.cjs.js +1 -0
- package/dist/builder/index.cjs.js +146 -58
- package/dist/builder/index.es.js +9076 -6479
- package/dist/builder-entry.d.ts +4 -0
- package/dist/client/api.d.ts +77 -47
- package/dist/client/configuration.d.ts +1 -1
- package/dist/client/index.cjs.js +1 -1
- package/dist/client/index.d.ts +1 -1
- package/dist/client/index.es.js +84 -82
- package/dist/components/Dashboard/Dashboard.d.ts +8 -15
- package/dist/components/Dashboard/DashboardGrid.d.ts +23 -6
- package/dist/components/Dashboard/DashboardTile.d.ts +10 -1
- package/dist/components/Dashboard/DashboardView.d.ts +36 -0
- package/dist/components/Dashboard/TileCard.d.ts +15 -4
- package/dist/components/Dashboard/TileFilterTag.d.ts +12 -0
- package/dist/components/Dashboard/index.d.ts +3 -0
- package/dist/components/Dashboard/telemetry.d.ts +15 -3
- package/dist/components/DashboardBuilder/AddTileDialog.d.ts +8 -1
- package/dist/components/DashboardBuilder/BuilderToolbar.d.ts +16 -5
- package/dist/components/DashboardBuilder/ChartPicker.d.ts +29 -0
- package/dist/components/DashboardBuilder/DashboardBuilder.d.ts +48 -21
- package/dist/components/DashboardBuilder/DashboardEditor.d.ts +17 -4
- package/dist/components/DashboardBuilder/DiffDialog.d.ts +2 -10
- package/dist/components/DashboardBuilder/FilterDialog.d.ts +2 -2
- package/dist/components/DashboardBuilder/FilterStrip.d.ts +3 -1
- package/dist/components/DashboardBuilder/InlineMarkdown.d.ts +18 -0
- package/dist/components/DashboardBuilder/InlineText.d.ts +19 -0
- package/dist/components/DashboardBuilder/NotebookEditor.d.ts +15 -0
- package/dist/components/DashboardBuilder/SaveNotice.d.ts +27 -0
- package/dist/components/DashboardBuilder/SettingsPopover.d.ts +19 -7
- package/dist/components/DashboardBuilder/SourceViewPicker.d.ts +9 -0
- package/dist/components/DashboardBuilder/TextTileBody.d.ts +7 -0
- package/dist/components/DashboardBuilder/TileFrame.d.ts +14 -12
- package/dist/components/DashboardBuilder/TileMenu.d.ts +11 -3
- package/dist/components/DashboardBuilder/annotationText.d.ts +4 -0
- package/dist/components/DashboardBuilder/catalog.d.ts +4 -0
- package/dist/components/DashboardBuilder/changedTile.d.ts +7 -0
- package/dist/components/DashboardBuilder/chartLine.d.ts +33 -0
- package/dist/components/DashboardBuilder/controls.d.ts +2 -2
- package/dist/components/DashboardBuilder/document.d.ts +42 -26
- package/dist/components/DashboardBuilder/documentSession.d.ts +100 -0
- package/dist/components/DashboardBuilder/legacyNotebook.d.ts +73 -0
- package/dist/components/DashboardBuilder/loadMalloy.d.ts +15 -0
- package/dist/components/DashboardBuilder/malloyText.d.ts +45 -5
- package/dist/components/DashboardBuilder/malloyTree.d.ts +119 -0
- package/dist/components/DashboardBuilder/newDashboard.d.ts +3 -1
- package/dist/components/DashboardBuilder/openDraft.d.ts +10 -0
- package/dist/components/DashboardBuilder/preview.d.ts +24 -4
- package/dist/components/DashboardBuilder/readDocument.d.ts +4 -1
- package/dist/components/DashboardBuilder/readForEditor.d.ts +16 -0
- package/dist/components/DashboardBuilder/sortable.d.ts +14 -0
- package/dist/components/DashboardBuilder/spliceDocument.d.ts +13 -13
- package/dist/components/DashboardBuilder/spliceResult.d.ts +11 -0
- package/dist/components/DashboardBuilder/tagParse.d.ts +24 -0
- package/dist/components/DashboardBuilder/telemetry.d.ts +43 -0
- package/dist/components/DashboardBuilder/testing/fixtures.d.ts +3 -1
- package/dist/components/DashboardBuilder/testing/inline.d.ts +4 -0
- package/dist/components/DashboardBuilder/useBuilderSession.d.ts +89 -0
- package/dist/components/DashboardBuilder/useBuilderShortcuts.d.ts +9 -6
- package/dist/components/DashboardBuilder/useDashboardEditor.d.ts +17 -55
- package/dist/components/DashboardBuilder/useDocumentEditor.d.ts +105 -0
- package/dist/components/DashboardBuilder/useExitGuard.d.ts +19 -0
- package/dist/components/DashboardBuilder/useFilterForm.d.ts +3 -3
- package/dist/components/DocumentCreate/canRetry.d.ts +6 -0
- package/dist/components/DocumentCreate/createDocument.d.ts +58 -0
- package/dist/components/DocumentCreate/documentPath.d.ts +10 -0
- package/dist/components/DocumentCreate/events.d.ts +10 -0
- package/dist/components/DocumentCreate/exportedSources.d.ts +7 -0
- package/dist/components/DocumentCreate/guards.d.ts +12 -0
- package/dist/components/DocumentCreate/index.d.ts +6 -0
- package/dist/components/DocumentCreate/newNotebook.d.ts +9 -0
- package/dist/components/DocumentCreate/useDocumentChoices.d.ts +32 -0
- package/dist/components/DocumentStorage/DocumentStorage.d.ts +2 -1
- package/dist/components/NarrowEditGate.d.ts +8 -0
- package/dist/components/Notebook/Notebook.d.ts +1 -1
- package/dist/components/Package/NewDocumentDialog.d.ts +33 -0
- package/dist/components/Package/Package.d.ts +1 -0
- package/dist/components/Package/documentLocation.d.ts +28 -0
- package/dist/components/Package/index.d.ts +4 -0
- package/dist/components/Package/useDocumentLocation.d.ts +12 -0
- package/dist/components/PackageSection.d.ts +2 -2
- package/dist/components/RenderedResult/RenderedResult.d.ts +2 -0
- package/dist/components/RenderedResult/ResultContainer.d.ts +8 -1
- package/dist/components/RenderedResult/ResultPanel.d.ts +3 -1
- package/dist/components/RenderedResult/resultSizing.d.ts +9 -1
- package/dist/components/ServerProvider.d.ts +2 -1
- package/dist/components/UnsavedChangesDialog.d.ts +9 -0
- package/dist/components/given/GivensPanel.d.ts +2 -2
- package/dist/components/index.d.ts +2 -0
- package/dist/hooks/index.d.ts +1 -0
- package/dist/hooks/useNarrowScreen.d.ts +4 -0
- package/dist/hooks/useQueryResult.d.ts +2 -0
- package/dist/hooks/useSettled.d.ts +14 -0
- package/dist/index.cjs.js +23 -24
- package/dist/index.es.js +9714 -7072
- package/dist/text/index.cjs.js +1 -0
- package/dist/text/index.d.ts +2 -0
- package/dist/text/index.es.js +6 -0
- package/dist/text-entry-BJwltZiy.cjs.js +7 -0
- package/dist/text-entry-CbbBcZl8.es.js +196 -0
- package/dist/text-entry.d.ts +2 -0
- package/dist/utils/clock.d.ts +2 -0
- package/package.json +10 -1
- package/src/builder-entry.ts +35 -0
- package/src/components/Dashboard/Dashboard.tsx +27 -278
- package/src/components/Dashboard/DashboardGrid.tsx +68 -7
- package/src/components/Dashboard/DashboardTile.spec.tsx +24 -0
- package/src/components/Dashboard/DashboardTile.tsx +31 -4
- package/src/components/Dashboard/DashboardView.spec.tsx +212 -0
- package/src/components/Dashboard/DashboardView.tsx +352 -0
- package/src/components/Dashboard/RowsDialog.tsx +1 -1
- package/src/components/Dashboard/TileCard.tsx +44 -10
- package/src/components/Dashboard/TileFilterTag.spec.tsx +73 -0
- package/src/components/Dashboard/TileFilterTag.tsx +78 -0
- package/src/components/Dashboard/defaultTileSpan.spec.ts +32 -0
- package/src/components/Dashboard/index.ts +3 -0
- package/src/components/Dashboard/telemetry.ts +16 -5
- package/src/components/DashboardBuilder/AddTileDialog.spec.tsx +88 -0
- package/src/components/DashboardBuilder/AddTileDialog.tsx +137 -111
- package/src/components/DashboardBuilder/BuilderToolbar.tsx +85 -27
- package/src/components/DashboardBuilder/ChartPicker.render.spec.tsx +25 -0
- package/src/components/DashboardBuilder/ChartPicker.spec.ts +77 -0
- package/src/components/DashboardBuilder/ChartPicker.tsx +153 -0
- package/src/components/DashboardBuilder/DashboardBuilder.charts.spec.tsx +205 -0
- package/src/components/DashboardBuilder/DashboardBuilder.exit.spec.tsx +192 -0
- package/src/components/DashboardBuilder/DashboardBuilder.inline.spec.tsx +329 -0
- package/src/components/DashboardBuilder/DashboardBuilder.notebook.spec.tsx +363 -0
- package/src/components/DashboardBuilder/DashboardBuilder.notice.spec.tsx +194 -0
- package/src/components/DashboardBuilder/DashboardBuilder.openDraft.spec.tsx +157 -0
- package/src/components/DashboardBuilder/DashboardBuilder.sources.spec.tsx +124 -0
- package/src/components/DashboardBuilder/DashboardBuilder.spec.tsx +103 -78
- package/src/components/DashboardBuilder/DashboardBuilder.textTiles.spec.tsx +177 -0
- package/src/components/DashboardBuilder/DashboardBuilder.tsx +610 -315
- package/src/components/DashboardBuilder/DashboardEditor.authoritative.spec.tsx +58 -8
- package/src/components/DashboardBuilder/DashboardEditor.emptyStart.spec.tsx +191 -0
- package/src/components/DashboardBuilder/DashboardEditor.filterTag.spec.tsx +162 -0
- package/src/components/DashboardBuilder/DashboardEditor.notebook.spec.tsx +316 -0
- package/src/components/DashboardBuilder/DashboardEditor.package.spec.tsx +56 -10
- package/src/components/DashboardBuilder/DashboardEditor.resourceUri.spec.tsx +5 -22
- package/src/components/DashboardBuilder/DashboardEditor.spec.tsx +96 -14
- package/src/components/DashboardBuilder/DashboardEditor.tsx +482 -332
- package/src/components/DashboardBuilder/DiffDialog.spec.tsx +25 -0
- package/src/components/DashboardBuilder/DiffDialog.tsx +5 -24
- package/src/components/DashboardBuilder/DrillDialog.tsx +1 -1
- package/src/components/DashboardBuilder/FilterDialog.tsx +10 -3
- package/src/components/DashboardBuilder/FilterStrip.tsx +26 -4
- package/src/components/DashboardBuilder/InlineMarkdown.tsx +184 -0
- package/src/components/DashboardBuilder/InlineText.tsx +106 -0
- package/src/components/DashboardBuilder/NotebookEditor.tsx +44 -0
- package/src/components/DashboardBuilder/SaveNotice.tsx +113 -0
- package/src/components/DashboardBuilder/SettingsPopover.spec.tsx +87 -0
- package/src/components/DashboardBuilder/SettingsPopover.tsx +225 -67
- package/src/components/DashboardBuilder/SourceViewPicker.tsx +122 -0
- package/src/components/DashboardBuilder/TextTileBody.tsx +28 -0
- package/src/components/DashboardBuilder/TileFrame.tsx +89 -115
- package/src/components/DashboardBuilder/TileMenu.tsx +168 -90
- package/src/components/DashboardBuilder/annotationText.spec.ts +33 -0
- package/src/components/DashboardBuilder/annotationText.ts +17 -0
- package/src/components/DashboardBuilder/artifactBlock.spec.ts +248 -0
- package/src/components/DashboardBuilder/artifactTagParity.spec.ts +41 -0
- package/src/components/DashboardBuilder/catalog.aggregate.spec.ts +49 -0
- package/src/components/DashboardBuilder/catalog.spec.ts +2 -0
- package/src/components/DashboardBuilder/catalog.ts +61 -21
- package/src/components/DashboardBuilder/changedTile.spec.ts +46 -0
- package/src/components/DashboardBuilder/changedTile.ts +28 -0
- package/src/components/DashboardBuilder/chartLine.spec.ts +180 -0
- package/src/components/DashboardBuilder/chartLine.ts +121 -0
- package/src/components/DashboardBuilder/controls.spec.ts +21 -10
- package/src/components/DashboardBuilder/controls.ts +22 -8
- package/src/components/DashboardBuilder/document.ts +53 -5
- package/src/components/DashboardBuilder/documentSession.spec.ts +209 -0
- package/src/components/DashboardBuilder/documentSession.ts +257 -0
- package/src/components/DashboardBuilder/emptyStart.spec.ts +131 -0
- package/src/components/DashboardBuilder/invariants.spec.ts +6 -2
- package/src/components/DashboardBuilder/legacyNotebook.chart.spec.ts +77 -0
- package/src/components/DashboardBuilder/legacyNotebook.convert.spec.ts +630 -0
- package/src/components/DashboardBuilder/legacyNotebook.read.spec.ts +338 -0
- package/src/components/DashboardBuilder/legacyNotebook.ts +1117 -0
- package/src/components/DashboardBuilder/loadMalloy.spec.ts +80 -0
- package/src/components/DashboardBuilder/loadMalloy.ts +39 -0
- package/src/components/DashboardBuilder/malloyText.spec.ts +48 -0
- package/src/components/DashboardBuilder/malloyText.ts +181 -27
- package/src/components/DashboardBuilder/malloyTree.ts +155 -7
- package/src/components/DashboardBuilder/newDashboard.spec.ts +46 -2
- package/src/components/DashboardBuilder/newDashboard.ts +30 -3
- package/src/components/DashboardBuilder/openDraft.ts +52 -0
- package/src/components/DashboardBuilder/preview.shapes.spec.ts +75 -0
- package/src/components/DashboardBuilder/preview.spec.ts +159 -12
- package/src/components/DashboardBuilder/preview.ts +83 -9
- package/src/components/DashboardBuilder/readDocument.spec.ts +139 -27
- package/src/components/DashboardBuilder/readDocument.ts +130 -44
- package/src/components/DashboardBuilder/readForEditor.spec.ts +80 -0
- package/src/components/DashboardBuilder/readForEditor.ts +47 -0
- package/src/components/DashboardBuilder/scannerShapes.spec.ts +14 -13
- package/src/components/DashboardBuilder/sortable.spec.ts +65 -0
- package/src/components/DashboardBuilder/sortable.tsx +2 -12
- package/src/components/DashboardBuilder/spliceDocument.chart.spec.ts +340 -0
- package/src/components/DashboardBuilder/spliceDocument.imports.spec.ts +167 -0
- package/src/components/DashboardBuilder/spliceDocument.spec.ts +229 -94
- package/src/components/DashboardBuilder/spliceDocument.ts +762 -130
- package/src/components/DashboardBuilder/spliceResult.ts +15 -0
- package/src/components/DashboardBuilder/statementExtent.spec.ts +33 -30
- package/src/components/DashboardBuilder/tagParse.spec.ts +149 -0
- package/src/components/DashboardBuilder/tagParse.ts +103 -0
- package/src/components/DashboardBuilder/telemetry.ts +47 -0
- package/src/components/DashboardBuilder/testing/artifactTagParity.json +197 -0
- package/src/components/DashboardBuilder/testing/fixtures.ts +16 -1
- package/src/components/DashboardBuilder/testing/inline.ts +25 -0
- package/src/components/DashboardBuilder/textTiles.spec.ts +718 -0
- package/src/components/DashboardBuilder/useBuilderSession.spec.ts +335 -0
- package/src/components/DashboardBuilder/useBuilderSession.ts +292 -0
- package/src/components/DashboardBuilder/useBuilderShortcuts.overlay.spec.tsx +65 -0
- package/src/components/DashboardBuilder/useBuilderShortcuts.spec.tsx +60 -0
- package/src/components/DashboardBuilder/useBuilderShortcuts.ts +36 -10
- package/src/components/DashboardBuilder/useDashboardEditor.spec.ts +16 -13
- package/src/components/DashboardBuilder/useDashboardEditor.ts +54 -181
- package/src/components/DashboardBuilder/useDocumentEditor.spec.ts +565 -0
- package/src/components/DashboardBuilder/useDocumentEditor.ts +399 -0
- package/src/components/DashboardBuilder/useExitGuard.spec.ts +159 -0
- package/src/components/DashboardBuilder/useExitGuard.ts +89 -0
- package/src/components/DashboardBuilder/useFilterForm.existing.spec.tsx +41 -0
- package/src/components/DashboardBuilder/useFilterForm.spec.tsx +24 -2
- package/src/components/DashboardBuilder/useFilterForm.ts +41 -13
- package/src/components/DashboardBuilder/useTileReorder.spec.tsx +28 -5
- package/src/components/DashboardBuilder/useTileReorder.ts +5 -7
- package/src/components/DocumentCreate/canRetry.spec.ts +32 -0
- package/src/components/DocumentCreate/canRetry.ts +19 -0
- package/src/components/DocumentCreate/createDocument.spec.ts +310 -0
- package/src/components/DocumentCreate/createDocument.ts +159 -0
- package/src/components/DocumentCreate/documentPath.ts +40 -0
- package/src/components/DocumentCreate/events.ts +15 -0
- package/src/components/DocumentCreate/exportedSources.ts +25 -0
- package/src/components/DocumentCreate/guards.ts +45 -0
- package/src/components/DocumentCreate/index.ts +27 -0
- package/src/components/DocumentCreate/newNotebook.spec.ts +156 -0
- package/src/components/DocumentCreate/newNotebook.ts +44 -0
- package/src/components/DocumentCreate/useDocumentChoices.spec.tsx +138 -0
- package/src/components/DocumentCreate/useDocumentChoices.ts +113 -0
- package/src/components/DocumentStorage/DocumentStorage.ts +2 -1
- package/src/components/Model/ModelExplorer.spec.tsx +2 -2
- package/src/components/Model/SourcesExplorer.tsx +2 -4
- package/src/components/NarrowEditGate.spec.tsx +73 -0
- package/src/components/NarrowEditGate.tsx +29 -0
- package/src/components/Notebook/Notebook.layout.spec.tsx +94 -0
- package/src/components/Notebook/Notebook.tsx +66 -27
- package/src/components/Package/NewDocumentDialog.spec.tsx +749 -0
- package/src/components/Package/NewDocumentDialog.tsx +401 -0
- package/src/components/Package/Package.artifacts.spec.tsx +190 -0
- package/src/components/Package/Package.create.spec.tsx +380 -0
- package/src/components/Package/Package.notebooks.spec.tsx +18 -0
- package/src/components/Package/Package.tsx +366 -171
- package/src/components/Package/documentLocation.spec.ts +81 -0
- package/src/components/Package/documentLocation.ts +55 -0
- package/src/components/Package/index.ts +13 -0
- package/src/components/Package/useDocumentLocation.ts +59 -0
- package/src/components/PackageSection.tsx +2 -2
- package/src/components/RenderedResult/RenderedResult.tsx +18 -1
- package/src/components/RenderedResult/ResultContainer.tsx +44 -11
- package/src/components/RenderedResult/ResultPanel.tsx +4 -0
- package/src/components/RenderedResult/resultSizing.spec.ts +68 -0
- package/src/components/RenderedResult/resultSizing.ts +14 -3
- package/src/components/ServerProvider.tsx +2 -1
- package/src/components/UnsavedChangesDialog.spec.tsx +70 -0
- package/src/components/UnsavedChangesDialog.tsx +49 -0
- package/src/components/given/GivensPanel.spec.tsx +6 -6
- package/src/components/given/GivensPanel.tsx +3 -3
- package/src/components/index.ts +2 -0
- package/src/hooks/index.ts +3 -0
- package/src/hooks/useNarrowScreen.ts +21 -0
- package/src/hooks/useQueryResult.ts +16 -0
- package/src/hooks/useSettled.spec.ts +41 -0
- package/src/hooks/useSettled.ts +27 -0
- package/src/mainEntry.spec.ts +145 -0
- package/src/text-entry.ts +12 -0
- package/src/textEntry.spec.ts +118 -0
- package/src/utils/clock.spec.ts +12 -0
- package/src/utils/clock.ts +6 -0
- package/vite.config.ts +4 -0
- package/dist/Dashboard-ChyylkTD.cjs.js +0 -136
- package/dist/RenderedResult-AJPMUkAD.cjs.js +0 -135
- package/dist/ServerProvider-CXrOKD6g.cjs.js +0 -1
- package/dist/components/DashboardBuilder/useTileResize.d.ts +0 -29
- package/dist/components/Package/NewDashboardDialog.d.ts +0 -18
- package/src/components/DashboardBuilder/useTileResize.spec.tsx +0 -121
- package/src/components/DashboardBuilder/useTileResize.ts +0 -110
- package/src/components/Package/NewDashboardDialog.spec.tsx +0 -129
- package/src/components/Package/NewDashboardDialog.tsx +0 -265
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
// Copyright (c) Credible Data Inc.
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
|
|
4
|
+
import { newDashboardSource } from "../DashboardBuilder/newDashboard";
|
|
5
|
+
import { saveTarget } from "../DashboardBuilder/documentSession";
|
|
6
|
+
import {
|
|
7
|
+
isDocumentNotFound,
|
|
8
|
+
type DocumentLocator,
|
|
9
|
+
type DocumentStorage,
|
|
10
|
+
type DocumentType,
|
|
11
|
+
type Workspace,
|
|
12
|
+
} from "../DocumentStorage/DocumentStorage";
|
|
13
|
+
import { documentPathForTitle, locatorFor } from "./documentPath";
|
|
14
|
+
import type { DocumentCreatedEvent } from "./events";
|
|
15
|
+
import { newDocumentProblem, type NewDocument } from "./guards";
|
|
16
|
+
import { newNotebookSource } from "./newNotebook";
|
|
17
|
+
|
|
18
|
+
/** How many names are tried before a create gives up: `title`, `title-2`, ... */
|
|
19
|
+
export const MAX_SLUG_ATTEMPTS = 20;
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Where a create may land, or `undefined` for nowhere. A non-authoritative
|
|
23
|
+
* browser workspace is never a target (`savesTo === "browser"`): a document
|
|
24
|
+
* created there would exist for one reader on one machine and look saved.
|
|
25
|
+
*/
|
|
26
|
+
export function createRoute(host: {
|
|
27
|
+
authoritative: boolean;
|
|
28
|
+
mutable: boolean;
|
|
29
|
+
/** The host keeps documents and the chosen workspace takes writes. */
|
|
30
|
+
canStore: boolean;
|
|
31
|
+
/** A pinned version takes no package writes. */
|
|
32
|
+
versionId?: string;
|
|
33
|
+
}): "package" | "storage" | undefined {
|
|
34
|
+
const { savesTo, writer } = saveTarget({ ...host, readFailed: false });
|
|
35
|
+
return savesTo === "browser" ? undefined : writer;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export type CreateTarget =
|
|
39
|
+
| {
|
|
40
|
+
route: "package";
|
|
41
|
+
/** Package-relative paths already in the package, as its listing gives them. */
|
|
42
|
+
existing: readonly string[];
|
|
43
|
+
/** A create-only write: no hash, so the server refuses a file that is there. */
|
|
44
|
+
write: (path: string, source: string) => Promise<void>;
|
|
45
|
+
}
|
|
46
|
+
| {
|
|
47
|
+
route: "storage";
|
|
48
|
+
storage: DocumentStorage;
|
|
49
|
+
workspace: Workspace;
|
|
50
|
+
environmentName: string;
|
|
51
|
+
packageName: string;
|
|
52
|
+
/** Package-relative paths the package already lists; the store may not know them yet. */
|
|
53
|
+
existing?: readonly string[];
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
export interface CreatedDocument {
|
|
57
|
+
kind: DocumentType;
|
|
58
|
+
/** Package-relative, e.g. `dashboards/sales.malloy`. */
|
|
59
|
+
path: string;
|
|
60
|
+
/** `sales` for `dashboards/sales.malloy`: what an editor route names. */
|
|
61
|
+
slug: string;
|
|
62
|
+
/** Set on the storage route: the new document's address there. */
|
|
63
|
+
locator?: DocumentLocator;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export interface CreateDocumentOptions {
|
|
67
|
+
kind: DocumentType;
|
|
68
|
+
document: NewDocument;
|
|
69
|
+
target: CreateTarget;
|
|
70
|
+
onCreated?: (created: CreatedDocument) => void;
|
|
71
|
+
onEvent?: (event: DocumentCreatedEvent) => void;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const slugOf = (path: string) =>
|
|
75
|
+
path.slice(path.indexOf("/") + 1, -".malloy".length);
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Write a new dashboard or notebook, never over an existing file, and say where
|
|
79
|
+
* it went. Reads nothing back: the host navigates from what this returns.
|
|
80
|
+
*
|
|
81
|
+
* On the storage route the listing alone cannot be trusted to be current, and
|
|
82
|
+
* `saveDocument` replaces what is there, so each candidate name is read first
|
|
83
|
+
* and used only when the read says the document is absent.
|
|
84
|
+
*/
|
|
85
|
+
export async function createDocument(
|
|
86
|
+
options: CreateDocumentOptions,
|
|
87
|
+
): Promise<CreatedDocument> {
|
|
88
|
+
const { kind, document, target, onCreated, onEvent } = options;
|
|
89
|
+
const problem = newDocumentProblem(kind, document);
|
|
90
|
+
if (problem) throw new Error(problem);
|
|
91
|
+
const source =
|
|
92
|
+
kind === "dashboard"
|
|
93
|
+
? newDashboardSource(document)
|
|
94
|
+
: newNotebookSource(document);
|
|
95
|
+
|
|
96
|
+
let created: CreatedDocument;
|
|
97
|
+
if (target.route === "package") {
|
|
98
|
+
const taken = new Set(target.existing);
|
|
99
|
+
let path: string | undefined;
|
|
100
|
+
for (let n = 1; n <= MAX_SLUG_ATTEMPTS && path === undefined; n++) {
|
|
101
|
+
const candidate = documentPathForTitle(kind, document.title, n);
|
|
102
|
+
if (taken.has(candidate)) continue;
|
|
103
|
+
try {
|
|
104
|
+
await target.write(candidate, source);
|
|
105
|
+
path = candidate;
|
|
106
|
+
} catch (error) {
|
|
107
|
+
// A create-only write refused as a conflict means the listing was stale; the next name is free to try.
|
|
108
|
+
if (!isConflict(error)) throw error;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
if (path === undefined) throw noFreeName(document.title);
|
|
112
|
+
created = { kind, path, slug: slugOf(path) };
|
|
113
|
+
} else {
|
|
114
|
+
const { storage, workspace, environmentName, packageName } = target;
|
|
115
|
+
const inPackage = new Set(target.existing);
|
|
116
|
+
const listed = new Set(
|
|
117
|
+
(await storage.listDocuments(workspace, kind)).map((l) => l.path),
|
|
118
|
+
);
|
|
119
|
+
let found: CreatedDocument | undefined;
|
|
120
|
+
for (let n = 1; n <= MAX_SLUG_ATTEMPTS && found === undefined; n++) {
|
|
121
|
+
const path = documentPathForTitle(kind, document.title, n);
|
|
122
|
+
const locator = locatorFor(
|
|
123
|
+
kind,
|
|
124
|
+
workspace.name,
|
|
125
|
+
environmentName,
|
|
126
|
+
packageName,
|
|
127
|
+
path,
|
|
128
|
+
);
|
|
129
|
+
if (listed.has(locator.path) || inPackage.has(path)) continue;
|
|
130
|
+
let present = true;
|
|
131
|
+
try {
|
|
132
|
+
await storage.getDocument(locator);
|
|
133
|
+
} catch (error) {
|
|
134
|
+
// Only absence frees the name; a backend that could not answer says nothing about it.
|
|
135
|
+
if (!isDocumentNotFound(error)) throw error;
|
|
136
|
+
present = false;
|
|
137
|
+
}
|
|
138
|
+
if (!present) found = { kind, path, slug: slugOf(path), locator };
|
|
139
|
+
}
|
|
140
|
+
if (found === undefined) throw noFreeName(document.title);
|
|
141
|
+
await storage.saveDocument(found.locator!, source);
|
|
142
|
+
created = found;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
onEvent?.({
|
|
146
|
+
type: `${kind}.created`,
|
|
147
|
+
where: target.route === "package" ? "package" : "host",
|
|
148
|
+
});
|
|
149
|
+
onCreated?.(created);
|
|
150
|
+
return created;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
const isConflict = (error: unknown) =>
|
|
154
|
+
(error as { response?: { status?: number } })?.response?.status === 409;
|
|
155
|
+
|
|
156
|
+
const noFreeName = (title: string) =>
|
|
157
|
+
new Error(
|
|
158
|
+
`No free file name for "${title}" after ${MAX_SLUG_ATTEMPTS} tries. Fix: choose a different title.`,
|
|
159
|
+
);
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
// Copyright (c) Credible Data Inc.
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
|
|
4
|
+
import { slugFor } from "../DashboardBuilder/newDashboard";
|
|
5
|
+
import type { DocumentLocator, DocumentType } from "../DocumentStorage";
|
|
6
|
+
|
|
7
|
+
export { newDashboardSource, slugFor } from "../DashboardBuilder/newDashboard";
|
|
8
|
+
|
|
9
|
+
/** The package-relative file for a slug: always `dashboards/<slug>.malloy` or `notebooks/<slug>.malloy`, the only shapes the server writes. */
|
|
10
|
+
export function documentPathFor(kind: DocumentType, slug: string): string {
|
|
11
|
+
return `${kind}s/${slug}.malloy`;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/** A title's slug, with a stand-in for a title that has no letters or digits. */
|
|
15
|
+
export function slugOrFallback(title: string): string {
|
|
16
|
+
return slugFor(title) || "untitled";
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** The file for a title; `suffix` 1 is the bare slug, 2 and up append `-N`. */
|
|
20
|
+
export function documentPathForTitle(
|
|
21
|
+
kind: DocumentType,
|
|
22
|
+
title: string,
|
|
23
|
+
suffix = 1,
|
|
24
|
+
): string {
|
|
25
|
+
const slug = slugOrFallback(title);
|
|
26
|
+
return documentPathFor(kind, suffix > 1 ? `${slug}-${suffix}` : slug);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** The storage key for a document's copy; the Console's editors and `createDocument` mint it the same way. */
|
|
30
|
+
export const locatorFor = (
|
|
31
|
+
kind: DocumentType,
|
|
32
|
+
workspace: string,
|
|
33
|
+
environmentName: string,
|
|
34
|
+
packageName: string,
|
|
35
|
+
path: string,
|
|
36
|
+
): DocumentLocator => ({
|
|
37
|
+
workspace,
|
|
38
|
+
type: kind,
|
|
39
|
+
path: `${environmentName}/${packageName}/${path}`,
|
|
40
|
+
});
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
// Copyright (c) Credible Data Inc.
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
|
|
4
|
+
/** A document the host created; `where` names which storage took the write, as on the `*.saved` events. */
|
|
5
|
+
export type DashboardCreatedEvent = {
|
|
6
|
+
type: "dashboard.created";
|
|
7
|
+
where: "package" | "host";
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
export type NotebookCreatedEvent = {
|
|
11
|
+
type: "notebook.created";
|
|
12
|
+
where: "package" | "host";
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export type DocumentCreatedEvent = DashboardCreatedEvent | NotebookCreatedEvent;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
// Copyright (c) Credible Data Inc.
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* The sources a model exports. `sources` also lists names the model only
|
|
6
|
+
* imports, and `import { x } from "../model"` cannot reach those; no
|
|
7
|
+
* `modelInfo`, or one that does not parse, offers nothing rather than a
|
|
8
|
+
* choice that would write a file that does not compile.
|
|
9
|
+
*/
|
|
10
|
+
export function exportedSources(modelInfo: string | undefined): Set<string> {
|
|
11
|
+
try {
|
|
12
|
+
const parsed = JSON.parse(modelInfo ?? "") as {
|
|
13
|
+
entries?: { kind?: string; name?: string }[];
|
|
14
|
+
};
|
|
15
|
+
return new Set(
|
|
16
|
+
(parsed.entries ?? []).flatMap((entry) =>
|
|
17
|
+
entry.kind === "source" && typeof entry.name === "string"
|
|
18
|
+
? [entry.name]
|
|
19
|
+
: [],
|
|
20
|
+
),
|
|
21
|
+
);
|
|
22
|
+
} catch {
|
|
23
|
+
return new Set();
|
|
24
|
+
}
|
|
25
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
// Copyright (c) Credible Data Inc.
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
|
|
4
|
+
import { annotationTextProblem } from "../DashboardBuilder/annotationText";
|
|
5
|
+
import { isBareName } from "../DashboardBuilder/malloyText";
|
|
6
|
+
import type { DocumentType } from "../DocumentStorage";
|
|
7
|
+
|
|
8
|
+
export const malloyName = (name: string) =>
|
|
9
|
+
isBareName(name) ? name : `\`${name}\``;
|
|
10
|
+
|
|
11
|
+
/** What a new document is made from: a view of a source in a model. */
|
|
12
|
+
export interface NewDocument {
|
|
13
|
+
title: string;
|
|
14
|
+
/** The model that declares the source, relative to the package root. */
|
|
15
|
+
modelPath: string;
|
|
16
|
+
source: string;
|
|
17
|
+
view: string;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** Why `input` cannot be written into a new `kind`, or undefined when it can. */
|
|
21
|
+
export function newDocumentProblem(
|
|
22
|
+
kind: DocumentType,
|
|
23
|
+
input: NewDocument,
|
|
24
|
+
): string | undefined {
|
|
25
|
+
const { title, modelPath, source, view } = input;
|
|
26
|
+
if (title.trim() === "") return "A title cannot be empty.";
|
|
27
|
+
const textProblem = annotationTextProblem("title", title);
|
|
28
|
+
if (textProblem) return textProblem;
|
|
29
|
+
if (kind === "notebook" && title.includes("|##"))
|
|
30
|
+
return "A title cannot contain `|##`, which closes a notebook's text cell.";
|
|
31
|
+
if (/["\\\r\n]/.test(modelPath) || modelPath.trim() === "")
|
|
32
|
+
return `The model path ${JSON.stringify(modelPath)} cannot be written as an import.`;
|
|
33
|
+
// A dashboard writes both names bare; a notebook back-quotes what is not an identifier.
|
|
34
|
+
for (const [what, name] of [
|
|
35
|
+
["source", source],
|
|
36
|
+
["view", view],
|
|
37
|
+
] as const)
|
|
38
|
+
if (
|
|
39
|
+
name.trim() === "" ||
|
|
40
|
+
/[`\r\n\\]/.test(name) ||
|
|
41
|
+
(kind === "dashboard" && !isBareName(name))
|
|
42
|
+
)
|
|
43
|
+
return `The ${what} name ${JSON.stringify(name)} cannot be written as a Malloy name.`;
|
|
44
|
+
return undefined;
|
|
45
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
// Copyright (c) Credible Data Inc.
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
|
|
4
|
+
export {
|
|
5
|
+
createDocument,
|
|
6
|
+
createRoute,
|
|
7
|
+
MAX_SLUG_ATTEMPTS,
|
|
8
|
+
type CreatedDocument,
|
|
9
|
+
type CreateDocumentOptions,
|
|
10
|
+
type CreateTarget,
|
|
11
|
+
} from "./createDocument";
|
|
12
|
+
export {
|
|
13
|
+
documentPathFor,
|
|
14
|
+
documentPathForTitle,
|
|
15
|
+
locatorFor,
|
|
16
|
+
newDashboardSource,
|
|
17
|
+
slugFor,
|
|
18
|
+
slugOrFallback,
|
|
19
|
+
} from "./documentPath";
|
|
20
|
+
export type {
|
|
21
|
+
DashboardCreatedEvent,
|
|
22
|
+
DocumentCreatedEvent,
|
|
23
|
+
NotebookCreatedEvent,
|
|
24
|
+
} from "./events";
|
|
25
|
+
export { newDocumentProblem, type NewDocument } from "./guards";
|
|
26
|
+
export { newNotebookSource } from "./newNotebook";
|
|
27
|
+
export { useDocumentChoices, type DocumentChoice } from "./useDocumentChoices";
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
// Copyright (c) Credible Data Inc.
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
|
|
4
|
+
import { describe, expect, it } from "bun:test";
|
|
5
|
+
import { lintNotebookText } from "../../../../server/src/service/notebook_lint";
|
|
6
|
+
import { isQueryTile, isTextTile } from "../DashboardBuilder/document";
|
|
7
|
+
import {
|
|
8
|
+
readDashboardDocument,
|
|
9
|
+
readFailed,
|
|
10
|
+
} from "../DashboardBuilder/readDocument";
|
|
11
|
+
import { documentPathFor, documentPathForTitle, slugFor } from "./documentPath";
|
|
12
|
+
import { newDocumentProblem } from "./guards";
|
|
13
|
+
import { newNotebookSource } from "./newNotebook";
|
|
14
|
+
|
|
15
|
+
const INPUT = {
|
|
16
|
+
title: 'Sales "West"',
|
|
17
|
+
modelPath: "models/storefront.malloy",
|
|
18
|
+
source: "order_items",
|
|
19
|
+
view: "by_category",
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
describe("newNotebookSource", () => {
|
|
23
|
+
it("lints clean as a notebook", () => {
|
|
24
|
+
const text = newNotebookSource(INPUT);
|
|
25
|
+
expect(lintNotebookText("notebooks/sales.malloy", text)).toEqual([]);
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
it("lints clean for a back-quoted source and a title with a heading hash", () => {
|
|
29
|
+
const text = newNotebookSource({
|
|
30
|
+
...INPUT,
|
|
31
|
+
title: "# Q3 | review",
|
|
32
|
+
source: "order items",
|
|
33
|
+
});
|
|
34
|
+
expect(text).toContain("import { `order items` }");
|
|
35
|
+
expect(lintNotebookText("notebooks/q3.malloy", text)).toEqual([]);
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it("reads back as a layout notebook: an intro text tile, then one query tile, and no run cell", async () => {
|
|
39
|
+
const text = newNotebookSource(INPUT);
|
|
40
|
+
const result = await readDashboardDocument(text);
|
|
41
|
+
if (readFailed(result)) throw new Error(result.reason);
|
|
42
|
+
const { document } = result;
|
|
43
|
+
expect(document.kind).toBe("notebook");
|
|
44
|
+
expect(
|
|
45
|
+
document.tiles.map((t) => (isTextTile(t) ? "text" : "query")),
|
|
46
|
+
).toEqual(["text", "query"]);
|
|
47
|
+
const query = document.tiles.find(isQueryTile);
|
|
48
|
+
expect(query?.name).toBe("by_category_tile");
|
|
49
|
+
expect(text).toContain(
|
|
50
|
+
"source: order_items_tiles is order_items extend {",
|
|
51
|
+
);
|
|
52
|
+
expect(text).not.toMatch(/^run:/m);
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it("writes the artifact tag as a block with a tile on each line, named for its view", () => {
|
|
56
|
+
expect(newNotebookSource(INPUT)).toContain(
|
|
57
|
+
'##| artifact { kind=notebook title="Sales \\"West\\""\n tiles=[\n intro { kind=text },\n "order_items_tiles -> by_category_tile"\n ]\n}\n|##\n',
|
|
58
|
+
);
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it("reads back for a back-quoted source too", async () => {
|
|
62
|
+
const result = await readDashboardDocument(
|
|
63
|
+
newNotebookSource({ ...INPUT, source: "order items" }),
|
|
64
|
+
);
|
|
65
|
+
if (readFailed(result)) throw new Error(result.reason);
|
|
66
|
+
expect(result.document.tiles.filter(isQueryTile)).toHaveLength(1);
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
it("imports the one source by name", () => {
|
|
70
|
+
expect(newNotebookSource(INPUT)).toContain(
|
|
71
|
+
'import { order_items } from "../models/storefront.malloy"',
|
|
72
|
+
);
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
it("throws on what the guard refuses", () => {
|
|
76
|
+
expect(() => newNotebookSource({ ...INPUT, title: "a\r\nb" })).toThrow();
|
|
77
|
+
expect(() => newNotebookSource({ ...INPUT, title: "a |## b" })).toThrow();
|
|
78
|
+
});
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
describe("newDocumentProblem", () => {
|
|
82
|
+
it("holds a dashboard to bare identifiers, as its writer does", () => {
|
|
83
|
+
expect(
|
|
84
|
+
newDocumentProblem("dashboard", { ...INPUT, source: "order items" }),
|
|
85
|
+
).toMatch(/source name/);
|
|
86
|
+
expect(
|
|
87
|
+
newDocumentProblem("notebook", { ...INPUT, source: "order items" }),
|
|
88
|
+
).toBeUndefined();
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
it("back-quotes a source named like a keyword, and a dashboard refuses it", () => {
|
|
92
|
+
const text = newNotebookSource({
|
|
93
|
+
...INPUT,
|
|
94
|
+
source: "source",
|
|
95
|
+
view: "is",
|
|
96
|
+
});
|
|
97
|
+
expect(text).toContain("import { `source` }");
|
|
98
|
+
expect(text).toContain("view: is_tile is `is`");
|
|
99
|
+
expect(lintNotebookText("notebooks/sales.malloy", text)).toEqual([]);
|
|
100
|
+
expect(
|
|
101
|
+
newDocumentProblem("dashboard", { ...INPUT, source: "date" }),
|
|
102
|
+
).toMatch(/source name/);
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
it("trims the title once, for the tag and the heading", () => {
|
|
106
|
+
const text = newNotebookSource({ ...INPUT, title: " Q3 " });
|
|
107
|
+
expect(text).toContain('title="Q3"');
|
|
108
|
+
expect(text).toContain("# Q3\n");
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
it("refuses names that cannot be written", () => {
|
|
112
|
+
expect(
|
|
113
|
+
newDocumentProblem("dashboard", { ...INPUT, view: "a`b" }),
|
|
114
|
+
).toMatch(/view name/);
|
|
115
|
+
expect(
|
|
116
|
+
newDocumentProblem("dashboard", { ...INPUT, modelPath: 'a".malloy' }),
|
|
117
|
+
).toMatch(/model path/);
|
|
118
|
+
expect(newDocumentProblem("dashboard", INPUT)).toBeUndefined();
|
|
119
|
+
expect(
|
|
120
|
+
newDocumentProblem("dashboard", { ...INPUT, title: "a |## b" }),
|
|
121
|
+
).toBeUndefined();
|
|
122
|
+
});
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
describe("document paths", () => {
|
|
126
|
+
it("always satisfy the server's write shape", () => {
|
|
127
|
+
let seed = 7;
|
|
128
|
+
const next = () => (seed = (seed * 1103515245 + 12345) & 0x7fffffff);
|
|
129
|
+
const alphabet = "aZ09 -_/\\.\"'`#%\n\té中\u{1f600}*()[]";
|
|
130
|
+
const shape = /^(dashboards|notebooks)\/[^/]+\.malloy$/;
|
|
131
|
+
for (let i = 0; i < 500; i++) {
|
|
132
|
+
const length = next() % 40;
|
|
133
|
+
let title = "";
|
|
134
|
+
for (let j = 0; j < length; j++)
|
|
135
|
+
title += [...alphabet][next() % [...alphabet].length];
|
|
136
|
+
for (const kind of ["dashboard", "notebook"] as const)
|
|
137
|
+
for (const suffix of [1, 2, 19])
|
|
138
|
+
expect(documentPathForTitle(kind, title, suffix)).toMatch(shape);
|
|
139
|
+
}
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
it("spells suffixes -2, -3 and names the kind's directory", () => {
|
|
143
|
+
expect(documentPathForTitle("notebook", "Sales")).toBe(
|
|
144
|
+
"notebooks/sales.malloy",
|
|
145
|
+
);
|
|
146
|
+
expect(documentPathForTitle("dashboard", "Sales", 3)).toBe(
|
|
147
|
+
"dashboards/sales-3.malloy",
|
|
148
|
+
);
|
|
149
|
+
expect(documentPathForTitle("dashboard", "***")).toBe(
|
|
150
|
+
"dashboards/untitled.malloy",
|
|
151
|
+
);
|
|
152
|
+
expect(documentPathFor("notebook", slugFor("A b"))).toBe(
|
|
153
|
+
"notebooks/a-b.malloy",
|
|
154
|
+
);
|
|
155
|
+
});
|
|
156
|
+
});
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
// Copyright (c) Credible Data Inc.
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
|
|
4
|
+
import { malloyName, newDocumentProblem, type NewDocument } from "./guards";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* A new notebook file in the layout format a dashboard uses: the givens flag,
|
|
8
|
+
* the artifact tag (a block, a tile per line) listing an intro text tile and one query tile, a named
|
|
9
|
+
* import of the one source it runs (reachable without a whole-file import),
|
|
10
|
+
* the intro block, and one extension of the source (`<source>_tiles`, the
|
|
11
|
+
* builder's naming) holding the picked view as the query tile.
|
|
12
|
+
*/
|
|
13
|
+
export function newNotebookSource(input: NewDocument): string {
|
|
14
|
+
const problem = newDocumentProblem("notebook", input);
|
|
15
|
+
if (problem) throw new Error(problem);
|
|
16
|
+
const { modelPath, source, view } = input;
|
|
17
|
+
const title = input.title.trim();
|
|
18
|
+
const quoted = `"${title.replace(/\\/g, "\\\\").replace(/"/g, '\\"')}"`;
|
|
19
|
+
const extension = malloyName(`${source}_tiles`);
|
|
20
|
+
const tile = malloyName(`${view}_tile`);
|
|
21
|
+
// The entry is a quoted string, so a back-quoted extension name is written inside it as is.
|
|
22
|
+
const entry = `"${extension} -> ${tile}"`;
|
|
23
|
+
return [
|
|
24
|
+
"##! experimental.givens",
|
|
25
|
+
`##| artifact { kind=notebook title=${quoted}`,
|
|
26
|
+
" tiles=[",
|
|
27
|
+
" intro { kind=text },",
|
|
28
|
+
` ${entry}`,
|
|
29
|
+
" ]",
|
|
30
|
+
"}",
|
|
31
|
+
"|##",
|
|
32
|
+
`import { ${malloyName(source)} } from "../${modelPath}"`,
|
|
33
|
+
"",
|
|
34
|
+
"##|(markdown) intro",
|
|
35
|
+
`# ${title}`,
|
|
36
|
+
"Say what this notebook is for, then let the queries below answer it.",
|
|
37
|
+
"|##",
|
|
38
|
+
"",
|
|
39
|
+
`source: ${extension} is ${malloyName(source)} extend {`,
|
|
40
|
+
` view: ${tile} is ${malloyName(view)}`,
|
|
41
|
+
"}",
|
|
42
|
+
"",
|
|
43
|
+
].join("\n");
|
|
44
|
+
}
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
// Copyright (c) Credible Data Inc.
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
|
|
4
|
+
import { act, renderHook, waitFor } from "@testing-library/react";
|
|
5
|
+
import { beforeEach, describe, expect, it, mock } from "bun:test";
|
|
6
|
+
import {
|
|
7
|
+
clearCache,
|
|
8
|
+
mockServerProvider,
|
|
9
|
+
serverWrapper,
|
|
10
|
+
} from "../../../test/serverProvider";
|
|
11
|
+
|
|
12
|
+
const modelInfo = JSON.stringify({
|
|
13
|
+
entries: [{ kind: "source", name: "orders" }],
|
|
14
|
+
});
|
|
15
|
+
const model = {
|
|
16
|
+
data: {
|
|
17
|
+
modelInfo,
|
|
18
|
+
sources: [{ name: "orders", views: [{ name: "by_day" }] }],
|
|
19
|
+
},
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
const getModel = mock(
|
|
23
|
+
(_env: string, _pkg: string, _path: string, _version?: string) =>
|
|
24
|
+
Promise.resolve(model),
|
|
25
|
+
);
|
|
26
|
+
|
|
27
|
+
mockServerProvider({ models: { getModel } });
|
|
28
|
+
|
|
29
|
+
const { useDocumentChoices } = await import("./useDocumentChoices");
|
|
30
|
+
|
|
31
|
+
const render = (models: string[], enabled = true) =>
|
|
32
|
+
renderHook(
|
|
33
|
+
() =>
|
|
34
|
+
useDocumentChoices({
|
|
35
|
+
environmentName: "env",
|
|
36
|
+
packageName: "pkg",
|
|
37
|
+
models,
|
|
38
|
+
enabled,
|
|
39
|
+
}),
|
|
40
|
+
{ wrapper: serverWrapper },
|
|
41
|
+
);
|
|
42
|
+
|
|
43
|
+
const callsFor = (path: string) =>
|
|
44
|
+
getModel.mock.calls.filter((c) => c[2] === path).length;
|
|
45
|
+
|
|
46
|
+
beforeEach(() => {
|
|
47
|
+
clearCache();
|
|
48
|
+
getModel.mockReset();
|
|
49
|
+
getModel.mockImplementation(() => Promise.resolve(model));
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
describe("useDocumentChoices: which lookups failed", () => {
|
|
53
|
+
it("reports no failures when every model loads", async () => {
|
|
54
|
+
const { result } = render(["a.malloy", "b.malloy"]);
|
|
55
|
+
await waitFor(() => expect(result.current.isSuccess).toBe(true));
|
|
56
|
+
expect([...result.current.choices.keys()]).toEqual([
|
|
57
|
+
"a.malloy",
|
|
58
|
+
"b.malloy",
|
|
59
|
+
]);
|
|
60
|
+
expect(result.current.failed).toEqual([]);
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it("keeps the other models' choices and names the one that failed", async () => {
|
|
64
|
+
getModel.mockImplementation((_e, _p, path) =>
|
|
65
|
+
path === "b.malloy"
|
|
66
|
+
? Promise.reject(new Error("boom"))
|
|
67
|
+
: Promise.resolve(model),
|
|
68
|
+
);
|
|
69
|
+
const { result } = render(["a.malloy", "b.malloy", "c.malloy"]);
|
|
70
|
+
await waitFor(() => expect(result.current.isSuccess).toBe(true));
|
|
71
|
+
expect([...result.current.choices.keys()]).toEqual([
|
|
72
|
+
"a.malloy",
|
|
73
|
+
"c.malloy",
|
|
74
|
+
]);
|
|
75
|
+
expect(result.current.failed).toEqual(["b.malloy"]);
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
it("lists every model, in order, when all lookups fail", async () => {
|
|
79
|
+
getModel.mockImplementation(() => Promise.reject(new Error("down")));
|
|
80
|
+
const { result } = render(["a.malloy", "b.malloy"]);
|
|
81
|
+
await waitFor(() => expect(result.current.isSuccess).toBe(true));
|
|
82
|
+
expect(result.current.choices.size).toBe(0);
|
|
83
|
+
expect(result.current.failed).toEqual(["a.malloy", "b.malloy"]);
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
it("retry refetches only the failed lookups and clears failed on success", async () => {
|
|
87
|
+
let healthy = false;
|
|
88
|
+
getModel.mockImplementation((_e, _p, path) =>
|
|
89
|
+
path === "b.malloy" && !healthy
|
|
90
|
+
? Promise.reject(new Error("boom"))
|
|
91
|
+
: Promise.resolve(model),
|
|
92
|
+
);
|
|
93
|
+
const { result } = render(["a.malloy", "b.malloy"]);
|
|
94
|
+
await waitFor(() => expect(result.current.failed).toEqual(["b.malloy"]));
|
|
95
|
+
expect(callsFor("a.malloy")).toBe(1);
|
|
96
|
+
healthy = true;
|
|
97
|
+
act(() => result.current.retry());
|
|
98
|
+
await waitFor(() => expect(result.current.failed).toEqual([]));
|
|
99
|
+
expect([...result.current.choices.keys()]).toEqual([
|
|
100
|
+
"a.malloy",
|
|
101
|
+
"b.malloy",
|
|
102
|
+
]);
|
|
103
|
+
expect(callsFor("a.malloy")).toBe(1);
|
|
104
|
+
expect(callsFor("b.malloy")).toBe(2);
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
it("offers a retry only when a failed lookup could succeed on a second attempt", async () => {
|
|
108
|
+
const status = (code: number) =>
|
|
109
|
+
Promise.reject(
|
|
110
|
+
Object.assign(new Error("x"), { response: { status: code } }),
|
|
111
|
+
);
|
|
112
|
+
getModel.mockImplementation((_e, _p, path) =>
|
|
113
|
+
path === "a.malloy" ? status(403) : status(404),
|
|
114
|
+
);
|
|
115
|
+
const denied = render(["a.malloy", "b.malloy"]);
|
|
116
|
+
await waitFor(() =>
|
|
117
|
+
expect(denied.result.current.failed).toEqual(["a.malloy", "b.malloy"]),
|
|
118
|
+
);
|
|
119
|
+
expect(denied.result.current.canRetry).toBe(false);
|
|
120
|
+
|
|
121
|
+
clearCache();
|
|
122
|
+
getModel.mockImplementation((_e, _p, path) =>
|
|
123
|
+
path === "a.malloy" ? status(404) : status(503),
|
|
124
|
+
);
|
|
125
|
+
const flaky = render(["a.malloy", "b.malloy"]);
|
|
126
|
+
await waitFor(() =>
|
|
127
|
+
expect(flaky.result.current.failed).toEqual(["a.malloy", "b.malloy"]),
|
|
128
|
+
);
|
|
129
|
+
expect(flaky.result.current.canRetry).toBe(true);
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
it("makes no calls while disabled", () => {
|
|
133
|
+
const { result } = render(["a.malloy"], false);
|
|
134
|
+
expect(getModel).not.toHaveBeenCalled();
|
|
135
|
+
expect(result.current.failed).toEqual([]);
|
|
136
|
+
expect(result.current.isSuccess).toBe(false);
|
|
137
|
+
});
|
|
138
|
+
});
|