@quillsql/react 2.10.38 → 2.11.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/Chart.d.ts +4 -0
- package/dist/cjs/Chart.d.ts.map +1 -1
- package/dist/cjs/Chart.js +5 -5
- package/dist/cjs/ChartBuilder.js +2 -2
- package/dist/cjs/Context.d.ts +1 -1
- package/dist/cjs/Context.d.ts.map +1 -1
- package/dist/cjs/Context.js +3 -1
- package/dist/cjs/Dashboard.d.ts +3 -1
- package/dist/cjs/Dashboard.d.ts.map +1 -1
- package/dist/cjs/Dashboard.js +4 -4
- package/dist/cjs/QuillProvider.d.ts +4 -1
- package/dist/cjs/QuillProvider.d.ts.map +1 -1
- package/dist/cjs/QuillProvider.js +2 -2
- package/dist/cjs/ReportBuilder.d.ts +40 -40
- package/dist/cjs/ReportBuilder.d.ts.map +1 -1
- package/dist/cjs/ReportBuilder.js +2026 -917
- package/dist/cjs/SQLEditor.d.ts.map +1 -1
- package/dist/cjs/SQLEditor.js +9 -0
- package/dist/cjs/components/Chart/LineChart.d.ts +5 -1
- package/dist/cjs/components/Chart/LineChart.d.ts.map +1 -1
- package/dist/cjs/components/Chart/LineChart.js +18 -6
- package/dist/cjs/components/QuillTable.d.ts +1 -1
- package/dist/cjs/components/QuillTable.d.ts.map +1 -1
- package/dist/cjs/components/QuillTable.js +157 -157
- package/dist/cjs/components/ReportBuilder/AddColumnPopover.d.ts +2 -0
- package/dist/cjs/components/ReportBuilder/AddColumnPopover.d.ts.map +1 -0
- package/dist/cjs/components/ReportBuilder/AddColumnPopover.js +128 -0
- package/dist/cjs/components/ReportBuilder/ast.d.ts +512 -0
- package/dist/cjs/components/ReportBuilder/ast.d.ts.map +1 -0
- package/dist/cjs/components/ReportBuilder/ast.js +210 -0
- package/dist/cjs/components/ReportBuilder/bigDateMap.d.ts +7 -0
- package/dist/cjs/components/ReportBuilder/bigDateMap.d.ts.map +1 -0
- package/dist/cjs/components/ReportBuilder/bigDateMap.js +689 -0
- package/dist/cjs/components/ReportBuilder/constants.d.ts +89 -0
- package/dist/cjs/components/ReportBuilder/constants.d.ts.map +1 -0
- package/dist/cjs/components/ReportBuilder/constants.js +130 -0
- package/dist/cjs/components/ReportBuilder/convert.d.ts +41 -0
- package/dist/cjs/components/ReportBuilder/convert.d.ts.map +1 -0
- package/dist/cjs/components/ReportBuilder/convert.js +730 -0
- package/dist/cjs/components/ReportBuilder/operators.d.ts +445 -0
- package/dist/cjs/components/ReportBuilder/operators.d.ts.map +1 -0
- package/dist/cjs/components/ReportBuilder/operators.js +552 -0
- package/dist/cjs/components/ReportBuilder/pivot.d.ts +10 -0
- package/dist/cjs/components/ReportBuilder/pivot.d.ts.map +1 -0
- package/dist/cjs/components/ReportBuilder/pivot.js +2 -0
- package/dist/cjs/components/ReportBuilder/postgres.d.ts +150 -0
- package/dist/cjs/components/ReportBuilder/postgres.d.ts.map +1 -0
- package/dist/cjs/components/ReportBuilder/postgres.js +365 -0
- package/dist/cjs/components/ReportBuilder/schema.d.ts +23 -0
- package/dist/cjs/components/ReportBuilder/schema.d.ts.map +1 -0
- package/dist/cjs/components/ReportBuilder/schema.js +2 -0
- package/dist/cjs/components/ReportBuilder/ui.d.ts +34 -0
- package/dist/cjs/components/ReportBuilder/ui.d.ts.map +1 -0
- package/dist/cjs/components/ReportBuilder/ui.js +389 -0
- package/dist/cjs/components/ReportBuilder/util.d.ts +76 -0
- package/dist/cjs/components/ReportBuilder/util.d.ts.map +1 -0
- package/dist/cjs/components/ReportBuilder/util.js +648 -0
- package/dist/cjs/components/UiComponents.d.ts +15 -2
- package/dist/cjs/components/UiComponents.d.ts.map +1 -1
- package/dist/cjs/components/UiComponents.js +50 -3
- package/dist/cjs/utils/crypto.d.ts +1 -1
- package/dist/cjs/utils/crypto.d.ts.map +1 -1
- package/dist/cjs/utils/crypto.js +9 -5
- package/dist/esm/Chart.d.ts +4 -0
- package/dist/esm/Chart.d.ts.map +1 -1
- package/dist/esm/Chart.js +5 -5
- package/dist/esm/ChartBuilder.js +1 -1
- package/dist/esm/Context.d.ts +1 -1
- package/dist/esm/Context.d.ts.map +1 -1
- package/dist/esm/Context.js +3 -1
- package/dist/esm/Dashboard.d.ts +3 -1
- package/dist/esm/Dashboard.d.ts.map +1 -1
- package/dist/esm/Dashboard.js +4 -4
- package/dist/esm/QuillProvider.d.ts +4 -1
- package/dist/esm/QuillProvider.d.ts.map +1 -1
- package/dist/esm/QuillProvider.js +2 -2
- package/dist/esm/ReportBuilder.d.ts +40 -40
- package/dist/esm/ReportBuilder.d.ts.map +1 -1
- package/dist/esm/ReportBuilder.js +2028 -917
- package/dist/esm/SQLEditor.d.ts.map +1 -1
- package/dist/esm/SQLEditor.js +9 -0
- package/dist/esm/components/Chart/LineChart.d.ts +5 -1
- package/dist/esm/components/Chart/LineChart.d.ts.map +1 -1
- package/dist/esm/components/Chart/LineChart.js +18 -6
- package/dist/esm/components/QuillTable.d.ts +1 -1
- package/dist/esm/components/QuillTable.d.ts.map +1 -1
- package/dist/esm/components/QuillTable.js +157 -157
- package/dist/esm/components/ReportBuilder/AddColumnPopover.d.ts +2 -0
- package/dist/esm/components/ReportBuilder/AddColumnPopover.d.ts.map +1 -0
- package/dist/esm/components/ReportBuilder/AddColumnPopover.js +125 -0
- package/dist/esm/components/ReportBuilder/ast.d.ts +512 -0
- package/dist/esm/components/ReportBuilder/ast.d.ts.map +1 -0
- package/dist/esm/components/ReportBuilder/ast.js +186 -0
- package/dist/esm/components/ReportBuilder/bigDateMap.d.ts +7 -0
- package/dist/esm/components/ReportBuilder/bigDateMap.d.ts.map +1 -0
- package/dist/esm/components/ReportBuilder/bigDateMap.js +686 -0
- package/dist/esm/components/ReportBuilder/constants.d.ts +89 -0
- package/dist/esm/components/ReportBuilder/constants.d.ts.map +1 -0
- package/dist/esm/components/ReportBuilder/constants.js +127 -0
- package/dist/esm/components/ReportBuilder/convert.d.ts +41 -0
- package/dist/esm/components/ReportBuilder/convert.d.ts.map +1 -0
- package/dist/esm/components/ReportBuilder/convert.js +719 -0
- package/dist/esm/components/ReportBuilder/operators.d.ts +445 -0
- package/dist/esm/components/ReportBuilder/operators.d.ts.map +1 -0
- package/dist/esm/components/ReportBuilder/operators.js +548 -0
- package/dist/esm/components/ReportBuilder/pivot.d.ts +10 -0
- package/dist/esm/components/ReportBuilder/pivot.d.ts.map +1 -0
- package/dist/esm/components/ReportBuilder/pivot.js +1 -0
- package/dist/esm/components/ReportBuilder/postgres.d.ts +150 -0
- package/dist/esm/components/ReportBuilder/postgres.d.ts.map +1 -0
- package/dist/esm/components/ReportBuilder/postgres.js +355 -0
- package/dist/esm/components/ReportBuilder/schema.d.ts +23 -0
- package/dist/esm/components/ReportBuilder/schema.d.ts.map +1 -0
- package/dist/esm/components/ReportBuilder/schema.js +1 -0
- package/dist/esm/components/ReportBuilder/ui.d.ts +34 -0
- package/dist/esm/components/ReportBuilder/ui.d.ts.map +1 -0
- package/dist/esm/components/ReportBuilder/ui.js +366 -0
- package/dist/esm/components/ReportBuilder/util.d.ts +76 -0
- package/dist/esm/components/ReportBuilder/util.d.ts.map +1 -0
- package/dist/esm/components/ReportBuilder/util.js +616 -0
- package/dist/esm/components/UiComponents.d.ts +15 -2
- package/dist/esm/components/UiComponents.d.ts.map +1 -1
- package/dist/esm/components/UiComponents.js +47 -2
- package/dist/esm/utils/crypto.d.ts +1 -1
- package/dist/esm/utils/crypto.d.ts.map +1 -1
- package/dist/esm/utils/crypto.js +9 -5
- package/package.json +1 -1
|
@@ -3,1002 +3,2111 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.getPostgresBasicType = void 0;
|
|
7
6
|
const jsx_runtime_1 = require("react/jsx-runtime");
|
|
8
|
-
// @ts-nocheck
|
|
9
7
|
const react_1 = require("react");
|
|
10
|
-
|
|
11
|
-
const
|
|
12
|
-
const
|
|
8
|
+
const axios_1 = __importDefault(require("axios"));
|
|
9
|
+
const UiComponents_1 = require("./components/UiComponents");
|
|
10
|
+
const core_1 = require("@dnd-kit/core");
|
|
11
|
+
const sortable_1 = require("@dnd-kit/sortable");
|
|
12
|
+
const utilities_1 = require("@dnd-kit/utilities");
|
|
13
13
|
const date_fns_1 = require("date-fns");
|
|
14
|
-
const
|
|
15
|
-
const
|
|
16
|
-
const dateRangePickerUtils_1 = require("./DateRangePicker/dateRangePickerUtils");
|
|
14
|
+
const Context_1 = require("./Context");
|
|
15
|
+
const ast_1 = require("./components/ReportBuilder/ast");
|
|
17
16
|
const ChartBuilder_1 = __importDefault(require("./ChartBuilder"));
|
|
18
|
-
const
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
17
|
+
const ui_1 = require("./components/ReportBuilder/ui");
|
|
18
|
+
const postgres_1 = require("./components/ReportBuilder/postgres");
|
|
19
|
+
const convert_1 = require("./components/ReportBuilder/convert");
|
|
20
|
+
const util_1 = require("./components/ReportBuilder/util");
|
|
21
|
+
const operators_1 = require("./components/ReportBuilder/operators");
|
|
22
|
+
const crypto_1 = require("./utils/crypto");
|
|
23
|
+
const constants_1 = require("./components/ReportBuilder/constants");
|
|
24
|
+
const AddColumnPopover_1 = __importDefault(require("./components/ReportBuilder/AddColumnPopover"));
|
|
25
|
+
/**
|
|
26
|
+
* Quill Report Builder
|
|
27
|
+
*
|
|
28
|
+
* Allows non-technical users to build SQL queries using either UI or AI and
|
|
29
|
+
* then edit them on the fly. Once users have constructed a query they like,
|
|
30
|
+
* they can click a button and add that report to their dashboard or export it
|
|
31
|
+
* as a CSV.
|
|
32
|
+
*/
|
|
33
|
+
function ReportBuilder({ path = '', initialTableName = 'transactions', onAddToDashboardComplete = () => void null, destinationDashboard = undefined, dashboardItem = undefined, organizationName = '', Button = ui_1.QuillButton, SecondaryButton = ui_1.QuillSecondaryButton, TextInput = ui_1.QuillTextInput, Select = ui_1.QuillSelect, Table = ui_1.QuillReportBuilderTable, Popover = ui_1.QuillPopover, Tabs = ui_1.QuillTabs, Checkbox = UiComponents_1.MemoizedCheckbox, Sidebar = ui_1.QuillSidebar, Container = ui_1.CustomContainer, HandleButton = ui_1.QuillHandleButton, SelectColumn = ui_1.QuillSelectColumn, DraggableColumn = ui_1.QuillDraggableColumn, ButtonLoadingState = ui_1.QuillButtonLoadingState, TableLoadingState = ui_1.QuillTableLoadingState, SidebarHeading = ui_1.QuillSidebarHeading, SidebarSubHeading = ui_1.QuillSidebarSubHeading, FilterPopover = ui_1.QuillFilterPopover, }) {
|
|
34
|
+
const [aiPrompt, setAiPrompt] = (0, react_1.useState)('');
|
|
35
|
+
const [errorMessage, setErrorMessage] = (0, react_1.useState)('');
|
|
36
|
+
const [baseAst, setBaseAst] = (0, react_1.useState)(null);
|
|
37
|
+
const [formData, setFormData] = (0, react_1.useState)(null);
|
|
38
|
+
const [orderedColumnNames, setOrderedColumnNames] = (0, react_1.useState)([]);
|
|
39
|
+
const [selectedColumns, setSelectedColumns] = (0, react_1.useState)([]);
|
|
40
|
+
const [schemaTables, setSchemaTables] = (0, react_1.useState)([]);
|
|
41
|
+
const [activeQuery, setActiveQuery] = (0, react_1.useState)('');
|
|
42
|
+
const [activeEditItem, setActiveEditItem] = (0, react_1.useState)(null);
|
|
43
|
+
const [activePath, setActivePath] = (0, react_1.useState)(null);
|
|
44
|
+
const [openPopover, setOpenPopover] = (0, react_1.useState)(null);
|
|
26
45
|
const [loading, setLoading] = (0, react_1.useState)(false);
|
|
27
|
-
const [
|
|
28
|
-
const
|
|
29
|
-
const [
|
|
46
|
+
const [isChartBuilderOpen, setIsChartBuilderOpen] = (0, react_1.useState)(false);
|
|
47
|
+
const [isPending, setIsPending] = (0, react_1.useState)(false);
|
|
48
|
+
const [isCopying, setIsCopying] = (0, react_1.useState)(false);
|
|
49
|
+
const [rows, setRows] = (0, react_1.useState)([]);
|
|
50
|
+
const [fields, setFields] = (0, react_1.useState)([]);
|
|
51
|
+
const [topLevelBinaryOperator, setTopLevelBinaryOperator] = (0, react_1.useState)('AND');
|
|
52
|
+
const [editPopoverKey, setEditPopoverKey] = (0, react_1.useState)(null);
|
|
53
|
+
const [uniqueValues, setUniqueValues] = (0, react_1.useState)({});
|
|
54
|
+
const [pivot, setPivot] = (0, react_1.useState)(null);
|
|
55
|
+
const [pivotData, setPivotData] = (0, react_1.useState)(null);
|
|
56
|
+
// eslint-disable-next-line no-unused-vars
|
|
57
|
+
const [client, _setClient] = (0, react_1.useContext)(Context_1.ClientContext);
|
|
58
|
+
const enforceOrderOnColumns = (columnNames) => {
|
|
59
|
+
if (pivot) {
|
|
60
|
+
const rowName = pivot.rowField;
|
|
61
|
+
const sortFn = (a, b) => a === rowName ? -1 : b === rowName ? 1 : 0;
|
|
62
|
+
const columnsInPivot = getColumnsInPivotExpanded();
|
|
63
|
+
return columnNames
|
|
64
|
+
.sort(sortFn)
|
|
65
|
+
.filter((c) => columnsInPivot.includes(c));
|
|
66
|
+
}
|
|
67
|
+
return columnNames;
|
|
68
|
+
};
|
|
69
|
+
/**
|
|
70
|
+
* Transforms an array of column names into an array of columnInfo objects
|
|
71
|
+
* with label, field, format, and fieldType keys.
|
|
72
|
+
*/
|
|
73
|
+
const processColumnsForChartBuilder = (columns) => {
|
|
74
|
+
return columns.map((col) => ({
|
|
75
|
+
label: col,
|
|
76
|
+
name: col,
|
|
77
|
+
displayName: col,
|
|
78
|
+
field: col,
|
|
79
|
+
format: (0, util_1.getPostgresBasicType)(fields.find((f) => f.name === col))?.replace('number', 'whole_number') || 'string',
|
|
80
|
+
fieldType: schemaTables
|
|
81
|
+
.flatMap((t) => t.columns)
|
|
82
|
+
.find((c) => c.name === col)?.fieldType || 'text',
|
|
83
|
+
}));
|
|
84
|
+
};
|
|
85
|
+
const clearAllState = () => {
|
|
86
|
+
// We're trying to not block the main thread while resetting all the state.
|
|
87
|
+
// This shouldn't be an issue since the dispatches shouldn't block, but
|
|
88
|
+
// this seems to work for now. ¯\_(ツ)_/¯
|
|
89
|
+
setTimeout(() => {
|
|
90
|
+
setAiPrompt('');
|
|
91
|
+
setBaseAst(null);
|
|
92
|
+
setFormData(null);
|
|
93
|
+
setOrderedColumnNames([]);
|
|
94
|
+
setSelectedColumns([]);
|
|
95
|
+
setSchemaTables([]);
|
|
96
|
+
setActiveQuery('');
|
|
97
|
+
setActiveEditItem(null);
|
|
98
|
+
setActivePath(null);
|
|
99
|
+
setOpenPopover(null);
|
|
100
|
+
setLoading(false);
|
|
101
|
+
setIsPending(false);
|
|
102
|
+
setRows([]);
|
|
103
|
+
setFields([]);
|
|
104
|
+
setTopLevelBinaryOperator('AND');
|
|
105
|
+
setEditPopoverKey(null);
|
|
106
|
+
// setUniqueValues({});
|
|
107
|
+
setPivot(null);
|
|
108
|
+
setPivotData(null);
|
|
109
|
+
}, 0);
|
|
110
|
+
};
|
|
30
111
|
(0, react_1.useEffect)(() => {
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
112
|
+
clearAllState();
|
|
113
|
+
}, [client]);
|
|
114
|
+
(0, react_1.useEffect)(() => {
|
|
115
|
+
if (activePath !== null) {
|
|
116
|
+
// update the modal with the new subtree
|
|
117
|
+
setActiveEditItem(getByKey(formData, activePath));
|
|
118
|
+
}
|
|
119
|
+
}, [formData]);
|
|
120
|
+
const getByKey = (formData, path) => {
|
|
121
|
+
if (!path)
|
|
122
|
+
return (0, util_1.deepCopy)(formData);
|
|
123
|
+
// Function to immutably update or delete nodes based on their path
|
|
124
|
+
const paths = path.split('.');
|
|
125
|
+
let current = (0, util_1.deepCopy)(formData);
|
|
126
|
+
for (let i = 0; i < paths.length; i++) {
|
|
127
|
+
if (current[paths[i]]) {
|
|
128
|
+
current = current[paths[i]];
|
|
41
129
|
}
|
|
42
130
|
}
|
|
43
|
-
|
|
44
|
-
|
|
131
|
+
return current;
|
|
132
|
+
};
|
|
133
|
+
const copyToClipboard = (str) => {
|
|
134
|
+
setIsCopying(true);
|
|
135
|
+
navigator.clipboard.writeText(str);
|
|
136
|
+
setTimeout(() => setIsCopying(false), 800);
|
|
137
|
+
};
|
|
138
|
+
const clearCheckboxes = () => {
|
|
139
|
+
const checkboxes = uniqueValues;
|
|
140
|
+
const newValues = {};
|
|
141
|
+
for (let table of Object.keys(checkboxes)) {
|
|
142
|
+
newValues[table] = {};
|
|
143
|
+
for (let column of Object.keys(checkboxes[table])) {
|
|
144
|
+
newValues[table][column] = {};
|
|
145
|
+
for (let variant of Object.keys(checkboxes[table][column])) {
|
|
146
|
+
newValues[table][column][variant] = false;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
45
149
|
}
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
metadata: {
|
|
62
|
-
clientId: publicKey,
|
|
63
|
-
publicKey: publicKey,
|
|
64
|
-
task: 'schema',
|
|
65
|
-
},
|
|
66
|
-
}),
|
|
67
|
-
});
|
|
68
|
-
const data = await response.json();
|
|
69
|
-
if (isSubscribed) {
|
|
70
|
-
setSchema(data.data.tables);
|
|
150
|
+
setUniqueValues(newValues);
|
|
151
|
+
};
|
|
152
|
+
const setCheckboxes = (node) => {
|
|
153
|
+
if (!['IN', 'NOT IN'].includes(node.operator))
|
|
154
|
+
return;
|
|
155
|
+
const selectedItems = node.right.value.flatMap((v) => v.args.value.map((x) => x.value.toLowerCase()));
|
|
156
|
+
const checkboxes = uniqueValues;
|
|
157
|
+
const newValues = {};
|
|
158
|
+
for (let table of Object.keys(checkboxes)) {
|
|
159
|
+
newValues[table] = {};
|
|
160
|
+
for (let column of Object.keys(checkboxes[table])) {
|
|
161
|
+
newValues[table][column] = {};
|
|
162
|
+
for (let variant of Object.keys(checkboxes[table][column])) {
|
|
163
|
+
newValues[table][column][variant] = selectedItems.includes(variant.toLowerCase());
|
|
164
|
+
}
|
|
71
165
|
}
|
|
72
166
|
}
|
|
73
|
-
|
|
74
|
-
|
|
167
|
+
setUniqueValues(newValues);
|
|
168
|
+
};
|
|
169
|
+
const fetchSqlQuery = async () => {
|
|
170
|
+
try {
|
|
171
|
+
const response = await axios_1.default.post(`https://quill-344421.uc.r.appspot.com/sqlify`, { ast: { ...baseAst, where: formData }, publicKey: client.publicKey });
|
|
172
|
+
setActiveQuery(response.data.query);
|
|
173
|
+
console.info(`%c[Query]: ${response.data.query}`, 'color: dimgray');
|
|
174
|
+
console.info(`%c[AST]:`, 'color: dimgray', baseAst);
|
|
175
|
+
fetchUponChange();
|
|
75
176
|
}
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
}
|
|
79
|
-
}
|
|
177
|
+
catch (error) {
|
|
178
|
+
console.error(error);
|
|
179
|
+
}
|
|
180
|
+
};
|
|
80
181
|
(0, react_1.useEffect)(() => {
|
|
81
|
-
|
|
82
|
-
|
|
182
|
+
setErrorMessage('');
|
|
183
|
+
if (baseAst || formData) {
|
|
184
|
+
fetchSqlQuery();
|
|
185
|
+
}
|
|
186
|
+
}, [baseAst]);
|
|
187
|
+
// Returns an array of all the column names in the pivot config
|
|
188
|
+
// if there are any, otherwise returns [].
|
|
189
|
+
const getColumnsInPivot = () => {
|
|
190
|
+
if (!pivot)
|
|
191
|
+
return [];
|
|
192
|
+
const { valueField, rowField, columnField } = pivot;
|
|
193
|
+
return [valueField, rowField, columnField].filter(Boolean);
|
|
194
|
+
};
|
|
195
|
+
// It's just like getColumnsInPivot but we expand the columnField
|
|
196
|
+
// if there is one to include all the variants just like it would
|
|
197
|
+
// show up in the table. (eg. category -> ...[Fuel, Food, Other])
|
|
198
|
+
const getColumnsInPivotExpanded = () => {
|
|
199
|
+
if (!pivot)
|
|
200
|
+
return [];
|
|
201
|
+
const tables = (0, ast_1.getTableNames)(baseAst);
|
|
202
|
+
if (tables.length !== 1)
|
|
203
|
+
return [];
|
|
204
|
+
const result = [];
|
|
205
|
+
const table = tables[0];
|
|
206
|
+
const { valueField, rowField, columnField } = pivot;
|
|
207
|
+
if (columnField && uniqueValues[table][columnField]) {
|
|
208
|
+
result.push(...Object.keys(uniqueValues[table][columnField]));
|
|
83
209
|
}
|
|
84
|
-
|
|
210
|
+
result.push(valueField, rowField);
|
|
211
|
+
return result.filter(Boolean);
|
|
212
|
+
};
|
|
85
213
|
(0, react_1.useEffect)(() => {
|
|
86
|
-
if (
|
|
87
|
-
|
|
214
|
+
if (errorMessage) {
|
|
215
|
+
console.error(errorMessage);
|
|
88
216
|
}
|
|
89
|
-
}, []);
|
|
217
|
+
}, [errorMessage]);
|
|
90
218
|
(0, react_1.useEffect)(() => {
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
219
|
+
const fetchDistinctHelper = async (column, table) => {
|
|
220
|
+
try {
|
|
221
|
+
const query = `SELECT DISTINCT ${column} FROM ${table};`;
|
|
222
|
+
const response = await axios_1.default.post(`https://quill-344421.uc.r.appspot.com/dashquery`, { orgId: 2, publicKey: client.publicKey, query: query });
|
|
223
|
+
if (response.data.errorMessage) {
|
|
224
|
+
console.error(response.data.errorMessage);
|
|
225
|
+
return null;
|
|
226
|
+
}
|
|
227
|
+
const options = response.data.rows.map((r) => r[column]);
|
|
228
|
+
const newCheckboxValues = options.reduce((obj, col) => {
|
|
229
|
+
obj[col] = false;
|
|
230
|
+
return obj;
|
|
231
|
+
}, {});
|
|
232
|
+
return { table, column, values: newCheckboxValues };
|
|
233
|
+
}
|
|
234
|
+
catch (e) {
|
|
235
|
+
console.error(e);
|
|
236
|
+
return null;
|
|
237
|
+
}
|
|
102
238
|
};
|
|
103
|
-
const
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
239
|
+
const fetchSchema = async () => {
|
|
240
|
+
try {
|
|
241
|
+
const response = await fetch(`https://quill-344421.uc.r.appspot.com/schema2/${client.publicKey}`).then((res) => res.json());
|
|
242
|
+
// Filter out hidden columns on tables back from schema2.
|
|
243
|
+
const tables = response?.tables;
|
|
244
|
+
for (const table of tables) {
|
|
245
|
+
table.columns = table.columns.filter((column) =>
|
|
246
|
+
// Quick and dirty fix for removing org ids from response.
|
|
247
|
+
// TODO: Fix this on the backend or something.
|
|
248
|
+
column.isVisible && column.displayName !== 'pm_company_id');
|
|
249
|
+
}
|
|
250
|
+
setSchemaTables(tables ?? []);
|
|
251
|
+
setOrderedColumnNames((tables ?? [])
|
|
252
|
+
// .filter((t: any) => t.displayName === initialTableName)
|
|
253
|
+
.flatMap((table) => table.columns.map((c) => `${table.displayName}.${c.displayName}`)));
|
|
254
|
+
// Fetch all the unique values in parallel
|
|
255
|
+
const pendingFetches = [];
|
|
256
|
+
for (let table of tables ?? []) {
|
|
257
|
+
for (let column of table.columns) {
|
|
258
|
+
if (!(0, ast_1.isTextColumnType)(column.fieldType))
|
|
259
|
+
continue;
|
|
260
|
+
const fetchPromise = fetchDistinctHelper(column.name, table.displayName);
|
|
261
|
+
pendingFetches.push(fetchPromise);
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
const newUniqueValues = {};
|
|
265
|
+
const resolvedPromises = await Promise.all(pendingFetches);
|
|
266
|
+
for (const resolvedData of resolvedPromises) {
|
|
267
|
+
if (resolvedData) {
|
|
268
|
+
const { table, column, values } = resolvedData;
|
|
269
|
+
if (!newUniqueValues[table]) {
|
|
270
|
+
newUniqueValues[table] = {};
|
|
271
|
+
}
|
|
272
|
+
newUniqueValues[table][column] = values;
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
if ((0, crypto_1.hashCode)(uniqueValues) !== (0, crypto_1.hashCode)(newUniqueValues)) {
|
|
276
|
+
setUniqueValues(newUniqueValues);
|
|
277
|
+
}
|
|
114
278
|
}
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
}
|
|
118
|
-
setData(resp.rows);
|
|
119
|
-
setColumns(resp.fields.map((elem) => (0, SQLEditor_1.convertPostgresColumn)(elem)));
|
|
120
|
-
if (selectedPivot) {
|
|
121
|
-
const { rows, columns } = (0, PivotModal_1.generatePivotTable)(selectedPivot, resp.rows, dateRange);
|
|
122
|
-
if (onChangePivot) {
|
|
123
|
-
onChangePivot(selectedPivot, columns, rows);
|
|
279
|
+
catch (error) {
|
|
280
|
+
console.error(error);
|
|
124
281
|
}
|
|
282
|
+
};
|
|
283
|
+
if (schemaTables.length === 0) {
|
|
284
|
+
fetchSchema();
|
|
125
285
|
}
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
286
|
+
}, [schemaTables]);
|
|
287
|
+
const updateFormData = (updates, { isDeletion = false, isInsertion = false, isReplaceSubtree = false, isAddVariant = false, isDeleteVariant = false, topLevelBinOp = 'OR', isCondition = undefined, }) => {
|
|
288
|
+
// Function to immutably update or delete nodes based on their path
|
|
289
|
+
// TODO: fix the following horible code
|
|
290
|
+
updates.forEach(({ path, value }) => {
|
|
291
|
+
const globalPath = [
|
|
292
|
+
activePath ?? isDeletion ? path : '',
|
|
293
|
+
isDeletion || isReplaceSubtree ? '' : path,
|
|
294
|
+
]
|
|
295
|
+
.filter(Boolean)
|
|
296
|
+
.join('.');
|
|
297
|
+
const paths = globalPath.split('.').filter((p) => p);
|
|
298
|
+
if (paths.length === 0 && !isInsertion && !isReplaceSubtree) {
|
|
299
|
+
setFormData(null);
|
|
300
|
+
setBaseAst((0, util_1.deepCopy)({
|
|
301
|
+
...constants_1.defaultAST,
|
|
302
|
+
...baseAst,
|
|
303
|
+
...(!baseAst?.columns && {
|
|
304
|
+
columns: getAllPossibleColumns().map((c) => {
|
|
305
|
+
const newColumn = (0, util_1.deepCopy)(constants_1.defaultColumn);
|
|
306
|
+
newColumn.expr.column = c.name;
|
|
307
|
+
return newColumn;
|
|
308
|
+
}),
|
|
309
|
+
}),
|
|
310
|
+
...(!baseAst?.from && {
|
|
311
|
+
from: [{ ...constants_1.defaultTable, table: initialTableName }],
|
|
312
|
+
}),
|
|
313
|
+
where: null,
|
|
314
|
+
}));
|
|
315
|
+
return;
|
|
129
316
|
}
|
|
130
|
-
if (
|
|
131
|
-
|
|
317
|
+
if (!formData && isInsertion) {
|
|
318
|
+
setFormData(value);
|
|
319
|
+
setBaseAst((0, util_1.deepCopy)({
|
|
320
|
+
...constants_1.defaultAST,
|
|
321
|
+
...baseAst,
|
|
322
|
+
...(!baseAst?.columns && {
|
|
323
|
+
columns: getAllPossibleColumns().map((c) => {
|
|
324
|
+
const newColumn = (0, util_1.deepCopy)(constants_1.defaultColumn);
|
|
325
|
+
newColumn.expr.column = c.name;
|
|
326
|
+
return newColumn;
|
|
327
|
+
}),
|
|
328
|
+
}),
|
|
329
|
+
...(!baseAst?.from && {
|
|
330
|
+
from: [{ ...constants_1.defaultTable, table: initialTableName }],
|
|
331
|
+
}),
|
|
332
|
+
where: value,
|
|
333
|
+
}));
|
|
334
|
+
return;
|
|
132
335
|
}
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
case 701: // float8
|
|
169
|
-
case 1700: // numeric
|
|
170
|
-
format = 'number';
|
|
171
|
-
break;
|
|
172
|
-
case 1082: // date
|
|
173
|
-
case 1083: // time
|
|
174
|
-
case 1184: // timestamptz
|
|
175
|
-
case 1114: // timestamp
|
|
176
|
-
format = 'date';
|
|
177
|
-
break;
|
|
178
|
-
case 1043: // varchar
|
|
179
|
-
default:
|
|
180
|
-
format = 'string';
|
|
181
|
-
}
|
|
182
|
-
}
|
|
183
|
-
else if (column.fieldType) {
|
|
184
|
-
// if column.dataTypeID doesn't exist, check column.fieldType
|
|
185
|
-
switch (column.fieldType) {
|
|
186
|
-
case 'int8':
|
|
187
|
-
case 'int2':
|
|
188
|
-
case 'int4':
|
|
189
|
-
case 'float4':
|
|
190
|
-
case 'float8':
|
|
191
|
-
case 'numeric':
|
|
192
|
-
format = 'number';
|
|
193
|
-
break;
|
|
194
|
-
case 'date':
|
|
195
|
-
case 'time':
|
|
196
|
-
case 'timestamptz':
|
|
197
|
-
case 'timestamp':
|
|
198
|
-
format = 'date';
|
|
199
|
-
break;
|
|
200
|
-
case 'varchar':
|
|
201
|
-
default:
|
|
202
|
-
format = 'string';
|
|
203
|
-
}
|
|
204
|
-
}
|
|
205
|
-
return format;
|
|
206
|
-
}
|
|
207
|
-
exports.getPostgresBasicType = getPostgresBasicType;
|
|
208
|
-
const newDateWhereAST = (column, dateRange, databaseType) => {
|
|
209
|
-
// all time means no filter
|
|
210
|
-
if (dateRange[2] === 'at') {
|
|
211
|
-
return null;
|
|
212
|
-
}
|
|
213
|
-
// if using preset
|
|
214
|
-
if (dateRange[2]) {
|
|
215
|
-
const timeInterval = dateRangePickerUtils_1.reportBuilderOptions.find((elem) => elem.value === dateRange[2])?.dayInterval;
|
|
216
|
-
switch (databaseType) {
|
|
217
|
-
case 'BigQuery': {
|
|
218
|
-
return {
|
|
336
|
+
let newState = (0, util_1.deepCopy)(formData);
|
|
337
|
+
let current = newState;
|
|
338
|
+
let parent = null;
|
|
339
|
+
let parentKey = null;
|
|
340
|
+
for (let i = 0; i < paths.length - 1; i++) {
|
|
341
|
+
if (current[paths[i]]) {
|
|
342
|
+
parent = current;
|
|
343
|
+
parentKey = paths[i];
|
|
344
|
+
current = current[paths[i]];
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
const lastKey = paths[paths.length - 1];
|
|
348
|
+
if (isDeletion) {
|
|
349
|
+
if (lastKey === 'left' || lastKey === 'right') {
|
|
350
|
+
if (parent) {
|
|
351
|
+
if (lastKey === 'right') {
|
|
352
|
+
parent[parentKey] = parent[parentKey].left;
|
|
353
|
+
}
|
|
354
|
+
else {
|
|
355
|
+
parent[parentKey] = parent[parentKey].right;
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
else {
|
|
359
|
+
delete current[lastKey];
|
|
360
|
+
if (newState?.left && !newState?.right) {
|
|
361
|
+
newState = newState.left;
|
|
362
|
+
}
|
|
363
|
+
else if (newState?.right && !newState?.left) {
|
|
364
|
+
newState = newState.right;
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
else if (isInsertion) {
|
|
370
|
+
newState = {
|
|
219
371
|
type: 'binary_expr',
|
|
220
|
-
operator:
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
args: {
|
|
225
|
-
type: 'expr_list',
|
|
226
|
-
value: [
|
|
227
|
-
{
|
|
228
|
-
type: 'column_ref',
|
|
229
|
-
table: null,
|
|
230
|
-
column: column,
|
|
231
|
-
},
|
|
232
|
-
],
|
|
233
|
-
},
|
|
234
|
-
over: null,
|
|
235
|
-
},
|
|
236
|
-
right: {
|
|
237
|
-
type: 'expr_list',
|
|
238
|
-
value: [
|
|
239
|
-
{
|
|
240
|
-
type: 'function',
|
|
241
|
-
name: 'DATE_SUB',
|
|
242
|
-
args: {
|
|
243
|
-
type: 'expr_list',
|
|
244
|
-
value: [
|
|
245
|
-
{
|
|
246
|
-
type: 'function',
|
|
247
|
-
name: 'CURRENT_DATE',
|
|
248
|
-
args: {
|
|
249
|
-
type: 'expr_list',
|
|
250
|
-
value: [],
|
|
251
|
-
},
|
|
252
|
-
over: null,
|
|
253
|
-
},
|
|
254
|
-
{
|
|
255
|
-
type: 'interval',
|
|
256
|
-
expr: {
|
|
257
|
-
type: 'number',
|
|
258
|
-
value: timeInterval,
|
|
259
|
-
},
|
|
260
|
-
unit: 'day',
|
|
261
|
-
},
|
|
262
|
-
],
|
|
263
|
-
},
|
|
264
|
-
over: null,
|
|
265
|
-
},
|
|
266
|
-
{
|
|
267
|
-
type: 'function',
|
|
268
|
-
name: 'CURRENT_DATE',
|
|
269
|
-
args: {
|
|
270
|
-
type: 'expr_list',
|
|
271
|
-
value: [],
|
|
272
|
-
},
|
|
273
|
-
over: null,
|
|
274
|
-
},
|
|
275
|
-
],
|
|
276
|
-
},
|
|
372
|
+
operator: topLevelBinOp,
|
|
373
|
+
isCondition: isCondition,
|
|
374
|
+
left: newState,
|
|
375
|
+
right: value,
|
|
277
376
|
};
|
|
278
377
|
}
|
|
279
|
-
|
|
280
|
-
|
|
378
|
+
else if (isAddVariant) {
|
|
379
|
+
const newVariant = (0, util_1.deepCopy)(constants_1.defaultVariant);
|
|
380
|
+
if (value) {
|
|
381
|
+
newVariant.args.value[0].value = value;
|
|
382
|
+
// if there is already a single default value there,
|
|
383
|
+
// let's remove it so when we push we replace.
|
|
384
|
+
if (current[lastKey].length === 1 &&
|
|
385
|
+
current[lastKey][0].args.value[0].value === '') {
|
|
386
|
+
current[lastKey].pop();
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
current[lastKey].push(newVariant);
|
|
390
|
+
}
|
|
391
|
+
else if (isDeleteVariant) {
|
|
392
|
+
if (value) {
|
|
393
|
+
const argList = current[lastKey];
|
|
394
|
+
argList.splice(argList.findIndex((arg) => arg.args.value[0].value === name), 1);
|
|
395
|
+
}
|
|
396
|
+
else {
|
|
397
|
+
current[lastKey].pop();
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
else if (isReplaceSubtree) {
|
|
401
|
+
if (lastKey) {
|
|
402
|
+
current[lastKey] = value;
|
|
403
|
+
}
|
|
404
|
+
else {
|
|
405
|
+
newState = value;
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
else {
|
|
409
|
+
if (typeof current[lastKey] === 'object' && current[lastKey] !== null) {
|
|
410
|
+
current[lastKey].value = value;
|
|
411
|
+
}
|
|
412
|
+
else {
|
|
413
|
+
current[lastKey] = value;
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
setFormData(newState);
|
|
417
|
+
setBaseAst({
|
|
418
|
+
...constants_1.defaultAST,
|
|
419
|
+
...baseAst,
|
|
420
|
+
...(!baseAst?.columns && {
|
|
421
|
+
columns: getAllPossibleColumns().map((c) => {
|
|
422
|
+
const newColumn = (0, util_1.deepCopy)(constants_1.defaultColumn);
|
|
423
|
+
newColumn.expr.column = c.name;
|
|
424
|
+
return newColumn;
|
|
425
|
+
}),
|
|
426
|
+
}),
|
|
427
|
+
...(!baseAst?.from && {
|
|
428
|
+
from: [{ ...constants_1.defaultTable, table: initialTableName }],
|
|
429
|
+
}),
|
|
430
|
+
where: { ...newState },
|
|
431
|
+
});
|
|
432
|
+
});
|
|
433
|
+
};
|
|
434
|
+
// TODO: Merge this function with the updateFormData function
|
|
435
|
+
const updateActiveItem = (updates, { isDeletion = false, isInsertion = false, isReplaceSubtree = false, isAddVariant = false, isDeleteVariant = false, column = undefined, }) => {
|
|
436
|
+
let newState = (0, util_1.deepCopy)(activeEditItem);
|
|
437
|
+
updates.forEach(({ path, value }) => {
|
|
438
|
+
let current = newState;
|
|
439
|
+
const globalPath = path;
|
|
440
|
+
const paths = globalPath.split('.').filter((p) => p);
|
|
441
|
+
if (paths.length === 0 && !isInsertion && !isReplaceSubtree) {
|
|
442
|
+
setActiveEditItem(null);
|
|
443
|
+
return;
|
|
444
|
+
}
|
|
445
|
+
const isOperatorChange = paths[paths.length - 1] === 'operator';
|
|
446
|
+
let parent = null;
|
|
447
|
+
let parentKey = null;
|
|
448
|
+
for (let i = 0; i < paths.length - 1; i++) {
|
|
449
|
+
let currentPath = paths[i];
|
|
450
|
+
let index;
|
|
451
|
+
if (paths[i].includes('||')) {
|
|
452
|
+
const splitPath = paths[i].split('||');
|
|
453
|
+
currentPath = splitPath[0];
|
|
454
|
+
index = splitPath[1];
|
|
455
|
+
}
|
|
456
|
+
if (current[currentPath]) {
|
|
457
|
+
parent = current;
|
|
458
|
+
parentKey = currentPath;
|
|
459
|
+
current = current[currentPath];
|
|
460
|
+
}
|
|
461
|
+
if (index) {
|
|
462
|
+
current = current[parseInt(index)];
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
const lastKey = paths[paths.length - 1];
|
|
466
|
+
if (isDeletion) {
|
|
467
|
+
if (lastKey === 'left' || lastKey === 'right') {
|
|
468
|
+
if (parent) {
|
|
469
|
+
if (lastKey === 'right') {
|
|
470
|
+
parent[parentKey] = parent[parentKey].left;
|
|
471
|
+
}
|
|
472
|
+
else {
|
|
473
|
+
parent[parentKey] = parent[parentKey].right;
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
else {
|
|
477
|
+
delete current[lastKey];
|
|
478
|
+
if (newState?.left && !newState?.right) {
|
|
479
|
+
newState = newState.left;
|
|
480
|
+
}
|
|
481
|
+
else if (newState?.right && !newState?.left) {
|
|
482
|
+
newState = newState.right;
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
}
|
|
487
|
+
else if (isInsertion) {
|
|
488
|
+
const columns = getAllPossibleColumns();
|
|
489
|
+
const defaultColumn = columns[0].name;
|
|
490
|
+
// TODO: I think this is a bug, take a closer look here
|
|
491
|
+
newState = {
|
|
281
492
|
type: 'binary_expr',
|
|
282
493
|
operator: 'AND',
|
|
283
|
-
left:
|
|
284
|
-
type: 'binary_expr',
|
|
285
|
-
operator: '>=',
|
|
286
|
-
left: {
|
|
287
|
-
type: 'column_ref',
|
|
288
|
-
table: null,
|
|
289
|
-
column: column,
|
|
290
|
-
},
|
|
291
|
-
right: {
|
|
292
|
-
type: 'binary_expr',
|
|
293
|
-
operator: '-',
|
|
294
|
-
left: {
|
|
295
|
-
type: 'function',
|
|
296
|
-
name: 'now',
|
|
297
|
-
args: {
|
|
298
|
-
type: 'expr_list',
|
|
299
|
-
value: [],
|
|
300
|
-
},
|
|
301
|
-
},
|
|
302
|
-
right: {
|
|
303
|
-
type: 'interval',
|
|
304
|
-
expr: {
|
|
305
|
-
type: 'single_quote_string',
|
|
306
|
-
value: `${timeInterval} day`,
|
|
307
|
-
},
|
|
308
|
-
unit: '',
|
|
309
|
-
},
|
|
310
|
-
},
|
|
311
|
-
},
|
|
494
|
+
left: newState,
|
|
312
495
|
right: {
|
|
313
|
-
|
|
314
|
-
operator: '<=',
|
|
496
|
+
...constants_1.defaultEntry,
|
|
315
497
|
left: {
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
column: column,
|
|
319
|
-
},
|
|
320
|
-
right: {
|
|
321
|
-
type: 'function',
|
|
322
|
-
name: 'now',
|
|
323
|
-
args: {
|
|
324
|
-
type: 'expr_list',
|
|
325
|
-
value: [],
|
|
326
|
-
},
|
|
498
|
+
...constants_1.defaultEntry.left,
|
|
499
|
+
column: defaultColumn,
|
|
327
500
|
},
|
|
328
501
|
},
|
|
329
502
|
};
|
|
330
503
|
}
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
const
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
(0, react_1.useEffect)(() => {
|
|
405
|
-
removePivot();
|
|
406
|
-
setCreatedPivots([]);
|
|
407
|
-
setRecommendedPivots([]);
|
|
408
|
-
}, [selectedTable]);
|
|
409
|
-
const selectFilter = (index) => {
|
|
410
|
-
const filter = filters[index];
|
|
411
|
-
const matchingColumn = selectedTable.columns.find((column) => column.name === filter.column);
|
|
412
|
-
if (indexBeingEdited === index) {
|
|
413
|
-
setIndexBeingEdited(-1);
|
|
414
|
-
setStringFilterValues([]);
|
|
415
|
-
setNumberStart(0);
|
|
416
|
-
setNumberEnd(0);
|
|
417
|
-
setFilterDateRange((0, dateRangePickerUtils_1.getRangeFromPreset)('90d'));
|
|
418
|
-
return;
|
|
419
|
-
}
|
|
420
|
-
setSelectedColumn(matchingColumn);
|
|
421
|
-
if (filter.columnType === 'string') {
|
|
422
|
-
setStringFilterValues(filter.stringFilterValues);
|
|
423
|
-
setIndexBeingEdited(index);
|
|
424
|
-
}
|
|
425
|
-
else if (filter.columnType === 'number') {
|
|
426
|
-
setNumberStart(filter.numberStart);
|
|
427
|
-
setNumberEnd(filter.numberEnd);
|
|
428
|
-
setIndexBeingEdited(index);
|
|
429
|
-
}
|
|
430
|
-
else if (filter.columnType === 'date') {
|
|
431
|
-
setFilterDateRange(filter.filterDateRange);
|
|
432
|
-
setIndexBeingEdited(index);
|
|
433
|
-
}
|
|
504
|
+
else if (isAddVariant) {
|
|
505
|
+
const newVariant = (0, util_1.deepCopy)(constants_1.defaultVariant);
|
|
506
|
+
if (value) {
|
|
507
|
+
newVariant.args.value[0].value = value;
|
|
508
|
+
// if there is already a single default value there,
|
|
509
|
+
// let's remove it so when we push we replace.
|
|
510
|
+
if (current[lastKey].length === 1 &&
|
|
511
|
+
current[lastKey][0].args.value[0].value === '') {
|
|
512
|
+
current[lastKey].pop();
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
current[lastKey].push(newVariant);
|
|
516
|
+
}
|
|
517
|
+
else if (isDeleteVariant) {
|
|
518
|
+
if (value) {
|
|
519
|
+
const argList = current[lastKey];
|
|
520
|
+
argList.splice(argList.findIndex((arg) => arg.args.value[0].value === value), 1);
|
|
521
|
+
}
|
|
522
|
+
else {
|
|
523
|
+
current[lastKey].pop();
|
|
524
|
+
}
|
|
525
|
+
// add back in a phantom element to prevent app from crashing
|
|
526
|
+
// when the user removes all variants and hits save.
|
|
527
|
+
if (current[lastKey].length === 0) {
|
|
528
|
+
const newVariant = (0, util_1.deepCopy)(constants_1.defaultVariant);
|
|
529
|
+
newVariant.args.value[0].value = '';
|
|
530
|
+
current[lastKey].push(newVariant);
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
else if (isReplaceSubtree) {
|
|
534
|
+
if (lastKey) {
|
|
535
|
+
current[lastKey] = value;
|
|
536
|
+
}
|
|
537
|
+
else {
|
|
538
|
+
newState = value;
|
|
539
|
+
}
|
|
540
|
+
}
|
|
541
|
+
else if (isOperatorChange) {
|
|
542
|
+
const newOp = value;
|
|
543
|
+
const oldOp = current[lastKey];
|
|
544
|
+
if (operators_1.OPERATOR_GROUPS[oldOp] === operators_1.OPERATOR_GROUPS[newOp]) {
|
|
545
|
+
current[lastKey] = value;
|
|
546
|
+
}
|
|
547
|
+
else {
|
|
548
|
+
const group = operators_1.OPERATOR_GROUPS[newOp];
|
|
549
|
+
const subtree = (0, operators_1.getDefaultOperatorSubtrees)(group, newOp, column, '', client.databaseType);
|
|
550
|
+
if (parentKey) {
|
|
551
|
+
parent[parentKey] = (0, util_1.deepCopy)(subtree);
|
|
552
|
+
}
|
|
553
|
+
else {
|
|
554
|
+
newState = (0, util_1.deepCopy)(subtree);
|
|
555
|
+
}
|
|
556
|
+
}
|
|
557
|
+
}
|
|
558
|
+
else {
|
|
559
|
+
if (typeof current[lastKey] === 'object' && current[lastKey] !== null) {
|
|
560
|
+
current[lastKey].value = value;
|
|
561
|
+
}
|
|
562
|
+
else {
|
|
563
|
+
current[lastKey] = value;
|
|
564
|
+
}
|
|
565
|
+
}
|
|
566
|
+
});
|
|
567
|
+
// Function to immutably update or delete nodes based on their path
|
|
568
|
+
setActiveEditItem(newState);
|
|
569
|
+
};
|
|
570
|
+
const handleChange = (updates) => {
|
|
571
|
+
const callback = isPending ? updateActiveItem : updateFormData;
|
|
572
|
+
callback(updates, {});
|
|
573
|
+
};
|
|
574
|
+
const handleChangeText = (updates) => {
|
|
575
|
+
const callback = isPending ? updateActiveItem : updateFormData;
|
|
576
|
+
callback(updates, {});
|
|
434
577
|
};
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
578
|
+
// Function to handle operator changes
|
|
579
|
+
const handleOperatorChange = (value, node, keyPrefix, column = null) => {
|
|
580
|
+
if (isPending) {
|
|
581
|
+
updateActiveItem([{ path: keyPrefix + 'operator', value }], { column });
|
|
438
582
|
}
|
|
439
583
|
else {
|
|
440
|
-
|
|
441
|
-
}
|
|
442
|
-
}, [selectedPivot, data, dateRange]);
|
|
443
|
-
const removePivot = () => {
|
|
444
|
-
setSelectedPivotIndex(-1);
|
|
445
|
-
setSelectedPivot(null);
|
|
446
|
-
if (onChangePivot) {
|
|
447
|
-
onChangePivot(null, null, null);
|
|
584
|
+
updateFormData([{ path: keyPrefix + 'operator', value }], { column });
|
|
448
585
|
}
|
|
449
586
|
};
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
}
|
|
587
|
+
// Function to replace an entire subtree with a given value.
|
|
588
|
+
const handleReplaceSubtree = (keyPrefix, newValue, pending = isPending) => {
|
|
589
|
+
const callback = pending ? updateActiveItem : updateFormData;
|
|
590
|
+
callback([{ path: keyPrefix, value: newValue }], {
|
|
591
|
+
isReplaceSubtree: true,
|
|
592
|
+
});
|
|
457
593
|
};
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
setFilters((filters) => {
|
|
462
|
-
const newFilters = [...filters];
|
|
463
|
-
newFilters[index] = {
|
|
464
|
-
column: selectedColumn.name,
|
|
465
|
-
columnType,
|
|
466
|
-
stringFilterValues,
|
|
467
|
-
tag: `${selectedColumn.name} (${stringFilterValues.join(', ')})`,
|
|
468
|
-
};
|
|
469
|
-
return newFilters;
|
|
470
|
-
});
|
|
471
|
-
}
|
|
472
|
-
else if (columnType === 'number') {
|
|
473
|
-
setFilters((filters) => {
|
|
474
|
-
const newFilters = [...filters];
|
|
475
|
-
newFilters[index] = {
|
|
476
|
-
column: selectedColumn.name,
|
|
477
|
-
columnType,
|
|
478
|
-
numberStart,
|
|
479
|
-
numberEnd,
|
|
480
|
-
tag: `${numberStart} < ${selectedColumn.name} < ${numberEnd}`,
|
|
481
|
-
};
|
|
482
|
-
return newFilters;
|
|
483
|
-
});
|
|
484
|
-
}
|
|
485
|
-
else if (columnType === 'date') {
|
|
486
|
-
const label = filterDateRange[2]
|
|
487
|
-
? dateRangePickerUtils_1.reportBuilderOptions.find((elem) => elem.value === filterDateRange[2])?.text
|
|
488
|
-
: `${(0, date_fns_1.format)(new Date(filterDateRange[0]), 'MMM dd')} - ${(0, date_fns_1.format)(new Date(filterDateRange[1]), 'MMM dd')}`;
|
|
489
|
-
setFilters((filters) => {
|
|
490
|
-
const newFilters = [...filters];
|
|
491
|
-
newFilters[index] = {
|
|
492
|
-
column: selectedColumn.name,
|
|
493
|
-
columnType,
|
|
494
|
-
filterDateRange,
|
|
495
|
-
tag: `${selectedColumn.name} (${label})`,
|
|
496
|
-
};
|
|
497
|
-
return newFilters;
|
|
498
|
-
});
|
|
499
|
-
}
|
|
500
|
-
setStringFilterValues([]);
|
|
501
|
-
setNumberStart(0);
|
|
502
|
-
setNumberEnd(0);
|
|
503
|
-
setFilterDateRange((0, dateRangePickerUtils_1.getRangeFromPreset)('90d'));
|
|
504
|
-
setIndexBeingEdited(-1);
|
|
505
|
-
return;
|
|
506
|
-
}
|
|
594
|
+
// Function to handle the deletion of expressions
|
|
595
|
+
const handleDelete = (key) => {
|
|
596
|
+
updateFormData([{ path: path + key, value: null }], { isDeletion: true });
|
|
507
597
|
};
|
|
508
|
-
//
|
|
509
|
-
const
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
598
|
+
// Function to handle the insertion of expressions
|
|
599
|
+
const handleInsertion = (value, op = 'OR', isCondition = undefined) => {
|
|
600
|
+
updateFormData([{ path, value }], {
|
|
601
|
+
isInsertion: true,
|
|
602
|
+
topLevelBinOp: op,
|
|
603
|
+
isCondition,
|
|
604
|
+
});
|
|
605
|
+
};
|
|
606
|
+
// Function to handle the insertion of expr_list variants
|
|
607
|
+
const handleInsertVariant = (key, name = null) => {
|
|
608
|
+
// note: if name, treat that as the name of the value to insert
|
|
609
|
+
const callback = isPending ? updateActiveItem : updateFormData;
|
|
610
|
+
callback([{ path: path + key, value: name }], { isAddVariant: true });
|
|
611
|
+
};
|
|
612
|
+
// Function to handle the insertion of expr_list variants
|
|
613
|
+
const handleDeleteVariant = (key, name = null) => {
|
|
614
|
+
// note: if name, treat that as the name of the valeu to delete
|
|
615
|
+
const callback = isPending ? updateActiveItem : updateFormData;
|
|
616
|
+
callback([{ path: path + key, value: name }], { isDeleteVariant: true });
|
|
617
|
+
};
|
|
618
|
+
const getColumnValueForColumnComparison = (node) => node.left.value ??
|
|
619
|
+
node.left.column ??
|
|
620
|
+
node.left.args?.value[0]?.value ??
|
|
621
|
+
node.left.args?.value[0]?.column ??
|
|
622
|
+
undefined;
|
|
623
|
+
/**
|
|
624
|
+
* Searches for the column by name and returns the field type.
|
|
625
|
+
*
|
|
626
|
+
* Searches the known schema and returns the fieldType of the first column
|
|
627
|
+
* it can find with the given name. Will first search through the current
|
|
628
|
+
* list of fields in the current query if any, then will default to searching
|
|
629
|
+
* through the whole schema.
|
|
630
|
+
*
|
|
631
|
+
* If more than one column exist with the given name, it will return the first
|
|
632
|
+
* one that it finds. This might not be the one that you intended.
|
|
633
|
+
*
|
|
634
|
+
* TODO: pass an optional table param to limit the search to a given table.
|
|
635
|
+
*
|
|
636
|
+
* @param columnName the name to search for.
|
|
637
|
+
* @returns the fieldType string or undefined if not found.
|
|
638
|
+
*/
|
|
639
|
+
const getColumnTypeByName = (columnName) => {
|
|
640
|
+
const field = fields.find((f) => f.name === columnName);
|
|
641
|
+
if (field)
|
|
642
|
+
return (0, util_1.getPostgresBasicType)(field);
|
|
643
|
+
const column = schemaTables
|
|
644
|
+
.flatMap((t) => t.columns)
|
|
645
|
+
.find((c) => c.name === columnName);
|
|
646
|
+
return column?.fieldType;
|
|
647
|
+
};
|
|
648
|
+
/**
|
|
649
|
+
* Render form fields based on the type of the node
|
|
650
|
+
* @param node the AST or subtree to render recursively
|
|
651
|
+
* @param keyPrefix a stringified version of the path from the root
|
|
652
|
+
*
|
|
653
|
+
* Note: The keyPrefix should be separated by '.' characters and each item
|
|
654
|
+
* should be a valid index into the node (eg. 'left.right.value' is a valid
|
|
655
|
+
* keyPrefix but 'left.args[0].value' is not -- should be 'left.args.0.value')
|
|
656
|
+
*/
|
|
657
|
+
const renderNode = (node, keyPrefix = '') => {
|
|
658
|
+
const dateComparisonPartialMatch = (0, util_1.formatDateComparisonNode)(node);
|
|
659
|
+
switch (node.type) {
|
|
660
|
+
case 'binary_expr':
|
|
661
|
+
if (dateComparisonPartialMatch ||
|
|
662
|
+
((0, util_1.isDateTruncEquals)(node) && client.databaseType !== 'BigQuery')) {
|
|
663
|
+
const { dateColumn,
|
|
664
|
+
// see onChange callback handleChange
|
|
665
|
+
// eslint-disable-next-line no-unused-vars
|
|
666
|
+
dateColumnPath, dateFilterType, intervalCount, intervalType, intervalPaths, } = (0, util_1.getDateFilterInfo)(node);
|
|
667
|
+
const isPlural = intervalCount && intervalCount > 1 ? 's' : '';
|
|
668
|
+
// Pull off the string literal date for "equals" comparisons
|
|
669
|
+
const rawDateStringEquals = node.right?.value ??
|
|
670
|
+
node.right?.args?.value[1]?.column ??
|
|
671
|
+
node.right?.args?.value[1]?.value;
|
|
672
|
+
const rawDateStringEqualsPath = (node.right?.value && 'node.right.value') ??
|
|
673
|
+
(node.right?.args?.value[1]?.column &&
|
|
674
|
+
'node.right.args.value.1.column') ??
|
|
675
|
+
(node.right?.args?.value[1]?.value &&
|
|
676
|
+
'node.right.args.value.1.value');
|
|
677
|
+
return ((0, jsx_runtime_1.jsxs)("div", { style: { display: 'flex', gap: 20 }, children: [(0, jsx_runtime_1.jsx)(Select, { value: dateColumn, onChange: (value) => {
|
|
678
|
+
const columnType = getColumnTypeByName(value);
|
|
679
|
+
if ((0, ast_1.isDateishColumnType)(columnType)) {
|
|
680
|
+
// handleChange(value, keyPrefix + dateColumnPath, "text");
|
|
681
|
+
handleOperatorChange('IN_THE_LAST', node, keyPrefix, value);
|
|
682
|
+
}
|
|
683
|
+
else if ((0, ast_1.isNumericColumnType)(columnType)) {
|
|
684
|
+
const newSubtree = (0, util_1.deepCopy)(constants_1.defaultNumericComparison);
|
|
685
|
+
newSubtree.left.column = value;
|
|
686
|
+
handleReplaceSubtree(keyPrefix, newSubtree);
|
|
687
|
+
}
|
|
688
|
+
else {
|
|
689
|
+
const newSubtree = (0, util_1.deepCopy)(constants_1.defaultEntry);
|
|
690
|
+
newSubtree.left.args.value[0].column = value;
|
|
691
|
+
handleReplaceSubtree(keyPrefix, newSubtree);
|
|
692
|
+
}
|
|
693
|
+
}, options: getAllPossibleColumns().map((column) => ({
|
|
694
|
+
label: column.displayName,
|
|
695
|
+
value: column.name,
|
|
696
|
+
})) }), (0, jsx_runtime_1.jsx)(Select, { value: dateFilterType, onChange: (value) => {
|
|
697
|
+
if (value === dateFilterType)
|
|
698
|
+
return null;
|
|
699
|
+
let newSubtree = {};
|
|
700
|
+
// TODO: implement one for each database type (eg. pg, snowflake, etc.)
|
|
701
|
+
if (value === 'in the last') {
|
|
702
|
+
newSubtree = (0, postgres_1.generateLastNPeriodsPostgres)({
|
|
703
|
+
dateField: dateColumn,
|
|
704
|
+
intervalPeriod: `${intervalCount ?? 1} ${intervalType}`,
|
|
705
|
+
});
|
|
706
|
+
}
|
|
707
|
+
else if (value === 'in the previous') {
|
|
708
|
+
newSubtree = (0, postgres_1.generatePreviousPeriodPostgres)({
|
|
709
|
+
dateField: dateColumn,
|
|
710
|
+
intervalPeriod: `${intervalCount ?? 1} ${intervalType}`,
|
|
711
|
+
currentPeriod: intervalType,
|
|
712
|
+
});
|
|
713
|
+
}
|
|
714
|
+
else if (value === 'in the current') {
|
|
715
|
+
newSubtree = (0, postgres_1.generateCurrentPeriodPostgres)({
|
|
716
|
+
dateField: dateColumn,
|
|
717
|
+
currentPeriod: intervalType,
|
|
718
|
+
});
|
|
719
|
+
}
|
|
720
|
+
else if (value === 'equals') {
|
|
721
|
+
newSubtree = (0, postgres_1.generateEqualsPostgres)({
|
|
722
|
+
dateField: dateColumn,
|
|
723
|
+
currentPeriod: intervalType,
|
|
724
|
+
timestamp: '2024-01-01',
|
|
725
|
+
});
|
|
726
|
+
}
|
|
727
|
+
// replace the entire subtree for this filter
|
|
728
|
+
handleReplaceSubtree(keyPrefix, newSubtree);
|
|
729
|
+
}, options: [
|
|
730
|
+
{ label: 'in the last', value: 'in the last' },
|
|
731
|
+
{ label: 'in the previous', value: 'in the previous' },
|
|
732
|
+
{ label: 'in the current', value: 'in the current' },
|
|
733
|
+
{ label: 'equals', value: 'equals' },
|
|
734
|
+
] }), !['in the current', 'equals'].includes(dateFilterType) && ((0, jsx_runtime_1.jsx)(TextInput, { value: intervalCount, type: "number", style: { width: '70px' }, onChange: (e) => {
|
|
735
|
+
const isPluralNow = e.target.value > 1 ? 's' : '';
|
|
736
|
+
intervalPaths.forEach((intervalPath) => handleChangeText([
|
|
737
|
+
{
|
|
738
|
+
value: `${e.target.value} ${intervalType}${isPluralNow}`,
|
|
739
|
+
path: keyPrefix + intervalPath,
|
|
740
|
+
},
|
|
741
|
+
]));
|
|
742
|
+
} })), (0, jsx_runtime_1.jsx)(Select, { value: intervalType, onChange: (value) => {
|
|
743
|
+
if (intervalPaths.length === 1) {
|
|
744
|
+
handleChangeText([
|
|
745
|
+
{
|
|
746
|
+
value: intervalCount !== null
|
|
747
|
+
? `${intervalCount} ${value}${isPlural}`
|
|
748
|
+
: value,
|
|
749
|
+
path: keyPrefix + intervalPaths[0],
|
|
750
|
+
},
|
|
751
|
+
]);
|
|
752
|
+
return;
|
|
753
|
+
}
|
|
754
|
+
let newSubtree;
|
|
755
|
+
if (dateFilterType === 'equals') {
|
|
756
|
+
newSubtree = (0, postgres_1.generateEqualsPostgres)({
|
|
757
|
+
dateField: dateColumn,
|
|
758
|
+
currentPeriod: value,
|
|
759
|
+
timestamp: rawDateStringEquals,
|
|
760
|
+
});
|
|
761
|
+
}
|
|
762
|
+
else {
|
|
763
|
+
newSubtree = (0, postgres_1.generateCurrentPeriodPostgres)({
|
|
764
|
+
dateField: dateColumn,
|
|
765
|
+
currentPeriod: value,
|
|
766
|
+
});
|
|
767
|
+
}
|
|
768
|
+
handleReplaceSubtree(keyPrefix, newSubtree);
|
|
769
|
+
}, options: [
|
|
770
|
+
{ label: `year${isPlural}`, value: 'year' },
|
|
771
|
+
{ label: `quarter${isPlural}`, value: 'quarter' },
|
|
772
|
+
{ label: `month${isPlural}`, value: 'month' },
|
|
773
|
+
{ label: `week${isPlural}`, value: 'week' },
|
|
774
|
+
{ label: `day${isPlural}`, value: 'day' },
|
|
775
|
+
{ label: `hour${isPlural}`, value: 'hour' },
|
|
776
|
+
] }), dateFilterType === 'equals' && ((0, jsx_runtime_1.jsx)(TextInput, { value: rawDateStringEquals, type: "text", style: { width: '120px' }, onChange: (e) => {
|
|
777
|
+
handleChangeText([
|
|
778
|
+
{
|
|
779
|
+
value: e.target.value,
|
|
780
|
+
path: keyPrefix + rawDateStringEqualsPath,
|
|
781
|
+
},
|
|
782
|
+
]);
|
|
783
|
+
} }))] }));
|
|
784
|
+
}
|
|
785
|
+
else if ((0, util_1.isInTheLastInterval)(node, client.databaseType)) {
|
|
786
|
+
const { dateColumn, dateFilterType, intervalCount, intervalType } = (0, util_1.getDateFilterInfo)(node);
|
|
787
|
+
const options = getAllPossibleColumns().map((column) => ({
|
|
788
|
+
label: column.displayName,
|
|
789
|
+
value: column.name,
|
|
790
|
+
}));
|
|
791
|
+
const plural = node.right.args.value[1].expr.value > 1 ? 's' : '';
|
|
792
|
+
return ((0, jsx_runtime_1.jsxs)("div", { style: {
|
|
793
|
+
display: 'flex',
|
|
794
|
+
flexDirection: 'row',
|
|
795
|
+
alignItems: 'center',
|
|
796
|
+
gap: 20,
|
|
797
|
+
}, children: [(0, jsx_runtime_1.jsx)("div", { children: (0, jsx_runtime_1.jsx)(Select, { value: node.left.column, onChange: (value) => {
|
|
798
|
+
const columnType = getColumnTypeByName(value);
|
|
799
|
+
if ((0, ast_1.isDateishColumnType)(columnType)) {
|
|
800
|
+
// handleChange(value, keyPrefix + dateColumnPath, "text");
|
|
801
|
+
handleOperatorChange('IN_THE_LAST', node, keyPrefix, value);
|
|
802
|
+
}
|
|
803
|
+
else if ((0, ast_1.isNumericColumnType)(columnType)) {
|
|
804
|
+
const newSubtree = (0, util_1.deepCopy)(constants_1.defaultNumericComparison);
|
|
805
|
+
newSubtree.left.column = value;
|
|
806
|
+
handleReplaceSubtree(keyPrefix, newSubtree);
|
|
807
|
+
}
|
|
808
|
+
else {
|
|
809
|
+
const newSubtree = (0, util_1.deepCopy)(constants_1.defaultEntry);
|
|
810
|
+
newSubtree.left.args.value[0].column = value;
|
|
811
|
+
handleReplaceSubtree(keyPrefix, newSubtree);
|
|
812
|
+
}
|
|
813
|
+
}, options: options }) }), (0, jsx_runtime_1.jsx)(Select, { value: dateFilterType, onChange: (value) => {
|
|
814
|
+
handleOperatorChange(value, node, keyPrefix, dateColumn);
|
|
815
|
+
}, options: [
|
|
816
|
+
{ label: 'in the last', value: 'IN_THE_LAST' },
|
|
817
|
+
{ label: 'in the previous', value: 'IN_THE_PREVIOUS' },
|
|
818
|
+
{ label: 'in the current', value: 'IN_THE_CURRENT' },
|
|
819
|
+
// { label: 'equals', value: 'equals' },
|
|
820
|
+
] }), (0, jsx_runtime_1.jsx)(TextInput, { defaultValue: node.right.args.value[1].expr.value, type: "number", style: { width: '120px', height: '42px' }, min: 0, onBlur: (e) => {
|
|
821
|
+
handleChange([
|
|
822
|
+
{
|
|
823
|
+
value: e.target.value,
|
|
824
|
+
path: keyPrefix + 'right.args.value||1.expr.value',
|
|
825
|
+
},
|
|
826
|
+
]);
|
|
827
|
+
} }), (0, jsx_runtime_1.jsx)("div", { children: (0, jsx_runtime_1.jsx)(Select, { value: node.right.args.value[1].unit, onChange: (value) => handleChange([
|
|
828
|
+
{ value, path: keyPrefix + 'right.args.value||1.unit' },
|
|
829
|
+
]), options: [
|
|
830
|
+
{ label: `year${plural}`, value: '* 365 DAY' },
|
|
831
|
+
{ label: `month${plural}`, value: '* 30 DAY' },
|
|
832
|
+
{ label: `week${plural}`, value: '* 7 DAY' },
|
|
833
|
+
{ label: `day${plural}`, value: 'DAY' },
|
|
834
|
+
] }) })] }));
|
|
835
|
+
}
|
|
836
|
+
else if ((0, util_1.isTheCurrentInterval)(node, client.databaseType)) {
|
|
837
|
+
const { dateFilterType } = (0, util_1.getDateFilterInfo)(node);
|
|
838
|
+
const options = getAllPossibleColumns().map((column) => ({
|
|
839
|
+
label: column.displayName,
|
|
840
|
+
value: column.name,
|
|
841
|
+
}));
|
|
842
|
+
return ((0, jsx_runtime_1.jsxs)("div", { style: {
|
|
843
|
+
display: 'flex',
|
|
844
|
+
flexDirection: 'row',
|
|
845
|
+
alignItems: 'center',
|
|
846
|
+
gap: 20,
|
|
847
|
+
}, children: [(0, jsx_runtime_1.jsx)(Select, { value: node.left.column, onChange: (value) => {
|
|
848
|
+
const columnType = getColumnTypeByName(value);
|
|
849
|
+
if ((0, ast_1.isDateishColumnType)(columnType)) {
|
|
850
|
+
// handleChange(value, keyPrefix + dateColumnPath, "text");
|
|
851
|
+
handleOperatorChange('IN_THE_LAST', node, keyPrefix, value);
|
|
852
|
+
}
|
|
853
|
+
else if ((0, ast_1.isNumericColumnType)(columnType)) {
|
|
854
|
+
const newSubtree = (0, util_1.deepCopy)(constants_1.defaultNumericComparison);
|
|
855
|
+
newSubtree.left.column = value;
|
|
856
|
+
handleReplaceSubtree(keyPrefix, newSubtree);
|
|
857
|
+
}
|
|
858
|
+
else {
|
|
859
|
+
const newSubtree = (0, util_1.deepCopy)(constants_1.defaultEntry);
|
|
860
|
+
newSubtree.left.args.value[0].column = value;
|
|
861
|
+
handleReplaceSubtree(keyPrefix, newSubtree);
|
|
862
|
+
}
|
|
863
|
+
}, options: options }), (0, jsx_runtime_1.jsx)(Select, { value: 'IN_THE_CURRENT', onChange: (value) => {
|
|
864
|
+
handleOperatorChange(value, node, keyPrefix, node.left.column);
|
|
865
|
+
}, options: [
|
|
866
|
+
{ label: 'in the last', value: 'IN_THE_LAST' },
|
|
867
|
+
{ label: 'in the previous', value: 'IN_THE_PREVIOUS' },
|
|
868
|
+
{ label: 'in the current', value: 'IN_THE_CURRENT' },
|
|
869
|
+
// { label: 'equals', value: 'equals' },
|
|
870
|
+
] }), (0, jsx_runtime_1.jsx)(Select, { value: node.left.args.value[1].column, onChange: (value) => {
|
|
871
|
+
handleChange([
|
|
872
|
+
{ value, path: 'right.args.value||1.column' },
|
|
873
|
+
{ value, path: 'left.args.value||1.column' },
|
|
874
|
+
]);
|
|
875
|
+
}, options: [
|
|
876
|
+
{ label: `year`, value: 'YEAR' },
|
|
877
|
+
{ label: `quarter`, value: 'QUARTER' },
|
|
878
|
+
{ label: `month`, value: 'MONTH' },
|
|
879
|
+
{ label: `week`, value: 'WEEK' },
|
|
880
|
+
] })] }));
|
|
881
|
+
}
|
|
882
|
+
else if ((0, util_1.isThePreviousInterval)(node, client.databaseType)) {
|
|
883
|
+
const options = getAllPossibleColumns().map((column) => ({
|
|
884
|
+
label: column.displayName,
|
|
885
|
+
value: column.name,
|
|
886
|
+
}));
|
|
887
|
+
return ((0, jsx_runtime_1.jsxs)("div", { style: {
|
|
888
|
+
display: 'flex',
|
|
889
|
+
flexDirection: 'row',
|
|
890
|
+
alignItems: 'center',
|
|
891
|
+
gap: 20,
|
|
892
|
+
}, children: [(0, jsx_runtime_1.jsx)(Select, { value: node.left.column, onChange: (value) => {
|
|
893
|
+
const columnType = getColumnTypeByName(value);
|
|
894
|
+
if ((0, ast_1.isDateishColumnType)(columnType)) {
|
|
895
|
+
// handleChange(value, keyPrefix + dateColumnPath, "text");
|
|
896
|
+
handleOperatorChange('IN_THE_LAST', node, keyPrefix, value);
|
|
897
|
+
}
|
|
898
|
+
else if ((0, ast_1.isNumericColumnType)(columnType)) {
|
|
899
|
+
const newSubtree = (0, util_1.deepCopy)(constants_1.defaultNumericComparison);
|
|
900
|
+
newSubtree.left.column = value;
|
|
901
|
+
handleReplaceSubtree(keyPrefix, newSubtree);
|
|
902
|
+
}
|
|
903
|
+
else {
|
|
904
|
+
const newSubtree = (0, util_1.deepCopy)(constants_1.defaultEntry);
|
|
905
|
+
newSubtree.left.args.value[0].column = value;
|
|
906
|
+
handleReplaceSubtree(keyPrefix, newSubtree);
|
|
907
|
+
}
|
|
908
|
+
}, opt: true, options: options }), (0, jsx_runtime_1.jsx)(Select, { value: 'IN_THE_PREVIOUS', onChange: (value) => {
|
|
909
|
+
handleOperatorChange(value, node, keyPrefix, node.left.column);
|
|
910
|
+
}, options: [
|
|
911
|
+
{ label: 'in the last', value: 'IN_THE_LAST' },
|
|
912
|
+
{ label: 'in the previous', value: 'IN_THE_PREVIOUS' },
|
|
913
|
+
{ label: 'in the current', value: 'IN_THE_CURRENT' },
|
|
914
|
+
// { label: 'equals', value: 'equals' },
|
|
915
|
+
] }), (0, jsx_runtime_1.jsx)(Select, { value: node.left.args.value[1].column, onChange: (value) => {
|
|
916
|
+
const dayConversion = {
|
|
917
|
+
YEAR: 365,
|
|
918
|
+
QUARTER: 90,
|
|
919
|
+
MONTH: 30,
|
|
920
|
+
WEEK: 7,
|
|
921
|
+
};
|
|
922
|
+
handleChange([
|
|
923
|
+
{
|
|
924
|
+
value,
|
|
925
|
+
path: 'left.args.value||1.column',
|
|
926
|
+
},
|
|
927
|
+
{
|
|
928
|
+
value,
|
|
929
|
+
path: 'right.args.value||1.column',
|
|
930
|
+
},
|
|
931
|
+
{
|
|
932
|
+
value: dayConversion[value] || 30,
|
|
933
|
+
path: 'right.args.value||0.args.value||1.expr.value',
|
|
934
|
+
},
|
|
935
|
+
]);
|
|
936
|
+
}, options: [
|
|
937
|
+
{ label: `year`, value: 'YEAR' },
|
|
938
|
+
{ label: `quarter`, value: 'QUARTER' },
|
|
939
|
+
{ label: `month`, value: 'MONTH' },
|
|
940
|
+
{ label: `week`, value: 'WEEK' },
|
|
941
|
+
] })] }));
|
|
942
|
+
}
|
|
943
|
+
else if ((0, util_1.isColumnComparison)(node)) {
|
|
944
|
+
const options = getAllPossibleColumns().map((column) => ({
|
|
945
|
+
label: column.displayName,
|
|
946
|
+
value: column.name,
|
|
947
|
+
}));
|
|
948
|
+
// grab the value of the left child of the column comparison
|
|
949
|
+
// operator (ie. the column name)
|
|
950
|
+
const leftChildValue = getColumnValueForColumnComparison(node);
|
|
951
|
+
const column = schemaTables
|
|
952
|
+
.flatMap((t) => t.columns)
|
|
953
|
+
.find((col) => col.name === leftChildValue);
|
|
954
|
+
const tables = (0, ast_1.getTableNames)(baseAst);
|
|
955
|
+
const table = tables.length === 1 ? tables[0] : initialTableName;
|
|
956
|
+
const columnType = column?.fieldType;
|
|
957
|
+
const operatorOptions = [
|
|
958
|
+
...((0, ast_1.isNumericColumnType)(columnType)
|
|
959
|
+
? [
|
|
960
|
+
{ label: 'equal to', value: '=' },
|
|
961
|
+
{ label: 'not equal to', value: '!=' },
|
|
962
|
+
{ label: 'greater than', value: '>' },
|
|
963
|
+
{ label: 'less than', value: '<' },
|
|
964
|
+
{ label: 'greater than or equal to', value: '>=' },
|
|
965
|
+
{ label: 'less than or equal to', value: '<=' },
|
|
966
|
+
{ label: 'is not null', value: 'IS NOT' },
|
|
967
|
+
{ label: 'is null', value: 'IS' },
|
|
968
|
+
]
|
|
969
|
+
: []),
|
|
970
|
+
...((0, ast_1.isTextColumnType)(columnType)
|
|
971
|
+
? [
|
|
972
|
+
{ label: 'is', value: 'LIKE' },
|
|
973
|
+
{ label: 'is not', value: 'NOT LIKE' },
|
|
974
|
+
{ label: 'is one of', value: 'IN' },
|
|
975
|
+
{ label: 'is not one of', value: 'NOT IN' },
|
|
976
|
+
{ label: 'is not null', value: 'IS NOT' },
|
|
977
|
+
{ label: 'is null', value: 'IS' },
|
|
978
|
+
]
|
|
979
|
+
: []),
|
|
980
|
+
...((0, ast_1.isDateishColumnType)(columnType)
|
|
981
|
+
? [
|
|
982
|
+
{ label: 'in the last', value: 'IN_THE_LAST' },
|
|
983
|
+
{
|
|
984
|
+
label: 'in the previous',
|
|
985
|
+
value: 'IN_THE_PREVIOUS',
|
|
986
|
+
},
|
|
987
|
+
{ label: 'in the current', value: 'IN_THE_CURRENT' },
|
|
988
|
+
{ label: 'equals', value: 'equals' },
|
|
989
|
+
{ label: 'is not null', value: 'IS NOT' },
|
|
990
|
+
{ label: 'is null', value: 'IS' },
|
|
991
|
+
]
|
|
992
|
+
: []),
|
|
523
993
|
];
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
994
|
+
return ((0, jsx_runtime_1.jsxs)("div", { style: {
|
|
995
|
+
display: 'flex',
|
|
996
|
+
gap: 12,
|
|
997
|
+
flexDirection: 'column',
|
|
998
|
+
width: '100%',
|
|
999
|
+
}, children: [(0, jsx_runtime_1.jsxs)("div", { style: {
|
|
1000
|
+
display: 'flex',
|
|
1001
|
+
gap: 20,
|
|
1002
|
+
// justifyContent: "space-between",
|
|
1003
|
+
flexDirection: (0, util_1.showNodeAsRow)(node, formData)
|
|
1004
|
+
? 'row'
|
|
1005
|
+
: 'column',
|
|
1006
|
+
width: '100%',
|
|
1007
|
+
}, children: [(0, jsx_runtime_1.jsx)(Select, { style: { width: 'min-content' }, value: leftChildValue, onChange: (value) => {
|
|
1008
|
+
const columnType = getColumnTypeByName(value);
|
|
1009
|
+
if ((0, ast_1.isDateishColumnType)(columnType)) {
|
|
1010
|
+
handleOperatorChange('IN_THE_LAST', node, keyPrefix, value);
|
|
1011
|
+
}
|
|
1012
|
+
else if ((0, ast_1.isNumericColumnType)(columnType)) {
|
|
1013
|
+
const newSubtree = (0, util_1.deepCopy)(constants_1.defaultNumericComparison);
|
|
1014
|
+
newSubtree.left.column = value;
|
|
1015
|
+
handleReplaceSubtree(keyPrefix, newSubtree);
|
|
1016
|
+
}
|
|
1017
|
+
else {
|
|
1018
|
+
const newSubtree = (0, util_1.deepCopy)(constants_1.defaultEntry);
|
|
1019
|
+
newSubtree.left.args.value[0].column = value;
|
|
1020
|
+
handleReplaceSubtree(keyPrefix, newSubtree);
|
|
1021
|
+
}
|
|
1022
|
+
}, options: options }), operatorOptions.length > 0 && ((0, jsx_runtime_1.jsx)(Select, { value: node.operator, onChange: (value) => {
|
|
1023
|
+
handleOperatorChange(value, node, keyPrefix, leftChildValue);
|
|
1024
|
+
}, style: { width: 'min-content' }, options: operatorOptions })), node.right &&
|
|
1025
|
+
node.right.type !== 'expr_list' &&
|
|
1026
|
+
renderNode(node.right, keyPrefix + 'right.')] }, keyPrefix), node.right && node.right.type === 'expr_list' && ((0, jsx_runtime_1.jsx)("div", { style: {
|
|
1027
|
+
display: 'grid',
|
|
1028
|
+
gridTemplateColumns: 'repeat(2, 1fr)',
|
|
1029
|
+
gap: 12,
|
|
1030
|
+
}, children: Object.keys(uniqueValues[table][leftChildValue] ?? {}).map((key) => ((0, jsx_runtime_1.jsxs)("label", { style: { display: 'flex', gap: 2 }, children: [(0, jsx_runtime_1.jsx)(Checkbox, { checked: uniqueValues[table][leftChildValue][key], onChange: (checked) => {
|
|
1031
|
+
const newValues = (0, util_1.deepCopy)(uniqueValues);
|
|
1032
|
+
newValues[table][leftChildValue][key] = checked;
|
|
1033
|
+
setUniqueValues(newValues);
|
|
1034
|
+
if (checked) {
|
|
1035
|
+
handleInsertVariant(keyPrefix + 'right.' + 'value', key);
|
|
1036
|
+
}
|
|
1037
|
+
else {
|
|
1038
|
+
handleDeleteVariant(keyPrefix + 'right.' + 'value', key);
|
|
1039
|
+
}
|
|
1040
|
+
} }), (0, jsx_runtime_1.jsx)("span", { children: key })] }, key))) }, keyPrefix + 'right.'))] }));
|
|
1041
|
+
}
|
|
1042
|
+
else {
|
|
1043
|
+
const columnName = node.left.column;
|
|
1044
|
+
const column = schemaTables[0]?.columns.find((col) => col.name === columnName);
|
|
1045
|
+
const columnType = column?.fieldType;
|
|
1046
|
+
return ((0, jsx_runtime_1.jsxs)("div", { style: {
|
|
1047
|
+
display: 'flex',
|
|
1048
|
+
gap: 12,
|
|
1049
|
+
justifyContent: 'space-between',
|
|
1050
|
+
flexDirection: (0, util_1.showNodeAsRow)(node, formData) ? 'row' : 'column',
|
|
1051
|
+
width: '100%',
|
|
1052
|
+
}, children: [node.left && renderNode(node.left, keyPrefix + 'left.'), (0, jsx_runtime_1.jsx)(Select, { value: node.operator, onChange: (value) => {
|
|
1053
|
+
handleOperatorChange(value, node, keyPrefix);
|
|
1054
|
+
}, style: { width: `100%` }, options: [
|
|
1055
|
+
// { label: `and`, value: "AND" },
|
|
1056
|
+
// { label: `or`, value: "OR" },
|
|
1057
|
+
...((0, ast_1.isNumericColumnType)(columnType)
|
|
1058
|
+
? [
|
|
1059
|
+
{ label: 'equal to', value: '=' },
|
|
1060
|
+
{ label: 'not equal to', value: '!=' },
|
|
1061
|
+
{ label: 'greater than', value: '>' },
|
|
1062
|
+
{ label: 'less than', value: '<' },
|
|
1063
|
+
{ label: 'greater than or equal to', value: '>=' },
|
|
1064
|
+
{ label: 'less than or equal to', value: '<=' },
|
|
1065
|
+
{ label: 'is not null', value: 'IS NOT' },
|
|
1066
|
+
{ label: 'is null', value: 'IS' },
|
|
1067
|
+
]
|
|
1068
|
+
: []),
|
|
1069
|
+
...((0, ast_1.isTextColumnType)(columnType)
|
|
1070
|
+
? [
|
|
1071
|
+
{ label: 'is', value: 'LIKE' },
|
|
1072
|
+
{ label: 'is not', value: 'NOT LIKE' },
|
|
1073
|
+
{ label: 'is one of', value: 'IN' },
|
|
1074
|
+
{ label: 'is not one of', value: 'NOT IN' },
|
|
1075
|
+
{ label: 'is not null', value: 'IS NOT' },
|
|
1076
|
+
{ label: 'is null', value: 'IS' },
|
|
1077
|
+
]
|
|
1078
|
+
: []),
|
|
1079
|
+
...((0, ast_1.isDateishColumnType)(columnType)
|
|
1080
|
+
? [
|
|
1081
|
+
{ label: 'in the last', value: 'IN_THE_LAST' },
|
|
1082
|
+
{ label: 'in the previous', value: 'IN_THE_PREVIOUS' },
|
|
1083
|
+
{ label: 'in the current', value: 'IN_THE_CURRENT' },
|
|
1084
|
+
{ label: 'is not null', value: 'IS NOT' },
|
|
1085
|
+
{ label: 'is null', value: 'IS' },
|
|
1086
|
+
]
|
|
1087
|
+
: []),
|
|
1088
|
+
// { label: `minus`, value: "-" },
|
|
1089
|
+
// { label: `plus`, value: "+" },
|
|
1090
|
+
] }), node.right && renderNode(node.right, keyPrefix + 'right.')] }, keyPrefix));
|
|
1091
|
+
}
|
|
1092
|
+
case 'column_ref': {
|
|
1093
|
+
const options = getAllPossibleColumns().map((column) => ({
|
|
1094
|
+
label: column.displayName,
|
|
1095
|
+
value: column.name,
|
|
1096
|
+
}));
|
|
1097
|
+
return ((0, jsx_runtime_1.jsx)(Select, { style: { width: '120px' }, value: node.column ?? options[0]?.value, onChange: (value) => {
|
|
1098
|
+
handleChange([{ value, path: keyPrefix + 'column' }]);
|
|
1099
|
+
}, options: options }));
|
|
530
1100
|
}
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
setNumberEnd(0);
|
|
547
|
-
setFilterDateRange((0, dateRangePickerUtils_1.getRangeFromPreset)('90d'));
|
|
548
|
-
return;
|
|
1101
|
+
case 'expr_list': {
|
|
1102
|
+
const len = node.value.length;
|
|
1103
|
+
return ((0, jsx_runtime_1.jsxs)("div", { style: { display: 'flex', flexDirection: 'row', gap: 12 }, children: [node.value.map((elem, index) => {
|
|
1104
|
+
if (elem.value) {
|
|
1105
|
+
return ((0, jsx_runtime_1.jsx)(TextInput, { type: elem.type === 'number' ? 'number' : 'text', defaultValue: elem.value, onBlur: (e) => handleChange([
|
|
1106
|
+
{
|
|
1107
|
+
value: elem.type === 'number'
|
|
1108
|
+
? parseFloat(e.target.value)
|
|
1109
|
+
: e.target.value,
|
|
1110
|
+
path: keyPrefix + `value.${index}.`,
|
|
1111
|
+
},
|
|
1112
|
+
]) }, `input_${index}`));
|
|
1113
|
+
}
|
|
1114
|
+
return renderNode(elem, keyPrefix + `value.${index}.`);
|
|
1115
|
+
}), len > 1 && ((0, jsx_runtime_1.jsx)(SecondaryButton, { onClick: () => handleDeleteVariant(keyPrefix + 'value'), children: "-" })), (0, jsx_runtime_1.jsx)(SecondaryButton, { onClick: () => handleInsertVariant(keyPrefix + 'value'), children: "+" })] }, keyPrefix));
|
|
549
1116
|
}
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
: `${(0, date_fns_1.format)(new Date(filterDateRange[0]), 'MMM dd')} - ${(0, date_fns_1.format)(new Date(filterDateRange[1]), 'MMM dd')}`;
|
|
554
|
-
setFilters((filters) => {
|
|
555
|
-
return [
|
|
556
|
-
...filters,
|
|
1117
|
+
case 'double_quote_string':
|
|
1118
|
+
case 'single_quote_string':
|
|
1119
|
+
return ((0, jsx_runtime_1.jsx)(TextInput, { type: "text", defaultValue: node.value.replaceAll('%', ''), style: { width: '120px' }, onBlur: (e) => handleChange([
|
|
557
1120
|
{
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
1121
|
+
value: node.type === 'number'
|
|
1122
|
+
? parseFloat(e.target.value)
|
|
1123
|
+
: e.target.value,
|
|
1124
|
+
path: keyPrefix + 'value',
|
|
562
1125
|
},
|
|
563
|
-
];
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
1126
|
+
]) }));
|
|
1127
|
+
case 'null':
|
|
1128
|
+
return (0, jsx_runtime_1.jsx)("div", {});
|
|
1129
|
+
case 'number':
|
|
1130
|
+
return ((0, jsx_runtime_1.jsx)(TextInput, { defaultValue: node.value, type: "number", style: { width: '120px' }, onBlur: (e) => {
|
|
1131
|
+
handleChange([
|
|
1132
|
+
{
|
|
1133
|
+
value: node.type === 'number'
|
|
1134
|
+
? parseFloat(e.target.value)
|
|
1135
|
+
: e.target.value,
|
|
1136
|
+
path: keyPrefix + 'value',
|
|
1137
|
+
},
|
|
1138
|
+
]);
|
|
1139
|
+
} }));
|
|
1140
|
+
case 'function':
|
|
1141
|
+
if (!node.args) {
|
|
1142
|
+
return (0, jsx_runtime_1.jsx)("label", {});
|
|
1143
|
+
}
|
|
1144
|
+
else if (node.args.type === 'expr_list' &&
|
|
1145
|
+
node.args.value.length === 1) {
|
|
1146
|
+
return ((0, jsx_runtime_1.jsx)("div", { style: { display: 'flex', flexDirection: 'row' }, children: node.args.value[0] &&
|
|
1147
|
+
renderNode(node.args.value[0], keyPrefix + 'args.value.0.') }));
|
|
1148
|
+
}
|
|
1149
|
+
else if (node.args.type === 'expr_list' &&
|
|
1150
|
+
node.args.value.length === 2) {
|
|
1151
|
+
return ((0, jsx_runtime_1.jsxs)("div", { style: { display: 'flex', flexDirection: 'row', gap: 20 }, children: [node.args.value[0] &&
|
|
1152
|
+
renderNode(node.args.value[0], keyPrefix + 'args.value.0.'), node.args.value[1] &&
|
|
1153
|
+
renderNode(node.args.value[1], keyPrefix + 'args.value.1.')] }));
|
|
1154
|
+
}
|
|
1155
|
+
return node.name;
|
|
1156
|
+
case 'interval':
|
|
1157
|
+
return ((0, jsx_runtime_1.jsx)("div", { style: { display: 'flex', flexDirection: 'row', gap: 20 }, children: renderNode(node.expr, keyPrefix + 'expr.') }));
|
|
1158
|
+
default:
|
|
1159
|
+
return null;
|
|
570
1160
|
}
|
|
571
1161
|
};
|
|
572
|
-
const
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
1162
|
+
const renderSentence = (formData, node, keyPrefix = '',
|
|
1163
|
+
// @depreciated TODO: remove next update
|
|
1164
|
+
// eslint-disable-next-line no-unused-vars
|
|
1165
|
+
isTopLevel = false,
|
|
1166
|
+
// @depreciated TODO: remove next update
|
|
1167
|
+
// eslint-disable-next-line no-unused-vars
|
|
1168
|
+
isTopLevelAnd = false, isParentRow = false) => {
|
|
1169
|
+
const isInTheCurrentIntervalSentence = (0, util_1.isTheCurrentInterval)(node, client.databaseType)
|
|
1170
|
+
? (0, util_1.getInTheCurrentIntervalSentence)(node, client.databaseType)
|
|
1171
|
+
: null;
|
|
1172
|
+
const isInTheLastIntervalSentence = (0, util_1.isInTheLastInterval)(node, client.databaseType)
|
|
1173
|
+
? (0, util_1.getInTheLastIntervalSentence)(node, client.databaseType)
|
|
1174
|
+
: null;
|
|
1175
|
+
const isInThePreviousIntervalSentence = (0, util_1.isThePreviousInterval)(node, client.databaseType)
|
|
1176
|
+
? (0, util_1.getInThePreviousIntervalSentence)(node, client.databaseType)
|
|
1177
|
+
: null;
|
|
1178
|
+
let dateComparisonPartialMatch = null;
|
|
1179
|
+
let dateEqualityPartialMatch = null;
|
|
1180
|
+
if (client.databaseType !== 'BigQuery') {
|
|
1181
|
+
dateComparisonPartialMatch = (0, util_1.formatDateComparisonNode)(node);
|
|
1182
|
+
dateEqualityPartialMatch = (0, util_1.tryConvertDateEquality)(node, client.databaseType);
|
|
1183
|
+
}
|
|
1184
|
+
const isRow = !(0, util_1.isTopLevelBoolean)(node);
|
|
1185
|
+
const isCard = isRow && !isParentRow;
|
|
1186
|
+
const OPS = {
|
|
1187
|
+
AND: 'and',
|
|
1188
|
+
OR: 'or',
|
|
1189
|
+
LIKE: 'is',
|
|
1190
|
+
BETWEEN: 'is between',
|
|
1191
|
+
IN: 'is one of',
|
|
1192
|
+
'NOT IN': 'is not one of',
|
|
1193
|
+
'NOT LIKE': 'is not',
|
|
1194
|
+
'!=': 'is not',
|
|
1195
|
+
'=': 'is',
|
|
1196
|
+
'<': 'is less than',
|
|
1197
|
+
'>': 'is greater than',
|
|
1198
|
+
'<=': 'is less than or equal to',
|
|
1199
|
+
'>=': 'is greater than or equal to',
|
|
1200
|
+
'<>': 'is not',
|
|
1201
|
+
'-': 'minus',
|
|
1202
|
+
'IS NOT': 'is not',
|
|
1203
|
+
IS: 'is ',
|
|
1204
|
+
};
|
|
1205
|
+
switch (node.type) {
|
|
1206
|
+
case 'binary_expr':
|
|
1207
|
+
return ((0, jsx_runtime_1.jsx)(ui_1.TagWrapper, { keyPrefix: keyPrefix, formData: formData, activeEditItem: activeEditItem, setEditPopoverKey: setEditPopoverKey, setActiveEditItem: setActiveEditItem, setCheckboxes: setCheckboxes, handleReplaceSubtree: handleReplaceSubtree, FilterPopover: FilterPopover, setActivePath: setActivePath, setOpenPopover: setOpenPopover, setIsPending: setIsPending, clearCheckboxes: clearCheckboxes, fetchSqlQuery: fetchSqlQuery, handleDelete: handleDelete, editPopoverKey: editPopoverKey, isCard: isCard, isRow: isRow, getByKey: getByKey, node: node, EditPopover: ui_1.EditPopover, Button: Button, renderNode: renderNode, Popover: Popover, style: {
|
|
1208
|
+
display: 'flex',
|
|
1209
|
+
gap: 3,
|
|
1210
|
+
flexDirection: isRow ? 'row' : 'column',
|
|
1211
|
+
padding: '1px',
|
|
1212
|
+
border: isCard ? '1px solid black' : 'none',
|
|
1213
|
+
whiteSpace: 'nowrap',
|
|
1214
|
+
overflow: 'hidden',
|
|
1215
|
+
textOverflow: 'ellipsis',
|
|
1216
|
+
}, children: dateComparisonPartialMatch ??
|
|
1217
|
+
dateEqualityPartialMatch ??
|
|
1218
|
+
isInTheCurrentIntervalSentence ??
|
|
1219
|
+
isInTheLastIntervalSentence ??
|
|
1220
|
+
isInThePreviousIntervalSentence ?? ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [node.left &&
|
|
1221
|
+
renderSentence(formData, node.left, keyPrefix + 'left.', false, false, isRow), isRow ? (' ' + OPS[node.operator] + ' ') : topLevelBinaryOperator === 'OR' ? ((0, jsx_runtime_1.jsx)(TopLevelBooleanSwitch, { node: node, keyPrefix: keyPrefix, handleOperatorChange: handleOperatorChange, Select: Select })) : null, node.right &&
|
|
1222
|
+
renderSentence(formData, node.right, keyPrefix + 'right.', false, false, isRow)] })) }));
|
|
1223
|
+
case 'column_ref':
|
|
1224
|
+
return node.column;
|
|
1225
|
+
case 'expr_list':
|
|
1226
|
+
if (node.value.length === 1) {
|
|
1227
|
+
const subQuery = renderSentence(formData, node.value[0]);
|
|
1228
|
+
if (subQuery) {
|
|
1229
|
+
return `${subQuery}`;
|
|
1230
|
+
}
|
|
1231
|
+
return '()';
|
|
1232
|
+
}
|
|
1233
|
+
return `${node.value
|
|
1234
|
+
.map((elem) => renderSentence(formData, elem))
|
|
1235
|
+
.join(', ')}`;
|
|
1236
|
+
case 'single_quote_string':
|
|
1237
|
+
return node.value.replaceAll('%', '');
|
|
1238
|
+
case 'double_quote_string':
|
|
1239
|
+
case 'number':
|
|
1240
|
+
return node.value;
|
|
1241
|
+
case 'null':
|
|
1242
|
+
return 'null';
|
|
1243
|
+
case 'interval':
|
|
1244
|
+
if (node.unit) {
|
|
1245
|
+
// eg. `INTERVAL '90' DAY` -> "90 days"
|
|
1246
|
+
return `${node.expr.value} ${node.unit}s`;
|
|
1247
|
+
}
|
|
1248
|
+
return node.expr.value;
|
|
1249
|
+
case 'function':
|
|
1250
|
+
if (node.name.toLowerCase() === 'lower' ||
|
|
1251
|
+
node.name.toLowerCase() === 'upper') {
|
|
1252
|
+
// how many transactions were above 20 and not fuel or made in the last 90 days (hint: use lower)
|
|
1253
|
+
if (node.args.value.length < 1)
|
|
1254
|
+
return null;
|
|
1255
|
+
if (node.args.value[0].value) {
|
|
1256
|
+
return node.args.value[0].value.replaceAll('%', '');
|
|
1257
|
+
}
|
|
1258
|
+
if (node.args.value[0].column)
|
|
1259
|
+
return node.args.value[0].column.replaceAll('%', '');
|
|
1260
|
+
return null;
|
|
1261
|
+
}
|
|
1262
|
+
if (node.name.toLowerCase() === 'current_date' ||
|
|
1263
|
+
node.name.toLowerCase() === 'now') {
|
|
1264
|
+
return 'today';
|
|
1265
|
+
}
|
|
1266
|
+
if (node.name.toLowerCase() === 'date_trunc') {
|
|
1267
|
+
// eg. date_trunc('month', now())
|
|
1268
|
+
if (!node.args)
|
|
1269
|
+
return null;
|
|
1270
|
+
if (node.args.type !== 'expr_list')
|
|
1271
|
+
return null;
|
|
1272
|
+
if (node.args.value?.length !== 2)
|
|
1273
|
+
return null;
|
|
1274
|
+
const interval = renderSentence(formData, node.args.value[0]);
|
|
1275
|
+
const timestamp = renderSentence(formData, node.args.value[1]);
|
|
1276
|
+
return `start of the ${interval} of ${timestamp}`;
|
|
1277
|
+
}
|
|
1278
|
+
return null;
|
|
1279
|
+
default:
|
|
1280
|
+
return null;
|
|
576
1281
|
}
|
|
577
1282
|
};
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
const resetAST = {
|
|
585
|
-
with: null,
|
|
586
|
-
type: 'select',
|
|
587
|
-
options: null,
|
|
588
|
-
distinct: { type: null },
|
|
589
|
-
columns: '*',
|
|
590
|
-
into: { position: null },
|
|
591
|
-
// where: newDateWhereAST(dateColumn, defaultDateRange),
|
|
592
|
-
where: null,
|
|
593
|
-
groupby: null,
|
|
594
|
-
having: null,
|
|
595
|
-
orderby: null,
|
|
596
|
-
limit: { seperator: '', value: [] },
|
|
597
|
-
window: null,
|
|
598
|
-
from: [{ db: null, table: selectedTable.displayName, as: null }],
|
|
599
|
-
};
|
|
600
|
-
setAST(resetAST);
|
|
601
|
-
setASTNoDateColumn(resetAST);
|
|
602
|
-
return;
|
|
1283
|
+
const getAllPossibleColumns = () => {
|
|
1284
|
+
if (!baseAst || !baseAst.from) {
|
|
1285
|
+
return schemaTables.flatMap((table) => table.columns.map((c) => ({
|
|
1286
|
+
...c,
|
|
1287
|
+
table: table.displayName,
|
|
1288
|
+
})));
|
|
603
1289
|
}
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
1290
|
+
// TODO: support infinitely nested FROM table lookups.
|
|
1291
|
+
// This currently only supports top-level table names in the FROM section
|
|
1292
|
+
// of queries (eg. FROM "table_name", not "FROM (SELECT * FROM other) AS table_name")
|
|
1293
|
+
const tableNamesInQuery = baseAst.from.map((tbl) => tbl.table);
|
|
1294
|
+
return schemaTables
|
|
1295
|
+
.filter((t) => tableNamesInQuery.includes(t.displayName))
|
|
1296
|
+
.flatMap((table) => table.columns.map((c) => ({
|
|
1297
|
+
...c,
|
|
1298
|
+
table: table.displayName,
|
|
1299
|
+
})));
|
|
1300
|
+
};
|
|
1301
|
+
const getDateColumns = () => {
|
|
1302
|
+
const allColumns = getAllPossibleColumns();
|
|
1303
|
+
return allColumns.filter((c) => (0, ast_1.isDateishColumnType)(c.fieldType));
|
|
1304
|
+
};
|
|
1305
|
+
const getNumericColumns = () => {
|
|
1306
|
+
const allColumns = getAllPossibleColumns();
|
|
1307
|
+
const selectedColumnNames = selectedColumns.map((col) => col.split('.')[1]);
|
|
1308
|
+
return allColumns
|
|
1309
|
+
.filter((column) => {
|
|
1310
|
+
return selectedColumnNames.includes(column.name);
|
|
1311
|
+
})
|
|
1312
|
+
.filter((c) => (0, ast_1.isNumericColumnType)(c.fieldType));
|
|
1313
|
+
};
|
|
1314
|
+
const getNonNumericColumns = () => {
|
|
1315
|
+
const allColumns = getAllPossibleColumns();
|
|
1316
|
+
const selectedColumnNames = selectedColumns.map((col) => col.split('.')[1]);
|
|
1317
|
+
return allColumns
|
|
1318
|
+
.filter((column) => selectedColumnNames.includes(column.name))
|
|
1319
|
+
.filter((c) => !(0, ast_1.isNumericColumnType)(c.fieldType));
|
|
1320
|
+
};
|
|
1321
|
+
const getStringColumns = () => {
|
|
1322
|
+
const allColumns = getAllPossibleColumns();
|
|
1323
|
+
const selectedColumnNames = selectedColumns.map((col) => col.split('.')[1]);
|
|
1324
|
+
return allColumns
|
|
1325
|
+
.filter((column) => selectedColumnNames.includes(column.name))
|
|
1326
|
+
.filter((c) => (0, ast_1.isTextColumnType)(c.fieldType));
|
|
1327
|
+
};
|
|
1328
|
+
/**
|
|
1329
|
+
* Return whether all columns have been selected (used to hide select all
|
|
1330
|
+
* and show clear button).
|
|
1331
|
+
*/
|
|
1332
|
+
const isSelectedAllColumns = () => {
|
|
1333
|
+
if (selectedColumns.length < 1)
|
|
1334
|
+
return false;
|
|
1335
|
+
const allColumns = orderedColumnNames.filter((row) => {
|
|
1336
|
+
const [table, _] = row.split('.');
|
|
1337
|
+
return selectedColumns[0].startsWith(table);
|
|
1338
|
+
});
|
|
1339
|
+
return selectedColumns.length === allColumns.length;
|
|
1340
|
+
};
|
|
1341
|
+
const nameToColumn = (name) => ({
|
|
1342
|
+
type: 'expr',
|
|
1343
|
+
expr: {
|
|
1344
|
+
type: 'column_ref',
|
|
1345
|
+
table: null,
|
|
1346
|
+
column: name,
|
|
1347
|
+
},
|
|
1348
|
+
as: null,
|
|
1349
|
+
});
|
|
1350
|
+
const SortableItem = ({ id, label, setSelectedColumns, selectedColumns, }) => {
|
|
1351
|
+
const { attributes, listeners, setNodeRef, transform, transition } = (0, sortable_1.useSortable)({ id: id });
|
|
1352
|
+
const style = {
|
|
1353
|
+
transform: utilities_1.CSS.Transform.toString(transform),
|
|
1354
|
+
transition,
|
|
1355
|
+
};
|
|
1356
|
+
const handleSelect = () => {
|
|
1357
|
+
setSelectedColumns((selectedColumns) => {
|
|
1358
|
+
if (selectedColumns.includes(id)) {
|
|
1359
|
+
return selectedColumns.filter((column) => column !== id);
|
|
666
1360
|
}
|
|
667
|
-
else
|
|
668
|
-
|
|
1361
|
+
else {
|
|
1362
|
+
return [...selectedColumns, id];
|
|
669
1363
|
}
|
|
670
|
-
|
|
671
|
-
|
|
1364
|
+
});
|
|
1365
|
+
};
|
|
1366
|
+
return ((0, jsx_runtime_1.jsx)("div", { style: { userSelect: 'none', ...style }, ref: setNodeRef, children: (0, jsx_runtime_1.jsx)(SelectColumn, { selected: selectedColumns?.includes(id), setSelected: handleSelect, label: label, children: (0, jsx_runtime_1.jsx)("div", { style: {
|
|
1367
|
+
cursor: 'grab',
|
|
1368
|
+
}, ...attributes, ...listeners, children: (0, jsx_runtime_1.jsx)(HandleButton, {}) }) }) }));
|
|
1369
|
+
};
|
|
1370
|
+
const AddConditionPopover = ({ onSave }) => {
|
|
1371
|
+
return ((0, jsx_runtime_1.jsxs)("div", { style: { display: 'flex', flexDirection: 'column', gap: 2 }, children: [(0, jsx_runtime_1.jsx)("h1", { style: {
|
|
1372
|
+
fontWeight: '600',
|
|
1373
|
+
fontSize: 18,
|
|
1374
|
+
margin: 0,
|
|
1375
|
+
textAlign: 'left',
|
|
1376
|
+
}, children: "Add condition" }), (0, jsx_runtime_1.jsx)(Tabs, { defaultValue: topLevelBinaryOperator, options: ui_1.DEFAULT_TAB_OPTIONS, onValueChange: (value) => setTopLevelBinaryOperator(value) }), activeEditItem && renderNode(activeEditItem), (0, jsx_runtime_1.jsx)("div", { style: {
|
|
1377
|
+
display: 'flex',
|
|
1378
|
+
flexDirection: 'row',
|
|
1379
|
+
gap: 8,
|
|
1380
|
+
justifyContent: 'end',
|
|
1381
|
+
}, children: (0, jsx_runtime_1.jsx)(Button, { onMouseUp: onSave, children: "Add condition" }) })] }));
|
|
1382
|
+
};
|
|
1383
|
+
const fetchUponChange = async () => {
|
|
1384
|
+
if ((formData || baseAst) && !loading) {
|
|
1385
|
+
try {
|
|
1386
|
+
setLoading(true);
|
|
1387
|
+
const res2 = await fetch('https://quill-344421.uc.r.appspot.com/patterns', {
|
|
1388
|
+
method: 'POST',
|
|
1389
|
+
headers: {
|
|
1390
|
+
'Content-Type': 'application/json',
|
|
1391
|
+
},
|
|
1392
|
+
body: JSON.stringify({
|
|
1393
|
+
ast: { ...baseAst, where: formData },
|
|
1394
|
+
publicKey: client.publicKey,
|
|
1395
|
+
orgId: '2',
|
|
1396
|
+
}),
|
|
1397
|
+
});
|
|
1398
|
+
const data2 = await res2.json();
|
|
1399
|
+
if (data2.rows && data2.rows.length) {
|
|
1400
|
+
const tables = (0, ast_1.getTableNames)(baseAst);
|
|
1401
|
+
const table = tables.length >= 1 ? tables[0] : initialTableName;
|
|
1402
|
+
if (pivot) {
|
|
1403
|
+
// Do all of this to make sure we have the right unique columns when applying a pivot
|
|
1404
|
+
let uniqueFormatted = {};
|
|
1405
|
+
const uniqueRecords = Array.from(new Set(data2.rows.map((row) => row[pivot.columnField]))).reduce((acc, curr) => {
|
|
1406
|
+
acc[curr] = false;
|
|
1407
|
+
return acc;
|
|
1408
|
+
}, {});
|
|
1409
|
+
uniqueFormatted[pivot.columnField] = uniqueRecords;
|
|
1410
|
+
const pivotedData = (0, convert_1.applyPivot)(data2, pivot, uniqueFormatted);
|
|
1411
|
+
console.info(`%c[Pivot]: ${JSON.stringify(pivot)}`, 'color: dimgray');
|
|
1412
|
+
setPivotData(pivotedData);
|
|
1413
|
+
}
|
|
1414
|
+
else {
|
|
1415
|
+
setRows(data2.rows);
|
|
1416
|
+
setFields(data2.fields);
|
|
1417
|
+
if (data2.errorMessage) {
|
|
1418
|
+
setErrorMessage(`Error: ${data2.errorMessage}`);
|
|
1419
|
+
}
|
|
1420
|
+
}
|
|
672
1421
|
}
|
|
673
1422
|
else {
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
operator: 'AND',
|
|
677
|
-
left: newAST.where,
|
|
678
|
-
right: newCondition,
|
|
679
|
-
};
|
|
1423
|
+
setRows([]);
|
|
1424
|
+
setFields([]);
|
|
680
1425
|
}
|
|
681
1426
|
}
|
|
682
|
-
|
|
1427
|
+
catch (e) {
|
|
1428
|
+
console.error(e);
|
|
1429
|
+
}
|
|
1430
|
+
finally {
|
|
1431
|
+
setLoading(false);
|
|
1432
|
+
}
|
|
683
1433
|
}
|
|
684
1434
|
};
|
|
685
|
-
//
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
1435
|
+
// Convert an array of columns to a map where the name is the
|
|
1436
|
+
// key and the value is the column node.
|
|
1437
|
+
const columnArrayToMap = (columns) => {
|
|
1438
|
+
const columnMap = {};
|
|
1439
|
+
for (const col of columns) {
|
|
1440
|
+
const key = col.expr?.value ?? col.expr?.column ?? col.as;
|
|
1441
|
+
columnMap[key] = col;
|
|
690
1442
|
}
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
let result;
|
|
706
|
-
if (basicType === 'number') {
|
|
707
|
-
let min = Infinity, max = -Infinity;
|
|
708
|
-
data.forEach(row => {
|
|
709
|
-
const value = row[column.name];
|
|
710
|
-
min = Math.min(min, value);
|
|
711
|
-
max = Math.max(max, value);
|
|
712
|
-
});
|
|
713
|
-
result = { min, max };
|
|
714
|
-
}
|
|
715
|
-
else if (basicType === 'string') {
|
|
716
|
-
const freqMap = {};
|
|
717
|
-
data.forEach(row => {
|
|
718
|
-
const value = row[column.name];
|
|
719
|
-
if (value !== null && value !== undefined) {
|
|
720
|
-
freqMap[value] = (freqMap[value] || 0) + 1;
|
|
1443
|
+
return columnMap;
|
|
1444
|
+
};
|
|
1445
|
+
const applyFormatting = (response, columns) => {
|
|
1446
|
+
const columnFormatters = {};
|
|
1447
|
+
const columnMap = columnArrayToMap(columns);
|
|
1448
|
+
response.fields.forEach((field) => {
|
|
1449
|
+
// TODO: columnMap[field.name] silently breaks for columnField columns
|
|
1450
|
+
const formatType = (0, util_1.getPostgresBasicType)(field);
|
|
1451
|
+
if (formatType === 'date') {
|
|
1452
|
+
columnFormatters[field.name] = (x) => {
|
|
1453
|
+
const d = new Date(x);
|
|
1454
|
+
// check if d is a valid date
|
|
1455
|
+
if (isNaN(d.getTime())) {
|
|
1456
|
+
return 'Invalid Date';
|
|
721
1457
|
}
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
1458
|
+
d.setMinutes(d.getMinutes() + d.getTimezoneOffset()); // TZ adjust
|
|
1459
|
+
if (columnMap[field.name]?.expr.type === 'function' &&
|
|
1460
|
+
columnMap[field.name]?.expr.name.toLowerCase() === 'date_trunc' &&
|
|
1461
|
+
columnMap[field.name]?.expr.args.value[0].value.toLowerCase() ===
|
|
1462
|
+
'month') {
|
|
1463
|
+
return d.toLocaleString('default', {
|
|
1464
|
+
month: 'short',
|
|
1465
|
+
year: 'numeric',
|
|
1466
|
+
});
|
|
1467
|
+
}
|
|
1468
|
+
else if (columnMap[field.name]?.expr.type === 'function' &&
|
|
1469
|
+
columnMap[field.name]?.expr.name.toLowerCase() === 'date_trunc' &&
|
|
1470
|
+
columnMap[field.name]?.expr.args.value[0].value.toLowerCase() ===
|
|
1471
|
+
'quarter') {
|
|
1472
|
+
return `Q${(0, date_fns_1.getQuarter)(d)} ${d.getFullYear()}`;
|
|
1473
|
+
}
|
|
1474
|
+
else if (columnMap[field.name]?.expr.type === 'function' &&
|
|
1475
|
+
columnMap[field.name]?.expr.name.toLowerCase() === 'date_trunc' &&
|
|
1476
|
+
columnMap[field.name]?.expr.args.value[0].value.toLowerCase() ===
|
|
1477
|
+
'year') {
|
|
1478
|
+
return d.toLocaleString('default', {
|
|
1479
|
+
year: 'numeric',
|
|
1480
|
+
});
|
|
1481
|
+
}
|
|
1482
|
+
return constants_1.DATE_FMT.format(d);
|
|
1483
|
+
};
|
|
1484
|
+
}
|
|
1485
|
+
else if (formatType === 'number') {
|
|
1486
|
+
columnFormatters[field.name] = (x) => {
|
|
1487
|
+
if (columnMap[field.name]?.expr.type === 'extract' &&
|
|
1488
|
+
columnMap[field.name]?.expr.args.field.toLowerCase() === 'dow') {
|
|
1489
|
+
return constants_1.DAY_OF_WEEK[x];
|
|
1490
|
+
}
|
|
1491
|
+
else if (columnMap[field.name]?.expr.type === 'extract' &&
|
|
1492
|
+
columnMap[field.name]?.expr.args.field.toLowerCase() === 'month') {
|
|
1493
|
+
return constants_1.MONTH_OF_YEAR[x - 1];
|
|
1494
|
+
}
|
|
1495
|
+
else if (`${x}`.includes('.')) {
|
|
1496
|
+
// return MONEY_FMT.format(Number(x ?? 0.0));
|
|
1497
|
+
return Number(x ?? 0.0).toFixed(2);
|
|
1498
|
+
}
|
|
1499
|
+
return x ?? 0.0;
|
|
1500
|
+
};
|
|
727
1501
|
}
|
|
728
1502
|
else {
|
|
729
|
-
|
|
1503
|
+
columnFormatters[field.name] = (x) => x;
|
|
1504
|
+
}
|
|
1505
|
+
});
|
|
1506
|
+
return response.rows.map((row) => {
|
|
1507
|
+
const newRow = {};
|
|
1508
|
+
Object.keys(row).forEach((key) => (newRow[key] = columnFormatters[key]
|
|
1509
|
+
? columnFormatters[key](row[key])
|
|
1510
|
+
: row[key]));
|
|
1511
|
+
return newRow;
|
|
1512
|
+
});
|
|
1513
|
+
};
|
|
1514
|
+
// Returns whether a where-clause contains a nested subquery.
|
|
1515
|
+
const isSubquery = (node) => {
|
|
1516
|
+
if (!node)
|
|
1517
|
+
return false;
|
|
1518
|
+
if (node.ast)
|
|
1519
|
+
return true;
|
|
1520
|
+
if (node.left && isSubquery(node.left))
|
|
1521
|
+
return true;
|
|
1522
|
+
if (node.right && isSubquery(node.right))
|
|
1523
|
+
return true;
|
|
1524
|
+
if (node.value && Array.isArray(node.value)) {
|
|
1525
|
+
for (const value of node.value) {
|
|
1526
|
+
if (isSubquery(value))
|
|
1527
|
+
return true;
|
|
730
1528
|
}
|
|
731
|
-
setComputedColumns({
|
|
732
|
-
...computedColumns,
|
|
733
|
-
[column.name]: result,
|
|
734
|
-
});
|
|
735
1529
|
}
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
1530
|
+
return false;
|
|
1531
|
+
};
|
|
1532
|
+
const handleAsk = async () => {
|
|
1533
|
+
if (!aiPrompt) {
|
|
1534
|
+
return;
|
|
1535
|
+
}
|
|
1536
|
+
try {
|
|
1537
|
+
setLoading(true);
|
|
1538
|
+
let res, data, ast;
|
|
1539
|
+
let numRetries = 0;
|
|
1540
|
+
const MAX_RETRIES = 3;
|
|
1541
|
+
// refetch the request if it comes back and we know it's invalid.
|
|
1542
|
+
// TODO: remove this to allow joins later down the road
|
|
1543
|
+
let isTableJoin = !ast || !ast.from || ast.from.length !== 1;
|
|
1544
|
+
while (isTableJoin || isSubquery(ast?.where)) {
|
|
1545
|
+
if (numRetries === MAX_RETRIES)
|
|
1546
|
+
break;
|
|
1547
|
+
if (!activeQuery || (ast && (isTableJoin || isSubquery(ast?.where)))) {
|
|
1548
|
+
res = await fetch('https://quill-344421.uc.r.appspot.com/magic', {
|
|
1549
|
+
method: 'POST',
|
|
1550
|
+
headers: { 'Content-Type': 'application/json' },
|
|
1551
|
+
body: JSON.stringify({
|
|
1552
|
+
initialQuestion: aiPrompt,
|
|
1553
|
+
publicKey: client.publicKey,
|
|
1554
|
+
}),
|
|
759
1555
|
});
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
1556
|
+
}
|
|
1557
|
+
else {
|
|
1558
|
+
res = await fetch('https://quill-344421.uc.r.appspot.com/magic/edit', {
|
|
1559
|
+
method: 'POST',
|
|
1560
|
+
headers: { 'Content-Type': 'application/json' },
|
|
1561
|
+
body: JSON.stringify({
|
|
1562
|
+
sqlQuery: activeQuery,
|
|
1563
|
+
initialQuestion: aiPrompt,
|
|
1564
|
+
publicKey: client.publicKey,
|
|
1565
|
+
}),
|
|
1566
|
+
});
|
|
1567
|
+
}
|
|
1568
|
+
data = await res.json();
|
|
1569
|
+
ast = data?.ast?.length ? data?.ast[0] : data?.ast;
|
|
1570
|
+
// TODO: Debug invalid table joins in handleAsk
|
|
1571
|
+
isTableJoin =
|
|
1572
|
+
ast?.type !== 'bigquery' &&
|
|
1573
|
+
(!ast || !ast.from || ast.from.length !== 1);
|
|
1574
|
+
numRetries += 1;
|
|
1575
|
+
}
|
|
1576
|
+
if (numRetries === MAX_RETRIES) {
|
|
1577
|
+
console.error('[Error]: Max retries exceeded.');
|
|
1578
|
+
console.info(`%c[Prompt]: ${aiPrompt}`, 'color: dimgray');
|
|
1579
|
+
setErrorMessage("Error: Couldn't process your request, please re-word your prompt.");
|
|
1580
|
+
return;
|
|
1581
|
+
}
|
|
1582
|
+
let newAst, groupByPivot;
|
|
1583
|
+
if (ast) {
|
|
1584
|
+
// Unwrap the ast object, supporting many possible types
|
|
1585
|
+
ast = ast.length ? ast[0] : ast;
|
|
1586
|
+
newAst = (0, convert_1.convertBigQuery)(ast);
|
|
1587
|
+
newAst = (0, convert_1.convertWildcardColumns)(newAst, schemaTables); // must go before groupby
|
|
1588
|
+
({ ast: newAst, pivot: groupByPivot } = (0, convert_1.convertGroupBy)(newAst, pivot, schemaTables));
|
|
1589
|
+
newAst = (0, convert_1.convertStringComparison)(newAst, client.databaseType);
|
|
1590
|
+
newAst = (0, convert_1.convertRemoveSimpleParentheses)(newAst);
|
|
1591
|
+
// newAst = convertDateComparison(newAst); // TODO
|
|
1592
|
+
ast = newAst; // so we fetch data for newAst later.
|
|
1593
|
+
const table = (0, ast_1.getTableNames)(newAst)[0] ?? initialTableName;
|
|
1594
|
+
setPivot(groupByPivot);
|
|
1595
|
+
setSelectedColumns((0, util_1.deepCopy)(newAst).columns?.map((column) => {
|
|
1596
|
+
if (column.expr.type === 'column_ref') {
|
|
1597
|
+
return `${table}.${column.expr.column}`;
|
|
1598
|
+
}
|
|
1599
|
+
else if (column.as) {
|
|
1600
|
+
return `${table}.${column.as}`;
|
|
765
1601
|
}
|
|
1602
|
+
return `${table}.${column.expr.value}`;
|
|
1603
|
+
}));
|
|
1604
|
+
setBaseAst((0, util_1.deepCopy)({ ...newAst }));
|
|
1605
|
+
setFormData((0, util_1.deepCopy)(newAst.where));
|
|
1606
|
+
setTopLevelBinaryOperator(
|
|
1607
|
+
// @ts-ignore
|
|
1608
|
+
newAst?.where ? newAst?.where?.operator : 'AND');
|
|
1609
|
+
if (groupByPivot)
|
|
1610
|
+
return; // the useEffect will handle the rest
|
|
1611
|
+
}
|
|
1612
|
+
const res2 = await fetch('https://quill-344421.uc.r.appspot.com/patterns', {
|
|
1613
|
+
method: 'POST',
|
|
1614
|
+
headers: {
|
|
1615
|
+
'Content-Type': 'application/json',
|
|
1616
|
+
},
|
|
1617
|
+
body: JSON.stringify({
|
|
1618
|
+
ast: ast,
|
|
1619
|
+
publicKey: client.publicKey,
|
|
1620
|
+
orgId: '2',
|
|
1621
|
+
}),
|
|
1622
|
+
});
|
|
1623
|
+
const data2 = await res2.json();
|
|
1624
|
+
if (data2.rows && data2.rows.length) {
|
|
1625
|
+
const tables = (0, ast_1.getTableNames)(newAst);
|
|
1626
|
+
const table = tables.length >= 1 ? tables[0] : initialTableName;
|
|
1627
|
+
if (groupByPivot) {
|
|
1628
|
+
const pivotedData = (0, convert_1.applyPivot)(data2, groupByPivot, uniqueValues[table]);
|
|
1629
|
+
console.info(`%c[Pivot]: ${JSON.stringify(groupByPivot)}`, 'color: dimgray');
|
|
1630
|
+
setPivotData(pivotedData);
|
|
1631
|
+
}
|
|
1632
|
+
else {
|
|
1633
|
+
setRows(data2.rows);
|
|
1634
|
+
setFields(data2.fields);
|
|
766
1635
|
}
|
|
767
1636
|
}
|
|
768
|
-
|
|
769
|
-
|
|
1637
|
+
else {
|
|
1638
|
+
setRows([]);
|
|
1639
|
+
setFields([]);
|
|
1640
|
+
}
|
|
1641
|
+
if (data2.query) {
|
|
1642
|
+
setActiveQuery(data2.query);
|
|
1643
|
+
}
|
|
1644
|
+
else {
|
|
1645
|
+
setActiveQuery('');
|
|
1646
|
+
}
|
|
1647
|
+
if (data2.errorMessage) {
|
|
1648
|
+
setErrorMessage(`Error: ${data2.errorMessage}`);
|
|
1649
|
+
}
|
|
1650
|
+
else {
|
|
1651
|
+
setErrorMessage('');
|
|
770
1652
|
}
|
|
1653
|
+
}
|
|
1654
|
+
catch (e) {
|
|
1655
|
+
console.error(e);
|
|
1656
|
+
setErrorMessage(`${e.name}: ${e.message}`);
|
|
1657
|
+
}
|
|
1658
|
+
finally {
|
|
1659
|
+
setLoading(false);
|
|
1660
|
+
setAiPrompt('');
|
|
1661
|
+
}
|
|
1662
|
+
};
|
|
1663
|
+
const handleDeleteColumn = (name) => {
|
|
1664
|
+
if (!baseAst || !baseAst.columns.length || selectedColumns.length === 1) {
|
|
1665
|
+
clearAllState();
|
|
1666
|
+
return;
|
|
1667
|
+
}
|
|
1668
|
+
setSelectedColumns((selectedColumns) => selectedColumns.filter((column) => !column.endsWith(name)));
|
|
1669
|
+
const columns = baseAst.columns.filter((col) => {
|
|
1670
|
+
if (col.expr.type === 'column_ref') {
|
|
1671
|
+
return col.expr.column !== name;
|
|
1672
|
+
}
|
|
1673
|
+
else if (col.as) {
|
|
1674
|
+
return col.as !== name;
|
|
1675
|
+
}
|
|
1676
|
+
return col.expr.value !== name;
|
|
1677
|
+
});
|
|
1678
|
+
if (columns.length === 0) {
|
|
1679
|
+
clearAllState();
|
|
1680
|
+
return;
|
|
1681
|
+
}
|
|
1682
|
+
const newAst = { ...baseAst, columns };
|
|
1683
|
+
setBaseAst((0, util_1.deepCopy)(newAst));
|
|
1684
|
+
};
|
|
1685
|
+
function TopLevelBooleanSwitch({ node, keyPrefix, handleOperatorChange, }) {
|
|
1686
|
+
return ((0, jsx_runtime_1.jsx)("div", { style: { width: 'fit-content' }, children: (0, jsx_runtime_1.jsx)(Tabs, { defaultValue: node.operator, options: ui_1.DEFAULT_TAB_OPTIONS, onValueChange: (value) => handleOperatorChange(value, node, keyPrefix) }) }));
|
|
1687
|
+
}
|
|
1688
|
+
const DraggableItem = ({ id, label, onDelete }) => {
|
|
1689
|
+
const { attributes, listeners, setNodeRef, transform, transition } = (0, sortable_1.useSortable)({ id: id });
|
|
1690
|
+
const style = {
|
|
1691
|
+
transform: utilities_1.CSS.Transform.toString(transform),
|
|
1692
|
+
transition,
|
|
771
1693
|
};
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
1694
|
+
return ((0, jsx_runtime_1.jsx)("div", { style: { ...style }, ref: setNodeRef, children: (0, jsx_runtime_1.jsx)(DraggableColumn, { label: label, onDelete: onDelete, children: (0, jsx_runtime_1.jsx)("div", { style: {
|
|
1695
|
+
cursor: 'grab',
|
|
1696
|
+
}, ...attributes, ...listeners, children: (0, jsx_runtime_1.jsx)(HandleButton, {}) }) }) }));
|
|
1697
|
+
};
|
|
1698
|
+
function DraggableColumns() {
|
|
1699
|
+
const sensors = (0, core_1.useSensors)((0, core_1.useSensor)(core_1.PointerSensor), (0, core_1.useSensor)(core_1.KeyboardSensor, {
|
|
1700
|
+
coordinateGetter: sortable_1.sortableKeyboardCoordinates,
|
|
1701
|
+
}));
|
|
1702
|
+
// When a drag event ends, switch the item order.
|
|
1703
|
+
function handleDragEnd(event) {
|
|
1704
|
+
const { active, over } = event;
|
|
1705
|
+
if (active.id !== over.id) {
|
|
1706
|
+
const oldIndex = orderedColumnNames.findIndex((c) => c.endsWith(active.id));
|
|
1707
|
+
const newIndex = orderedColumnNames.findIndex((c) => c.endsWith(over.id));
|
|
1708
|
+
const newOrder = (0, sortable_1.arrayMove)(orderedColumnNames, oldIndex, newIndex);
|
|
1709
|
+
setOrderedColumnNames(newOrder);
|
|
1710
|
+
const orderedSelectedColumns = [];
|
|
1711
|
+
for (const value of newOrder) {
|
|
1712
|
+
const [_, column] = value.split('.');
|
|
1713
|
+
if (selectedColumns.includes(value)) {
|
|
1714
|
+
orderedSelectedColumns.push(column);
|
|
784
1715
|
}
|
|
785
1716
|
}
|
|
1717
|
+
// If there is already an AST saved in state, only update the columns
|
|
1718
|
+
// otherwise fill in the defaultAST shape and also update columns.
|
|
1719
|
+
const fallbackAST = {
|
|
1720
|
+
...constants_1.defaultAST,
|
|
1721
|
+
from: [{ ...constants_1.defaultTable }],
|
|
1722
|
+
columns: orderedSelectedColumns.map((name) => nameToColumn(name)),
|
|
1723
|
+
};
|
|
1724
|
+
const newBaseAst = {
|
|
1725
|
+
...baseAst,
|
|
1726
|
+
columns: baseAst?.columns.length
|
|
1727
|
+
? orderedSelectedColumns.map((name) => nameToColumn(name))
|
|
1728
|
+
: baseAst?.columns,
|
|
1729
|
+
};
|
|
1730
|
+
const newAst = baseAst ? newBaseAst : fallbackAST;
|
|
1731
|
+
setBaseAst(newAst);
|
|
786
1732
|
}
|
|
787
|
-
|
|
788
|
-
|
|
1733
|
+
}
|
|
1734
|
+
const columnNamesInAst = baseAst?.columns.map((col) => {
|
|
1735
|
+
if (col.expr.type === 'column_ref') {
|
|
1736
|
+
return col.expr.column;
|
|
789
1737
|
}
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
1738
|
+
else if (col.as) {
|
|
1739
|
+
return col.as;
|
|
1740
|
+
}
|
|
1741
|
+
else if (col.expr && col.expr.type === 'aggr_func') {
|
|
1742
|
+
if (col.expr.args) {
|
|
1743
|
+
return `${col.expr.name.toLowerCase()}(${col.expr.args.expr.value})`;
|
|
1744
|
+
}
|
|
1745
|
+
return col.expr.name;
|
|
1746
|
+
}
|
|
1747
|
+
return col.expr.value;
|
|
1748
|
+
}) ?? [];
|
|
1749
|
+
return ((0, jsx_runtime_1.jsx)(core_1.DndContext, { sensors: sensors, collisionDetection: core_1.closestCenter, onDragEnd: handleDragEnd, children: (0, jsx_runtime_1.jsx)(sortable_1.SortableContext, { items: columnNamesInAst, strategy: sortable_1.verticalListSortingStrategy, children: (0, jsx_runtime_1.jsxs)("div", { style: {
|
|
1750
|
+
display: 'flex',
|
|
1751
|
+
flexDirection: 'column',
|
|
1752
|
+
gap: 8,
|
|
1753
|
+
}, children: [columnNamesInAst.map((name) => ((0, jsx_runtime_1.jsx)(DraggableItem, { id: name, label: name, onDelete: () => handleDeleteColumn(name) }, name))), columnNamesInAst?.length > 0 && (0, jsx_runtime_1.jsx)("div", { style: { height: 6 } })] }) }) }));
|
|
795
1754
|
}
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
const styles = tagStyle || {
|
|
824
|
-
cursor: 'pointer',
|
|
825
|
-
borderRadius: 8,
|
|
826
|
-
border: '1px solid',
|
|
827
|
-
backgroundColor: '#EFF0FC',
|
|
828
|
-
paddingLeft: '12px',
|
|
829
|
-
paddingRight: '8px',
|
|
830
|
-
height: 30,
|
|
831
|
-
display: 'flex',
|
|
832
|
-
alignItems: 'center',
|
|
833
|
-
fontSize: 13,
|
|
834
|
-
fontWeight: 'medium',
|
|
835
|
-
color: theme?.primaryTextColor,
|
|
836
|
-
fontFamily: theme?.fontFamily,
|
|
837
|
-
whiteSpace: 'nowrap',
|
|
838
|
-
textOverflow: 'ellipsis',
|
|
839
|
-
outline: 'none',
|
|
840
|
-
maxWidth: 120,
|
|
841
|
-
};
|
|
842
|
-
const borderColor = {
|
|
843
|
-
borderColor: isSelected
|
|
844
|
-
? selectedTagBorderColor || '#B3B4BD'
|
|
845
|
-
: styles.borderColor || '#EFF0FC',
|
|
846
|
-
};
|
|
847
|
-
return ((0, jsx_runtime_1.jsxs)("div", { id: id, onClick: handleSelectFilter, style: { ...styles, ...borderColor }, children: [(0, jsx_runtime_1.jsx)("div", { style: {
|
|
848
|
-
textOverflow: 'ellipsis',
|
|
849
|
-
whiteSpace: 'nowrap',
|
|
850
|
-
overflow: 'hidden',
|
|
851
|
-
}, children: label }), (0, jsx_runtime_1.jsx)("div", {
|
|
852
|
-
// onClick={handleRemoveFilter}
|
|
853
|
-
onClick: e => {
|
|
854
|
-
e.stopPropagation(); // Prevents the event from bubbling up to the parent
|
|
855
|
-
handleRemoveFilter();
|
|
856
|
-
}, style: {
|
|
857
|
-
display: 'flex',
|
|
858
|
-
flexDirection: 'row',
|
|
859
|
-
alignItems: 'center',
|
|
860
|
-
cursor: 'pointer',
|
|
861
|
-
paddingLeft: '6px',
|
|
862
|
-
}, children: (0, jsx_runtime_1.jsx)("svg", { xmlns: "http://www.w3.org/2000/svg", viewBox: "0 0 24 24", fill: tagStyle?.color || theme?.primaryTextColor, height: "20", width: "20", children: (0, jsx_runtime_1.jsx)("path", { fillRule: "evenodd", d: "M5.47 5.47a.75.75 0 011.06 0L12 10.94l5.47-5.47a.75.75 0 111.06 1.06L13.06 12l5.47 5.47a.75.75 0 11-1.06 1.06L12 13.06l-5.47 5.47a.75.75 0 01-1.06-1.06L10.94 12 5.47 6.53a.75.75 0 010-1.06z", clipRule: "evenodd" }) }) })] }));
|
|
863
|
-
}
|
|
864
|
-
const AddFilterModal2 = ({ filters, selectedColumn, numberStart, numberEnd, setDateRange, dateRange, columnStats, stringFilterValues, setStringFilterValues, addFilter, setSelectedColumn, setNumberStart, setNumberEnd, selectedTable, columnType, removeFilter, selectFilter, indexBeingEdited, updateFilter, SelectComponent, ButtonComponent, PopoverComponent, LabelComponent, theme, TextInputComponent, tagStyle, selectedTagBorderColor, parentRef, }) => {
|
|
865
|
-
const [isOpen, setIsOpen] = (0, react_1.useState)(false);
|
|
866
|
-
return ((0, jsx_runtime_1.jsxs)("div", { style: { display: 'flex', flexDirection: 'column' }, children: [(0, jsx_runtime_1.jsxs)("div", { style: {
|
|
867
|
-
position: 'relative',
|
|
868
|
-
display: 'inline-block',
|
|
869
|
-
textAlign: 'left',
|
|
870
|
-
}, children: [(0, jsx_runtime_1.jsx)("div", { style: {
|
|
871
|
-
display: 'flex',
|
|
872
|
-
flexDirection: 'row',
|
|
873
|
-
alignItems: 'center',
|
|
874
|
-
}, children: filters.length > 0 && ((0, jsx_runtime_1.jsx)("span", { style: {
|
|
875
|
-
height: 10,
|
|
876
|
-
width: 10,
|
|
877
|
-
backgroundColor: theme.primaryButtonColor,
|
|
878
|
-
borderRadius: '50%',
|
|
879
|
-
position: 'absolute',
|
|
880
|
-
top: -2,
|
|
881
|
-
right: -2,
|
|
882
|
-
} })) }), (0, jsx_runtime_1.jsx)(PopoverComponent, { parentRef: parentRef, label: "Add filter", style: { right: 0, minWidth: 400, overflow: 'visible' }, isOpen: isOpen, onClose: () => setIsOpen(false), title: "Add filter", setIsOpen: setIsOpen, children: (0, jsx_runtime_1.jsxs)("div", { style: {
|
|
883
|
-
backgroundColor: 'rgb(255, 255, 255)',
|
|
1755
|
+
const allNumericColumns = getNumericColumns().map((column) => ({
|
|
1756
|
+
label: column.displayName,
|
|
1757
|
+
value: column.name,
|
|
1758
|
+
}));
|
|
1759
|
+
const allNonNumericColumns = getNonNumericColumns().map((column) => ({
|
|
1760
|
+
label: column.displayName,
|
|
1761
|
+
value: column.name,
|
|
1762
|
+
}));
|
|
1763
|
+
const allStringColumns = getStringColumns().map((column) => ({
|
|
1764
|
+
label: column.displayName,
|
|
1765
|
+
value: column.name,
|
|
1766
|
+
}));
|
|
1767
|
+
if (loading) {
|
|
1768
|
+
return ((0, jsx_runtime_1.jsxs)("div", { style: { display: 'flex', flexDirection: 'row', height: '100%' }, children: [(0, jsx_runtime_1.jsxs)(Sidebar, { children: [(0, jsx_runtime_1.jsx)(SidebarHeading, { label: "Columns" }), (0, jsx_runtime_1.jsx)("div", { style: { height: 4, width: '100%' } }), (0, jsx_runtime_1.jsx)(DraggableColumns, {}), (0, jsx_runtime_1.jsx)(Popover, { isOpen: openPopover === 'AddColumnPopover', trigger: (0, jsx_runtime_1.jsx)(SecondaryButton, { onClick: () => {
|
|
1769
|
+
if (!openPopover) {
|
|
1770
|
+
setOpenPopover('AddColumnPopover');
|
|
1771
|
+
}
|
|
1772
|
+
}, children: "Select columns" }), title: "Select columns", onClose: () => {
|
|
1773
|
+
setIsPending(false);
|
|
1774
|
+
setActiveEditItem(null);
|
|
1775
|
+
setActivePath(null);
|
|
1776
|
+
setOpenPopover(null);
|
|
1777
|
+
}, children: (0, jsx_runtime_1.jsx)(AddColumnPopover_1.default, { onSave: () => {
|
|
1778
|
+
setActiveEditItem(null);
|
|
1779
|
+
setActivePath(null);
|
|
1780
|
+
setOpenPopover(null);
|
|
1781
|
+
}, orderedColumnNames: orderedColumnNames, setOrderedColumnNames: setOrderedColumnNames, selectedColumns: selectedColumns, setSelectedColumns: setSelectedColumns, isSelectedAllColumns: isSelectedAllColumns, clearAllState: clearAllState, nameToColumn: nameToColumn, baseAst: baseAst, setBaseAst: setBaseAst, pivot: pivot, initialTableName: initialTableName, defaultAST: constants_1.defaultAST, defaultTable: constants_1.defaultTable, setPivot: setPivot, TextInput: TextInput, SelectColumn: SelectColumn, SecondaryButton: SecondaryButton, Button: Button, HandleButton: HandleButton }) }), (0, jsx_runtime_1.jsx)("div", { style: { height: 28, width: '100%' } }), (0, jsx_runtime_1.jsx)(SidebarHeading, { label: "Filters" }), (0, jsx_runtime_1.jsx)("div", { style: { height: 4, width: '100%' } }), formData && ((0, jsx_runtime_1.jsx)("div", { style: {
|
|
884
1782
|
display: 'flex',
|
|
885
1783
|
flexDirection: 'column',
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
1784
|
+
gap: 8,
|
|
1785
|
+
marginBottom: 12,
|
|
1786
|
+
}, children: renderSentence(formData, formData, '', true) })), (0, jsx_runtime_1.jsxs)("div", { style: {
|
|
1787
|
+
display: 'flex',
|
|
1788
|
+
flexDirection: 'column',
|
|
1789
|
+
gap: 2.5,
|
|
1790
|
+
alignItems: 'flex-start',
|
|
1791
|
+
}, children: [(0, jsx_runtime_1.jsx)(Popover, { isOpen: openPopover === 'AddFilterPopover', title: 'Add filter', trigger: (0, jsx_runtime_1.jsx)(SecondaryButton, { onClick: () => {
|
|
1792
|
+
if (!openPopover) {
|
|
1793
|
+
const value = orderedColumnNames[0];
|
|
1794
|
+
const [_table, column] = value.split('.');
|
|
1795
|
+
const columnType = getColumnTypeByName(column);
|
|
1796
|
+
if ((0, ast_1.isNumericColumnType)(columnType)) {
|
|
1797
|
+
const newSubtree = (0, util_1.deepCopy)(constants_1.defaultNumericComparison);
|
|
1798
|
+
newSubtree.left.column = column;
|
|
1799
|
+
setActiveEditItem(newSubtree);
|
|
1800
|
+
}
|
|
1801
|
+
else {
|
|
1802
|
+
const newSubtree = (0, util_1.deepCopy)(constants_1.defaultEntry);
|
|
1803
|
+
newSubtree.left.args.value[0].column = column;
|
|
1804
|
+
setActiveEditItem(newSubtree);
|
|
1805
|
+
}
|
|
1806
|
+
setOpenPopover('AddFilterPopover');
|
|
1807
|
+
setActivePath('');
|
|
1808
|
+
setIsPending(true);
|
|
1809
|
+
}
|
|
1810
|
+
}, children: "Add filter" }), onClose: () => {
|
|
1811
|
+
setIsPending(false);
|
|
1812
|
+
setActivePath(null);
|
|
1813
|
+
setOpenPopover(null);
|
|
1814
|
+
clearCheckboxes();
|
|
1815
|
+
setTimeout(() => {
|
|
1816
|
+
setActiveEditItem(null);
|
|
1817
|
+
}, 300);
|
|
1818
|
+
}, children: (0, jsx_runtime_1.jsx)(ui_1.AddFilterPopover, { onSave: () => {
|
|
1819
|
+
if ((0, util_1.isNodeEmptyCollection)(activeEditItem)) {
|
|
1820
|
+
setIsPending(false);
|
|
1821
|
+
setActivePath(null);
|
|
1822
|
+
setOpenPopover(null);
|
|
1823
|
+
clearCheckboxes();
|
|
1824
|
+
setTimeout(() => {
|
|
1825
|
+
setActiveEditItem(null);
|
|
1826
|
+
}, 300);
|
|
1827
|
+
}
|
|
1828
|
+
else {
|
|
1829
|
+
setIsPending(false);
|
|
1830
|
+
handleInsertion(activeEditItem, 'AND', false);
|
|
1831
|
+
setActivePath(null);
|
|
1832
|
+
setOpenPopover(null);
|
|
1833
|
+
clearCheckboxes();
|
|
1834
|
+
setTimeout(() => {
|
|
1835
|
+
setActiveEditItem(null);
|
|
1836
|
+
}, 300);
|
|
1837
|
+
}
|
|
1838
|
+
}, Button: Button, renderNode: renderNode, activeEditItem: activeEditItem }) }), baseAst?.where &&
|
|
1839
|
+
false && ( // temp removed the AddConditionPopover
|
|
1840
|
+
(0, jsx_runtime_1.jsx)(Popover, { isOpen: openPopover === 'AddConditionPopover', trigger: (0, jsx_runtime_1.jsx)(SecondaryButton, { onClick: () => {
|
|
1841
|
+
if (!openPopover) {
|
|
1842
|
+
setActiveEditItem((0, util_1.deepCopy)(constants_1.defaultEntry));
|
|
1843
|
+
setOpenPopover('AddConditionPopover');
|
|
1844
|
+
setActivePath('');
|
|
1845
|
+
setIsPending(true);
|
|
1846
|
+
}
|
|
1847
|
+
}, children: "Add condition" }), onClose: () => {
|
|
1848
|
+
setIsPending(false);
|
|
1849
|
+
setTimeout(() => {
|
|
1850
|
+
setActiveEditItem(null);
|
|
1851
|
+
}, 300);
|
|
1852
|
+
setActivePath(null);
|
|
1853
|
+
setOpenPopover(null);
|
|
1854
|
+
clearCheckboxes();
|
|
1855
|
+
}, children: (0, jsx_runtime_1.jsx)(AddConditionPopover, { onSave: () => {
|
|
1856
|
+
if ((0, util_1.isNodeEmptyCollection)(activeEditItem)) {
|
|
1857
|
+
setIsPending(false);
|
|
1858
|
+
setTimeout(() => {
|
|
1859
|
+
setActiveEditItem(null);
|
|
1860
|
+
}, 300);
|
|
1861
|
+
setActivePath(null);
|
|
1862
|
+
setOpenPopover(null);
|
|
1863
|
+
clearCheckboxes();
|
|
1864
|
+
}
|
|
1865
|
+
else {
|
|
1866
|
+
setIsPending(false);
|
|
1867
|
+
handleInsertion(activeEditItem, topLevelBinaryOperator, true);
|
|
1868
|
+
setTimeout(() => {
|
|
1869
|
+
setActiveEditItem(null);
|
|
1870
|
+
}, 300);
|
|
1871
|
+
setActivePath(null);
|
|
1872
|
+
setOpenPopover(null);
|
|
1873
|
+
clearCheckboxes();
|
|
1874
|
+
}
|
|
1875
|
+
} }) }))] }), (0, jsx_runtime_1.jsx)("div", { style: { height: 28, width: '100%' } }), (0, jsx_runtime_1.jsx)(SidebarHeading, { label: "Pivot" }), (0, jsx_runtime_1.jsx)("div", { style: { height: 4, width: '100%' } }), pivot !== null && ((0, jsx_runtime_1.jsxs)("div", { style: {
|
|
1876
|
+
display: 'flex',
|
|
1877
|
+
flexDirection: 'column',
|
|
1878
|
+
gap: 12,
|
|
1879
|
+
marginBottom: 12,
|
|
1880
|
+
}, children: [(0, jsx_runtime_1.jsxs)("div", { children: [(0, jsx_runtime_1.jsx)(SidebarSubHeading, { label: "Aggregation" }), (0, jsx_runtime_1.jsx)(Select, { onChange: void null, value: pivot.aggregationType, options: [
|
|
1881
|
+
{ label: 'sum', value: 'sum' },
|
|
1882
|
+
{ label: 'avg', value: 'avg' },
|
|
1883
|
+
{ label: 'min', value: 'min' },
|
|
1884
|
+
{ label: 'max', value: 'max' },
|
|
1885
|
+
{ label: 'count', value: 'count' },
|
|
1886
|
+
] })] }), (0, jsx_runtime_1.jsxs)("div", { children: [(0, jsx_runtime_1.jsx)(SidebarSubHeading, { label: "Value field" }), (0, jsx_runtime_1.jsx)(Select, { onChange: void null, value: pivot.valueField, options: [
|
|
1887
|
+
{ label: 'Select', value: '' },
|
|
1888
|
+
...allNumericColumns,
|
|
1889
|
+
] })] }), (0, jsx_runtime_1.jsxs)("div", { children: [(0, jsx_runtime_1.jsx)(SidebarSubHeading, { label: "Group rows by" }), (0, jsx_runtime_1.jsx)(Select, { onChange: void null, value: pivot.rowField ?? '', options: allNonNumericColumns })] }), (0, jsx_runtime_1.jsxs)("div", { children: [(0, jsx_runtime_1.jsx)(SidebarSubHeading, { label: "Group columns by" }), (0, jsx_runtime_1.jsx)(Select, { onChange: void null, value: pivot.columnField ?? '', options: [
|
|
1890
|
+
{ label: 'Select', value: '' },
|
|
1891
|
+
...allStringColumns,
|
|
1892
|
+
] })] })] })), (0, jsx_runtime_1.jsx)(SecondaryButton, { children: pivot === null ? 'Add pivot' : 'Delete pivot' })] }), (0, jsx_runtime_1.jsxs)(Container, { children: [(0, jsx_runtime_1.jsxs)("form", { style: {
|
|
1893
|
+
display: 'flex',
|
|
1894
|
+
flexDirection: 'row',
|
|
1895
|
+
gap: 12,
|
|
1896
|
+
padding: 1,
|
|
1897
|
+
}, children: [(0, jsx_runtime_1.jsx)(TextInput, { placeholder: "Ask a question...", type: "text", style: { width: '100%', fontSize: 14 }, value: aiPrompt }), (0, jsx_runtime_1.jsx)(ButtonLoadingState, {}), baseAst && ((0, jsx_runtime_1.jsx)(SecondaryButton, { type: "button", onClick: clearAllState, children: "New report" }))] }), baseAst && ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)(TableLoadingState, {}), (0, jsx_runtime_1.jsxs)("div", { style: {
|
|
915
1898
|
display: 'flex',
|
|
916
1899
|
flexDirection: 'row',
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
color: theme?.primaryTextColor,
|
|
961
|
-
fontFamily: theme?.fontFamily,
|
|
962
|
-
}, children: value })] }, value) }, value))) })), (0, jsx_runtime_1.jsx)("div", { style: { height: 20 } }), (0, jsx_runtime_1.jsx)("div", { children: (0, jsx_runtime_1.jsx)(ButtonComponent, { id: "custom-button", onClick: () => {
|
|
963
|
-
if (columnType === 'date' && !dateRange) {
|
|
964
|
-
return;
|
|
1900
|
+
gap: '12px',
|
|
1901
|
+
}, children: [(0, jsx_runtime_1.jsx)("div", { style: { width: '100%' } }), (0, jsx_runtime_1.jsx)(SecondaryButton, { type: "button", onClick: () => copyToClipboard(activeQuery), children: isCopying ? '✅ Copied' : 'Copy SQL' }), (0, jsx_runtime_1.jsx)(Button, { children: "Add to dashboard" })] })] }))] }), (0, jsx_runtime_1.jsx)("style", { children: `body{margin:0;}` })] }));
|
|
1902
|
+
}
|
|
1903
|
+
return ((0, jsx_runtime_1.jsxs)("div", { style: {
|
|
1904
|
+
display: 'flex',
|
|
1905
|
+
flexDirection: 'row',
|
|
1906
|
+
height: '100%',
|
|
1907
|
+
overflowY: 'auto',
|
|
1908
|
+
}, children: [(0, jsx_runtime_1.jsxs)(Sidebar, { children: [(0, jsx_runtime_1.jsx)(SidebarHeading, { label: "Columns" }), (0, jsx_runtime_1.jsx)("div", { style: { height: 4, width: '100%' } }), (0, jsx_runtime_1.jsx)(DraggableColumns, {}), (0, jsx_runtime_1.jsx)(Popover, { isOpen: openPopover === 'AddColumnPopover', title: "Select columns", trigger: (0, jsx_runtime_1.jsx)(SecondaryButton, { onClick: () => {
|
|
1909
|
+
if (!openPopover) {
|
|
1910
|
+
setOpenPopover('AddColumnPopover');
|
|
1911
|
+
}
|
|
1912
|
+
}, children: "Select columns" }), onClose: () => {
|
|
1913
|
+
// delay onClose callback so onClick no-ops
|
|
1914
|
+
setTimeout(() => {
|
|
1915
|
+
setIsPending(false);
|
|
1916
|
+
setActiveEditItem(null);
|
|
1917
|
+
setActivePath(null);
|
|
1918
|
+
setOpenPopover(null);
|
|
1919
|
+
}, 100);
|
|
1920
|
+
}, children: (0, jsx_runtime_1.jsx)(AddColumnPopover_1.default, { onSave: () => {
|
|
1921
|
+
setActiveEditItem(null);
|
|
1922
|
+
setActivePath(null);
|
|
1923
|
+
setOpenPopover(null);
|
|
1924
|
+
}, orderedColumnNames: orderedColumnNames, setOrderedColumnNames: setOrderedColumnNames, selectedColumns: selectedColumns, setSelectedColumns: setSelectedColumns, isSelectedAllColumns: isSelectedAllColumns, clearAllState: clearAllState, nameToColumn: nameToColumn, baseAst: baseAst, setBaseAst: setBaseAst, pivot: pivot, initialTableName: initialTableName, defaultAST: constants_1.defaultAST, defaultTable: constants_1.defaultTable, setPivot: setPivot, TextInput: TextInput, SelectColumn: SelectColumn, SecondaryButton: SecondaryButton, Button: Button, HandleButton: HandleButton }) }), (0, jsx_runtime_1.jsx)("div", { style: { height: 28, width: '100%' } }), (0, jsx_runtime_1.jsx)(SidebarHeading, { label: "Filters" }), (0, jsx_runtime_1.jsx)("div", { style: { height: 4, width: '100%' } }), formData && ((0, jsx_runtime_1.jsx)("div", { style: {
|
|
1925
|
+
display: 'flex',
|
|
1926
|
+
flexDirection: 'column',
|
|
1927
|
+
gap: 8,
|
|
1928
|
+
marginBottom: 12,
|
|
1929
|
+
}, children: renderSentence(formData, formData, '', true) })), (0, jsx_runtime_1.jsxs)("div", { style: {
|
|
1930
|
+
display: 'flex',
|
|
1931
|
+
flexDirection: 'column',
|
|
1932
|
+
gap: 2.5,
|
|
1933
|
+
alignItems: 'flex-start',
|
|
1934
|
+
}, children: [(0, jsx_runtime_1.jsx)(Popover, { title: 'Add filter', isOpen: openPopover === 'AddFilterPopover', trigger: (0, jsx_runtime_1.jsx)(SecondaryButton, { onClick: (_e) => {
|
|
1935
|
+
if (!openPopover) {
|
|
1936
|
+
const value = orderedColumnNames[0];
|
|
1937
|
+
const [_table, column] = value.split('.');
|
|
1938
|
+
const columnType = getColumnTypeByName(column);
|
|
1939
|
+
if ((0, ast_1.isNumericColumnType)(columnType)) {
|
|
1940
|
+
const newSubtree = (0, util_1.deepCopy)(constants_1.defaultNumericComparison);
|
|
1941
|
+
newSubtree.left.column = column;
|
|
1942
|
+
setActiveEditItem(newSubtree);
|
|
965
1943
|
}
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
1944
|
+
else {
|
|
1945
|
+
const newSubtree = (0, util_1.deepCopy)(constants_1.defaultEntry);
|
|
1946
|
+
newSubtree.left.args.value[0].column = column;
|
|
1947
|
+
setActiveEditItem(newSubtree);
|
|
969
1948
|
}
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1949
|
+
setOpenPopover('AddFilterPopover');
|
|
1950
|
+
setActivePath('');
|
|
1951
|
+
setIsPending(true);
|
|
1952
|
+
}
|
|
1953
|
+
}, children: "Add filter" }), onClose: () => {
|
|
1954
|
+
// delay onClose callback so onClick no-ops
|
|
1955
|
+
setTimeout(() => {
|
|
1956
|
+
setIsPending(false);
|
|
1957
|
+
setActivePath(null);
|
|
1958
|
+
setOpenPopover(null);
|
|
1959
|
+
clearCheckboxes();
|
|
1960
|
+
setActiveEditItem(null);
|
|
1961
|
+
}, 200);
|
|
1962
|
+
}, children: (0, jsx_runtime_1.jsx)(ui_1.AddFilterPopover, { onSave: () => {
|
|
1963
|
+
if ((0, util_1.isNodeEmptyCollection)(activeEditItem)) {
|
|
1964
|
+
setIsPending(false);
|
|
1965
|
+
setActivePath(null);
|
|
1966
|
+
setOpenPopover(null);
|
|
1967
|
+
clearCheckboxes();
|
|
1968
|
+
setTimeout(() => {
|
|
1969
|
+
setActiveEditItem(null);
|
|
1970
|
+
}, 300);
|
|
1971
|
+
}
|
|
1972
|
+
else {
|
|
1973
|
+
setIsPending(false);
|
|
1974
|
+
handleInsertion(activeEditItem, 'AND', false);
|
|
1975
|
+
setActivePath(null);
|
|
1976
|
+
setOpenPopover(null);
|
|
1977
|
+
clearCheckboxes();
|
|
1978
|
+
setTimeout(() => {
|
|
1979
|
+
setActiveEditItem(null);
|
|
1980
|
+
}, 300);
|
|
1981
|
+
}
|
|
1982
|
+
}, Button: Button, renderNode: renderNode, activeEditItem: activeEditItem }) }), baseAst?.where &&
|
|
1983
|
+
false && ( // temp removed the AddConditionPopover
|
|
1984
|
+
(0, jsx_runtime_1.jsx)(Popover, { isOpen: openPopover === 'AddConditionPopover', trigger: (0, jsx_runtime_1.jsx)(SecondaryButton, { onClick: () => {
|
|
1985
|
+
if (!openPopover) {
|
|
1986
|
+
setActiveEditItem((0, util_1.deepCopy)(constants_1.defaultEntry));
|
|
1987
|
+
setOpenPopover('AddConditionPopover');
|
|
1988
|
+
setActivePath('');
|
|
1989
|
+
setIsPending(true);
|
|
1990
|
+
}
|
|
1991
|
+
}, children: "Add condition" }), onClose: () => {
|
|
1992
|
+
// delay onClose callback so onClick no-ops
|
|
1993
|
+
setTimeout(() => {
|
|
1994
|
+
setIsPending(false);
|
|
1995
|
+
setActiveEditItem(null);
|
|
1996
|
+
setActivePath(null);
|
|
1997
|
+
setOpenPopover(null);
|
|
1998
|
+
clearCheckboxes();
|
|
1999
|
+
}, 200);
|
|
2000
|
+
}, children: (0, jsx_runtime_1.jsx)(AddConditionPopover, { onSave: () => {
|
|
2001
|
+
if ((0, util_1.isNodeEmptyCollection)(activeEditItem)) {
|
|
2002
|
+
setIsPending(false);
|
|
2003
|
+
setTimeout(() => {
|
|
2004
|
+
setActiveEditItem(null);
|
|
2005
|
+
}, 300);
|
|
2006
|
+
setActivePath(null);
|
|
2007
|
+
setOpenPopover(null);
|
|
2008
|
+
clearCheckboxes();
|
|
2009
|
+
}
|
|
2010
|
+
else {
|
|
2011
|
+
setIsPending(false);
|
|
2012
|
+
handleInsertion(activeEditItem, topLevelBinaryOperator, true);
|
|
2013
|
+
setTimeout(() => {
|
|
2014
|
+
setActiveEditItem(null);
|
|
2015
|
+
}, 300);
|
|
2016
|
+
setActivePath(null);
|
|
2017
|
+
setOpenPopover(null);
|
|
2018
|
+
clearCheckboxes();
|
|
2019
|
+
}
|
|
2020
|
+
} }) }))] }), (0, jsx_runtime_1.jsx)("div", { style: { height: 28, width: '100%' } }), (0, jsx_runtime_1.jsx)(SidebarHeading, { label: "Pivot" }), (0, jsx_runtime_1.jsx)("div", { style: { height: 4, width: '100%' } }), pivot !== null && ((0, jsx_runtime_1.jsxs)("div", { style: {
|
|
2021
|
+
display: 'flex',
|
|
2022
|
+
flexDirection: 'column',
|
|
2023
|
+
gap: 12,
|
|
2024
|
+
marginBottom: 12,
|
|
2025
|
+
}, children: [(0, jsx_runtime_1.jsxs)("div", { children: [(0, jsx_runtime_1.jsx)(SidebarSubHeading, { label: "Aggregation" }), (0, jsx_runtime_1.jsx)(Select, { value: pivot.aggregationType, onChange: (value) => {
|
|
2026
|
+
setBaseAst((0, util_1.deepCopy)(baseAst)); // trigger refetch
|
|
2027
|
+
setPivot({ ...pivot, aggregationType: value });
|
|
2028
|
+
}, options: [
|
|
2029
|
+
{ label: 'sum', value: 'sum' },
|
|
2030
|
+
{ label: 'avg', value: 'avg' },
|
|
2031
|
+
{ label: 'min', value: 'min' },
|
|
2032
|
+
{ label: 'max', value: 'max' },
|
|
2033
|
+
{ label: 'count', value: 'count' },
|
|
2034
|
+
] })] }), (0, jsx_runtime_1.jsxs)("div", { children: [(0, jsx_runtime_1.jsx)(SidebarSubHeading, { label: "Value field" }), (0, jsx_runtime_1.jsx)(Select, { value: pivot.valueField, onChange: (value) => {
|
|
2035
|
+
setBaseAst((0, util_1.deepCopy)(baseAst)); // trigger refetch
|
|
2036
|
+
setPivot({ ...pivot, valueField: value });
|
|
2037
|
+
}, options: [{ label: 'Select', value: '' }, ...allNumericColumns] })] }), (0, jsx_runtime_1.jsxs)("div", { children: [(0, jsx_runtime_1.jsx)(SidebarSubHeading, { label: "Group rows by" }), (0, jsx_runtime_1.jsx)(Select, { value: pivot.rowField ?? '', onChange: (value) => {
|
|
2038
|
+
setBaseAst((0, util_1.deepCopy)(baseAst)); // trigger refetch
|
|
2039
|
+
setPivot({
|
|
2040
|
+
...pivot,
|
|
2041
|
+
rowField: value === '' ? undefined : value,
|
|
2042
|
+
});
|
|
2043
|
+
}, options: [
|
|
2044
|
+
{ label: 'Select', value: '' },
|
|
2045
|
+
...allNonNumericColumns,
|
|
2046
|
+
] })] }), (0, jsx_runtime_1.jsxs)("div", { children: [(0, jsx_runtime_1.jsx)(SidebarSubHeading, { label: "Group columns by" }), (0, jsx_runtime_1.jsx)(Select, { value: pivot.columnField ?? '', onChange: (value) => {
|
|
2047
|
+
setBaseAst((0, util_1.deepCopy)(baseAst)); // trigger refetch
|
|
2048
|
+
setPivot({
|
|
2049
|
+
...pivot,
|
|
2050
|
+
columnField: value === '' ? undefined : value,
|
|
2051
|
+
});
|
|
2052
|
+
}, options: [
|
|
2053
|
+
{ label: 'Select', value: '' },
|
|
2054
|
+
...allStringColumns,
|
|
2055
|
+
].filter((option) => option.value !== pivot.rowField) })] })] })), (0, jsx_runtime_1.jsx)(SecondaryButton, { onClick: () => {
|
|
2056
|
+
if (pivot) {
|
|
2057
|
+
setBaseAst((0, util_1.deepCopy)(baseAst));
|
|
2058
|
+
setPivot(null);
|
|
2059
|
+
setPivotData(null);
|
|
2060
|
+
}
|
|
2061
|
+
else {
|
|
2062
|
+
if (allNumericColumns.length === 0) {
|
|
2063
|
+
setErrorMessage('Unable to create pivot: Unsupported Schema.');
|
|
2064
|
+
return;
|
|
2065
|
+
}
|
|
2066
|
+
if (!baseAst) {
|
|
2067
|
+
let ast = (0, util_1.deepCopy)({
|
|
2068
|
+
...constants_1.defaultAST,
|
|
2069
|
+
columns: getAllPossibleColumns()
|
|
2070
|
+
.filter((c) => c.table === initialTableName)
|
|
2071
|
+
.map((c) => {
|
|
2072
|
+
const newColumn = (0, util_1.deepCopy)(constants_1.defaultColumn);
|
|
2073
|
+
newColumn.expr.column = c.name;
|
|
2074
|
+
return newColumn;
|
|
2075
|
+
}),
|
|
2076
|
+
from: [{ ...constants_1.defaultTable, table: initialTableName }],
|
|
2077
|
+
where: null,
|
|
2078
|
+
});
|
|
2079
|
+
ast = (0, convert_1.convertWildcardColumns)(ast, schemaTables);
|
|
2080
|
+
setBaseAst(ast);
|
|
2081
|
+
}
|
|
2082
|
+
else {
|
|
2083
|
+
setBaseAst((0, util_1.deepCopy)(baseAst));
|
|
2084
|
+
}
|
|
2085
|
+
setPivot({
|
|
2086
|
+
aggregationType: 'sum',
|
|
2087
|
+
valueField: allNumericColumns[0].value,
|
|
2088
|
+
rowField: allNonNumericColumns[0]?.value,
|
|
2089
|
+
columnField: undefined,
|
|
2090
|
+
});
|
|
2091
|
+
}
|
|
2092
|
+
}, children: pivot === null ? 'Add pivot' : 'Delete pivot' }), (0, jsx_runtime_1.jsx)("div", { style: { height: 12, width: '100%' } })] }), (0, jsx_runtime_1.jsxs)(Container, { children: [(0, jsx_runtime_1.jsxs)("form", { onSubmit: handleAsk, style: {
|
|
2093
|
+
display: 'flex',
|
|
2094
|
+
flexDirection: 'row',
|
|
2095
|
+
gap: 12,
|
|
2096
|
+
padding: 1,
|
|
2097
|
+
}, children: [(0, jsx_runtime_1.jsx)(TextInput, { type: "text", value: aiPrompt, style: { width: '100%', fontSize: 14 }, onChange: (e) => setAiPrompt(e.target.value), placeholder: baseAst ? 'Ask a follow-up question...' : 'Ask a question...' }), (0, jsx_runtime_1.jsx)(Button, { type: "submit", onClick: handleAsk, children: "Ask AI" }), baseAst && ((0, jsx_runtime_1.jsx)(SecondaryButton, { type: "button", onClick: clearAllState, children: "New report" }))] }), baseAst && ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [loading && errorMessage.length === 0 ? ((0, jsx_runtime_1.jsx)(TableLoadingState, {})) : ((0, jsx_runtime_1.jsx)(Table, { rows: applyFormatting({
|
|
2098
|
+
rows: pivotData?.rows || rows,
|
|
2099
|
+
fields: pivotData?.fields || fields,
|
|
2100
|
+
}, baseAst?.columns ?? []), columns: enforceOrderOnColumns(Object.keys((pivotData?.rows[0] || rows[0]) ?? {})), error: errorMessage, rowsPerPage: 20 })), (0, jsx_runtime_1.jsxs)("div", { style: {
|
|
2101
|
+
display: 'flex',
|
|
2102
|
+
flexDirection: 'row',
|
|
2103
|
+
gap: '12px',
|
|
2104
|
+
}, children: [errorMessage && ((0, jsx_runtime_1.jsx)("div", { style: {
|
|
2105
|
+
color: 'red',
|
|
2106
|
+
fontSize: 14,
|
|
2107
|
+
padding: '12px',
|
|
2108
|
+
whiteSpace: 'nowrap',
|
|
2109
|
+
}, children: errorMessage })), (0, jsx_runtime_1.jsx)("div", { style: { width: '100%' } }), (0, jsx_runtime_1.jsx)(SecondaryButton, { type: "button", onClick: () => copyToClipboard(activeQuery), children: isCopying ? '✅ Copied' : 'Copy SQL' }), (0, jsx_runtime_1.jsx)(Button, { onClick: () => {
|
|
2110
|
+
setIsChartBuilderOpen(true);
|
|
2111
|
+
}, children: "Add to dashboard" })] })] }))] }), (0, jsx_runtime_1.jsx)("style", { children: `body{margin:0;}` }), (0, jsx_runtime_1.jsx)(ChartBuilder_1.default, { rows: applyFormatting({ rows, fields }, baseAst?.columns ?? []), columns: processColumnsForChartBuilder(Object.keys(rows[0] ?? {})), fields: fields, pivot: pivot, query: activeQuery, showTableFormatOptions: true, showDateFieldOptions: true, showAccessControlOptions: true, title: "Add to dashboard", isEditMode: false, isOpen: isChartBuilderOpen, setIsOpen: setIsChartBuilderOpen, onAddToDashboardComplete: onAddToDashboardComplete, destinationDashboard: destinationDashboard, dashboardItem: dashboardItem, organizationName: organizationName })] }));
|
|
2112
|
+
}
|
|
2113
|
+
exports.default = ReportBuilder;
|