-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SceneQueryRunner: decouple time range comparisons
Prior to this commit, `SceneQueryRunner` had special handling for `SceneTimeRangeCompare` objects, explicitly searching for them in the scene graph, adding additional queries, and transforming resulting queries. This made it difficult to re-use the general behaviour of 'running additional queries' in other objects. This commit introduces a new interface, `SceneRequestAdder`, which can be implemented to inform the query runner that it should run additional requests (returned by `getExtraRequests`) and transform the results in some fashion. Instead of searching the graph for `SceneTimeRangeCompare` objects, the query runner searches for implementors of `SceneRequestAdder` and uses those instead. The specifics of how it searches for these is a little bit fuzzy and should probably be improved: it walks up the graph until it finds at least one adder at the current level or in any children of the current level; adds any others at that level or in the children; then returns. `SceneTimeRangeCompare` has been refactored to make use of this new interface. I've also got a separate object which also implements it which is working well including when both are enabled.
- Loading branch information
Showing
7 changed files
with
169 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { DataQueryRequest, PanelData } from "@grafana/data"; | ||
|
||
import { SceneObjectBase } from "../core/SceneObjectBase"; | ||
import { SceneObjectState } from "../core/types"; | ||
|
||
// A transformation function called by the query runner with responses | ||
// to any extra requests. | ||
// | ||
// See the docs for `extraRequestProcessingOperator` for more information. | ||
export type TransformFunc = (primary: PanelData, secondary: PanelData) => PanelData; | ||
|
||
// An extra request that should be run by a query runner, and an optional | ||
// transform that should be called with the response data. | ||
export interface ExtraRequest { | ||
// The request. | ||
req: DataQueryRequest; | ||
// An optional transformation function. | ||
transform?: TransformFunc; | ||
} | ||
|
||
// Indicates that this type wants to add extra requests to a query runner. | ||
export interface SceneRequestAdder<T extends SceneObjectState> extends SceneObjectBase<T> { | ||
// Get any extra requests and their required transformations. | ||
getExtraRequests(request: DataQueryRequest): ExtraRequest[]; | ||
// Determine whether a query should be rerun. | ||
shouldRerun(prev: T, next: T): boolean; | ||
} | ||
|
||
export function isRequestAdder(obj: any): obj is SceneRequestAdder<any> { | ||
return typeof obj === 'object' && 'getExtraRequests' in obj; | ||
} |
33 changes: 33 additions & 0 deletions
33
packages/scenes/src/querying/extraRequestProcessingOperator.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { PanelData } from '@grafana/data'; | ||
import { map, Observable } from 'rxjs'; | ||
import { TransformFunc } from './SceneRequestAdder'; | ||
|
||
// Passthrough transformation for use with ExtraRequests. | ||
export const passthroughTransform: TransformFunc = (_, secondary) => secondary; | ||
|
||
// Factory function which takes a map from request ID to transform functions and | ||
// returns an rxjs operator which operates on an array of panel data responses. | ||
// The responses must have length at least 2; the first is treated as the 'primary' | ||
// response and the rest as secondary responses. | ||
// | ||
// Each secondary response is transformed according to the transform function | ||
// identified by it's request ID. The transform function is passed the primary | ||
// response and the secondary response to be processed. | ||
// | ||
// The output is a single frame with the primary series and all transformed | ||
// secondary series combined. | ||
export const extraRequestProcessingOperator = (transforms: Map<string, TransformFunc>) => | ||
(data: Observable<[PanelData, PanelData, ...PanelData[]]>) => { | ||
return data.pipe( | ||
map(([primary, ...secondaries]) => { | ||
const frames = secondaries.flatMap((s) => { | ||
const transformed = transforms.get(s.request!.requestId)?.(primary, s) ?? s; | ||
return transformed.series; | ||
}); | ||
return { | ||
...primary, | ||
series: [...primary.series, ...frames], | ||
}; | ||
}) | ||
); | ||
} |
46 changes: 0 additions & 46 deletions
46
packages/scenes/src/querying/timeShiftQueryResponseOperator.ts
This file was deleted.
Oops, something went wrong.