DashboardScenes: TemplateSrv should return timeRange from scenes if context found (#90735)

* templateSrv should return timeRange from scenes if context found

* add test

* prettier

* test
This commit is contained in:
Victor Marin 2024-07-23 14:50:49 +03:00 committed by GitHub
parent d75475ae03
commit 01e161c5b1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 39 additions and 3 deletions

View File

@ -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' });
});
});
});

View File

@ -51,7 +51,7 @@ export class TemplateSrv implements BaseTemplateSrv {
private regex = variableRegex;
private index: any = {};
private grafanaVariables = new Map<string, any>();
private timeRange?: TimeRange | null = null;
private _timeRange?: TimeRange | null = null;
private _adhocFiltersDeprecationWarningLogged = new Map<string, boolean>();
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();
}