Skip to content

Commit 783064b

Browse files
committed
fix: better code
1 parent 9231f1d commit 783064b

File tree

2 files changed

+81
-56
lines changed

2 files changed

+81
-56
lines changed

src/containers/Tenant/Query/QueryEditor/QueryEditor.tsx

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,9 @@ export default function QueryEditor(props: QueryEditorProps) {
110110

111111
const [sendQuery] = queryApi.useUseSendQueryMutation();
112112

113+
const isStreamingSupported = useStreamingAvailable();
114+
const [streamQuery] = queryApi.useUseStreamQueryMutation();
115+
113116
React.useEffect(() => {
114117
if (savedPath !== tenantName) {
115118
dispatch(setTenantPath(tenantName));
@@ -140,8 +143,6 @@ export default function QueryEditor(props: QueryEditorProps) {
140143
return historyQueries[historyQueries.length - 1].queryText;
141144
});
142145

143-
const isStreamingSupported = useStreamingAvailable();
144-
145146
const handleSendExecuteClick = useEventHandler((text?: string) => {
146147
const query = text ?? input;
147148

@@ -153,15 +154,25 @@ export default function QueryEditor(props: QueryEditorProps) {
153154
}
154155
const queryId = uuidv4();
155156

156-
sendQuery({
157-
actionType: 'execute',
158-
query,
159-
database: tenantName,
160-
querySettings,
161-
enableTracingLevel,
162-
queryId,
163-
isStreaming: isStreamingSupported,
164-
});
157+
if (isStreamingSupported) {
158+
streamQuery({
159+
actionType: 'execute',
160+
query,
161+
database: tenantName,
162+
querySettings,
163+
enableTracingLevel,
164+
queryId,
165+
});
166+
} else {
167+
sendQuery({
168+
actionType: 'execute',
169+
query,
170+
database: tenantName,
171+
querySettings,
172+
enableTracingLevel,
173+
queryId,
174+
});
175+
}
165176

166177
dispatch(setShowPreview(false));
167178

src/store/reducers/query/query.ts

Lines changed: 59 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,6 @@ const slice = createSlice({
5757
}
5858
},
5959
setQueryResult: (state, action: PayloadAction<QueryResult | undefined>) => {
60-
console.log('[QueryState] Setting query result:', {
61-
type: action.payload?.type,
62-
isLoading: action.payload?.isLoading,
63-
hasError: Boolean(action.payload?.error),
64-
});
6560
state.result = action.payload;
6661
},
6762
saveQueryToHistory: (
@@ -235,7 +230,6 @@ interface SendQueryParams extends QueryRequestParams {
235230
// flag whether to send new tracing header or not
236231
// default: not send
237232
enableTracingLevel?: boolean;
238-
isStreaming?: boolean;
239233
}
240234

241235
interface QueryStats {
@@ -245,6 +239,65 @@ interface QueryStats {
245239

246240
export const queryApi = api.injectEndpoints({
247241
endpoints: (build) => ({
242+
useStreamQuery: build.mutation<null, SendQueryParams>({
243+
queryFn: async (
244+
{query, database, querySettings = {}, enableTracingLevel, queryId},
245+
{signal, dispatch},
246+
) => {
247+
dispatch(setQueryResult({type: 'execute', queryId, isLoading: true}));
248+
dispatch(setStreamingState(true));
249+
250+
const {action, syntax} = getActionAndSyntaxFromQueryMode(
251+
'execute',
252+
querySettings?.queryMode,
253+
);
254+
255+
try {
256+
await window.api.viewer.streamQuery(
257+
{
258+
query,
259+
database,
260+
action,
261+
syntax,
262+
stats: querySettings.statisticsMode,
263+
tracingLevel:
264+
querySettings.tracingLevel && enableTracingLevel
265+
? TracingLevelNumber[querySettings.tracingLevel]
266+
: undefined,
267+
limit_rows: isNumeric(querySettings.limitRows)
268+
? Number(querySettings.limitRows)
269+
: undefined,
270+
transaction_mode:
271+
querySettings.transactionMode === 'implicit'
272+
? undefined
273+
: querySettings.transactionMode,
274+
timeout: isNumeric(querySettings.timeout)
275+
? Number(querySettings.timeout) * 1000
276+
: undefined,
277+
output_chunk_max_size: 10,
278+
},
279+
{
280+
signal,
281+
onChunk: (chunk) => {
282+
dispatch(addStreamingChunk(chunk));
283+
},
284+
},
285+
);
286+
287+
return {data: null};
288+
} catch (error) {
289+
dispatch(
290+
setQueryResult({
291+
type: 'execute',
292+
error,
293+
isLoading: false,
294+
queryId,
295+
}),
296+
);
297+
return {error};
298+
}
299+
},
300+
}),
248301
useSendQuery: build.mutation<null, SendQueryParams>({
249302
queryFn: async (
250303
{
@@ -254,7 +307,6 @@ export const queryApi = api.injectEndpoints({
254307
querySettings = {},
255308
enableTracingLevel,
256309
queryId,
257-
isStreaming,
258310
},
259311
{signal, dispatch},
260312
) => {
@@ -267,44 +319,6 @@ export const queryApi = api.injectEndpoints({
267319
);
268320

269321
try {
270-
if (isStreaming && actionType === 'execute') {
271-
dispatch(setStreamingState(true));
272-
273-
await window.api.viewer.streamQuery(
274-
{
275-
query,
276-
database,
277-
action,
278-
syntax,
279-
stats: querySettings.statisticsMode,
280-
tracingLevel:
281-
querySettings.tracingLevel && enableTracingLevel
282-
? TracingLevelNumber[querySettings.tracingLevel]
283-
: undefined,
284-
limit_rows: isNumeric(querySettings.limitRows)
285-
? Number(querySettings.limitRows)
286-
: undefined,
287-
transaction_mode:
288-
querySettings.transactionMode === 'implicit'
289-
? undefined
290-
: querySettings.transactionMode,
291-
timeout: isNumeric(querySettings.timeout)
292-
? Number(querySettings.timeout) * 1000
293-
: undefined,
294-
output_chunk_max_size: 10,
295-
},
296-
{
297-
signal,
298-
onChunk: (chunk) => {
299-
dispatch(addStreamingChunk(chunk));
300-
},
301-
},
302-
);
303-
304-
return {data: null};
305-
}
306-
307-
// Legacy implementation for older versions and non-execute queries
308322
const response = await window.api.viewer.sendQuery(
309323
{
310324
query,

0 commit comments

Comments
 (0)