TimeSrv: Add Scenes compatibility patch (#75728)

* TimeSrv: Add scenes compatibility patch

* Get that lint out of my way
This commit is contained in:
Dominik Prokop
2023-09-29 05:56:31 -07:00
committed by GitHub
parent bc9dd475b8
commit fccd262417
3 changed files with 34 additions and 0 deletions
@@ -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');
});
});
});
@@ -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);
}
+6
View File
@@ -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;