From 4f58dee2b7fd13b4fd6e5866bb8963d2b007ea6c Mon Sep 17 00:00:00 2001 From: Ben Sully Date: Wed, 24 Apr 2024 14:44:18 +0100 Subject: [PATCH] Rename 'transform' to 'processor' and 'adder' to 'supplementer' Not sure these names are much better but they're possibly less ambiguous. --- .../src/components/SceneTimeRangeCompare.tsx | 12 +++++------ packages/scenes/src/index.ts | 2 +- .../scenes/src/querying/SceneQueryRunner.ts | 20 +++++++++--------- .../scenes/src/querying/SceneRequestAdder.ts | 21 ++++++++++--------- .../extraRequestProcessingOperator.ts | 20 +++++++++--------- 5 files changed, 38 insertions(+), 37 deletions(-) diff --git a/packages/scenes/src/components/SceneTimeRangeCompare.tsx b/packages/scenes/src/components/SceneTimeRangeCompare.tsx index 30a18d24a..b29641231 100644 --- a/packages/scenes/src/components/SceneTimeRangeCompare.tsx +++ b/packages/scenes/src/components/SceneTimeRangeCompare.tsx @@ -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'; @@ -38,7 +38,7 @@ export const DEFAULT_COMPARE_OPTIONS = [ export class SceneTimeRangeCompare extends SceneObjectBase - implements SceneRequestAdder { + implements SceneRequestSupplementer { static Component = SceneTimeRangeCompareRenderer; protected _urlSync = new SceneObjectUrlSyncConfig(this, { keys: ['compareWith'] }); @@ -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) { @@ -110,7 +110,7 @@ export class SceneTimeRangeCompare targets, range: compareRange, }, - transform: timeShiftAlignmentTransform, + processor: timeShiftAlignmentProcessor, }); } return extraRequests; @@ -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 || ''); diff --git a/packages/scenes/src/index.ts b/packages/scenes/src/index.ts index c95e434df..9b04b2426 100644 --- a/packages/scenes/src/index.ts +++ b/packages/scenes/src/index.ts @@ -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'; diff --git a/packages/scenes/src/querying/SceneQueryRunner.ts b/packages/scenes/src/querying/SceneQueryRunner.ts index b83085fa6..23f7371c3 100644 --- a/packages/scenes/src/querying/SceneQueryRunner.ts +++ b/packages/scenes/src/querying/SceneQueryRunner.ts @@ -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'; @@ -407,7 +407,7 @@ export class SceneQueryRunner extends SceneObjectBase 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); @@ -417,7 +417,7 @@ export class SceneQueryRunner extends SceneObjectBase 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); } @@ -464,7 +464,7 @@ export class SceneQueryRunner extends SceneObjectBase implemen private prepareRequests = ( timeRange: SceneTimeRangeLike, ds: DataSourceApi - ): { primary: DataQueryRequest, secondaries: DataQueryRequest[], transformations: Map } => { + ): { primary: DataQueryRequest, secondaries: DataQueryRequest[], processors: Map } => { const { minInterval, queries } = this.state; let request: DataQueryRequest = { @@ -530,16 +530,16 @@ export class SceneQueryRunner extends SceneObjectBase 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) => { @@ -585,7 +585,7 @@ export class SceneQueryRunner extends SceneObjectBase implemen * * This will return a map from id to the closest adder for each id. */ - private getClosestRequestAdders(): Map> { + private getClosestRequestAdders(): Map> { const found = new Map(); if (!this.parent) { return new Map(); diff --git a/packages/scenes/src/querying/SceneRequestAdder.ts b/packages/scenes/src/querying/SceneRequestAdder.ts index 80fd8c1e5..d94af7da1 100644 --- a/packages/scenes/src/querying/SceneRequestAdder.ts +++ b/packages/scenes/src/querying/SceneRequestAdder.ts @@ -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 extends SceneObjectBase { - // Get any extra requests and their required transformations. - getExtraRequests(request: DataQueryRequest): ExtraRequest[]; +export interface SceneRequestSupplementer extends SceneObjectBase { + // 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 { - return typeof obj === 'object' && 'getExtraRequests' in obj; +export function isRequestAdder(obj: any): obj is SceneRequestSupplementer { + return typeof obj === 'object' && 'getSupplementalRequests' in obj; } diff --git a/packages/scenes/src/querying/extraRequestProcessingOperator.ts b/packages/scenes/src/querying/extraRequestProcessingOperator.ts index 1c56042aa..debda3c18 100644 --- a/packages/scenes/src/querying/extraRequestProcessingOperator.ts +++ b/packages/scenes/src/querying/extraRequestProcessingOperator.ts @@ -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) => +export const extraRequestProcessingOperator = (processors: Map) => (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,