-
Notifications
You must be signed in to change notification settings - Fork 13.7k
/
index.tsx
762 lines (715 loc) · 22.3 KB
/
index.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
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
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import {
useCallback,
useEffect,
useState,
memo,
ChangeEvent,
MouseEvent,
} from 'react';
import { shallowEqual, useDispatch, useSelector } from 'react-redux';
import { useHistory } from 'react-router-dom';
import { pick } from 'lodash';
import ButtonGroup from 'src/components/ButtonGroup';
import Alert from 'src/components/Alert';
import Button from 'src/components/Button';
import { nanoid } from 'nanoid';
import {
QueryState,
styled,
t,
tn,
useTheme,
usePrevious,
css,
getNumberFormatter,
getExtensionsRegistry,
ErrorLevel,
ErrorTypeEnum,
} from '@superset-ui/core';
import ErrorMessageWithStackTrace from 'src/components/ErrorMessage/ErrorMessageWithStackTrace';
import {
ISaveableDatasource,
ISimpleColumn,
SaveDatasetModal,
} from 'src/SqlLab/components/SaveDatasetModal';
import { EXPLORE_CHART_DEFAULT, SqlLabRootState } from 'src/SqlLab/types';
import { mountExploreUrl } from 'src/explore/exploreUtils';
import { postFormData } from 'src/explore/exploreUtils/formData';
import ProgressBar from 'src/components/ProgressBar';
import Loading from 'src/components/Loading';
import Card from 'src/components/Card';
import Label from 'src/components/Label';
import { Tooltip } from 'src/components/Tooltip';
import FilterableTable from 'src/components/FilterableTable';
import CopyToClipboard from 'src/components/CopyToClipboard';
import { addDangerToast } from 'src/components/MessageToasts/actions';
import { prepareCopyToClipboardTabularData } from 'src/utils/common';
import { getItem, LocalStorageKeys } from 'src/utils/localStorageHelpers';
import {
addQueryEditor,
clearQueryResults,
CtasEnum,
fetchQueryResults,
reFetchQueryResults,
reRunQuery,
} from 'src/SqlLab/actions/sqlLab';
import { URL_PARAMS } from 'src/constants';
import useLogAction from 'src/logger/useLogAction';
import {
LOG_ACTIONS_SQLLAB_COPY_RESULT_TO_CLIPBOARD,
LOG_ACTIONS_SQLLAB_CREATE_CHART,
LOG_ACTIONS_SQLLAB_DOWNLOAD_CSV,
} from 'src/logger/LogUtils';
import Icons from 'src/components/Icons';
import { findPermission } from 'src/utils/findPermission';
import ExploreCtasResultsButton from '../ExploreCtasResultsButton';
import ExploreResultsButton from '../ExploreResultsButton';
import HighlightedSql from '../HighlightedSql';
import QueryStateLabel from '../QueryStateLabel';
enum LimitingFactor {
Query = 'QUERY',
QueryAndDropdown = 'QUERY_AND_DROPDOWN',
Dropdown = 'DROPDOWN',
NotLimited = 'NOT_LIMITED',
}
export interface ResultSetProps {
cache?: boolean;
csv?: boolean;
database?: Record<string, any>;
displayLimit: number;
height: number;
queryId: string;
search?: boolean;
showSql?: boolean;
showSqlInline?: boolean;
visualize?: boolean;
defaultQueryLimit: number;
}
const ResultContainer = styled.div`
display: flex;
flex-direction: column;
row-gap: ${({ theme }) => theme.gridUnit * 2}px;
`;
const ResultlessStyles = styled.div`
position: relative;
min-height: ${({ theme }) => theme.gridUnit * 25}px;
[role='alert'] {
margin-top: ${({ theme }) => theme.gridUnit * 2}px;
}
.sql-result-track-job {
margin-top: ${({ theme }) => theme.gridUnit * 2}px;
}
`;
// Making text render line breaks/tabs as is as monospace,
// but wrapping text too so text doesn't overflow
const MonospaceDiv = styled.div`
font-family: ${({ theme }) => theme.typography.families.monospace};
white-space: pre;
word-break: break-word;
overflow-x: auto;
white-space: pre-wrap;
`;
const ReturnedRows = styled.div`
font-size: ${({ theme }) => theme.typography.sizes.s}px;
line-height: 1;
`;
const ResultSetControls = styled.div`
display: flex;
justify-content: space-between;
`;
const ResultSetButtons = styled.div`
display: grid;
grid-auto-flow: column;
padding-right: ${({ theme }) => 2 * theme.gridUnit}px;
`;
const ROWS_CHIP_WIDTH = 100;
const GAP = 8;
const extensionsRegistry = getExtensionsRegistry();
const ResultSet = ({
cache = false,
csv = true,
database = {},
displayLimit,
height,
queryId,
search = true,
showSql = false,
showSqlInline = false,
visualize = true,
defaultQueryLimit,
}: ResultSetProps) => {
const user = useSelector(({ user }: SqlLabRootState) => user, shallowEqual);
const query = useSelector(
({ sqlLab: { queries } }: SqlLabRootState) =>
pick(queries[queryId], [
'id',
'errorMessage',
'cached',
'results',
'resultsKey',
'dbId',
'tab',
'sql',
'sqlEditorId',
'templateParams',
'schema',
'rows',
'queryLimit',
'limitingFactor',
'trackingUrl',
'state',
'errors',
'link',
'ctas',
'ctas_method',
'tempSchema',
'tempTable',
'isDataPreview',
'progress',
'extra',
]),
shallowEqual,
);
const ResultTable =
extensionsRegistry.get('sqleditor.extension.resultTable') ??
FilterableTable;
const theme = useTheme();
const [searchText, setSearchText] = useState('');
const [cachedData, setCachedData] = useState<Record<string, unknown>[]>([]);
const [showSaveDatasetModal, setShowSaveDatasetModal] = useState(false);
const [alertIsOpen, setAlertIsOpen] = useState(false);
const history = useHistory();
const dispatch = useDispatch();
const logAction = useLogAction({ queryId, sqlEditorId: query.sqlEditorId });
const reRunQueryIfSessionTimeoutErrorOnMount = useCallback(() => {
if (
query.errorMessage &&
query.errorMessage.indexOf('session timed out') > 0
) {
dispatch(reRunQuery(query));
}
}, []);
useEffect(() => {
// only do this the first time the component is rendered/mounted
reRunQueryIfSessionTimeoutErrorOnMount();
}, [reRunQueryIfSessionTimeoutErrorOnMount]);
const fetchResults = (q: typeof query, timeout?: number) => {
dispatch(fetchQueryResults(q, displayLimit, timeout));
};
const prevQuery = usePrevious(query);
useEffect(() => {
if (cache && query.cached && query?.results?.data?.length > 0) {
setCachedData(query.results.data);
dispatch(clearQueryResults(query));
}
if (query.resultsKey && query.resultsKey !== prevQuery?.resultsKey) {
fetchResults(query);
}
}, [query, cache]);
const calculateAlertRefHeight = (alertElement: HTMLElement | null) => {
if (alertElement) {
setAlertIsOpen(true);
} else {
setAlertIsOpen(false);
}
};
const popSelectStar = (tempSchema: string | null, tempTable: string) => {
const qe = {
id: nanoid(11),
name: tempTable,
autorun: false,
dbId: query.dbId,
sql: `SELECT * FROM ${tempSchema ? `${tempSchema}.` : ''}${tempTable}`,
};
dispatch(addQueryEditor(qe));
};
const changeSearch = (event: ChangeEvent<HTMLInputElement>) => {
setSearchText(event.target.value);
};
const createExploreResultsOnClick = async (clickEvent: MouseEvent) => {
const { results } = query;
const openInNewWindow = clickEvent.metaKey;
logAction(LOG_ACTIONS_SQLLAB_CREATE_CHART, {});
if (results?.query_id) {
const key = await postFormData(results.query_id, 'query', {
...EXPLORE_CHART_DEFAULT,
datasource: `${results.query_id}__query`,
...{
all_columns: results.columns.map(column => column.column_name),
},
});
const url = mountExploreUrl(null, {
[URL_PARAMS.formDataKey.name]: key,
});
if (openInNewWindow) {
window.open(url, '_blank', 'noreferrer');
} else {
history.push(url);
}
} else {
addDangerToast(t('Unable to create chart without a query id.'));
}
};
const getExportCsvUrl = (clientId: string) =>
`/api/v1/sqllab/export/${clientId}/`;
const renderControls = () => {
if (search || visualize || csv) {
let { data } = query.results;
if (cache && query.cached) {
data = cachedData;
}
const { columns } = query.results;
// Added compute logic to stop user from being able to Save & Explore
const datasource: ISaveableDatasource = {
columns: query.results.columns as ISimpleColumn[],
name: query?.tab || 'Untitled',
dbId: query?.dbId,
sql: query?.sql,
templateParams: query?.templateParams,
schema: query?.schema,
};
const canExportData = findPermission(
'can_export_csv',
'SQLLab',
user?.roles,
);
return (
<ResultSetControls>
<SaveDatasetModal
visible={showSaveDatasetModal}
onHide={() => setShowSaveDatasetModal(false)}
buttonTextOnSave={t('Save & Explore')}
buttonTextOnOverwrite={t('Overwrite & Explore')}
modalDescription={t(
'Save this query as a virtual dataset to continue exploring',
)}
datasource={datasource}
/>
<ResultSetButtons>
{visualize && database?.allows_virtual_table_explore && (
<ExploreResultsButton
database={database}
onClick={createExploreResultsOnClick}
/>
)}
{csv && canExportData && (
<Button
buttonSize="small"
href={getExportCsvUrl(query.id)}
data-test="export-csv-button"
onClick={() => logAction(LOG_ACTIONS_SQLLAB_DOWNLOAD_CSV, {})}
>
<i className="fa fa-file-text-o" /> {t('Download to CSV')}
</Button>
)}
{canExportData && (
<CopyToClipboard
text={prepareCopyToClipboardTabularData(data, columns)}
wrapped={false}
copyNode={
<Button
buttonSize="small"
data-test="copy-to-clipboard-button"
>
<i className="fa fa-clipboard" /> {t('Copy to Clipboard')}
</Button>
}
hideTooltip
onCopyEnd={() =>
logAction(LOG_ACTIONS_SQLLAB_COPY_RESULT_TO_CLIPBOARD, {})
}
/>
)}
</ResultSetButtons>
{search && (
<input
type="text"
onChange={changeSearch}
value={searchText}
className="form-control input-sm"
placeholder={t('Filter results')}
/>
)}
</ResultSetControls>
);
}
return <div />;
};
const renderRowsReturned = (alertMessage: boolean) => {
const { results, rows, queryLimit, limitingFactor } = query;
let limitMessage = '';
const limitReached = results?.displayLimitReached;
const limit = queryLimit || results.query.limit;
const isAdmin = !!user?.roles?.Admin;
const rowsCount = Math.min(rows || 0, results?.data?.length || 0);
const displayMaxRowsReachedMessage = {
withAdmin: t(
'The number of results displayed is limited to %(rows)d by the configuration DISPLAY_MAX_ROW. ' +
'Please add additional limits/filters or download to csv to see more rows up to ' +
'the %(limit)d limit.',
{ rows: rowsCount, limit },
),
withoutAdmin: t(
'The number of results displayed is limited to %(rows)d. ' +
'Please add additional limits/filters, download to csv, or contact an admin ' +
'to see more rows up to the %(limit)d limit.',
{ rows: rowsCount, limit },
),
};
const shouldUseDefaultDropdownAlert =
limit === defaultQueryLimit && limitingFactor === LimitingFactor.Dropdown;
if (limitingFactor === LimitingFactor.Query && csv) {
limitMessage = t(
'The number of rows displayed is limited to %(rows)d by the query',
{ rows },
);
} else if (
limitingFactor === LimitingFactor.Dropdown &&
!shouldUseDefaultDropdownAlert
) {
limitMessage = t(
'The number of rows displayed is limited to %(rows)d by the limit dropdown.',
{ rows },
);
} else if (limitingFactor === LimitingFactor.QueryAndDropdown) {
limitMessage = t(
'The number of rows displayed is limited to %(rows)d by the query and limit dropdown.',
{ rows },
);
}
const formattedRowCount = getNumberFormatter()(rows);
const rowsReturnedMessage = t('%(rows)d rows returned', {
rows,
});
const tooltipText = `${rowsReturnedMessage}. ${limitMessage}`;
if (alertMessage) {
return (
<>
{!limitReached && shouldUseDefaultDropdownAlert && (
<div ref={calculateAlertRefHeight}>
<Alert
type="warning"
message={t('%(rows)d rows returned', { rows })}
onClose={() => setAlertIsOpen(false)}
description={t(
'The number of rows displayed is limited to %(rows)d by the dropdown.',
{ rows },
)}
/>
</div>
)}
{limitReached && (
<div ref={calculateAlertRefHeight}>
<Alert
type="warning"
onClose={() => setAlertIsOpen(false)}
message={t('%(rows)d rows returned', { rows: rowsCount })}
description={
isAdmin
? displayMaxRowsReachedMessage.withAdmin
: displayMaxRowsReachedMessage.withoutAdmin
}
/>
</div>
)}
</>
);
}
const showRowsReturned =
showSqlInline || (!limitReached && !shouldUseDefaultDropdownAlert);
return (
<>
{showRowsReturned && (
<ReturnedRows>
<Tooltip
id="sqllab-rowcount-tooltip"
title={tooltipText}
placement="left"
>
<Label
css={css`
line-height: ${theme.typography.sizes.l}px;
`}
>
{limitMessage && (
<Icons.ExclamationCircleOutlined
css={css`
font-size: ${theme.typography.sizes.m}px;
margin-right: ${theme.gridUnit}px;
`}
/>
)}
{tn('%s row', '%s rows', rows, formattedRowCount)}
</Label>
</Tooltip>
</ReturnedRows>
)}
</>
);
};
const limitReached = query?.results?.displayLimitReached;
let sql;
let exploreDBId = query.dbId;
if (database?.explore_database_id) {
exploreDBId = database.explore_database_id;
}
let trackingUrl;
if (
query.trackingUrl &&
query.state !== QueryState.Success &&
query.state !== QueryState.Fetching
) {
trackingUrl = (
<Button
className="sql-result-track-job"
buttonSize="small"
href={query.trackingUrl}
target="_blank"
>
{query.state === QueryState.Running
? t('Track job')
: t('See query details')}
</Button>
);
}
if (showSql) {
sql = (
<HighlightedSql
sql={query.sql}
{...(showSqlInline && { maxLines: 1, maxWidth: 60 })}
/>
);
}
if (query.state === QueryState.Stopped) {
return <Alert type="warning" message={t('Query was stopped')} />;
}
if (query.state === QueryState.Failed) {
const errors = [];
if (query.errorMessage) {
errors.push({
error_type: ErrorTypeEnum.GENERIC_DB_ENGINE_ERROR,
extra: {},
level: 'error' as ErrorLevel,
message: query.errorMessage,
});
}
errors.push(...(query.extra?.errors || []), ...(query.errors || []));
return (
<ResultlessStyles>
{errors.map((error, index) => (
<ErrorMessageWithStackTrace
key={index}
title={t('Database error')}
error={error}
subtitle={<MonospaceDiv>{error.message}</MonospaceDiv>}
copyText={error.message || undefined}
link={query.link}
source="sqllab"
/>
))}
{errors.some(
error => error?.error_type === ErrorTypeEnum.FRONTEND_TIMEOUT_ERROR,
) ? (
<Button
className="sql-result-track-job"
buttonSize="small"
onClick={() => fetchResults(query, 0)}
>
{t('Retry fetching results')}
</Button>
) : (
trackingUrl
)}
</ResultlessStyles>
);
}
if (query.state === QueryState.Success && query.ctas) {
const { tempSchema, tempTable } = query;
let object = 'Table';
if (query.ctas_method === CtasEnum.View) {
object = 'View';
}
return (
<div>
<Alert
type="info"
message={
<>
{t(object)} [
<strong>
{tempSchema ? `${tempSchema}.` : ''}
{tempTable}
</strong>
] {t('was created')}
<ButtonGroup>
<Button
buttonSize="small"
css={{ marginRight: theme.gridUnit }}
onClick={() => popSelectStar(tempSchema, tempTable)}
>
{t('Query in a new tab')}
</Button>
<ExploreCtasResultsButton
table={tempTable}
schema={tempSchema}
dbId={exploreDBId}
/>
</ButtonGroup>
</>
}
/>
</div>
);
}
if (query.state === QueryState.Success && query.results) {
const { results } = query;
// Accounts for offset needed for height of ResultSetRowsReturned component if !limitReached
const rowMessageHeight = !limitReached ? 32 : 0;
// Accounts for offset needed for height of Alert if this.state.alertIsOpen
const alertContainerHeight = 70;
// We need to calculate the height of this.renderRowsReturned()
// if we want results panel to be proper height because the
// FilterTable component needs an explicit height to render
// the Table component
const rowsHeight = alertIsOpen
? height - alertContainerHeight
: height - rowMessageHeight;
let data;
if (cache && query.cached) {
data = cachedData;
} else if (results?.data) {
({ data } = results);
}
if (data && data.length > 0) {
const expandedColumns = results.expanded_columns
? results.expanded_columns.map(col => col.column_name)
: [];
const allowHTML = getItem(
LocalStorageKeys.SqllabIsRenderHtmlEnabled,
true,
);
return (
<ResultContainer>
{renderControls()}
{showSql && showSqlInline ? (
<>
<div
css={css`
display: flex;
justify-content: space-between;
gap: ${GAP}px;
`}
>
<Card
css={[
css`
height: 28px;
width: calc(100% - ${ROWS_CHIP_WIDTH + GAP}px);
code {
width: 100%;
overflow: hidden;
white-space: nowrap !important;
text-overflow: ellipsis;
display: block;
}
`,
]}
>
{sql}
</Card>
{renderRowsReturned(false)}
</div>
{renderRowsReturned(true)}
</>
) : (
<>
{renderRowsReturned(false)}
{renderRowsReturned(true)}
{sql}
</>
)}
<ResultTable
data={data}
queryId={query.id}
orderedColumnKeys={results.columns.map(col => col.column_name)}
height={rowsHeight}
filterText={searchText}
expandedColumns={expandedColumns}
allowHTML={allowHTML}
/>
</ResultContainer>
);
}
if (data && data.length === 0) {
return <Alert type="warning" message={t('The query returned no data')} />;
}
}
if (query.cached || (query.state === QueryState.Success && !query.results)) {
if (query.isDataPreview) {
return (
<Button
buttonSize="small"
buttonStyle="primary"
onClick={() =>
dispatch(
reFetchQueryResults({
...query,
isDataPreview: true,
}),
)
}
>
{t('Fetch data preview')}
</Button>
);
}
if (query.resultsKey) {
return (
<Button
buttonSize="small"
buttonStyle="primary"
onClick={() => fetchResults(query)}
>
{t('Refetch results')}
</Button>
);
}
}
let progressBar;
if (query.progress > 0) {
progressBar = (
<ProgressBar percent={parseInt(query.progress.toFixed(0), 10)} striped />
);
}
const progressMsg = query?.extra?.progress ?? null;
return (
<ResultlessStyles>
<div>{!progressBar && <Loading position="normal" />}</div>
{/* show loading bar whenever progress bar is completed but needs time to render */}
<div>{query.progress === 100 && <Loading position="normal" />}</div>
<QueryStateLabel query={query} />
<div>{progressMsg && <Alert type="success" message={progressMsg} />}</div>
<div>{query.progress !== 100 && progressBar}</div>
{trackingUrl && <div>{trackingUrl}</div>}
</ResultlessStyles>
);
};
export default memo(ResultSet);