Skip to content

Commit

Permalink
SceneTimeRange: Fixes weekstart issue when evaluting data math (#1002)
Browse files Browse the repository at this point in the history
  • Loading branch information
torkelo authored Dec 16, 2024
1 parent 3958245 commit 1edea24
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
16 changes: 14 additions & 2 deletions packages/scenes/src/core/SceneTimeRange.test.tsx
Original file line number Diff line number Diff line change
@@ -1,26 +1,38 @@
import { toUtc, dateMath } from '@grafana/data';
import { toUtc, dateMath, setWeekStart } from '@grafana/data';
import { SceneFlexItem, SceneFlexLayout } from '../components/layout/SceneFlexLayout';
import { PanelBuilders } from './PanelBuilders';
import { SceneTimeRange } from './SceneTimeRange';
import { RefreshEvent } from '@grafana/runtime';
import { RefreshEvent, config } from '@grafana/runtime';
import { EmbeddedScene } from '../components/EmbeddedScene';
import { SceneReactObject } from '../components/SceneReactObject';

jest.mock('@grafana/data', () => ({
...jest.requireActual('@grafana/data'),
setWeekStart: jest.fn(),
}));

function simulateDelay(newDateString: string, scene: EmbeddedScene) {
jest.setSystemTime(new Date(newDateString));
scene.activate();
}

config.bootData = { user: { weekStart: 'monday' } } as any;

describe('SceneTimeRange', () => {
it('when created should evaluate time range', () => {
const timeRange = new SceneTimeRange({ from: 'now-1h', to: 'now' });
expect(timeRange.state.value.raw.from).toBe('now-1h');
});

it('When weekStart i set should call on activation', () => {
const timeRange = new SceneTimeRange({ from: 'now-1h', to: 'now', weekStart: 'saturday' });
const deactivate = timeRange.activate();
expect(setWeekStart).toHaveBeenCalledWith('saturday');

deactivate();
expect(setWeekStart).toHaveBeenCalledWith('monday');
});

it('when time range refreshed should evaluate and update value', async () => {
const timeRange = new SceneTimeRange({ from: 'now-30s', to: 'now' });
const startTime = timeRange.state.value.from.valueOf();
Expand Down
14 changes: 12 additions & 2 deletions packages/scenes/src/core/SceneTimeRange.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getTimeZone, rangeUtil, TimeRange, toUtc } from '@grafana/data';
import { getTimeZone, rangeUtil, setWeekStart, TimeRange, toUtc } from '@grafana/data';
import { TimeZone } from '@grafana/schema';

import { SceneObjectUrlSyncConfig } from '../services/SceneObjectUrlSyncConfig';
Expand All @@ -8,7 +8,7 @@ import { SceneTimeRangeLike, SceneTimeRangeState, SceneObjectUrlValues } from '.
import { getClosest } from './sceneGraph/utils';
import { parseUrlParam } from '../utils/parseUrlParam';
import { evaluateTimeRange } from '../utils/evaluateTimeRange';
import { locationService, RefreshEvent } from '@grafana/runtime';
import { config, locationService, RefreshEvent } from '@grafana/runtime';
import { isValid } from '../utils/date';

export class SceneTimeRange extends SceneObjectBase<SceneTimeRangeState> implements SceneTimeRangeLike {
Expand Down Expand Up @@ -55,9 +55,19 @@ export class SceneTimeRange extends SceneObjectBase<SceneTimeRangeState> impleme
}
}

if (this.state.weekStart) {
setWeekStart(this.state.weekStart);
}

if (rangeUtil.isRelativeTimeRange(this.state.value.raw)) {
this.refreshIfStale();
}

return () => {
if (this.state.weekStart) {
setWeekStart(config.bootData.user.weekStart);
}
};
}

private refreshIfStale() {
Expand Down

0 comments on commit 1edea24

Please sign in to comment.