Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SceneTimeRange: Fixes weekstart issue when evaluting data math #1002

Merged
merged 1 commit into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading