Skip to content

Commit

Permalink
Rename 'transform' to 'processor' and 'adder' to 'supplementer'
Browse files Browse the repository at this point in the history
Not sure these names are much better but they're possibly less ambiguous.
  • Loading branch information
sd2k committed May 2, 2024
1 parent e34941b commit 79d35df
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 37 deletions.
12 changes: 6 additions & 6 deletions packages/scenes/src/components/SceneTimeRangeCompare.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { sceneGraph } from '../core/sceneGraph';
import { SceneObjectBase } from '../core/SceneObjectBase';
import { SceneComponentProps, SceneObjectState, SceneObjectUrlValues } from '../core/types';
import { DataQueryExtended } from '../querying/SceneQueryRunner';
import { ExtraRequest, SceneRequestAdder, TransformFunc } from '../querying/SceneRequestAdder';
import { ExtraRequest, ProcessorFunc, SceneRequestSupplementer } from '../querying/SceneRequestAdder';
import { SceneObjectUrlSyncConfig } from '../services/SceneObjectUrlSyncConfig';
import { getCompareSeriesRefId } from '../utils/getCompareSeriesRefId';
import { parseUrlParam } from '../utils/parseUrlParam';
Expand Down Expand Up @@ -38,7 +38,7 @@ export const DEFAULT_COMPARE_OPTIONS = [

export class SceneTimeRangeCompare
extends SceneObjectBase<SceneTimeRangeCompareState>
implements SceneRequestAdder<SceneTimeRangeCompareState> {
implements SceneRequestSupplementer<SceneTimeRangeCompareState> {

static Component = SceneTimeRangeCompareRenderer;
protected _urlSync = new SceneObjectUrlSyncConfig(this, { keys: ['compareWith'] });
Expand Down Expand Up @@ -95,7 +95,7 @@ export class SceneTimeRangeCompare
};

// Get a time shifted request to compare with the primary request.
public getExtraRequests(request: DataQueryRequest): ExtraRequest[] {
public getSupplementalRequests(request: DataQueryRequest): ExtraRequest[] {
const extraRequests: ExtraRequest[] = [];
const compareRange = this.getCompareTimeRange(request.range);
if (!compareRange) {
Expand All @@ -110,7 +110,7 @@ export class SceneTimeRangeCompare
targets,
range: compareRange,
},
transform: timeShiftAlignmentTransform,
processor: timeShiftAlignmentProcessor,
});
}
return extraRequests;
Expand Down Expand Up @@ -176,11 +176,11 @@ export class SceneTimeRangeCompare
}
}

