From 01e161c5b1a23b074dbff0eb96c9eca0f6a272e0 Mon Sep 17 00:00:00 2001 From: Victor Marin <36818606+mdvictor@users.noreply.github.com> Date: Tue, 23 Jul 2024 14:50:49 +0300 Subject: [PATCH] DashboardScenes: TemplateSrv should return timeRange from scenes if context found (#90735) * templateSrv should return timeRange from scenes if context found * add test * prettier * test --- .../features/templating/template_srv.test.ts | 26 +++++++++++++++++++ .../app/features/templating/template_srv.ts | 16 +++++++++--- 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/public/app/features/templating/template_srv.test.ts b/public/app/features/templating/template_srv.test.ts index 059056388b9..e96fce7f5d3 100644 --- a/public/app/features/templating/template_srv.test.ts +++ b/public/app/features/templating/template_srv.test.ts @@ -30,12 +30,16 @@ variableAdapters.setInit(() => [ ]); const interpolateMock = jest.fn(); +const timeRangeMock = jest.fn().mockReturnValue({ + state: { value: { from: dateTime(1594671549254), to: dateTime(1594671549254), raw: { from: '12', to: '14' } } }, +}); jest.mock('@grafana/scenes', () => ({ ...jest.requireActual('@grafana/scenes'), sceneGraph: { ...jest.requireActual('@grafana/scenes').sceneGraph, interpolate: (...args: unknown[]) => interpolateMock(...args), + getTimeRange: (...args: unknown[]) => timeRangeMock(...args), }, })); @@ -889,5 +893,27 @@ describe('templateSrv', () => { expect(podVar.current.value).toEqual(['pA', 'pB']); expect(podVar.current.text).toEqual(['podA', 'podB']); }); + + it('Should return timeRange from scenes context', () => { + window.__grafanaSceneContext = new EmbeddedScene({ + body: new SceneCanvasText({ text: 'hello' }), + }); + _templateSrv.updateTimeRange({ + from: dateTime(1594671549254), + to: dateTime(1594671549254), + raw: { from: '10', to: '10' }, + }); + + const deactivate = window.__grafanaSceneContext.activate(); + + expect(_templateSrv.timeRange).not.toBeNull(); + expect(_templateSrv.timeRange).not.toBeUndefined(); + expect(_templateSrv.timeRange!.raw).toEqual({ from: '12', to: '14' }); + expect(timeRangeMock).toHaveBeenCalled(); + + deactivate(); + + expect(_templateSrv.timeRange!.raw).toEqual({ from: '10', to: '10' }); + }); }); }); diff --git a/public/app/features/templating/template_srv.ts b/public/app/features/templating/template_srv.ts index 76d4fb33c3e..e0618c7404d 100644 --- a/public/app/features/templating/template_srv.ts +++ b/public/app/features/templating/template_srv.ts @@ -51,7 +51,7 @@ export class TemplateSrv implements BaseTemplateSrv { private regex = variableRegex; private index: any = {}; private grafanaVariables = new Map(); - private timeRange?: TimeRange | null = null; + private _timeRange?: TimeRange | null = null; private _adhocFiltersDeprecationWarningLogged = new Map(); constructor(private dependencies: TemplateSrvDependencies = runtimeDependencies) { @@ -60,7 +60,7 @@ export class TemplateSrv implements BaseTemplateSrv { init(variables: any, timeRange?: TimeRange) { this._variables = variables; - this.timeRange = timeRange; + this._timeRange = timeRange; this.updateIndex(); } @@ -83,6 +83,16 @@ export class TemplateSrv implements BaseTemplateSrv { return this.dependencies.getVariables(); } + get timeRange(): TimeRange | null | undefined { + if (window.__grafanaSceneContext && window.__grafanaSceneContext.isActive) { + const sceneTimeRange = sceneGraph.getTimeRange(window.__grafanaSceneContext); + + return sceneTimeRange.state.value; + } + + return this._timeRange; + } + updateIndex() { const existsOrEmpty = (value: unknown) => value || value === ''; @@ -110,7 +120,7 @@ export class TemplateSrv implements BaseTemplateSrv { } updateTimeRange(timeRange: TimeRange) { - this.timeRange = timeRange; + this._timeRange = timeRange; this.updateIndex(); }