-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathQueryResultViewer.tsx
More file actions
344 lines (306 loc) · 11.5 KB
/
QueryResultViewer.tsx
File metadata and controls
344 lines (306 loc) · 11.5 KB
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
import React from 'react';
import type {Settings} from '@gravity-ui/react-data-table';
import type {ControlGroupOption} from '@gravity-ui/uikit';
import {ClipboardButton, Flex, RadioButton, Text} from '@gravity-ui/uikit';
import EnableFullscreenButton from '../../../../components/EnableFullscreenButton/EnableFullscreenButton';
import Fullscreen from '../../../../components/Fullscreen/Fullscreen';
import {Illustration} from '../../../../components/Illustration';
import {LoaderWrapper} from '../../../../components/LoaderWrapper/LoaderWrapper';
import {QueryExecutionStatus} from '../../../../components/QueryExecutionStatus';
import {disableFullscreen} from '../../../../store/reducers/fullscreen';
import type {QueryResult} from '../../../../store/reducers/query/types';
import type {ValueOf} from '../../../../types/common';
import type {QueryAction} from '../../../../types/store/query';
import {cn} from '../../../../utils/cn';
import {USE_SHOW_PLAN_SVG_KEY} from '../../../../utils/constants';
import {getStringifiedData} from '../../../../utils/dataFormatters/dataFormatters';
import {useSetting, useTypedDispatch} from '../../../../utils/hooks';
import {PaneVisibilityToggleButtons} from '../../utils/paneVisibilityToggleHelpers';
import {QuerySettingsBanner} from '../QuerySettingsBanner/QuerySettingsBanner';
import {QueryStoppedBanner} from '../QueryStoppedBanner/QueryStoppedBanner';
import {getPreparedResult} from '../utils/getPreparedResult';
import {isQueryCancelledError} from '../utils/isQueryCancelledError';
import {Ast} from './components/Ast/Ast';
import {Graph} from './components/Graph/Graph';
import {QueryInfoDropdown} from './components/QueryInfoDropdown/QueryInfoDropdown';
import {QueryJSONViewer} from './components/QueryJSONViewer/QueryJSONViewer';
import {QueryResultError} from './components/QueryResultError/QueryResultError';
import {ResultSetsViewer} from './components/ResultSetsViewer/ResultSetsViewer';
import {SimplifiedPlan} from './components/SimplifiedPlan/SimplifiedPlan';
import {StubMessage} from './components/Stub/Stub';
import {TraceButton} from './components/TraceButton/TraceButton';
import i18n from './i18n';
import './QueryResultViewer.scss';
const b = cn('ydb-query-result');
const RESULT_OPTIONS_IDS = {
result: 'result',
schema: 'schema',
simplified: 'simplified',
json: 'json',
stats: 'stats',
ast: 'ast',
} as const;
type SectionID = ValueOf<typeof RESULT_OPTIONS_IDS>;
const RESULT_OPTIONS_TITLES: Record<SectionID, string> = {
get result() {
return i18n('action.result');
},
get schema() {
return i18n('action.schema');
},
get simplified() {
return i18n('action.explain-plan');
},
get json() {
return i18n('action.json');
},
get stats() {
return i18n('action.stats');
},
get ast() {
return i18n('action.ast');
},
};
const EXECUTE_SECTIONS: SectionID[] = ['result', 'schema', 'simplified', 'stats'];
const EXPLAIN_SECTIONS: SectionID[] = ['schema', 'simplified', 'json', 'ast'];
interface ExecuteResultProps {
result: QueryResult;
resultType?: QueryAction;
isResultsCollapsed?: boolean;
theme?: string;
tenantName: string;
queryText?: string;
tableSettings?: Partial<Settings>;
onCollapseResults: VoidFunction;
onExpandResults: VoidFunction;
}
export function QueryResultViewer({
result,
resultType = 'execute',
isResultsCollapsed,
theme,
tenantName,
queryText,
tableSettings,
onCollapseResults,
onExpandResults,
}: ExecuteResultProps) {
const dispatch = useTypedDispatch();
const isExecute = resultType === 'execute';
const isExplain = resultType === 'explain';
const [selectedResultSet, setSelectedResultSet] = React.useState(0);
const [activeSection, setActiveSection] = React.useState<SectionID>(() => {
return isExecute ? RESULT_OPTIONS_IDS.result : RESULT_OPTIONS_IDS.schema;
});
const [useShowPlanToSvg] = useSetting<boolean>(USE_SHOW_PLAN_SVG_KEY);
const {error, isLoading, data = {}} = result;
const {preparedPlan, simplifiedPlan, stats, resultSets, ast} = data;
React.useEffect(() => {
if (resultType === 'execute' && !EXECUTE_SECTIONS.includes(activeSection)) {
setActiveSection('result');
}
if (resultType === 'explain' && !EXPLAIN_SECTIONS.includes(activeSection)) {
setActiveSection('schema');
}
}, [activeSection, resultType]);
const radioButtonOptions: ControlGroupOption<SectionID>[] = React.useMemo(() => {
let sections: SectionID[] = [];
if (isExecute) {
sections = EXECUTE_SECTIONS;
} else if (isExplain) {
sections = EXPLAIN_SECTIONS;
}
return sections.map((section) => {
return {
value: section,
content: RESULT_OPTIONS_TITLES[section],
};
});
}, [isExecute, isExplain]);
React.useEffect(() => {
return () => {
dispatch(disableFullscreen());
};
}, [dispatch]);
const onSelectSection = (value: SectionID) => {
setActiveSection(value);
};
const getStatsToCopy = () => {
switch (activeSection) {
case RESULT_OPTIONS_IDS.result: {
const currentResult = data?.resultSets?.[selectedResultSet];
const textResults = getPreparedResult(currentResult?.result);
return textResults;
}
case RESULT_OPTIONS_IDS.json: {
return preparedPlan?.pristine;
}
case RESULT_OPTIONS_IDS.simplified:
return simplifiedPlan?.pristine;
case RESULT_OPTIONS_IDS.stats:
return stats;
case RESULT_OPTIONS_IDS.ast:
return ast;
default:
return undefined;
}
};
const renderClipboardButton = () => {
if (isLoading) {
return null;
}
const statsToCopy = getStatsToCopy();
const copyText = getStringifiedData(statsToCopy);
if (!copyText) {
return null;
}
return (
<ClipboardButton
text={copyText}
view="flat-secondary"
title={i18n('action.copy', {activeSection})}
/>
);
};
const renderQueryInfoDropdown = () => {
if (isLoading || isQueryCancelledError(error)) {
return null;
}
return (
<QueryInfoDropdown
queryResultsInfo={{
queryText,
ast: data.ast,
stats: data.stats,
plan: data.plan,
}}
error={error}
database={tenantName}
hasPlanToSvg={Boolean(data?.plan && useShowPlanToSvg && isExecute)}
/>
);
};
const renderStubMessage = () => {
return (
<StubMessage
message={i18n('description.empty-result', {
activeSection: RESULT_OPTIONS_TITLES[activeSection],
})}
/>
);
};
const renderCommonErrorView = (isStopped: boolean) => {
return (
<Flex justifyContent="center" alignItems="center" width="100%" gap={8}>
<Illustration name="error" className={b('illustration')} />
<Flex direction="column" gap={2}>
<Text variant="subheader-2">
{isStopped ? i18n('stopped.title') : i18n('error.title')}
</Text>
<Text color="complementary">
{isStopped ? i18n('stopped.description') : i18n('error.description')}
</Text>
</Flex>
</Flex>
);
};
const renderResultSection = () => {
const isStopped = isQueryCancelledError(error);
if (activeSection === RESULT_OPTIONS_IDS.result) {
if (error && isStopped && !resultSets?.length) {
return renderCommonErrorView(isStopped);
}
return (
<ResultSetsViewer
resultSets={resultSets}
error={error}
selectedResultSet={selectedResultSet}
tableSettings={tableSettings}
setSelectedResultSet={setSelectedResultSet}
/>
);
}
if (error) {
return isExecute || isStopped ? (
renderCommonErrorView(isStopped)
) : (
<QueryResultError error={error} />
);
}
if (activeSection === RESULT_OPTIONS_IDS.schema) {
if (!preparedPlan?.nodes?.length) {
return renderStubMessage();
}
return <Graph theme={theme} explain={preparedPlan} />;
}
if (activeSection === RESULT_OPTIONS_IDS.json) {
if (!preparedPlan?.pristine) {
return renderStubMessage();
}
return <QueryJSONViewer data={preparedPlan?.pristine} />;
}
if (activeSection === RESULT_OPTIONS_IDS.simplified) {
if (!simplifiedPlan?.plan?.length) {
return renderStubMessage();
}
return <SimplifiedPlan plan={simplifiedPlan.plan} />;
}
if (activeSection === RESULT_OPTIONS_IDS.stats) {
if (!stats) {
return renderStubMessage();
}
return <QueryJSONViewer data={stats} />;
}
if (activeSection === RESULT_OPTIONS_IDS.ast) {
if (!ast) {
return renderStubMessage();
}
return <Ast ast={ast} theme={theme} />;
}
return null;
};
const renderLeftControls = () => {
return (
<div className={b('controls-left')}>
{radioButtonOptions.length && activeSection ? (
<RadioButton
options={radioButtonOptions}
value={activeSection}
onUpdate={onSelectSection}
/>
) : null}
<QueryExecutionStatus error={error} loading={isLoading} />
{data?.traceId && isExecute ? <TraceButton traceId={data.traceId} /> : null}
</div>
);
};
const renderRightControls = () => {
return (
<div className={b('controls-right')}>
{renderQueryInfoDropdown()}
{renderClipboardButton()}
<EnableFullscreenButton />
<PaneVisibilityToggleButtons
onCollapse={onCollapseResults}
onExpand={onExpandResults}
isCollapsed={isResultsCollapsed}
initialDirection="bottom"
/>
</div>
);
};
const isCancelled = isQueryCancelledError(error);
return (
<React.Fragment>
<div className={b('controls')}>
{renderLeftControls()}
{renderRightControls()}
</div>
{isLoading || isCancelled ? null : <QuerySettingsBanner />}
{isCancelled && data.resultSets?.length ? <QueryStoppedBanner /> : null}
<LoaderWrapper loading={isLoading && (!data.resultSets || activeSection !== 'result')}>
<Fullscreen className={b('result')}>{renderResultSection()}</Fullscreen>
</LoaderWrapper>
</React.Fragment>
);
}