@malloy-publisher/sdk 0.12.0 → 0.12.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/README.md +79 -6
- package/dist/{RenderedResult-SgDlxzs9.cjs.js → RenderedResult-Cgw4iqev.cjs.js} +1 -1
- package/dist/{RenderedResult-D8bCgThO.es.js → RenderedResult-DEjFM1DL.es.js} +2 -2
- package/dist/ServerProvider-BRQl_cBo.cjs.js +1 -0
- package/dist/{ServerProvider-DBw-thY0.es.js → ServerProvider-RnliA2Ao.es.js} +1084 -723
- package/dist/builder/index.cjs.js +1 -1
- package/dist/builder/index.es.js +3 -3
- package/dist/client/api.d.ts +754 -41
- package/dist/client/index.cjs.js +1 -1
- package/dist/client/index.es.js +120 -110
- package/dist/components/Materializations/MaterializationDetailDialog.d.ts +16 -1
- package/dist/components/Model/ExplorerPage.d.ts +9 -0
- package/dist/components/Model/Model.d.ts +27 -11
- package/dist/components/ModelViewer/AccessChips.d.ts +34 -0
- package/dist/components/ModelViewer/EmptyRow.d.ts +23 -0
- package/dist/components/ModelViewer/Facts.d.ts +15 -0
- package/dist/components/ModelViewer/FieldGroup.d.ts +31 -0
- package/dist/components/ModelViewer/FieldRow.d.ts +69 -0
- package/dist/components/ModelViewer/GivenFacts.d.ts +21 -0
- package/dist/components/ModelViewer/HeaderBar.d.ts +75 -0
- package/dist/components/ModelViewer/InlineDoc.d.ts +13 -0
- package/dist/components/ModelViewer/ModelGivensSection.d.ts +32 -0
- package/dist/components/ModelViewer/ModelSection.d.ts +21 -0
- package/dist/components/ModelViewer/ModelViewer.d.ts +91 -0
- package/dist/components/ModelViewer/QueriesGroup.d.ts +15 -0
- package/dist/components/ModelViewer/RowGlyph.d.ts +27 -0
- package/dist/components/ModelViewer/RunResult.d.ts +46 -0
- package/dist/components/ModelViewer/SampleValues.d.ts +20 -0
- package/dist/components/ModelViewer/SchemaJoin.d.ts +51 -0
- package/dist/components/ModelViewer/SchemaView.d.ts +52 -0
- package/dist/components/ModelViewer/SegmentIcons.d.ts +70 -0
- package/dist/components/ModelViewer/SourcesDropdown.d.ts +53 -0
- package/dist/components/ModelViewer/SourcesNav.d.ts +25 -0
- package/dist/components/ModelViewer/StatusPill.d.ts +56 -0
- package/dist/components/ModelViewer/ValueChip.d.ts +17 -0
- package/dist/components/ModelViewer/ViewRow.d.ts +36 -0
- package/dist/components/ModelViewer/clickableBox.d.ts +29 -0
- package/dist/components/ModelViewer/codeDisplay.d.ts +44 -0
- package/dist/components/ModelViewer/docAnnotations.d.ts +90 -0
- package/dist/components/ModelViewer/explorerModel.d.ts +12 -0
- package/dist/components/ModelViewer/extensions.d.ts +94 -0
- package/dist/components/ModelViewer/fieldCode.d.ts +74 -0
- package/dist/components/ModelViewer/fieldEntities.d.ts +45 -0
- package/dist/components/ModelViewer/fieldExpansion.d.ts +41 -0
- package/dist/components/ModelViewer/givenUsage.d.ts +71 -0
- package/dist/components/ModelViewer/headerCard.d.ts +31 -0
- package/dist/components/ModelViewer/httpStatus.d.ts +4 -0
- package/dist/components/ModelViewer/icons.d.ts +31 -0
- package/dist/components/ModelViewer/index.d.ts +9 -0
- package/dist/components/ModelViewer/joinAccess.d.ts +52 -0
- package/dist/components/ModelViewer/malloyRunText.d.ts +22 -0
- package/dist/components/ModelViewer/malloyText.d.ts +21 -0
- package/dist/components/ModelViewer/modelGivens.d.ts +61 -0
- package/dist/components/ModelViewer/modelGivensContext.d.ts +9 -0
- package/dist/components/ModelViewer/modelHeaderPresentation.d.ts +28 -0
- package/dist/components/ModelViewer/modelTextDeclarations.d.ts +32 -0
- package/dist/components/ModelViewer/modelTextParse.d.ts +27 -0
- package/dist/components/ModelViewer/namedQueryCode.d.ts +12 -0
- package/dist/components/ModelViewer/offSurface.d.ts +54 -0
- package/dist/components/ModelViewer/rowLayers.d.ts +30 -0
- package/dist/components/ModelViewer/runGate.d.ts +49 -0
- package/dist/components/ModelViewer/schemaModel.d.ts +109 -0
- package/dist/components/ModelViewer/sectionOpenCommand.d.ts +45 -0
- package/dist/components/ModelViewer/sourceAccess.d.ts +67 -0
- package/dist/components/ModelViewer/sourceLineage.d.ts +14 -0
- package/dist/components/ModelViewer/sourcePicker.d.ts +66 -0
- package/dist/components/ModelViewer/surface.d.ts +47 -0
- package/dist/components/ModelViewer/text.d.ts +6 -0
- package/dist/components/ModelViewer/theme.d.ts +63 -0
- package/dist/components/ModelViewer/tokens.d.ts +52 -0
- package/dist/components/ModelViewer/ui.d.ts +48 -0
- package/dist/components/ModelViewer/useDimensionSamples.d.ts +76 -0
- package/dist/components/ModelViewer/useModelSources.d.ts +92 -0
- package/dist/components/ModelViewer/useRunQuery.d.ts +115 -0
- package/dist/components/ReportBuilder/loadMalloy.d.ts +4 -0
- package/dist/components/ServerProvider.d.ts +17 -2
- package/dist/components/filter/utils.d.ts +1 -1
- package/dist/components/given/GivenInput.d.ts +7 -1
- package/dist/components/given/GivensPanel.d.ts +3 -1
- package/dist/components/given/filterSummary.d.ts +6 -0
- package/dist/components/given/useGivenUrlParams.d.ts +11 -0
- package/dist/components/index.d.ts +2 -1
- package/dist/components/modelPage.d.ts +36 -0
- package/dist/hooks/useDimensionalFilterRangeData.d.ts +9 -0
- package/dist/hooks/useSuggestOptions.d.ts +26 -2
- package/dist/index.cjs.js +47 -19
- package/dist/index.es.js +14137 -9697
- package/dist/useCompiledDocument-D88_2cGS.cjs.js +143 -0
- package/dist/{useCompiledDocument-CXMPtJkJ.es.js → useCompiledDocument-Dmm6xkom.es.js} +6975 -6893
- package/dist/utils/filterCondition.d.ts +14 -0
- package/dist/utils/malloyLiteral.d.ts +20 -1
- package/package.json +2 -2
- package/src/components/Materializations/MaterializationDetailDialog.tsx +33 -7
- package/src/components/Materializations/Materializations.tsx +47 -4
- package/src/components/Materializations/Materializations.versions.spec.tsx +140 -1
- package/src/components/Model/ExplorerPage.tsx +269 -0
- package/src/components/Model/Model.tsx +66 -250
- package/src/components/Model/ModelExplorer.spec.tsx +64 -1
- package/src/components/ModelViewer/AccessChips.tsx +149 -0
- package/src/components/ModelViewer/EmptyRow.tsx +71 -0
- package/src/components/ModelViewer/Facts.tsx +46 -0
- package/src/components/ModelViewer/FieldGroup.tsx +137 -0
- package/src/components/ModelViewer/FieldRow.tsx +566 -0
- package/src/components/ModelViewer/GivenFacts.tsx +89 -0
- package/src/components/ModelViewer/HeaderBar.tsx +362 -0
- package/src/components/ModelViewer/InlineDoc.tsx +50 -0
- package/src/components/ModelViewer/ModelGivensSection.tsx +142 -0
- package/src/components/ModelViewer/ModelSection.tsx +98 -0
- package/src/components/ModelViewer/ModelViewer.spec.tsx +937 -0
- package/src/components/ModelViewer/ModelViewer.tsx +777 -0
- package/src/components/ModelViewer/QueriesGroup.tsx +107 -0
- package/src/components/ModelViewer/RowGlyph.tsx +138 -0
- package/src/components/ModelViewer/RunResult.tsx +298 -0
- package/src/components/ModelViewer/SampleValues.tsx +97 -0
- package/src/components/ModelViewer/SchemaJoin.tsx +258 -0
- package/src/components/ModelViewer/SchemaView.tsx +222 -0
- package/src/components/ModelViewer/SegmentIcons.tsx +129 -0
- package/src/components/ModelViewer/SourcesDropdown.tsx +265 -0
- package/src/components/ModelViewer/SourcesNav.tsx +75 -0
- package/src/components/ModelViewer/StatusPill.tsx +145 -0
- package/src/components/ModelViewer/ValueChip.tsx +39 -0
- package/src/components/ModelViewer/ViewRow.tsx +274 -0
- package/src/components/ModelViewer/clickableBox.ts +59 -0
- package/src/components/ModelViewer/codeDisplay.spec.ts +176 -0
- package/src/components/ModelViewer/codeDisplay.ts +118 -0
- package/src/components/ModelViewer/docAnnotations.spec.ts +181 -0
- package/src/components/ModelViewer/docAnnotations.ts +244 -0
- package/src/components/ModelViewer/docAnnotations.typeDetail.spec.ts +114 -0
- package/src/components/ModelViewer/explorerModel.spec.ts +43 -0
- package/src/components/ModelViewer/explorerModel.ts +31 -0
- package/src/components/ModelViewer/extensions.tsx +112 -0
- package/src/components/ModelViewer/fieldCode.spec.ts +168 -0
- package/src/components/ModelViewer/fieldCode.ts +118 -0
- package/src/components/ModelViewer/fieldEntities.ts +63 -0
- package/src/components/ModelViewer/fieldExpansion.spec.ts +116 -0
- package/src/components/ModelViewer/fieldExpansion.ts +95 -0
- package/src/components/ModelViewer/givenUsage.spec.ts +304 -0
- package/src/components/ModelViewer/givenUsage.ts +325 -0
- package/src/components/ModelViewer/headerCard.tsx +102 -0
- package/src/components/ModelViewer/httpStatus.ts +15 -0
- package/src/components/ModelViewer/icons.tsx +67 -0
- package/src/components/ModelViewer/index.ts +26 -0
- package/src/components/ModelViewer/joinAccess.spec.ts +95 -0
- package/src/components/ModelViewer/joinAccess.ts +54 -0
- package/src/components/ModelViewer/malloyRunText.spec.ts +48 -0
- package/src/components/ModelViewer/malloyRunText.ts +51 -0
- package/src/components/ModelViewer/malloyText.spec.ts +47 -0
- package/src/components/ModelViewer/malloyText.ts +77 -0
- package/src/components/ModelViewer/modelGivens.ts +228 -0
- package/src/components/ModelViewer/modelGivensContext.ts +13 -0
- package/src/components/ModelViewer/modelHeaderPresentation.spec.ts +31 -0
- package/src/components/ModelViewer/modelHeaderPresentation.ts +41 -0
- package/src/components/ModelViewer/modelTextDeclarations.spec.ts +336 -0
- package/src/components/ModelViewer/modelTextDeclarations.ts +228 -0
- package/src/components/ModelViewer/modelTextParse.ts +112 -0
- package/src/components/ModelViewer/namedQueryCode.spec.ts +58 -0
- package/src/components/ModelViewer/namedQueryCode.ts +26 -0
- package/src/components/ModelViewer/offSurface.tsx +124 -0
- package/src/components/ModelViewer/rowLayers.tsx +104 -0
- package/src/components/ModelViewer/runGate.spec.ts +219 -0
- package/src/components/ModelViewer/runGate.ts +145 -0
- package/src/components/ModelViewer/schemaModel.defaultSource.spec.ts +126 -0
- package/src/components/ModelViewer/schemaModel.queries.spec.ts +37 -0
- package/src/components/ModelViewer/schemaModel.ts +300 -0
- package/src/components/ModelViewer/sectionOpenCommand.tsx +125 -0
- package/src/components/ModelViewer/sourceAccess.spec.ts +130 -0
- package/src/components/ModelViewer/sourceAccess.ts +194 -0
- package/src/components/ModelViewer/sourceLineage.spec.ts +73 -0
- package/src/components/ModelViewer/sourceLineage.ts +38 -0
- package/src/components/ModelViewer/sourcePicker.spec.ts +294 -0
- package/src/components/ModelViewer/sourcePicker.ts +162 -0
- package/src/components/ModelViewer/surface.ts +51 -0
- package/src/components/ModelViewer/text.tsx +33 -0
- package/src/components/ModelViewer/theme.spec.ts +38 -0
- package/src/components/ModelViewer/theme.ts +58 -0
- package/src/components/ModelViewer/tokens.ts +87 -0
- package/src/components/ModelViewer/ui.tsx +201 -0
- package/src/components/ModelViewer/useDimensionSamples.spec.tsx +139 -0
- package/src/components/ModelViewer/useDimensionSamples.ts +220 -0
- package/src/components/ModelViewer/useModelSources.spec.ts +110 -0
- package/src/components/ModelViewer/useModelSources.ts +238 -0
- package/src/components/ModelViewer/useRunQuery.spec.ts +50 -0
- package/src/components/ModelViewer/useRunQuery.ts +223 -0
- package/src/components/Package/Package.agents.spec.tsx +5 -4
- package/src/components/Package/Package.tsx +8 -1
- package/src/components/ReportBuilder/emptyStart.spec.ts +8 -0
- package/src/components/ReportBuilder/loadMalloy.ts +8 -1
- package/src/components/ServerProvider.tsx +29 -3
- package/src/components/filter/utils.ts +4 -60
- package/src/components/given/FilterButton.tsx +4 -1
- package/src/components/given/GivenInput.tsx +8 -0
- package/src/components/given/GivensPanel.tsx +6 -1
- package/src/components/given/filterSummary.spec.ts +31 -1
- package/src/components/given/filterSummary.ts +41 -19
- package/src/components/given/useGivenUrlParams.spec.tsx +32 -0
- package/src/components/given/useGivenUrlParams.ts +49 -14
- package/src/components/index.ts +6 -1
- package/src/components/legacy/malloynb/Notebook.spec.tsx +38 -4
- package/src/components/legacy/malloynb/NotebookCell.tsx +8 -2
- package/src/components/modelPage.spec.ts +79 -0
- package/src/components/modelPage.ts +48 -0
- package/src/hooks/useDimensionFilters.ts +3 -58
- package/src/hooks/useDimensionFiltersQuery.ts +3 -58
- package/src/hooks/useDimensionalFilterRangeData.spec.ts +57 -0
- package/src/hooks/useDimensionalFilterRangeData.ts +8 -63
- package/src/hooks/useSuggestOptions.dom.spec.ts +54 -1
- package/src/hooks/useSuggestOptions.spec.ts +71 -0
- package/src/hooks/useSuggestOptions.ts +102 -12
- package/src/utils/filterCondition.spec.ts +87 -0
- package/src/utils/filterCondition.ts +66 -0
- package/src/utils/malloyLiteral.spec.ts +53 -1
- package/src/utils/malloyLiteral.ts +47 -12
- package/dist/ServerProvider-BHfg1Ed6.cjs.js +0 -1
- package/dist/useCompiledDocument-D7X_K6YU.cjs.js +0 -142
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
// Copyright (c) Credible Data Inc.
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
|
|
4
|
+
import { 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
|
+
import type { ExplorerField } from "./schemaModel";
|
|
12
|
+
|
|
13
|
+
const result = JSON.stringify({
|
|
14
|
+
data: {
|
|
15
|
+
kind: "array_cell",
|
|
16
|
+
array_value: [
|
|
17
|
+
{
|
|
18
|
+
kind: "record_cell",
|
|
19
|
+
record_value: [
|
|
20
|
+
{ kind: "string_cell", string_value: "shipped" },
|
|
21
|
+
{ kind: "number_cell", number_value: 9 },
|
|
22
|
+
],
|
|
23
|
+
},
|
|
24
|
+
{
|
|
25
|
+
kind: "record_cell",
|
|
26
|
+
record_value: [
|
|
27
|
+
{ kind: "string_cell", string_value: "returned" },
|
|
28
|
+
{ kind: "number_cell", number_value: 2 },
|
|
29
|
+
],
|
|
30
|
+
},
|
|
31
|
+
],
|
|
32
|
+
},
|
|
33
|
+
});
|
|
34
|
+
const executeQueryModel = mock((..._args: unknown[]) =>
|
|
35
|
+
Promise.resolve({ data: { result } }),
|
|
36
|
+
);
|
|
37
|
+
mockServerProvider({ models: { executeQueryModel } });
|
|
38
|
+
|
|
39
|
+
const { useDimensionSamples, isExpectedNoSamples } = await import(
|
|
40
|
+
"./useDimensionSamples"
|
|
41
|
+
);
|
|
42
|
+
|
|
43
|
+
const STATUS: ExplorerField = {
|
|
44
|
+
name: "status",
|
|
45
|
+
kind: "dimension",
|
|
46
|
+
indexed: true,
|
|
47
|
+
};
|
|
48
|
+
const run = { projectName: "env", packageName: "pkg", versionId: "1.0.0" };
|
|
49
|
+
|
|
50
|
+
beforeEach(() => {
|
|
51
|
+
clearCache();
|
|
52
|
+
executeQueryModel.mockClear();
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
describe("useDimensionSamples", () => {
|
|
56
|
+
it("reads a tagged dimension's common values with one quoted query", async () => {
|
|
57
|
+
const { result: hook } = renderHook(
|
|
58
|
+
() =>
|
|
59
|
+
useDimensionSamples({
|
|
60
|
+
run,
|
|
61
|
+
modelPath: "shop.malloy",
|
|
62
|
+
sourceName: "orders",
|
|
63
|
+
field: STATUS,
|
|
64
|
+
}),
|
|
65
|
+
{ wrapper: serverWrapper },
|
|
66
|
+
);
|
|
67
|
+
await waitFor(() =>
|
|
68
|
+
expect(hook.current.samples).toEqual(["shipped", "returned"]),
|
|
69
|
+
);
|
|
70
|
+
expect(executeQueryModel).toHaveBeenCalledTimes(1);
|
|
71
|
+
const [env, pkg, path, request] = executeQueryModel.mock.calls[0] as [
|
|
72
|
+
string,
|
|
73
|
+
string,
|
|
74
|
+
string,
|
|
75
|
+
{ query: string; versionId?: string },
|
|
76
|
+
];
|
|
77
|
+
expect([env, pkg, path, request.versionId]).toEqual([
|
|
78
|
+
"env",
|
|
79
|
+
"pkg",
|
|
80
|
+
"shop.malloy",
|
|
81
|
+
"1.0.0",
|
|
82
|
+
]);
|
|
83
|
+
expect(request.query).toContain("run: `orders` -> {");
|
|
84
|
+
expect(request.query).toContain("group_by: `status`");
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
it("asks nothing for a dimension the model doesn't tag", () => {
|
|
88
|
+
const { result: hook } = renderHook(
|
|
89
|
+
() =>
|
|
90
|
+
useDimensionSamples({
|
|
91
|
+
run,
|
|
92
|
+
modelPath: "shop.malloy",
|
|
93
|
+
sourceName: "orders",
|
|
94
|
+
field: { ...STATUS, indexed: false },
|
|
95
|
+
}),
|
|
96
|
+
{ wrapper: serverWrapper },
|
|
97
|
+
);
|
|
98
|
+
expect(hook.current).toEqual({
|
|
99
|
+
samples: [],
|
|
100
|
+
loading: false,
|
|
101
|
+
failed: false,
|
|
102
|
+
});
|
|
103
|
+
expect(executeQueryModel).not.toHaveBeenCalled();
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
it("asks a host's fetcher instead, once per build it serves", async () => {
|
|
107
|
+
const fetchSamples = mock(() => Promise.resolve(["a", "b"]));
|
|
108
|
+
const { result: hook, rerender } = renderHook(
|
|
109
|
+
({ generation }: { generation: string }) =>
|
|
110
|
+
useDimensionSamples({
|
|
111
|
+
run,
|
|
112
|
+
modelPath: "shop.malloy",
|
|
113
|
+
sourceName: "orders",
|
|
114
|
+
field: STATUS,
|
|
115
|
+
fetchSamples,
|
|
116
|
+
generation,
|
|
117
|
+
}),
|
|
118
|
+
{ wrapper: serverWrapper, initialProps: { generation: "r1" } },
|
|
119
|
+
);
|
|
120
|
+
await waitFor(() => expect(hook.current.samples).toEqual(["a", "b"]));
|
|
121
|
+
rerender({ generation: "r1" });
|
|
122
|
+
expect(fetchSamples).toHaveBeenCalledTimes(1);
|
|
123
|
+
rerender({ generation: "r2" });
|
|
124
|
+
await waitFor(() => expect(fetchSamples).toHaveBeenCalledTimes(2));
|
|
125
|
+
expect(executeQueryModel).not.toHaveBeenCalled();
|
|
126
|
+
});
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
describe("isExpectedNoSamples", () => {
|
|
130
|
+
const withStatus = (status: number) => ({ response: { status } });
|
|
131
|
+
it("reads a client error about the field as no values, and the rest as failures", () => {
|
|
132
|
+
expect(isExpectedNoSamples(withStatus(400))).toBe(true);
|
|
133
|
+
expect(isExpectedNoSamples(withStatus(404))).toBe(true);
|
|
134
|
+
expect(isExpectedNoSamples(withStatus(401))).toBe(false);
|
|
135
|
+
expect(isExpectedNoSamples(withStatus(429))).toBe(false);
|
|
136
|
+
expect(isExpectedNoSamples(withStatus(500))).toBe(false);
|
|
137
|
+
expect(isExpectedNoSamples(new Error("network"))).toBe(false);
|
|
138
|
+
});
|
|
139
|
+
});
|
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
// Copyright (c) Credible Data Inc.
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
|
|
4
|
+
import type * as Malloy from "@malloydata/malloy-interfaces";
|
|
5
|
+
import { useQuery } from "@tanstack/react-query";
|
|
6
|
+
import { useServer } from "../ServerProvider";
|
|
7
|
+
import { httpStatusOf } from "./httpStatus";
|
|
8
|
+
import { sampleValuesQuery } from "./malloyRunText";
|
|
9
|
+
import { runGate } from "./runGate";
|
|
10
|
+
import type { ExplorerField } from "./schemaModel";
|
|
11
|
+
import {
|
|
12
|
+
givensOfRun,
|
|
13
|
+
runQueryOptions,
|
|
14
|
+
runReading,
|
|
15
|
+
type RunContext,
|
|
16
|
+
} from "./useRunQuery";
|
|
17
|
+
|
|
18
|
+
export const SAMPLE_LIMIT = 12;
|
|
19
|
+
/** A host's values move slowly; read them again after a while. */
|
|
20
|
+
const SAMPLES_STALE_MS = 5 * 60_000;
|
|
21
|
+
|
|
22
|
+
/** What a host's sample fetcher is asked for. */
|
|
23
|
+
export interface SampleValuesRequest {
|
|
24
|
+
modelPath: string;
|
|
25
|
+
sourceName: string;
|
|
26
|
+
fieldName: string;
|
|
27
|
+
/** At most this many values are shown. */
|
|
28
|
+
limit: number;
|
|
29
|
+
signal?: AbortSignal;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** What a host's sample fetcher can say beyond the values themselves. */
|
|
33
|
+
export interface SampleValuesPage {
|
|
34
|
+
/** The most common values, most common first. */
|
|
35
|
+
values: string[];
|
|
36
|
+
/** How many distinct values the dimension has, for the card's caption. */
|
|
37
|
+
total?: number | null;
|
|
38
|
+
/** Why there are none to show ("the index is still building"), said in their place. */
|
|
39
|
+
unavailable?: string;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* A host's way to read a dimension's sample values (an index it maintains, say)
|
|
44
|
+
* instead of the viewer's own query: the values alone, or a
|
|
45
|
+
* {@link SampleValuesPage}. Rejecting with a 4xx that says the field has nothing
|
|
46
|
+
* to sample reads as "no values" (see {@link isExpectedNoSamples}).
|
|
47
|
+
*/
|
|
48
|
+
export type SampleValuesFetcher = (
|
|
49
|
+
request: SampleValuesRequest,
|
|
50
|
+
) => Promise<string[] | SampleValuesPage>;
|
|
51
|
+
|
|
52
|
+
interface UseDimensionSamplesArgs {
|
|
53
|
+
run: RunContext;
|
|
54
|
+
/** Model path the source belongs to (e.g. "flights.malloy"). */
|
|
55
|
+
modelPath: string;
|
|
56
|
+
/** Source the selected field belongs to. */
|
|
57
|
+
sourceName: string;
|
|
58
|
+
/** The field to sample, or null while the card is collapsed (hook idles). */
|
|
59
|
+
field: ExplorerField | null;
|
|
60
|
+
/** The host's fetcher; the viewer runs {@link sampleValuesQuery} without one. */
|
|
61
|
+
fetchSamples?: SampleValuesFetcher;
|
|
62
|
+
/** A host's build of the values it serves: a new one is a new cache entry,
|
|
63
|
+
* so a reopened row never shows the old values. */
|
|
64
|
+
generation?: string;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Whether a rejected sample read is an answer that the dimension simply has
|
|
69
|
+
* nothing to sample, so the card says "no values" rather than "couldn't be
|
|
70
|
+
* loaded": a client error, except the ones that say nothing about the field (a
|
|
71
|
+
* lapsed session 401, a timeout 408, a rate limit 429). Every 5xx, and every
|
|
72
|
+
* rejection with no status (the network), is a read that failed.
|
|
73
|
+
*/
|
|
74
|
+
export function isExpectedNoSamples(error: unknown): boolean {
|
|
75
|
+
const status = httpStatusOf(error);
|
|
76
|
+
if (status === undefined) return false;
|
|
77
|
+
return (
|
|
78
|
+
status >= 400 &&
|
|
79
|
+
status < 500 &&
|
|
80
|
+
status !== 401 &&
|
|
81
|
+
status !== 408 &&
|
|
82
|
+
status !== 429
|
|
83
|
+
);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** The first column of each row of a result, as text. */
|
|
87
|
+
export function firstColumnValues(resultJson: string | undefined): string[] {
|
|
88
|
+
if (!resultJson) return [];
|
|
89
|
+
const { data } = JSON.parse(resultJson) as Pick<Malloy.Result, "data">;
|
|
90
|
+
if (data?.kind !== "array_cell") return [];
|
|
91
|
+
return data.array_value.flatMap((row) => {
|
|
92
|
+
if (row.kind !== "record_cell") return [];
|
|
93
|
+
const cell = row.record_value[0];
|
|
94
|
+
if (!cell) return [];
|
|
95
|
+
switch (cell.kind) {
|
|
96
|
+
case "string_cell":
|
|
97
|
+
return [cell.string_value];
|
|
98
|
+
case "number_cell":
|
|
99
|
+
return [String(cell.number_value)];
|
|
100
|
+
case "boolean_cell":
|
|
101
|
+
return [String(cell.boolean_value)];
|
|
102
|
+
case "date_cell":
|
|
103
|
+
return [cell.date_value];
|
|
104
|
+
case "timestamp_cell":
|
|
105
|
+
return [cell.timestamp_value];
|
|
106
|
+
default:
|
|
107
|
+
return [];
|
|
108
|
+
}
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Lazily fetch sample values for a dimension. Only for dimensions the model tags
|
|
114
|
+
* `#(index)` / `#(index_values)`: the tag is the modeler saying its values are
|
|
115
|
+
* worth listing, and without it opening a row costs nothing. Everything else
|
|
116
|
+
* resolves to "no samples", as does a host's refusal that answers the question.
|
|
117
|
+
* Any other error is reported as `failed`, never as an empty list: the card
|
|
118
|
+
* must not say there are no values when it could not ask.
|
|
119
|
+
*
|
|
120
|
+
* Without a host fetcher this is one capped `group_by` query through the
|
|
121
|
+
* Publisher, run as a view's is (`runQueryOptions`: the same request, cache
|
|
122
|
+
* and `onQueryRun`), with the given values of the givens its source reads, and
|
|
123
|
+
* gated the same way. Shared by every mount: opening, shutting and reopening a
|
|
124
|
+
* row reads it once.
|
|
125
|
+
*/
|
|
126
|
+
export function useDimensionSamples({
|
|
127
|
+
run,
|
|
128
|
+
modelPath,
|
|
129
|
+
sourceName,
|
|
130
|
+
field,
|
|
131
|
+
fetchSamples,
|
|
132
|
+
generation,
|
|
133
|
+
}: UseDimensionSamplesArgs): {
|
|
134
|
+
samples: string[];
|
|
135
|
+
/** How many distinct values there are, when a host says. */
|
|
136
|
+
total?: number | null;
|
|
137
|
+
/** Why there are none: a host's reason, or the given a run needs first. */
|
|
138
|
+
unavailable?: string;
|
|
139
|
+
loading: boolean;
|
|
140
|
+
failed: boolean;
|
|
141
|
+
} {
|
|
142
|
+
const { server, apiClients } = useServer();
|
|
143
|
+
const eligible = field?.kind === "dimension" && field.indexed === true;
|
|
144
|
+
// The viewer's own query reads its source's givens and gates as a view's
|
|
145
|
+
// run does: one that would be refused for a missing given says so, rather
|
|
146
|
+
// than sending a doomed query.
|
|
147
|
+
const readGivens = givensOfRun(run, sourceName);
|
|
148
|
+
const gate =
|
|
149
|
+
eligible && !fetchSamples
|
|
150
|
+
? runGate(
|
|
151
|
+
readGivens,
|
|
152
|
+
run.requiredFiltersBySource?.get(sourceName),
|
|
153
|
+
run.givenValues,
|
|
154
|
+
)
|
|
155
|
+
: undefined;
|
|
156
|
+
const blocked = gate?.kind === "blocked" ? gate.reason : undefined;
|
|
157
|
+
const fieldName = eligible && !blocked ? field.name : undefined;
|
|
158
|
+
|
|
159
|
+
const own = useQuery({
|
|
160
|
+
...runQueryOptions({
|
|
161
|
+
server,
|
|
162
|
+
apiClients,
|
|
163
|
+
run: runReading(run, readGivens),
|
|
164
|
+
modelPath,
|
|
165
|
+
query: sampleValuesQuery(sourceName, fieldName ?? "", SAMPLE_LIMIT),
|
|
166
|
+
}),
|
|
167
|
+
enabled: !!fieldName && !fetchSamples,
|
|
168
|
+
refetchOnWindowFocus: false,
|
|
169
|
+
refetchOnReconnect: false,
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
const host = useQuery({
|
|
173
|
+
queryKey: [
|
|
174
|
+
"model-viewer-samples",
|
|
175
|
+
server,
|
|
176
|
+
run.projectName,
|
|
177
|
+
run.packageName,
|
|
178
|
+
run.versionId ?? "",
|
|
179
|
+
modelPath,
|
|
180
|
+
sourceName,
|
|
181
|
+
fieldName,
|
|
182
|
+
generation ?? "host",
|
|
183
|
+
],
|
|
184
|
+
enabled: !!fieldName && !!fetchSamples,
|
|
185
|
+
staleTime: SAMPLES_STALE_MS,
|
|
186
|
+
retry: false,
|
|
187
|
+
refetchOnWindowFocus: false,
|
|
188
|
+
queryFn: async ({ signal }): Promise<SampleValuesPage> => {
|
|
189
|
+
try {
|
|
190
|
+
const page = await fetchSamples!({
|
|
191
|
+
modelPath,
|
|
192
|
+
sourceName,
|
|
193
|
+
fieldName: fieldName!,
|
|
194
|
+
limit: SAMPLE_LIMIT,
|
|
195
|
+
signal,
|
|
196
|
+
});
|
|
197
|
+
const read = Array.isArray(page) ? { values: page } : page;
|
|
198
|
+
return { ...read, values: read.values.slice(0, SAMPLE_LIMIT) };
|
|
199
|
+
} catch (error) {
|
|
200
|
+
// A host's refusal can be its answer that there is nothing to
|
|
201
|
+
// sample. The viewer's own query has no such answer, so a 4xx
|
|
202
|
+
// from it (a query that does not compile) is a failure.
|
|
203
|
+
if (isExpectedNoSamples(error)) return { values: [] };
|
|
204
|
+
throw error;
|
|
205
|
+
}
|
|
206
|
+
},
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
const read = fetchSamples ? host : own;
|
|
210
|
+
const page: SampleValuesPage | undefined = fetchSamples
|
|
211
|
+
? host.data
|
|
212
|
+
: own.data && { values: firstColumnValues(own.data.data.result) };
|
|
213
|
+
return {
|
|
214
|
+
samples: page?.values ?? [],
|
|
215
|
+
total: page?.total,
|
|
216
|
+
unavailable: blocked ?? page?.unavailable,
|
|
217
|
+
loading: !!fieldName && read.isPending,
|
|
218
|
+
failed: read.isError,
|
|
219
|
+
};
|
|
220
|
+
}
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
// Copyright (c) Credible Data Inc.
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
|
|
4
|
+
import { describe, expect, it } from "bun:test";
|
|
5
|
+
import { fetchCompiledModel } from "./useModelSources";
|
|
6
|
+
|
|
7
|
+
// The compiled-model shaping is shared by two hooks under one cache key, and it
|
|
8
|
+
// is where the wire's shape is normalized: `sourceText` must be a STRING to be
|
|
9
|
+
// the file's text. Through the router a declared-but-unsent field arrives as
|
|
10
|
+
// `null`, and a cast would have let that through to `.replace(...)`.
|
|
11
|
+
function clientReturning(data: unknown) {
|
|
12
|
+
return {
|
|
13
|
+
models: { getModel: async () => ({ data }) },
|
|
14
|
+
} as unknown as Parameters<typeof fetchCompiledModel>[0];
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
describe("fetchCompiledModel", () => {
|
|
18
|
+
it("keeps a string sourceText and reads nothing else as the text", async () => {
|
|
19
|
+
const text = "source: flights is duckdb.table('flights.parquet')\n";
|
|
20
|
+
expect(
|
|
21
|
+
(
|
|
22
|
+
await fetchCompiledModel(
|
|
23
|
+
clientReturning({ sourceText: text }),
|
|
24
|
+
"p",
|
|
25
|
+
"k",
|
|
26
|
+
"m.malloy",
|
|
27
|
+
)
|
|
28
|
+
).sourceText,
|
|
29
|
+
).toBe(text);
|
|
30
|
+
expect(
|
|
31
|
+
(
|
|
32
|
+
await fetchCompiledModel(
|
|
33
|
+
clientReturning({ sourceText: null }),
|
|
34
|
+
"p",
|
|
35
|
+
"k",
|
|
36
|
+
"m.malloy",
|
|
37
|
+
)
|
|
38
|
+
).sourceText,
|
|
39
|
+
).toBeUndefined();
|
|
40
|
+
expect(
|
|
41
|
+
(await fetchCompiledModel(clientReturning({}), "p", "k", "m.malloy"))
|
|
42
|
+
.sourceText,
|
|
43
|
+
).toBeUndefined();
|
|
44
|
+
expect(
|
|
45
|
+
(
|
|
46
|
+
await fetchCompiledModel(
|
|
47
|
+
clientReturning({ sourceText: 42 }),
|
|
48
|
+
"p",
|
|
49
|
+
"k",
|
|
50
|
+
"m.malloy",
|
|
51
|
+
)
|
|
52
|
+
).sourceText,
|
|
53
|
+
).toBeUndefined();
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it("parses sourceInfos, carries queries, and shape-checks givens", async () => {
|
|
57
|
+
const data = {
|
|
58
|
+
sourceInfos: [
|
|
59
|
+
JSON.stringify({ name: "flights", schema: { fields: [] } }),
|
|
60
|
+
],
|
|
61
|
+
queries: [{ name: "q1" }],
|
|
62
|
+
givens: [
|
|
63
|
+
{ name: "ROLE", type: "string", default: "'analyst'" },
|
|
64
|
+
{ bogus: true },
|
|
65
|
+
],
|
|
66
|
+
};
|
|
67
|
+
const out = await fetchCompiledModel(
|
|
68
|
+
clientReturning(data),
|
|
69
|
+
"p",
|
|
70
|
+
"k",
|
|
71
|
+
"m.malloy",
|
|
72
|
+
);
|
|
73
|
+
expect(out.sourceInfos.map((s) => s.name)).toEqual(["flights"]);
|
|
74
|
+
expect(out.queries).toEqual([{ name: "q1" }]);
|
|
75
|
+
expect(out.givens.map((g) => [g.name, g.default])).toEqual([
|
|
76
|
+
["ROLE", "'analyst'"],
|
|
77
|
+
]);
|
|
78
|
+
});
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
describe("fetchCompiledModel access declarations", () => {
|
|
82
|
+
it("keeps each source's #(authorize) and #(access_filter) rather than dropping sources[]", async () => {
|
|
83
|
+
const data = {
|
|
84
|
+
sources: [
|
|
85
|
+
{ name: "open", authorize: [], accessFilter: [] },
|
|
86
|
+
{ name: "payroll", authorize: ["'finance' in $GROUPS"] },
|
|
87
|
+
{
|
|
88
|
+
name: "regional",
|
|
89
|
+
accessFilter: ["region in $GROUPS"],
|
|
90
|
+
authorize: null,
|
|
91
|
+
},
|
|
92
|
+
],
|
|
93
|
+
};
|
|
94
|
+
const out = await fetchCompiledModel(
|
|
95
|
+
clientReturning(data),
|
|
96
|
+
"p",
|
|
97
|
+
"k",
|
|
98
|
+
"m.malloy",
|
|
99
|
+
);
|
|
100
|
+
expect(out.access.get("payroll")).toEqual({
|
|
101
|
+
authorize: ["'finance' in $GROUPS"],
|
|
102
|
+
accessFilter: [],
|
|
103
|
+
});
|
|
104
|
+
expect(out.access.get("regional")).toEqual({
|
|
105
|
+
authorize: [],
|
|
106
|
+
accessFilter: ["region in $GROUPS"],
|
|
107
|
+
});
|
|
108
|
+
expect(out.access.has("open")).toBe(false);
|
|
109
|
+
});
|
|
110
|
+
});
|