diff --git a/public/app/features/dashboard/services/TimeSrv.test.ts b/public/app/features/dashboard/services/TimeSrv.test.ts index 8048d6edeb5..5fb8640adf8 100644 --- a/public/app/features/dashboard/services/TimeSrv.test.ts +++ b/public/app/features/dashboard/services/TimeSrv.test.ts @@ -3,6 +3,7 @@ import { ContextSrvStub } from 'test/specs/helpers'; import { dateTime, isDateTime } from '@grafana/data'; import { config, HistoryWrapper, locationService, setLocationService } from '@grafana/runtime'; +import { SceneTimeRange } from '@grafana/scenes'; import { TimeModel } from '../state/TimeModel'; @@ -342,4 +343,25 @@ describe('timeSrv', () => { }); }); }); + + describe('Scenes compatibility', () => { + it('should use scene provided range if active', () => { + timeSrv.setTime({ from: 'now-6h', to: 'now' }); + + const timeRange = new SceneTimeRange({ + from: 'now-1h', + to: 'now', + }); + window.__timeRangeSceneObject = timeRange; + + let time = timeSrv.timeRange(); + expect(time.raw.from).toBe('now-6h'); + expect(time.raw.to).toBe('now'); + + timeRange.activate(); + time = timeSrv.timeRange(); + expect(time.raw.from).toBe('now-1h'); + expect(time.raw.to).toBe('now'); + }); + }); }); diff --git a/public/app/features/dashboard/services/TimeSrv.ts b/public/app/features/dashboard/services/TimeSrv.ts index 92e3d162347..accd2c13824 100644 --- a/public/app/features/dashboard/services/TimeSrv.ts +++ b/public/app/features/dashboard/services/TimeSrv.ts @@ -338,6 +338,12 @@ export class TimeSrv { }; timeRange(): TimeRange { + // Scenes can set this global object to the current time range. + // This is a patch to support data sources that rely on TimeSrv.getTimeRange() + if (window.__timeRangeSceneObject && window.__timeRangeSceneObject.isActive) { + return window.__timeRangeSceneObject.state.value; + } + return getTimeRange(this.time, this.timeModel); } diff --git a/public/app/types/window.d.ts b/public/app/types/window.d.ts index 0365573751a..311937d2554 100644 --- a/public/app/types/window.d.ts +++ b/public/app/types/window.d.ts @@ -1,5 +1,11 @@ export declare global { interface Window { + __timeRangeSceneObject: { + isActive: boolean; + state: { + value: TimeRange; + }; + }; __grafana_app_bundle_loaded: boolean; __grafana_public_path__: string; __grafana_load_failed: () => void;