Skip to content

Commit 6bf5727

Browse files
committed
fix: better code
1 parent cfe5cb2 commit 6bf5727

File tree

5 files changed

+40
-84
lines changed

5 files changed

+40
-84
lines changed

src/containers/Tenant/Query/QueryResult/QueryResultViewer.tsx

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -217,20 +217,9 @@ export function QueryResultViewer({
217217
return <QueryResultError error={error} />;
218218
}
219219
if (activeSection === RESULT_OPTIONS_IDS.result) {
220-
if (!streaming) {
221-
return (
222-
<ResultSetsViewer
223-
type="regular"
224-
resultSets={resultSets || []}
225-
selectedResultSet={selectedResultSet}
226-
setSelectedResultSet={setSelectedResultSet}
227-
/>
228-
);
229-
}
230220
return (
231221
<ResultSetsViewer
232-
type="streaming"
233-
streaming={streaming}
222+
resultSets={resultSets || []}
234223
selectedResultSet={selectedResultSet}
235224
setSelectedResultSet={setSelectedResultSet}
236225
/>

src/containers/Tenant/Query/QueryResult/components/ResultSetsViewer/ResultSetsViewer.tsx

Lines changed: 8 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {Tabs, Text} from '@gravity-ui/uikit';
22

33
import {QueryResultTable} from '../../../../../../components/QueryResultTable';
4-
import type {ParsedResultSet, StreamingState} from '../../../../../../types/store/query';
4+
import type {ParsedResultSet} from '../../../../../../types/store/query';
55
import {getArray} from '../../../../../../utils';
66
import {cn} from '../../../../../../utils/cn';
77
import i18n from '../../i18n';
@@ -10,45 +10,24 @@ import './ResultSetsViewer.scss';
1010

1111
const b = cn('ydb-query-result-sets-viewer');
1212

13-
interface BaseResultSetsViewerProps {
13+
interface ResultSetsViewerProps {
14+
resultSets?: ParsedResultSet[];
1415
selectedResultSet: number;
1516
setSelectedResultSet: (resultSet: number) => void;
1617
}
1718

18-
interface RegularResultSetsProps extends BaseResultSetsViewerProps {
19-
type: 'regular';
20-
resultSets: ParsedResultSet[];
21-
}
22-
23-
interface StreamingResultSetsProps extends BaseResultSetsViewerProps {
24-
type: 'streaming';
25-
streaming: StreamingState;
26-
}
27-
28-
type ResultSetsViewerProps = RegularResultSetsProps | StreamingResultSetsProps;
29-
3019
export function ResultSetsViewer(props: ResultSetsViewerProps) {
31-
const {selectedResultSet, setSelectedResultSet} = props;
32-
33-
const resultsSetsCount = props.type === 'streaming' ? 1 : props.resultSets.length;
20+
const {selectedResultSet, setSelectedResultSet, resultSets} = props;
3421

35-
const currentResult =
36-
props.type === 'streaming'
37-
? {
38-
result: props.streaming.rows,
39-
columns: props.streaming.columns,
40-
truncated: false,
41-
}
42-
: props.resultSets[selectedResultSet];
22+
const resultsSetsCount = resultSets?.length || 0;
23+
const currentResult = resultSets?.[selectedResultSet];
4324

25+
console.log(currentResult);
4426
const renderTabs = () => {
4527
if (resultsSetsCount > 1) {
4628
const tabsItems = getArray(resultsSetsCount).map((item) => ({
4729
id: String(item),
48-
title:
49-
props.type === 'streaming'
50-
? `Chunk #${item + 1}`
51-
: `Result #${item + 1}${props.resultSets[item]?.truncated ? ' (T)' : ''}`,
30+
title: `Result #${item + 1}${resultSets?.[item]?.truncated ? ' (T)' : ''}`,
5231
}));
5332

5433
return (

src/store/reducers/query/query.ts

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,7 @@ import type {PayloadAction} from '@reduxjs/toolkit';
33

44
import {settingsManager} from '../../../services/settings';
55
import {TracingLevelNumber} from '../../../types/api/query';
6-
import type {
7-
QueryAction,
8-
QueryRequestParams,
9-
QuerySettings,
10-
StreamingState,
11-
} from '../../../types/store/query';
6+
import type {QueryAction, QueryRequestParams, QuerySettings} from '../../../types/store/query';
127
import type {StreamingChunk} from '../../../types/store/streaming';
138
import {QUERIES_HISTORY_KEY} from '../../../utils/constants';
149
import {isQueryErrorResponse} from '../../../utils/query';
@@ -141,50 +136,55 @@ const slice = createSlice({
141136
setQueryHistoryFilter: (state, action: PayloadAction<string>) => {
142137
state.history.filter = action.payload;
143138
},
144-
setStreamingState: (state, action: PayloadAction<StreamingState>) => {
139+
setStreamingState: (state, action: PayloadAction<boolean>) => {
145140
if (state.result) {
146-
state.result.streaming = {
147-
active: action.payload.active,
148-
progress: action.payload.progress,
149-
queryId: '',
150-
rows: [],
151-
columns: undefined, // Reset columns for new query
152-
};
141+
state.result.streaming = action.payload;
142+
if (action.payload) {
143+
state.result.data = prepareQueryData(null);
144+
}
153145
}
154146
},
155147
addStreamingChunk: (state, action: PayloadAction<StreamingChunk>) => {
156-
if (!state.result?.streaming) {
148+
if (!state.result?.streaming || !state.result.data) {
157149
return;
158150
}
159151

160152
const chunk = action.payload;
161153

162154
if (isSessionChunk(chunk)) {
163-
state.result.streaming.queryId = chunk.meta.query_id;
155+
state.result.queryId = chunk.meta.query_id;
164156
} else if (isStreamDataChunk(chunk)) {
165157
const {
166158
result: {columns, rows},
167159
} = chunk;
168160

169-
if (columns && !state.result.streaming.columns) {
170-
state.result.streaming.columns = columns;
161+
if (columns && !state.result.data.resultSets?.[0]?.columns?.length) {
162+
if (state.result.data.resultSets) {
163+
state.result.data.resultSets[0].columns = columns;
164+
} else {
165+
state.result.data.resultSets = [{columns, result: []}];
166+
}
171167
}
172168
// Convert and append new rows
173-
if (rows.length > 0 && state.result.streaming.columns) {
169+
if (rows.length > 0 && state.result.data.resultSets?.[0]?.columns) {
174170
const convertedRows = convertToKeyValueRows(
175171
rows,
176-
state.result.streaming.columns,
172+
state.result.data.resultSets[0].columns,
177173
);
178-
state.result.streaming.rows = [
179-
...(state.result.streaming.rows || []),
180-
...convertedRows,
181-
];
174+
175+
if (state.result.data.resultSets[0].result) {
176+
state.result.data.resultSets[0].result.push(...convertedRows);
177+
} else {
178+
state.result.data.resultSets[0].result = convertedRows;
179+
}
182180
}
183181
} else if (isQueryResponseChunk(chunk)) {
184-
state.result.streaming.active = false;
182+
state.result.streaming = false;
185183
state.result.isLoading = false;
186-
state.result.streaming.plan = chunk.plan;
187-
state.result.streaming.stats = chunk.stats;
184+
if (state.result.data) {
185+
state.result.data.plan = chunk.plan;
186+
state.result.data.stats = chunk.stats;
187+
}
188188
}
189189
},
190190
},
@@ -243,9 +243,7 @@ export const queryApi = api.injectEndpoints({
243243

244244
try {
245245
if (isStreaming && actionType === 'execute') {
246-
dispatch(
247-
setStreamingState({active: true, progress: 0, queryId: '', rows: []}),
248-
);
246+
dispatch(setStreamingState(true));
249247

250248
await window.api.viewer.streamQuery(
251249
{

src/store/reducers/query/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import type {
77
SimlifiedPlanOperatorOtherParams,
88
SimplifiedNode,
99
} from '../../../types/api/query';
10-
import type {IQueryResult, QueryAction, StreamingState} from '../../../types/store/query';
10+
import type {IQueryResult, QueryAction} from '../../../types/store/query';
1111

1212
export interface QueryInHistory {
1313
queryId?: string;
@@ -52,7 +52,7 @@ export interface QueryResult {
5252
isTraceReady?: true;
5353
queryId: string;
5454
isLoading: boolean;
55-
streaming?: StreamingState;
55+
streaming?: boolean;
5656
}
5757

5858
export interface QueryState {

src/types/store/query.ts

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,6 @@ export interface ParsedResultSet {
2626
truncated?: boolean;
2727
}
2828

29-
export interface StreamingState {
30-
active: boolean;
31-
progress: number;
32-
columns?: ColumnType[];
33-
rows: KeyValueRow[];
34-
plan?: QueryPlan;
35-
stats?: TKqpStatsQuery;
36-
queryId: string;
37-
}
38-
3929
export interface IQueryResult {
4030
resultSets?: ParsedResultSet[];
4131
columns?: ColumnType[];
@@ -44,7 +34,7 @@ export interface IQueryResult {
4434
ast?: string;
4535
traceId?: string;
4636
truncated?: boolean;
47-
streaming?: StreamingState;
37+
streaming?: boolean;
4838
}
4939

5040
export interface QueryRequestParams {

0 commit comments

Comments
 (0)