Skip to content

Commit

Permalink
SceneQueryRunner: make some fields and methods protected
Browse files Browse the repository at this point in the history
PR #587 is quite a large change and introduces more concepts to scenes, so
isn't easy to justify merging right now. As a workaround, I'd like to be
able to subclass and override some methods of the SceneQueryRunner to
implement some similar behaviour in a different library, but I need
various fields and methods to be accessible to the subclass, which this
PR does by making them protected rather than private.

Note: I can _almost_ just completely copy/paste the class into my own
code rather than extending it, but the [`getQueriesForVariables`] function
looks for all _instances_ of SceneQueryRunner, which my copy/pasted class
would not match (unlike a subclass, which should), so I think some
ad-hoc variable support would break :(

`getQueriesForVariables`: https://github.com/grafana/scenes/blob/07c5dc66746d36208cd121b938883db8ef2243f7/packages/scenes/src/variables/utils.ts#L88-L94
  • Loading branch information
sd2k committed May 24, 2024
1 parent 07c5dc6 commit 23942cb
Showing 1 changed file with 27 additions and 25 deletions.
52 changes: 27 additions & 25 deletions packages/scenes/src/querying/SceneQueryRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
DataSourceApi,
DataTopic,
PanelData,
ScopedVars,
preProcessPanelData,
rangeUtil,
} from '@grafana/data';
Expand Down Expand Up @@ -76,20 +77,21 @@ export interface DataQueryExtended extends DataQuery {
}

export class SceneQueryRunner extends SceneObjectBase<QueryRunnerState> implements SceneDataProvider {
private _querySub?: Unsubscribable;
private _dataLayersSub?: Unsubscribable;
private _dataLayersMerger = new DataLayersMerger();
private _timeSub?: Unsubscribable;
private _timeSubRange?: SceneTimeRangeLike;
private _containerWidth?: number;
private _variableValueRecorder = new VariableValueRecorder();
private _results = new ReplaySubject<SceneDataProviderResult>(1);
private _scopedVars = { __sceneObject: { value: this, text: '__sceneObject' } };
private _layerAnnotations?: DataFrame[];
private _resultAnnotations?: DataFrame[];

private _adhocFiltersVar?: AdHocFiltersVariable;
private _groupByVar?: GroupByVariable;

protected _querySub?: Unsubscribable;
protected _dataLayersSub?: Unsubscribable;
protected _dataLayersMerger = new DataLayersMerger();
protected _timeSub?: Unsubscribable;
protected _timeSubRange?: SceneTimeRangeLike;
protected _containerWidth?: number;
protected _variableValueRecorder = new VariableValueRecorder();
protected _results = new ReplaySubject<SceneDataProviderResult>(1);
protected _scopedVars: ScopedVars = { __sceneObject: { value: this, text: '__sceneObject' } };
protected _layerAnnotations?: DataFrame[];
protected _resultAnnotations?: DataFrame[];

protected _adhocFiltersVar?: AdHocFiltersVariable;
protected _groupByVar?: GroupByVariable;

public getResultsStream() {
return this._results;
Expand All @@ -107,7 +109,7 @@ export class SceneQueryRunner extends SceneObjectBase<QueryRunnerState> implemen
this.addActivationHandler(() => this._onActivate());
}

private _onActivate() {
protected _onActivate() {
const timeRange = sceneGraph.getTimeRange(this);
const comparer = this.getTimeCompare();

Expand Down Expand Up @@ -135,7 +137,7 @@ export class SceneQueryRunner extends SceneObjectBase<QueryRunnerState> implemen
}

// This method subscribes to all SceneDataLayers up until the root, and combines the results into data provided from SceneQueryRunner
private _handleDataLayers() {
protected _handleDataLayers() {
const dataLayers = sceneGraph.getDataLayers(this);

if (dataLayers.length === 0) {
Expand Down Expand Up @@ -247,7 +249,7 @@ export class SceneQueryRunner extends SceneObjectBase<QueryRunnerState> implemen
return variable.state.applyMode === 'auto' && datasource?.uid === variable.state.datasource?.uid;
}

private shouldRunQueriesOnActivate() {
protected shouldRunQueriesOnActivate() {
if (this._variableValueRecorder.hasDependenciesChanged(this)) {
writeSceneLog(
'SceneQueryRunner',
Expand Down Expand Up @@ -285,7 +287,7 @@ export class SceneQueryRunner extends SceneObjectBase<QueryRunnerState> implemen
return true;
}

private _onDeactivate(): void {
protected _onDeactivate(): void {
if (this._querySub) {
this._querySub.unsubscribe();
this._querySub = undefined;
Expand Down Expand Up @@ -330,7 +332,7 @@ export class SceneQueryRunner extends SceneObjectBase<QueryRunnerState> implemen
return Boolean(this.state._hasFetchedData);
}

private subscribeToTimeRangeChanges(timeRange: SceneTimeRangeLike) {
protected subscribeToTimeRangeChanges(timeRange: SceneTimeRangeLike) {
if (this._timeSubRange === timeRange) {
// Nothing to do, already subscribed
return;
Expand All @@ -352,7 +354,7 @@ export class SceneQueryRunner extends SceneObjectBase<QueryRunnerState> implemen
this.runWithTimeRange(timeRange);
}

private getMaxDataPoints() {
protected getMaxDataPoints() {
if (this.state.maxDataPoints) {
return this.state.maxDataPoints;
}
Expand All @@ -373,7 +375,7 @@ export class SceneQueryRunner extends SceneObjectBase<QueryRunnerState> implemen
});
}

private async runWithTimeRange(timeRange: SceneTimeRangeLike) {
protected async runWithTimeRange(timeRange: SceneTimeRangeLike) {
// If no maxDataPoints specified we might need to wait for container width to be set from the outside
if (!this.state.maxDataPoints && this.state.maxDataPointsFromWidth && !this._containerWidth) {
return;
Expand Down Expand Up @@ -554,7 +556,7 @@ export class SceneQueryRunner extends SceneObjectBase<QueryRunnerState> implemen
return [request, secondaryRequest];
};

private onDataReceived = (data: PanelData) => {
protected onDataReceived = (data: PanelData) => {
// Will combine annotations from SQR queries (frames with meta.dataTopic === DataTopic.Annotations)
const preProcessedData = preProcessPanelData(data, this.state.data);

Expand Down Expand Up @@ -586,7 +588,7 @@ export class SceneQueryRunner extends SceneObjectBase<QueryRunnerState> implemen
return data;
}

private _setNoDataState() {
protected _setNoDataState() {
if (this.state.data !== emptyPanelData) {
this.setState({ data: emptyPanelData });
}
Expand All @@ -596,7 +598,7 @@ export class SceneQueryRunner extends SceneObjectBase<QueryRunnerState> implemen
* Will walk up the scene graph and find the closest time range compare object
* It performs buttom-up search, including shallow search across object children for supporting controls/header actions
*/
private getTimeCompare() {
protected getTimeCompare() {
if (!this.parent) {
return null;
}
Expand All @@ -618,7 +620,7 @@ export class SceneQueryRunner extends SceneObjectBase<QueryRunnerState> implemen
/**
* Walk up scene graph and find the closest filterset with matching data source
*/
private findAndSubscribeToAdHocFilters(uid: string | undefined) {
protected findAndSubscribeToAdHocFilters(uid: string | undefined) {
const filtersVar = findActiveAdHocFilterVariableByUid(uid);

if (this._adhocFiltersVar !== filtersVar) {
Expand Down

0 comments on commit 23942cb

Please sign in to comment.