Skip to content

Commit 85d95c0

Browse files
committed
fix: cancelling query
1 parent b6128c3 commit 85d95c0

File tree

5 files changed

+34
-11
lines changed

5 files changed

+34
-11
lines changed

src/containers/Tenant/Query/CancelQueryButton/CancelQueryButton.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,16 @@ const b = cn('cancel-query-button');
1414
interface CancelQueryButtonProps {
1515
queryId: string;
1616
tenantName: string;
17+
onClick?: VoidFunction;
1718
}
1819

19-
export function CancelQueryButton({queryId, tenantName}: CancelQueryButtonProps) {
20+
export function CancelQueryButton({queryId, tenantName, onClick}: CancelQueryButtonProps) {
2021
const [sendCancelQuery, cancelQueryResponse] = cancelQueryApi.useCancelQueryMutation();
2122

2223
const onStopButtonClick = React.useCallback(() => {
2324
sendCancelQuery({queryId, database: tenantName});
24-
}, [queryId, sendCancelQuery, tenantName]);
25+
onClick?.();
26+
}, [onClick, queryId, sendCancelQuery, tenantName]);
2527

2628
return (
2729
<Button

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

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -107,12 +107,13 @@ export default function QueryEditor(props: QueryEditorProps) {
107107
LAST_USED_QUERY_ACTION_KEY,
108108
);
109109
const [lastExecutedQueryText, setLastExecutedQueryText] = React.useState<string>('');
110+
const isStreamingSupported = useStreamingAvailable();
110111

111112
const [sendQuery] = queryApi.useUseSendQueryMutation();
112-
113-
const isStreamingSupported = useStreamingAvailable();
114113
const [streamQuery] = queryApi.useUseStreamQueryMutation();
115114

115+
const runningQueryRef = React.useRef<{abort: VoidFunction} | null>(null);
116+
116117
React.useEffect(() => {
117118
if (savedPath !== tenantName) {
118119
dispatch(setTenantPath(tenantName));
@@ -155,7 +156,7 @@ export default function QueryEditor(props: QueryEditorProps) {
155156
const queryId = uuidv4();
156157

157158
if (isStreamingSupported) {
158-
streamQuery({
159+
runningQueryRef.current = streamQuery({
159160
actionType: 'execute',
160161
query,
161162
database: tenantName,
@@ -164,7 +165,7 @@ export default function QueryEditor(props: QueryEditorProps) {
164165
queryId,
165166
});
166167
} else {
167-
sendQuery({
168+
runningQueryRef.current = sendQuery({
168169
actionType: 'execute',
169170
query,
170171
database: tenantName,
@@ -199,7 +200,7 @@ export default function QueryEditor(props: QueryEditorProps) {
199200

200201
const queryId = uuidv4();
201202

202-
sendQuery({
203+
runningQueryRef.current = sendQuery({
203204
actionType: 'explain',
204205
query: input,
205206
database: tenantName,
@@ -221,6 +222,12 @@ export default function QueryEditor(props: QueryEditorProps) {
221222
}
222223
});
223224

225+
const handleCancelRunningQuery = React.useCallback(() => {
226+
if (runningQueryRef.current) {
227+
runningQueryRef.current.abort();
228+
}
229+
}, []);
230+
224231
const editorWillUnmount = () => {
225232
window.ydbEditor = undefined;
226233
};
@@ -388,6 +395,7 @@ export default function QueryEditor(props: QueryEditorProps) {
388395
path={path}
389396
showPreview={showPreview}
390397
queryText={lastExecutedQueryText}
398+
onCancelRunningQuery={handleCancelRunningQuery}
391399
/>
392400
</div>
393401
</SplitPane>
@@ -407,6 +415,7 @@ interface ResultProps {
407415
path: string;
408416
showPreview?: boolean;
409417
queryText: string;
418+
onCancelRunningQuery: VoidFunction;
410419
}
411420
function Result({
412421
resultVisibilityState,
@@ -419,6 +428,7 @@ function Result({
419428
path,
420429
showPreview,
421430
queryText,
431+
onCancelRunningQuery,
422432
}: ResultProps) {
423433
if (showPreview) {
424434
return <Preview database={tenantName} path={path} type={type} />;
@@ -435,6 +445,7 @@ function Result({
435445
onExpandResults={onExpandResultHandler}
436446
onCollapseResults={onCollapseResultHandler}
437447
queryText={queryText}
448+
onCancelRunningQuery={onCancelRunningQuery}
438449
/>
439450
);
440451
}

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,10 @@ interface ExecuteResultProps {
8181
isResultsCollapsed?: boolean;
8282
theme?: string;
8383
tenantName: string;
84+
queryText?: string;
85+
onCancelRunningQuery?: VoidFunction;
8486
onCollapseResults: VoidFunction;
8587
onExpandResults: VoidFunction;
86-
queryText?: string;
8788
}
8889

8990
export function QueryResultViewer({
@@ -93,6 +94,7 @@ export function QueryResultViewer({
9394
theme,
9495
tenantName,
9596
queryText,
97+
onCancelRunningQuery,
9698
onCollapseResults,
9799
onExpandResults,
98100
}: ExecuteResultProps) {
@@ -288,7 +290,11 @@ export function QueryResultViewer({
288290
{isLoading ? (
289291
<React.Fragment>
290292
<ElapsedTime className={b('elapsed-time')} />
291-
<CancelQueryButton queryId={queryId} tenantName={tenantName} />
293+
<CancelQueryButton
294+
queryId={queryId}
295+
tenantName={tenantName}
296+
onClick={onCancelRunningQuery}
297+
/>
292298
</React.Fragment>
293299
) : null}
294300
{data?.traceId && isExecute ? (

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const b = cn('ydb-query-result-error ');
1010
export function QueryResultError({error}: {error: unknown}) {
1111
const parsedError = parseQueryError(error);
1212

13-
// "Stopped" message is displayd in QueryExecutionStatus
13+
// "Stopped" message is displayed in QueryExecutionStatus
1414
// There is no need to display "Query is cancelled" message too
1515
if (!parsedError || isQueryCancelledError(error)) {
1616
return null;
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1+
import type {IResponseError} from '../../../../types/api/error';
12
import {parseQueryError} from '../../../../utils/query';
23

34
export function isQueryCancelledError(error: unknown) {
45
const parsedError = parseQueryError(error);
5-
return typeof parsedError === 'object' && parsedError.error?.message === 'Query was cancelled';
6+
return (
7+
(error as IResponseError)?.isCancelled ||
8+
(typeof parsedError === 'object' && parsedError.error?.message === 'Query was cancelled')
9+
);
610
}

0 commit comments

Comments
 (0)