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