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

SceneDataTransformer: Interpolate transformation options #1051

Merged
merged 4 commits into from
Feb 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 57 additions & 1 deletion packages/scenes/src/querying/SceneDataTransformer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ import { mockTransformationsRegistry } from '../utils/mockTransformationsRegistr
import { SceneQueryRunner } from './SceneQueryRunner';
import { SceneTimeRange } from '../core/SceneTimeRange';
import { subscribeToStateUpdates } from '../../utils/test/utils';
import { SceneVariableSet } from '../variables/sets/SceneVariableSet';
import { TextBoxVariable } from '../variables/variants/TextBoxVariable';
import { activateFullSceneTree } from '../utils/test/activateFullSceneTree';

class TestSceneObject extends SceneObjectBase<{}> {}

Expand Down Expand Up @@ -129,6 +132,8 @@ jest.mock('@grafana/runtime', () => ({

describe('SceneDataTransformer', () => {
let customTransformerSpy = jest.fn();
let transformerSpy = jest.fn();
let annotationTransformerSpy = jest.fn();

let sourceDataNode: SceneDataNode;
let customTransformOperator: CustomTransformOperator;
Expand All @@ -143,7 +148,7 @@ describe('SceneDataTransformer', () => {
id: 'transformer1',
name: 'Custom Transformer',
operator: (options) => (source) => {
// transformerSpy1(options);
transformerSpy(options);
return source.pipe(
map((data) => {
return data.map((frame) => {
Expand Down Expand Up @@ -187,6 +192,7 @@ describe('SceneDataTransformer', () => {
id: 'annotationTransformer',
name: 'Custom annotationTransformer',
operator: (options) => (source) => {
annotationTransformerSpy(options);
return source.pipe(
map((data) => {
return data.map((frame) => ({
Expand Down Expand Up @@ -232,6 +238,8 @@ describe('SceneDataTransformer', () => {

afterEach(() => {
customTransformerSpy.mockClear();
transformerSpy.mockClear();
annotationTransformerSpy.mockClear();
});

it('applies transformations to closest data node', () => {
Expand Down Expand Up @@ -704,6 +712,54 @@ describe('SceneDataTransformer', () => {
expect(transformer.state.data?.state).toBe(LoadingState.Loading);
});
});

it('interpolates transformation options before applying', () => {
const transformationNode = new SceneDataTransformer({
transformations: [
{
...transformer1config,
options: {
options: '$myVariable',
},
},
{
...annotationTransformerConfig,
options: {
options: 'annotation-transformation-$myVariable',
},
},
],
});

const consumer = new TestSceneObject({
$data: transformationNode,
});

const textVar = new TextBoxVariable({ name: 'myVariable', value: 'Text Variable Value' });
const scene = new SceneFlexLayout({
$data: sourceDataNode,
$variables: new SceneVariableSet({ variables: [textVar] }),
children: [new SceneFlexItem({ body: consumer })],
});

activateFullSceneTree(scene);

expect(transformerSpy).toHaveBeenCalledTimes(1);
expect(transformerSpy).toHaveBeenLastCalledWith({ options: 'Text Variable Value' });
expect(annotationTransformerSpy).toHaveBeenCalledTimes(1);
expect(annotationTransformerSpy).toHaveBeenLastCalledWith({
options: 'annotation-transformation-Text Variable Value',
});

textVar.setValue('New Text Variable Value');

expect(transformerSpy).toHaveBeenCalledTimes(2);
expect(transformerSpy).toHaveBeenLastCalledWith({ options: 'New Text Variable Value' });
expect(annotationTransformerSpy).toHaveBeenCalledTimes(2);
expect(annotationTransformerSpy).toHaveBeenLastCalledWith({
options: 'annotation-transformation-New Text Variable Value',
});
});
});

export interface SceneObjectSearchBoxState extends SceneObjectState {
Expand Down
29 changes: 27 additions & 2 deletions packages/scenes/src/querying/SceneDataTransformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,9 @@ export class SceneDataTransformer extends SceneObjectBase<SceneDataTransformerSt
return;
}

const seriesTransformations = this.state.transformations
let interpolatedTransformations = this._interpolateVariablesInTransformationConfigs(data);

const seriesTransformations = interpolatedTransformations
.filter((transformation) => {
if ('options' in transformation || 'topic' in transformation) {
return transformation.topic == null || transformation.topic === DataTopic.Series;
Expand All @@ -159,7 +161,7 @@ export class SceneDataTransformer extends SceneObjectBase<SceneDataTransformerSt
})
.map((transformation) => ('operator' in transformation ? transformation.operator : transformation));

const annotationsTransformations = this.state.transformations
const annotationsTransformations = interpolatedTransformations
.filter((transformation) => {
if ('options' in transformation || 'topic' in transformation) {
return transformation.topic === DataTopic.Annotations;
Expand Down Expand Up @@ -220,4 +222,27 @@ export class SceneDataTransformer extends SceneObjectBase<SceneDataTransformerSt
this._prevDataFromSource = data;
});
}

private _interpolateVariablesInTransformationConfigs(
data: PanelData
): Array<DataTransformerConfig | CustomTransformerDefinition> {
const transformations = this.state.transformations;

if (this._variableDependency.getNames().size === 0) {
return transformations;
}

const onlyObjects = transformations.every((t) => typeof t === 'object');

// If all transformations are config object we can interpolate them all at once
if (onlyObjects) {
return JSON.parse(sceneGraph.interpolate(this, JSON.stringify(transformations), data.request?.scopedVars));
}

return transformations.map((t) => {
return typeof t === 'object'
? JSON.parse(sceneGraph.interpolate(this, JSON.stringify(t), data.request?.scopedVars))
: t;
});
}
}