// Transformation function for use with time shifted comparison series.
// Processor function for use with time shifted comparison series.
// This aligns the secondary series with the primary and adds custom
// metadata and config to the secondary series' fields so that it is
// rendered appropriately.
const timeShiftAlignmentTransform: TransformFunc = (primary, secondary) => {
const timeShiftAlignmentProcessor: ProcessorFunc = (primary, secondary) => {
const diff = secondary.timeRange.from.diff(primary.timeRange.from);
secondary.series.forEach((series) => {
series.refId = getCompareSeriesRefId(series.refId || '');
Expand Down
2 changes: 1 addition & 1 deletion packages/scenes/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export { SceneTimeRange } from './core/SceneTimeRange';
export { SceneTimeZoneOverride } from './core/SceneTimeZoneOverride';

export { SceneQueryRunner, type QueryRunnerState } from './querying/SceneQueryRunner';
export { type ExtraRequest, type SceneRequestAdder, type TransformFunc } from './querying/SceneRequestAdder';
export { type ExtraRequest, type SceneRequestSupplementer as SceneRequestAdder, type ProcessorFunc } from './querying/SceneRequestAdder';
export { SceneDataLayerSet, SceneDataLayerSetBase } from './querying/SceneDataLayerSet';
export { SceneDataLayerBase } from './querying/layers/SceneDataLayerBase';
export { SceneDataLayerControls } from './querying/layers/SceneDataLayerControls';
Expand Down
20 changes: 10 additions & 10 deletions packages/scenes/src/querying/SceneQueryRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ import { writeSceneLog } from '../utils/writeSceneLog';
import { VariableValueRecorder } from '../variables/VariableValueRecorder';
import { emptyPanelData } from '../core/SceneDataNode';
import { getClosest } from '../core/sceneGraph/utils';
import { isRequestAdder, SceneRequestAdder, TransformFunc } from './SceneRequestAdder';
import { passthroughTransform, extraRequestProcessingOperator } from './extraRequestProcessingOperator';
import { isRequestAdder, ProcessorFunc, SceneRequestSupplementer } from './SceneRequestAdder';
import { passthroughProcessor, extraRequestProcessingOperator } from './extraRequestProcessingOperator';
import { filterAnnotations } from './layers/annotations/filterAnnotations';
import { getEnrichedDataRequest } from './getEnrichedDataRequest';
import { findActiveAdHocFilterVariableByUid } from '../variables/adhoc/patchGetAdhocFilters';
Expand Down Expand Up @@ -394,7 +394,7 @@ export class SceneQueryRunner extends SceneObjectBase<QueryRunnerState> implemen
this.findAndSubscribeToAdHocFilters(datasource?.uid);

const runRequest = getRunRequest();
const { primary, secondaries, transformations } = this.prepareRequests(timeRange, ds);
const { primary, secondaries, processors } = this.prepareRequests(timeRange, ds);

writeSceneLog('SceneQueryRunner', 'Starting runRequest', this.state.key);

Expand All @@ -404,7 +404,7 @@ export class SceneQueryRunner extends SceneObjectBase<QueryRunnerState> implemen
const [sReq, ...otherSReqs] = secondaries;
const secondaryStreams = otherSReqs.map((r) => runRequest(ds, r));
// change subscribe callback below to pipe operator
const op = extraRequestProcessingOperator(transformations);
const op = extraRequestProcessingOperator(processors);
stream = forkJoin([stream, runRequest(ds, sReq), ...secondaryStreams]).pipe(op);
}

Expand Down Expand Up @@ -449,7 +449,7 @@ export class SceneQueryRunner extends SceneObjectBase<QueryRunnerState> implemen
private prepareRequests = (
timeRange: SceneTimeRangeLike,
ds: DataSourceApi
): { primary: DataQueryRequest, secondaries: DataQueryRequest[], transformations: Map<string, TransformFunc> } => {
): { primary: DataQueryRequest, secondaries: DataQueryRequest[], processors: Map<string, ProcessorFunc> } => {
const { minInterval, queries } = this.state;

let request: DataQueryRequest<DataQueryExtended> = {
Expand Down Expand Up @@ -514,16 +514,16 @@ export class SceneQueryRunner extends SceneObjectBase<QueryRunnerState> implemen

const primaryTimeRange = timeRange.state.value;
let secondaryRequests: DataQueryRequest[] = [];
let secondaryTransformations = new Map();
let secondaryProcessors = new Map();
for (const adder of this.getClosestRequestAdders().values() ?? []) {
for (const { req, transform } of adder.getExtraRequests(request)) {
for (const { req, processor } of adder.getSupplementalRequests(request)) {
const requestId = getNextRequestId();
secondaryRequests.push({ ...req, requestId })
secondaryTransformations.set(requestId, transform ?? passthroughTransform);
secondaryProcessors.set(requestId, processor ?? passthroughProcessor);
}
}
request.range = primaryTimeRange;
return { primary: request, secondaries: secondaryRequests, transformations: secondaryTransformations };
return { primary: request, secondaries: secondaryRequests, processors: secondaryProcessors };
};

private onDataReceived = (data: PanelData) => {
Expand Down Expand Up @@ -569,7 +569,7 @@ export class SceneQueryRunner extends SceneObjectBase<QueryRunnerState> implemen
*
* This will return a map from id to the closest adder for each id.
*/
private getClosestRequestAdders(): Map<string, SceneRequestAdder<any>> {
private getClosestRequestAdders(): Map<string, SceneRequestSupplementer<any>> {
const found = new Map();
if (!this.parent) {
return new Map();
Expand Down
21 changes: 11 additions & 10 deletions packages/scenes/src/querying/SceneRequestAdder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,30 @@ 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
// A processor 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;
export type ProcessorFunc = (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.
// processor that should be called with the response data.
export interface ExtraRequest {
// The request.
req: DataQueryRequest;
// An optional transformation function.
transform?: TransformFunc;
// An optional function used to process the data before passing it
// to any transformations or visualizations.
processor?: ProcessorFunc;
}

// 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[];
export interface SceneRequestSupplementer<T extends SceneObjectState> extends SceneObjectBase<T> {
// Get any supplemental requests and their required processors.
getSupplementalRequests(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;
export function isRequestAdder(obj: any): obj is SceneRequestSupplementer<any> {
return typeof obj === 'object' && 'getSupplementalRequests' in obj;
}
20 changes: 10 additions & 10 deletions packages/scenes/src/querying/extraRequestProcessingOperator.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
import { PanelData } from '@grafana/data';
import { map, Observable } from 'rxjs';
import { TransformFunc } from './SceneRequestAdder';
import { ProcessorFunc } from './SceneRequestAdder';

// Passthrough transformation for use with ExtraRequests.
export const passthroughTransform: TransformFunc = (_, secondary) => secondary;
// Passthrough processor for use with ExtraRequests.
export const passthroughProcessor: ProcessorFunc = (_, secondary) => secondary;

// Factory function which takes a map from request ID to transform functions and
// Factory function which takes a map from request ID to processor 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
// Each secondary response is transformed according to the processor function
// identified by it's request ID. The processor 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
// The output is a single frame with the primary series and all processed
// secondary series combined.
export const extraRequestProcessingOperator = (transforms: Map<string, TransformFunc>) =>
export const extraRequestProcessingOperator = (processors: Map<string, ProcessorFunc>) =>
(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;
const processed = processors.get(s.request!.requestId)?.(primary, s) ?? s;
return processed.series;
});
return {
...primary,
Expand Down

0 comments on commit 79d35df

Please sign in to comment.