Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VizPanel: Allow queries to be cancelled #220

Merged
merged 13 commits into from
Jun 19, 2023
5 changes: 5 additions & 0 deletions packages/scenes/src/components/VizPanel/VizPanelRenderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import { sceneGraph } from '../../core/sceneGraph';
import { isSceneObject, SceneComponentProps } from '../../core/types';

import { VizPanel } from './VizPanel';
import { getClosest } from '../../core/sceneGraph/utils';
import { SceneQueryRunner } from '../../querying/SceneQueryRunner';

export function VizPanelRenderer({ model }: SceneComponentProps<VizPanel>) {
const {
Expand All @@ -35,6 +37,8 @@ export function VizPanelRenderer({ model }: SceneComponentProps<VizPanel>) {
const dataObject = sceneGraph.getData(model);
const rawData = dataObject.useState();
const dataWithFieldConfig = model.applyFieldConfig(rawData.data!);
// maybe move this logic to VizPanel itself?
const queryRunner = getClosest(model, (s) => (s.state.$data instanceof SceneQueryRunner) ? s.state.$data : undefined);
kaydelaney marked this conversation as resolved.
Show resolved Hide resolved
dprokop marked this conversation as resolved.
Show resolved Hide resolved

// Interpolate title
const titleInterpolated = model.interpolate(title, undefined, 'text');
Expand Down Expand Up @@ -105,6 +109,7 @@ export function VizPanelRenderer({ model }: SceneComponentProps<VizPanel>) {
dragClassCancel={dragClassCancel}
padding={plugin.noPadding ? 'none' : 'md'}
menu={panelMenu}
onCancelQuery={() => queryRunner?.cancelQuery()}
kaydelaney marked this conversation as resolved.
Show resolved Hide resolved
>
{(innerWidth, innerHeight) => (
<>
Expand Down
9 changes: 8 additions & 1 deletion packages/scenes/src/querying/SceneQueryRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,13 @@ export class SceneQueryRunner extends SceneObjectBase<QueryRunnerState> implemen
return this.state.maxDataPoints ?? this._containerWidth ?? 500;
}

public cancelQuery() {
this._querySub?.unsubscribe();
this.setState({
data: { ...this.state.data!, state: LoadingState.Done },
});
}

private async runWithTimeRange(timeRange: SceneTimeRangeLike) {
// Skip executing queries if variable dependency is in loading state
if (sceneGraph.hasVariableDependencyInLoadingState(this)) {
Expand All @@ -161,7 +168,7 @@ export class SceneQueryRunner extends SceneObjectBase<QueryRunnerState> implemen
return;
}

// If we where waiting for variables clear that flag
// If we were waiting for variables, clear that flag
if (this.state.isWaitingForVariables) {
this.setState({ isWaitingForVariables: false });
}
Expand Down