@malloy-publisher/sdk 0.9.13 → 0.9.15
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/RenderedResult-BClN_PG6.cjs.js +154 -0
- package/dist/{RenderedResult-DnBtf-cI.es.js → RenderedResult-YBzVMVH2.es.js} +295 -234
- package/dist/{ServerProvider-BV7IoDMR.es.js → ServerProvider-D7neZcTk.es.js} +283 -265
- package/dist/ServerProvider-e2369oom.cjs.js +1 -0
- package/dist/builder/index.cjs.js +80 -79
- package/dist/builder/index.es.js +7430 -6548
- package/dist/client/api.d.ts +30 -6
- package/dist/client/index.cjs.js +1 -1
- package/dist/client/index.es.js +1 -1
- package/dist/components/Dashboard/DashboardGrid.d.ts +49 -1
- package/dist/components/Dashboard/DashboardTile.d.ts +5 -2
- package/dist/components/Dashboard/DashboardView.d.ts +9 -2
- package/dist/components/Dashboard/index.d.ts +1 -1
- package/dist/components/Dashboard/promoteMeasureRow.d.ts +5 -7
- package/dist/components/Dashboard/useStuck.d.ts +18 -12
- package/dist/components/RenderedResult/RenderedResult.d.ts +3 -2
- package/dist/components/RenderedResult/ResultContainer.d.ts +11 -1
- package/dist/components/RenderedResult/ResultPanel.d.ts +5 -1
- package/dist/components/RenderedResult/chartPresentation.d.ts +15 -0
- package/dist/components/RenderedResult/chartShapeProblem.d.ts +55 -0
- package/dist/components/RenderedResult/resultSizing.d.ts +42 -6
- package/dist/components/Report/Report.d.ts +7 -1
- package/dist/components/Report/ReportPage.d.ts +7 -0
- package/dist/components/Report/layoutRules.d.ts +45 -0
- package/dist/components/ReportBuilder/AddTileDialog.d.ts +2 -1
- package/dist/components/ReportBuilder/BuilderGrid.d.ts +14 -1
- package/dist/components/ReportBuilder/ChartPicker.d.ts +4 -2
- package/dist/components/ReportBuilder/EditorSurface.d.ts +17 -1
- package/dist/components/ReportBuilder/ReportEditor.d.ts +18 -0
- package/dist/components/ReportBuilder/TileFrame.d.ts +33 -1
- package/dist/components/ReportBuilder/TileMenu.d.ts +5 -2
- package/dist/components/ReportBuilder/catalog.d.ts +23 -0
- package/dist/components/ReportBuilder/chartLine.d.ts +38 -5
- package/dist/components/ReportBuilder/controls.d.ts +2 -2
- package/dist/components/ReportBuilder/document.d.ts +53 -5
- package/dist/components/ReportBuilder/imports.d.ts +7 -0
- package/dist/components/ReportBuilder/placement.d.ts +75 -0
- package/dist/components/ReportBuilder/preview.d.ts +59 -10
- package/dist/components/ReportBuilder/sortable.d.ts +3 -1
- package/dist/components/ReportBuilder/useTileHeight.d.ts +39 -0
- package/dist/components/ReportBuilder/useTileReorder.d.ts +27 -3
- package/dist/components/ReportBuilder/useTileResize.d.ts +8 -0
- package/dist/index.cjs.js +7 -7
- package/dist/index.es.js +3328 -3287
- package/dist/text/index.es.js +1 -1
- package/dist/{text-entry-72YFLeNk.es.js → text-entry-YKJZbRPo.es.js} +2 -2
- package/dist/theme/resolveTheme.d.ts +15 -0
- package/dist/{useCompiledDocument-B4QF1mIl.es.js → useCompiledDocument-BGk38oaW.es.js} +5097 -4805
- package/dist/useCompiledDocument-D9t5ry_t.cjs.js +142 -0
- package/dist/utils/blankQuoted.d.ts +15 -0
- package/package.json +1 -1
- package/src/components/Dashboard/DashboardGrid.tsx +127 -10
- package/src/components/Dashboard/DashboardTile.tsx +11 -1
- package/src/components/Dashboard/DashboardView.tsx +63 -10
- package/src/components/Dashboard/TileCard.tsx +6 -0
- package/src/components/Dashboard/index.ts +1 -0
- package/src/components/Dashboard/promoteMeasureRow.spec.ts +19 -0
- package/src/components/Dashboard/promoteMeasureRow.ts +15 -36
- package/src/components/Dashboard/tileHeight.spec.ts +72 -0
- package/src/components/Dashboard/useStuck.ts +76 -28
- package/src/components/Package/NewDocumentDialog.spec.tsx +20 -16
- package/src/components/Package/NewDocumentDialog.tsx +10 -11
- package/src/components/RenderedResult/RenderedResult.lifecycle.spec.tsx +167 -2
- package/src/components/RenderedResult/RenderedResult.tsx +21 -6
- package/src/components/RenderedResult/ResultContainer.tsx +60 -7
- package/src/components/RenderedResult/ResultPanel.tsx +21 -1
- package/src/components/RenderedResult/chartPresentation.spec.ts +153 -0
- package/src/components/RenderedResult/chartPresentation.ts +166 -0
- package/src/components/RenderedResult/chartShapeProblem.spec.tsx +245 -0
- package/src/components/RenderedResult/chartShapeProblem.ts +219 -0
- package/src/components/RenderedResult/resultSizing.spec.ts +75 -3
- package/src/components/RenderedResult/resultSizing.ts +65 -11
- package/src/components/Report/Report.spec.tsx +19 -0
- package/src/components/Report/Report.tsx +8 -1
- package/src/components/Report/ReportPage.tsx +13 -1
- package/src/components/Report/layoutRules.spec.ts +43 -3
- package/src/components/Report/layoutRules.ts +70 -0
- package/src/components/Report/tileOverflow.spec.tsx +162 -0
- package/src/components/ReportBuilder/AddTileDialog.spec.tsx +2 -2
- package/src/components/ReportBuilder/AddTileDialog.tsx +11 -6
- package/src/components/ReportBuilder/BuilderBar.tsx +1 -1
- package/src/components/ReportBuilder/BuilderDialogs.tsx +20 -0
- package/src/components/ReportBuilder/BuilderGrid.tsx +126 -20
- package/src/components/ReportBuilder/ChartPicker.spec.ts +91 -4
- package/src/components/ReportBuilder/ChartPicker.tsx +45 -10
- package/src/components/ReportBuilder/DrillDialog.tsx +8 -2
- package/src/components/ReportBuilder/EditorSurface.tsx +110 -28
- package/src/components/ReportBuilder/FilterDialog.tsx +17 -8
- package/src/components/ReportBuilder/FilterStrip.tsx +2 -1
- package/src/components/ReportBuilder/ReportBuilder.charts.spec.tsx +193 -2
- package/src/components/ReportBuilder/ReportBuilder.controls.spec.tsx +4 -2
- package/src/components/ReportBuilder/ReportBuilder.notebook.spec.tsx +2 -2
- package/src/components/ReportBuilder/ReportBuilder.spec.tsx +143 -2
- package/src/components/ReportBuilder/ReportBuilder.tsx +73 -19
- package/src/components/ReportBuilder/ReportEditor.fileModel.spec.tsx +178 -0
- package/src/components/ReportBuilder/ReportEditor.modelFilters.spec.tsx +3 -5
- package/src/components/ReportBuilder/ReportEditor.queryVersion.spec.tsx +292 -0
- package/src/components/ReportBuilder/ReportEditor.readOnlyTitle.spec.tsx +92 -0
- package/src/components/ReportBuilder/ReportEditor.tsx +35 -0
- package/src/components/ReportBuilder/TileFrame.tsx +251 -6
- package/src/components/ReportBuilder/TileMenu.tsx +259 -20
- package/src/components/ReportBuilder/ViewPicker.tsx +4 -3
- package/src/components/ReportBuilder/addTileToDocument.spec.ts +34 -0
- package/src/components/ReportBuilder/addTileToDocument.ts +26 -1
- package/src/components/ReportBuilder/catalog.aggregate.spec.ts +137 -1
- package/src/components/ReportBuilder/catalog.ts +90 -37
- package/src/components/ReportBuilder/chartLine.spec.ts +42 -5
- package/src/components/ReportBuilder/chartLine.ts +80 -2
- package/src/components/ReportBuilder/controls.ts +4 -3
- package/src/components/ReportBuilder/document.ts +65 -7
- package/src/components/ReportBuilder/emptyStart.spec.ts +1 -1
- package/src/components/ReportBuilder/entryLayout.spec.ts +151 -0
- package/src/components/ReportBuilder/imports.ts +1 -1
- package/src/components/ReportBuilder/invariants.spec.ts +18 -10
- package/src/components/ReportBuilder/layout.ts +3 -0
- package/src/components/ReportBuilder/placement.spec.ts +193 -0
- package/src/components/ReportBuilder/placement.ts +250 -0
- package/src/components/ReportBuilder/preview.shapes.spec.ts +46 -6
- package/src/components/ReportBuilder/preview.spec.ts +151 -11
- package/src/components/ReportBuilder/preview.ts +203 -40
- package/src/components/ReportBuilder/preview.unsaved.compile.spec.ts +190 -0
- package/src/components/ReportBuilder/readDocument.spec.ts +46 -2
- package/src/components/ReportBuilder/readDocument.ts +194 -23
- package/src/components/ReportBuilder/scannerShapes.spec.ts +10 -10
- package/src/components/ReportBuilder/sortable.tsx +4 -0
- package/src/components/ReportBuilder/spliceDocument.chart.spec.ts +1 -1
- package/src/components/ReportBuilder/spliceDocument.color.spec.ts +80 -0
- package/src/components/ReportBuilder/spliceDocument.colspan.spec.ts +78 -0
- package/src/components/ReportBuilder/spliceDocument.spec.ts +110 -18
- package/src/components/ReportBuilder/spliceDocument.ts +139 -52
- package/src/components/ReportBuilder/statementExtent.spec.ts +3 -3
- package/src/components/ReportBuilder/testing/reportGivenOrder.json +42 -0
- package/src/components/ReportBuilder/textTiles.spec.ts +2 -2
- package/src/components/ReportBuilder/tileDisplayTitle.ts +2 -2
- package/src/components/ReportBuilder/useTileEditing.ts +16 -3
- package/src/components/ReportBuilder/useTileHeight.ts +149 -0
- package/src/components/ReportBuilder/useTileReorder.ts +143 -6
- package/src/components/ReportBuilder/useTileResize.ts +16 -1
- package/src/components/given/FilterButton.spec.tsx +27 -11
- package/src/components/given/FilterButton.tsx +19 -6
- package/src/theme/resolveTheme.spec.ts +72 -1
- package/src/theme/resolveTheme.ts +43 -0
- package/src/utils/blankQuoted.spec.ts +50 -0
- package/src/utils/blankQuoted.ts +32 -0
- package/src/utils/malloyText.ts +2 -1
- package/dist/RenderedResult-DtQtNV0d.cjs.js +0 -153
- package/dist/ServerProvider-BIFPCzTD.cjs.js +0 -1
- package/dist/useCompiledDocument-D0OAMejE.cjs.js +0 -142
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
// Copyright (c) Credible Data Inc.
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
|
|
4
|
+
import { render, screen } from "@testing-library/react";
|
|
5
|
+
import { describe, expect, it } from "bun:test";
|
|
6
|
+
import type { QueryResultState } from "../../hooks/useQueryResult";
|
|
7
|
+
import {
|
|
8
|
+
chartShapeGap,
|
|
9
|
+
chartShapeProblem,
|
|
10
|
+
marksCalculation,
|
|
11
|
+
requestedChart,
|
|
12
|
+
} from "./chartShapeProblem";
|
|
13
|
+
import { ResultPanel } from "./ResultPanel";
|
|
14
|
+
|
|
15
|
+
// Fields as the server returns them: every output column is a "dimension",
|
|
16
|
+
// and a total is marked by `calculation` in its internal note.
|
|
17
|
+
const total = (name: string) => ({
|
|
18
|
+
name,
|
|
19
|
+
kind: "dimension",
|
|
20
|
+
type: { kind: "number_type" },
|
|
21
|
+
annotations: [{ value: '#(malloy) reference_id = "x" calculation\n' }],
|
|
22
|
+
});
|
|
23
|
+
const groupBy = (name: string) => ({
|
|
24
|
+
name,
|
|
25
|
+
kind: "dimension",
|
|
26
|
+
type: { kind: "string_type" },
|
|
27
|
+
annotations: [],
|
|
28
|
+
});
|
|
29
|
+
const result = (fields: object[], lines: string[]) =>
|
|
30
|
+
JSON.stringify({
|
|
31
|
+
schema: { fields },
|
|
32
|
+
data: { kind: "array_cell", array_value: [] },
|
|
33
|
+
annotations: lines.map((value) => ({ value })),
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
describe("requestedChart", () => {
|
|
37
|
+
it("reads the lines in order, a later one winning and a negation taking one off", () => {
|
|
38
|
+
expect(requestedChart(["# line_chart\n"])).toBe("line_chart");
|
|
39
|
+
expect(requestedChart(["# bar_chart\n", "# -bar_chart -viz\n"])).toBe(
|
|
40
|
+
undefined,
|
|
41
|
+
);
|
|
42
|
+
expect(requestedChart(["# -bar_chart -viz line_chart\n"])).toBe(
|
|
43
|
+
"line_chart",
|
|
44
|
+
);
|
|
45
|
+
expect(requestedChart(["# viz=bar\n"])).toBe("bar_chart");
|
|
46
|
+
expect(requestedChart(['#(doc) "a line_chart"\n'])).toBe(undefined);
|
|
47
|
+
expect(requestedChart(['# label="bar_chart" line_chart\n'])).toBe(
|
|
48
|
+
"line_chart",
|
|
49
|
+
);
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
// Result annotations come from the model. A line of one quote and many
|
|
53
|
+
// escaped quotes made the quoted-value regex this used quadratic.
|
|
54
|
+
it("reads a long unclosed quoted value in linear time", () => {
|
|
55
|
+
const hostile = `# line_chart label="${'\\"'.repeat(200_000)}\n`;
|
|
56
|
+
const started = performance.now();
|
|
57
|
+
expect(requestedChart([hostile])).toBe("line_chart");
|
|
58
|
+
expect(
|
|
59
|
+
chartShapeProblem(result([groupBy("month")], [hostile])),
|
|
60
|
+
).toStartWith("This chart has no total for the y-axis.");
|
|
61
|
+
expect(performance.now() - started).toBeLessThan(500);
|
|
62
|
+
});
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
describe("marksCalculation", () => {
|
|
66
|
+
it("reads calculation as one of the note's own properties only", () => {
|
|
67
|
+
expect(
|
|
68
|
+
marksCalculation(
|
|
69
|
+
'#(malloy) reference_id = "x" calculation drill_expression { kind = field_reference name = n }\n',
|
|
70
|
+
),
|
|
71
|
+
).toBe(true);
|
|
72
|
+
// A group-by literally named `calculation`: the word is in its drill
|
|
73
|
+
// expression and in a quoted value, never a property of the note.
|
|
74
|
+
expect(
|
|
75
|
+
marksCalculation(
|
|
76
|
+
'#(malloy) reference_id = "calculation" drill_expression { kind = field_reference name = calculation }\n',
|
|
77
|
+
),
|
|
78
|
+
).toBe(false);
|
|
79
|
+
expect(marksCalculation("#(malloy) label = calculation\n")).toBe(false);
|
|
80
|
+
expect(marksCalculation("# calculation\n")).toBe(false);
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it("counts a group-by named calculation as a group-by, so a line chart over it draws", () => {
|
|
84
|
+
const named = {
|
|
85
|
+
name: "calculation",
|
|
86
|
+
kind: "dimension",
|
|
87
|
+
type: { kind: "string_type" },
|
|
88
|
+
annotations: [
|
|
89
|
+
{
|
|
90
|
+
value: "#(malloy) drill_expression { kind = field_reference name = calculation }\n",
|
|
91
|
+
},
|
|
92
|
+
],
|
|
93
|
+
};
|
|
94
|
+
expect(
|
|
95
|
+
chartShapeProblem(
|
|
96
|
+
result([named, total("revenue")], ["# line_chart\n"]),
|
|
97
|
+
),
|
|
98
|
+
).toBeUndefined();
|
|
99
|
+
});
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
describe("chartShapeGap", () => {
|
|
103
|
+
it("names what a chart that plots rows is missing, and nothing for any other chart", () => {
|
|
104
|
+
expect(chartShapeGap("line_chart", { groupBys: 0, totals: 2 })).toBe(
|
|
105
|
+
"no_x_axis",
|
|
106
|
+
);
|
|
107
|
+
expect(chartShapeGap("bar_chart", { groupBys: 1, totals: 0 })).toBe(
|
|
108
|
+
"no_y_axis",
|
|
109
|
+
);
|
|
110
|
+
expect(
|
|
111
|
+
chartShapeGap("scatter_chart", { groupBys: 2, totals: 0 }),
|
|
112
|
+
).toBeUndefined();
|
|
113
|
+
expect(chartShapeGap("combo_chart", { groupBys: 1, totals: 3 })).toBe(
|
|
114
|
+
"combo_totals",
|
|
115
|
+
);
|
|
116
|
+
expect(
|
|
117
|
+
chartShapeGap("combo_chart", { groupBys: 1, totals: 2 }),
|
|
118
|
+
).toBeUndefined();
|
|
119
|
+
expect(
|
|
120
|
+
chartShapeGap("big_value", { groupBys: 0, totals: 0 }),
|
|
121
|
+
).toBeUndefined();
|
|
122
|
+
});
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
describe("chartShapeProblem", () => {
|
|
126
|
+
const kpis = [total("revenue"), total("order_count")];
|
|
127
|
+
|
|
128
|
+
it("names the missing x-axis on a line chart of totals only, in plain words", () => {
|
|
129
|
+
expect(chartShapeProblem(result(kpis, ["# line_chart\n"]))).toBe(
|
|
130
|
+
"This chart has no field for the x-axis. A line chart needs a group by, such as a date or a category. Show it as big numbers or a table instead, or add a group by to its query.",
|
|
131
|
+
);
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
it("names the missing y-axis on a bar chart with no total", () => {
|
|
135
|
+
expect(
|
|
136
|
+
chartShapeProblem(result([groupBy("carrier")], ["# bar_chart\n"])),
|
|
137
|
+
).toBe(
|
|
138
|
+
"This chart has no total for the y-axis. A bar chart needs at least one aggregate, such as a count or a sum.",
|
|
139
|
+
);
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
it("counts the totals a bar and line chart was given", () => {
|
|
143
|
+
expect(
|
|
144
|
+
chartShapeProblem(
|
|
145
|
+
result(
|
|
146
|
+
[groupBy("carrier"), ...kpis, total("distance")],
|
|
147
|
+
["# combo_chart\n"],
|
|
148
|
+
),
|
|
149
|
+
),
|
|
150
|
+
).toBe(
|
|
151
|
+
"A bar and line chart needs exactly two totals, the first drawn as bars and the second as a line. This one has 3.",
|
|
152
|
+
);
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
it("says nothing when the chart can draw, when no chart is asked for, or when channels are placed by hand", () => {
|
|
156
|
+
expect(
|
|
157
|
+
chartShapeProblem(
|
|
158
|
+
result([groupBy("carrier"), ...kpis], ["# line_chart\n"]),
|
|
159
|
+
),
|
|
160
|
+
).toBeUndefined();
|
|
161
|
+
expect(chartShapeProblem(result(kpis, []))).toBeUndefined();
|
|
162
|
+
expect(
|
|
163
|
+
chartShapeProblem(
|
|
164
|
+
result(
|
|
165
|
+
[{ ...total("revenue"), annotations: [{ value: "# x\n" }] }],
|
|
166
|
+
["# line_chart\n"],
|
|
167
|
+
),
|
|
168
|
+
),
|
|
169
|
+
).toBeUndefined();
|
|
170
|
+
expect(chartShapeProblem("not json")).toBeUndefined();
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
// The renderer reads channels written on the chart tag itself, and counts
|
|
174
|
+
// nothing then: the form its own combo error recommends.
|
|
175
|
+
it("leaves channels placed on the chart tag to the renderer", () => {
|
|
176
|
+
expect(
|
|
177
|
+
chartShapeProblem(
|
|
178
|
+
result(
|
|
179
|
+
[groupBy("carrier"), ...kpis, total("distance")],
|
|
180
|
+
["# combo_chart { y=revenue y2=order_count }\n"],
|
|
181
|
+
),
|
|
182
|
+
),
|
|
183
|
+
).toBeUndefined();
|
|
184
|
+
expect(
|
|
185
|
+
chartShapeProblem(
|
|
186
|
+
result(
|
|
187
|
+
[groupBy("month"), groupBy("revenue")],
|
|
188
|
+
["# line_chart { y=revenue }\n"],
|
|
189
|
+
),
|
|
190
|
+
),
|
|
191
|
+
).toBeUndefined();
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
// `viz=` wins over every chart tag in the renderer, so a table drawn over
|
|
195
|
+
// an inherited `# bar_chart` is not a bar chart missing its x-axis.
|
|
196
|
+
it("reads a viz= it does not know as no chart that plots rows", () => {
|
|
197
|
+
expect(requestedChart(["# bar_chart\n", "# viz=table\n"])).toBe(
|
|
198
|
+
"viz=table",
|
|
199
|
+
);
|
|
200
|
+
expect(
|
|
201
|
+
chartShapeProblem(result(kpis, ["# bar_chart\n", "# viz=table\n"])),
|
|
202
|
+
).toBeUndefined();
|
|
203
|
+
// A quoted value cannot be read, so it is left to the renderer too.
|
|
204
|
+
expect(
|
|
205
|
+
chartShapeProblem(result(kpis, ["# bar_chart\n", '# viz="bar"\n'])),
|
|
206
|
+
).toBeUndefined();
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
// The renderer's scatter plots the first two columns, whatever they are.
|
|
210
|
+
it("lets a scatter chart plot two totals against each other", () => {
|
|
211
|
+
expect(
|
|
212
|
+
chartShapeGap("scatter_chart", { groupBys: 0, totals: 2 }),
|
|
213
|
+
).toBeUndefined();
|
|
214
|
+
expect(chartShapeGap("scatter_chart", { groupBys: 0, totals: 1 })).toBe(
|
|
215
|
+
"no_x_axis",
|
|
216
|
+
);
|
|
217
|
+
expect(
|
|
218
|
+
chartShapeProblem(result(kpis, ["# scatter_chart\n"])),
|
|
219
|
+
).toBeUndefined();
|
|
220
|
+
});
|
|
221
|
+
});
|
|
222
|
+
|
|
223
|
+
describe("ResultPanel with a chart its result cannot fill", () => {
|
|
224
|
+
it("says what is missing in place of the renderer's error", () => {
|
|
225
|
+
const state = {
|
|
226
|
+
isSuccess: true,
|
|
227
|
+
isError: false,
|
|
228
|
+
fetchStatus: "idle",
|
|
229
|
+
data: {
|
|
230
|
+
data: {
|
|
231
|
+
result: result(
|
|
232
|
+
[total("revenue"), total("order_count")],
|
|
233
|
+
["# line_chart\n"],
|
|
234
|
+
),
|
|
235
|
+
},
|
|
236
|
+
},
|
|
237
|
+
} as unknown as QueryResultState;
|
|
238
|
+
render(<ResultPanel state={state} context="orders -> kpis" />);
|
|
239
|
+
expect(
|
|
240
|
+
screen
|
|
241
|
+
.getByRole("status")
|
|
242
|
+
.textContent?.startsWith("This chart has no field for the x-axis."),
|
|
243
|
+
).toBe(true);
|
|
244
|
+
});
|
|
245
|
+
});
|
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
// Copyright (c) Credible Data Inc.
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* What stops a result's chart from drawing, said in the reader's words, or
|
|
6
|
+
* undefined when nothing does.
|
|
7
|
+
*
|
|
8
|
+
* The renderer refuses a chart its result cannot fill, but says so in its own
|
|
9
|
+
* terms ("Line chart settings error: Malloy Line Chart: No dimensions found.
|
|
10
|
+
* A line chart must have at least 1 dimension for the x axis."), which a
|
|
11
|
+
* reader who never wrote the query cannot act on. This reads the same two
|
|
12
|
+
* facts the renderer checks, which chart the result asks for and how many of
|
|
13
|
+
* its columns are group-bys and totals, so the panel can say what is missing
|
|
14
|
+
* before the renderer is asked.
|
|
15
|
+
*
|
|
16
|
+
* Conservative by design: anything it cannot read (a channel placed by hand,
|
|
17
|
+
* on a field or on the chart tag, a `viz=` it does not know, a result that
|
|
18
|
+
* does not parse) is left to the renderer.
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
import { blankQuoted } from "../../utils/blankQuoted";
|
|
22
|
+
|
|
23
|
+
/** The charts that plot rows along an x axis, by their tag and their `viz=` name. */
|
|
24
|
+
const ROW_CHARTS: Record<string, string> = {
|
|
25
|
+
line_chart: "A line chart",
|
|
26
|
+
bar_chart: "A bar chart",
|
|
27
|
+
scatter_chart: "A scatter chart",
|
|
28
|
+
combo_chart: "A bar and line chart",
|
|
29
|
+
};
|
|
30
|
+
const VIZ_NAMES: Record<string, string> = {
|
|
31
|
+
line: "line_chart",
|
|
32
|
+
bar: "bar_chart",
|
|
33
|
+
scatter: "scatter_chart",
|
|
34
|
+
combo: "combo_chart",
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
/** A field tag that names a chart channel: the author placed fields by hand. */
|
|
38
|
+
const CHANNEL_TAGS = new Set(["x", "y", "y2", "series"]);
|
|
39
|
+
|
|
40
|
+
/** An output column as a result's schema, or a view's in the catalog, describes it. */
|
|
41
|
+
export interface ResultField {
|
|
42
|
+
kind?: string;
|
|
43
|
+
type?: { kind?: string };
|
|
44
|
+
annotations?: Array<{ value?: string }>;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
interface ResultShape {
|
|
48
|
+
schema?: { fields?: ResultField[] };
|
|
49
|
+
annotations?: Array<{ value?: string }>;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** The words of a `#` tag line, quoted values blanked; doc and internal notes have none. */
|
|
53
|
+
function tagWords(line: string): string[] {
|
|
54
|
+
const text = line.trim();
|
|
55
|
+
if (!text.startsWith("#") || text.startsWith("#(") || text.startsWith('#"'))
|
|
56
|
+
return [];
|
|
57
|
+
return blankQuoted(text.slice(1))
|
|
58
|
+
.split(/[\s{}[\],]+/)
|
|
59
|
+
.filter(Boolean);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* The chart the result's lines ask for, read in order: a later line overrides,
|
|
64
|
+
* `-tag` takes one off.
|
|
65
|
+
*
|
|
66
|
+
* A `viz=` wins over every chart tag, as it does in the renderer, whatever its
|
|
67
|
+
* value. One this does not know (`viz=table`, or a quoted value, blanked to
|
|
68
|
+
* `""`) comes back as written, which is no chart that plots rows, so the
|
|
69
|
+
* result is left to the renderer rather than read as an inherited chart tag.
|
|
70
|
+
*/
|
|
71
|
+
export function requestedChart(lines: readonly string[]): string | undefined {
|
|
72
|
+
const on: string[] = [];
|
|
73
|
+
let viz: string | undefined;
|
|
74
|
+
for (const line of lines)
|
|
75
|
+
for (const word of tagWords(line)) {
|
|
76
|
+
if (word.startsWith("-")) {
|
|
77
|
+
const name = word.slice(1);
|
|
78
|
+
if (name === "viz") viz = undefined;
|
|
79
|
+
const at = on.indexOf(name);
|
|
80
|
+
if (at >= 0) on.splice(at, 1);
|
|
81
|
+
} else if (word.startsWith("viz=")) {
|
|
82
|
+
viz = VIZ_NAMES[word.slice(4)] ?? word;
|
|
83
|
+
} else if (ROW_CHARTS[word] !== undefined || word === "big_value") {
|
|
84
|
+
const at = on.indexOf(word);
|
|
85
|
+
if (at >= 0) on.splice(at, 1);
|
|
86
|
+
on.push(word);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
return viz ?? on.at(-1);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** Whether the lines ask for a chart that plots rows (bar, line, scatter, bar and line), not big numbers or a table. */
|
|
93
|
+
export function requestsRowChart(lines: readonly string[]): boolean {
|
|
94
|
+
const chart = requestedChart(lines);
|
|
95
|
+
return chart !== undefined && ROW_CHARTS[chart] !== undefined;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** A result's or a view's output columns by role: group-bys for the x axis, totals for the y axis. */
|
|
99
|
+
export interface ColumnCounts {
|
|
100
|
+
groupBys: number;
|
|
101
|
+
totals: number;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** What a chart that plots rows is missing: a group-by, a total, or exactly two totals for a bar and line chart. */
|
|
105
|
+
export type ChartShapeGap = "no_x_axis" | "no_y_axis" | "combo_totals";
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* What keeps `chart` from drawing columns of this shape, or undefined when
|
|
109
|
+
* nothing does or `chart` does not plot rows. The one rule both the builder's
|
|
110
|
+
* chart picker and the result panel apply. A scatter chart plots its first
|
|
111
|
+
* two columns against each other, whatever they are, so two group-bys draw,
|
|
112
|
+
* and so do two totals with no group-by.
|
|
113
|
+
*/
|
|
114
|
+
export function chartShapeGap(
|
|
115
|
+
chart: string,
|
|
116
|
+
{ groupBys, totals }: ColumnCounts,
|
|
117
|
+
): ChartShapeGap | undefined {
|
|
118
|
+
if (ROW_CHARTS[chart] === undefined) return undefined;
|
|
119
|
+
if (chart === "scatter_chart")
|
|
120
|
+
return groupBys === 0 && totals < 2 ? "no_x_axis" : undefined;
|
|
121
|
+
if (groupBys === 0) return "no_x_axis";
|
|
122
|
+
if (chart === "combo_chart")
|
|
123
|
+
return totals === 2 ? undefined : "combo_totals";
|
|
124
|
+
if (totals === 0) return "no_y_axis";
|
|
125
|
+
return undefined;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Whether a `#(malloy)` note marks its column a total: `calculation` as one of
|
|
130
|
+
* the note's own properties. The note also carries a `drill_expression { … }`
|
|
131
|
+
* whose `name = …` can be a group-by literally named `calculation`, so only
|
|
132
|
+
* words outside any `{…}` or `[…]` and outside quotes count, and never a
|
|
133
|
+
* property's value.
|
|
134
|
+
*/
|
|
135
|
+
export function marksCalculation(note: string): boolean {
|
|
136
|
+
const text = note.trim();
|
|
137
|
+
if (!text.startsWith("#(malloy)")) return false;
|
|
138
|
+
const body = blankQuoted(text.slice("#(malloy)".length));
|
|
139
|
+
let top = "";
|
|
140
|
+
let depth = 0;
|
|
141
|
+
for (const ch of body) {
|
|
142
|
+
if (ch === "{" || ch === "[") depth++;
|
|
143
|
+
else if (ch === "}" || ch === "]") depth = Math.max(0, depth - 1);
|
|
144
|
+
else if (depth === 0) top += ch === "=" ? " = " : ch;
|
|
145
|
+
}
|
|
146
|
+
const words = top.split(/\s+/);
|
|
147
|
+
return words.some(
|
|
148
|
+
(word, at) => word === "calculation" && words[at - 1] !== "=",
|
|
149
|
+
);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Whether an output column is a total. The one definition: the result panel,
|
|
154
|
+
* the builder's catalog and the KPI promotion all read a column's role here.
|
|
155
|
+
*/
|
|
156
|
+
export const isTotalColumn = (field: ResultField | undefined): boolean =>
|
|
157
|
+
field !== undefined &&
|
|
158
|
+
(field.kind === "measure" ||
|
|
159
|
+
(field.annotations ?? []).some((note) =>
|
|
160
|
+
marksCalculation(note?.value ?? ""),
|
|
161
|
+
));
|
|
162
|
+
|
|
163
|
+
/** Whether an output column is a nest, which no chart axis takes. */
|
|
164
|
+
export const isNestColumn = (field: ResultField): boolean =>
|
|
165
|
+
field.kind === "join" || field.type?.kind === "array_type";
|
|
166
|
+
|
|
167
|
+
/** Output columns by role: a group-by is neither a total nor a nest. */
|
|
168
|
+
export function columnCounts(fields: readonly ResultField[]): ColumnCounts {
|
|
169
|
+
const totals = fields.filter(isTotalColumn).length;
|
|
170
|
+
const groupBys = fields.filter(
|
|
171
|
+
(field) => !isTotalColumn(field) && !isNestColumn(field),
|
|
172
|
+
).length;
|
|
173
|
+
return { groupBys, totals };
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/** A tag word that places a field on a chart channel: `y=revenue` on the chart tag, or a bare `y` on a field. */
|
|
177
|
+
const placesChannel = (word: string) => CHANNEL_TAGS.has(word.split("=")[0]);
|
|
178
|
+
|
|
179
|
+
export function chartShapeProblem(
|
|
180
|
+
result: string | undefined,
|
|
181
|
+
): string | undefined {
|
|
182
|
+
if (!result) return undefined;
|
|
183
|
+
let parsed: ResultShape;
|
|
184
|
+
try {
|
|
185
|
+
parsed = JSON.parse(result) as ResultShape;
|
|
186
|
+
} catch {
|
|
187
|
+
return undefined;
|
|
188
|
+
}
|
|
189
|
+
const fields = parsed?.schema?.fields;
|
|
190
|
+
if (!Array.isArray(fields) || fields.length === 0) return undefined;
|
|
191
|
+
const lines = (parsed.annotations ?? [])
|
|
192
|
+
.map((note) => note?.value)
|
|
193
|
+
.filter((value): value is string => typeof value === "string");
|
|
194
|
+
const chart = requestedChart(lines);
|
|
195
|
+
const name = chart && ROW_CHARTS[chart];
|
|
196
|
+
if (!chart || !name) return undefined;
|
|
197
|
+
// Placed by hand, on a field (`# y`) or on the chart tag itself
|
|
198
|
+
// (`# combo_chart { y=revenue y2=orders }`): the renderer then reads those
|
|
199
|
+
// channels rather than counting columns, so a count says nothing.
|
|
200
|
+
const handPlaced =
|
|
201
|
+
lines.some((line) => tagWords(line).some(placesChannel)) ||
|
|
202
|
+
fields.some((field) =>
|
|
203
|
+
(field.annotations ?? []).some((note) =>
|
|
204
|
+
tagWords(note.value ?? "").some(placesChannel),
|
|
205
|
+
),
|
|
206
|
+
);
|
|
207
|
+
if (handPlaced) return undefined;
|
|
208
|
+
const { groupBys, totals } = columnCounts(fields);
|
|
209
|
+
switch (chartShapeGap(chart, { groupBys, totals })) {
|
|
210
|
+
case "no_x_axis":
|
|
211
|
+
return `This chart has no field for the x-axis. ${name} needs a group by, such as a date or a category. Show it as big numbers or a table instead, or add a group by to its query.`;
|
|
212
|
+
case "combo_totals":
|
|
213
|
+
return `${name} needs exactly two totals, the first drawn as bars and the second as a line. This one has ${totals}.`;
|
|
214
|
+
case "no_y_axis":
|
|
215
|
+
return `This chart has no total for the y-axis. ${name} needs at least one aggregate, such as a count or a sum.`;
|
|
216
|
+
default:
|
|
217
|
+
return undefined;
|
|
218
|
+
}
|
|
219
|
+
}
|
|
@@ -7,9 +7,11 @@ import {
|
|
|
7
7
|
contentNodeDepth,
|
|
8
8
|
INITIAL_RENDER_HEIGHT,
|
|
9
9
|
initialResultHeight,
|
|
10
|
+
LINEAR_TABLE_MAX_HEIGHT,
|
|
10
11
|
measureContentHeight,
|
|
11
12
|
remeasuresAfterReady,
|
|
12
13
|
resolveResultHeight,
|
|
14
|
+
resultCap,
|
|
13
15
|
resultSizing,
|
|
14
16
|
UNCAPPED_CONTAINER_HEIGHT,
|
|
15
17
|
} from "./resultSizing";
|
|
@@ -20,7 +22,7 @@ describe("resultSizing", () => {
|
|
|
20
22
|
// spellings "bar_chart" and "line_chart" never come back from it. Getting
|
|
21
23
|
// this wrong is invisible: the chart is simply measured, and a `# line_chart`
|
|
22
24
|
// tile quietly shrinks to the inset height it drew itself at.
|
|
23
|
-
it.each(["bar", "line", "chart", "scatter_chart"])(
|
|
25
|
+
it.each(["bar", "line", "chart", "scatter_chart", "combo"])(
|
|
24
26
|
"calls %s container-sized",
|
|
25
27
|
(renderAs) => {
|
|
26
28
|
expect(resultSizing(renderAs)).toBe("container");
|
|
@@ -112,7 +114,7 @@ describe("remeasuresAfterReady", () => {
|
|
|
112
114
|
// The one root re-measuring is not safe for, and the reason it used to be
|
|
113
115
|
// tables only: a chart insets itself inside its box, so feeding its height
|
|
114
116
|
// back would ratchet the container down every pass. It is never measured now.
|
|
115
|
-
it.each(["bar", "line", "chart", "scatter_chart"])(
|
|
117
|
+
it.each(["bar", "line", "chart", "scatter_chart", "combo"])(
|
|
116
118
|
"never measures %s at all",
|
|
117
119
|
(renderAs) => {
|
|
118
120
|
expect(remeasuresAfterReady(renderAs)).toBe(false);
|
|
@@ -246,6 +248,22 @@ describe("resolveResultHeight", () => {
|
|
|
246
248
|
it("stays at the seed while a content-sized root measures zero", () => {
|
|
247
249
|
expect(at("content", 0, 400)).toBe(400);
|
|
248
250
|
});
|
|
251
|
+
|
|
252
|
+
// An uncapped report tile (a linear report's) first paints at the tile
|
|
253
|
+
// cap rather than 2000px, and then grows or shrinks to what it measures.
|
|
254
|
+
it("first paints an uncapped tile at the height it is given, and measures past it", () => {
|
|
255
|
+
const tile = (contentHeight: number | undefined) =>
|
|
256
|
+
resolveResultHeight({
|
|
257
|
+
sizing: contentHeight === undefined ? undefined : "content",
|
|
258
|
+
contentHeight,
|
|
259
|
+
maxHeight: undefined,
|
|
260
|
+
firstPaintHeight: 400,
|
|
261
|
+
});
|
|
262
|
+
expect(tile(undefined)).toBe(400);
|
|
263
|
+
expect(tile(900)).toBe(900);
|
|
264
|
+
expect(tile(120)).toBe(120);
|
|
265
|
+
expect(initialResultHeight(300, 400)).toBe(300);
|
|
266
|
+
});
|
|
249
267
|
});
|
|
250
268
|
|
|
251
269
|
describe("resolveResultHeight with a cell to fill", () => {
|
|
@@ -269,10 +287,22 @@ describe("resolveResultHeight with a cell to fill", () => {
|
|
|
269
287
|
expect(fill("content", 4000, 400, 520)).toBe(520);
|
|
270
288
|
});
|
|
271
289
|
|
|
272
|
-
it("leaves a chart at its cap", () => {
|
|
290
|
+
it("leaves a chart at its cap in a taller cell", () => {
|
|
273
291
|
expect(fill("container", undefined, 400, 520)).toBe(400);
|
|
274
292
|
});
|
|
275
293
|
|
|
294
|
+
// A tile with a set rowspan hands its chart less room than the cap, which
|
|
295
|
+
// counts the tile's heading and padding too. Drawn at the cap, the chart ran
|
|
296
|
+
// past the cell and its x-axis labels were clipped.
|
|
297
|
+
it("shrinks a chart to a cell shorter than its cap", () => {
|
|
298
|
+
expect(fill("container", undefined, 464, 330)).toBe(330);
|
|
299
|
+
expect(fill("container", undefined, undefined, 250)).toBe(250);
|
|
300
|
+
});
|
|
301
|
+
|
|
302
|
+
it("ignores a cell not measured yet", () => {
|
|
303
|
+
expect(fill("container", undefined, 400, 0)).toBe(400);
|
|
304
|
+
});
|
|
305
|
+
|
|
276
306
|
it("keeps the seed for a table not measured yet", () => {
|
|
277
307
|
expect(fill("content", undefined, 400, 520)).toBe(400);
|
|
278
308
|
expect(fill("content", 0, 400, 520)).toBe(400);
|
|
@@ -315,3 +345,45 @@ describe("resolveResultHeight with a cell to fill", () => {
|
|
|
315
345
|
expect(basis(100)).toBe(100);
|
|
316
346
|
});
|
|
317
347
|
});
|
|
348
|
+
|
|
349
|
+
describe("resultCap", () => {
|
|
350
|
+
const cap = (
|
|
351
|
+
renderAs: string | undefined,
|
|
352
|
+
maxHeight: number | undefined,
|
|
353
|
+
tableMaxHeight: number | undefined,
|
|
354
|
+
) => resultCap({ renderAs, maxHeight, tableMaxHeight });
|
|
355
|
+
|
|
356
|
+
// A linear report: tiles are uncapped, and a table stops at 700 and scrolls.
|
|
357
|
+
it("holds a table root to the table cap when nothing else caps it", () => {
|
|
358
|
+
expect(cap("table", undefined, LINEAR_TABLE_MAX_HEIGHT)).toBe(700);
|
|
359
|
+
});
|
|
360
|
+
|
|
361
|
+
it("takes the lower of the two caps for a table", () => {
|
|
362
|
+
expect(cap("table", 400, 700)).toBe(400);
|
|
363
|
+
expect(cap("table", 900, 700)).toBe(700);
|
|
364
|
+
});
|
|
365
|
+
|
|
366
|
+
// KPI cards, lists, maps and charts keep growing to fit in a linear report.
|
|
367
|
+
it.each(["big_value", "list", "shape_map", "bar", "line"])(
|
|
368
|
+
"leaves a %s root at the tile cap",
|
|
369
|
+
(renderAs) => {
|
|
370
|
+
expect(cap(renderAs, undefined, 700)).toBeUndefined();
|
|
371
|
+
expect(cap(renderAs, 400, 700)).toBe(400);
|
|
372
|
+
},
|
|
373
|
+
);
|
|
374
|
+
|
|
375
|
+
// The renderer caps the tables inside a `# dashboard` itself, at its own
|
|
376
|
+
// default or the author's `table.max_height`, `none` included.
|
|
377
|
+
it("leaves a # dashboard root to the renderer's own table cap", () => {
|
|
378
|
+
expect(cap("dashboard", undefined, 700)).toBeUndefined();
|
|
379
|
+
});
|
|
380
|
+
|
|
381
|
+
it("does not apply the table cap before the root is known", () => {
|
|
382
|
+
expect(cap(undefined, undefined, 700)).toBeUndefined();
|
|
383
|
+
});
|
|
384
|
+
|
|
385
|
+
it("adds nothing without a table cap", () => {
|
|
386
|
+
expect(cap("table", 400, undefined)).toBe(400);
|
|
387
|
+
expect(cap("table", undefined, undefined)).toBeUndefined();
|
|
388
|
+
});
|
|
389
|
+
});
|