mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
TimeRange: Fixes updating time range from url and browser history (#48657)
This commit is contained in:
parent
f45dc224d9
commit
b71aa912c6
@ -169,14 +169,9 @@ export class TimeSrv {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let paramsJSON: Record<string, string> = {};
|
|
||||||
params.forEach(function (value, key) {
|
|
||||||
paramsJSON[key] = value;
|
|
||||||
});
|
|
||||||
|
|
||||||
// but if refresh explicitly set then use that
|
// but if refresh explicitly set then use that
|
||||||
this.refresh = getRefreshFromUrl({
|
this.refresh = getRefreshFromUrl({
|
||||||
params: paramsJSON,
|
urlRefresh: params.get('refresh'),
|
||||||
currentRefresh: this.refresh,
|
currentRefresh: this.refresh,
|
||||||
refreshIntervals: Array.isArray(this.timeModel?.timepicker?.refresh_intervals)
|
refreshIntervals: Array.isArray(this.timeModel?.timepicker?.refresh_intervals)
|
||||||
? this.timeModel?.timepicker?.refresh_intervals
|
? this.timeModel?.timepicker?.refresh_intervals
|
||||||
@ -203,7 +198,7 @@ export class TimeSrv {
|
|||||||
if (from !== urlRange.from || to !== urlRange.to) {
|
if (from !== urlRange.from || to !== urlRange.to) {
|
||||||
// issue update
|
// issue update
|
||||||
this.initTimeFromUrl();
|
this.initTimeFromUrl();
|
||||||
this.setTime(this.time, true);
|
this.setTime(this.time, false);
|
||||||
}
|
}
|
||||||
} else if (this.timeHasChangedSinceLoad()) {
|
} else if (this.timeHasChangedSinceLoad()) {
|
||||||
this.setTime(this.timeAtLoad, true);
|
this.setTime(this.timeAtLoad, true);
|
||||||
|
@ -3,13 +3,12 @@ import { getRefreshFromUrl } from './getRefreshFromUrl';
|
|||||||
describe('getRefreshFromUrl', () => {
|
describe('getRefreshFromUrl', () => {
|
||||||
describe('when refresh is not part of params', () => {
|
describe('when refresh is not part of params', () => {
|
||||||
it('then it should return current refresh value', () => {
|
it('then it should return current refresh value', () => {
|
||||||
const params = {};
|
|
||||||
const currentRefresh = false;
|
const currentRefresh = false;
|
||||||
const minRefreshInterval = '5s';
|
const minRefreshInterval = '5s';
|
||||||
const isAllowedIntervalFn = () => false;
|
const isAllowedIntervalFn = () => false;
|
||||||
|
|
||||||
const actual = getRefreshFromUrl({
|
const actual = getRefreshFromUrl({
|
||||||
params,
|
urlRefresh: null,
|
||||||
currentRefresh,
|
currentRefresh,
|
||||||
minRefreshInterval,
|
minRefreshInterval,
|
||||||
isAllowedIntervalFn,
|
isAllowedIntervalFn,
|
||||||
@ -22,14 +21,13 @@ describe('getRefreshFromUrl', () => {
|
|||||||
describe('when refresh is part of params', () => {
|
describe('when refresh is part of params', () => {
|
||||||
describe('and refresh is an existing and valid interval', () => {
|
describe('and refresh is an existing and valid interval', () => {
|
||||||
it('then it should return the refresh value', () => {
|
it('then it should return the refresh value', () => {
|
||||||
const params = { refresh: '10s' };
|
|
||||||
const currentRefresh = '';
|
const currentRefresh = '';
|
||||||
const minRefreshInterval = '5s';
|
const minRefreshInterval = '5s';
|
||||||
const isAllowedIntervalFn = () => true;
|
const isAllowedIntervalFn = () => true;
|
||||||
const refreshIntervals = ['5s', '10s', '30s'];
|
const refreshIntervals = ['5s', '10s', '30s'];
|
||||||
|
|
||||||
const actual = getRefreshFromUrl({
|
const actual = getRefreshFromUrl({
|
||||||
params,
|
urlRefresh: '10s',
|
||||||
currentRefresh,
|
currentRefresh,
|
||||||
minRefreshInterval,
|
minRefreshInterval,
|
||||||
isAllowedIntervalFn,
|
isAllowedIntervalFn,
|
||||||
@ -61,7 +59,7 @@ describe('getRefreshFromUrl', () => {
|
|||||||
'when called with refresh:{$refresh}, isAllowedInterval:{$isAllowedInterval}, minRefreshInterval:{$minRefreshInterval}, refreshIntervals:{$refreshIntervals} then it should return: $expected',
|
'when called with refresh:{$refresh}, isAllowedInterval:{$isAllowedInterval}, minRefreshInterval:{$minRefreshInterval}, refreshIntervals:{$refreshIntervals} then it should return: $expected',
|
||||||
({ refresh, isAllowedInterval, minRefreshInterval, refreshIntervals, expected }) => {
|
({ refresh, isAllowedInterval, minRefreshInterval, refreshIntervals, expected }) => {
|
||||||
const actual = getRefreshFromUrl({
|
const actual = getRefreshFromUrl({
|
||||||
params: { refresh },
|
urlRefresh: refresh,
|
||||||
currentRefresh: 'currentRefresh',
|
currentRefresh: 'currentRefresh',
|
||||||
minRefreshInterval,
|
minRefreshInterval,
|
||||||
isAllowedIntervalFn: () => isAllowedInterval,
|
isAllowedIntervalFn: () => isAllowedInterval,
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
import { defaultIntervals } from '@grafana/ui';
|
import { defaultIntervals } from '@grafana/ui';
|
||||||
|
|
||||||
interface Args {
|
interface Args {
|
||||||
params: Record<string, string>;
|
urlRefresh: string | null;
|
||||||
currentRefresh: string | boolean | undefined;
|
currentRefresh: string | boolean | undefined;
|
||||||
isAllowedIntervalFn: (interval: string) => boolean;
|
isAllowedIntervalFn: (interval: string) => boolean;
|
||||||
minRefreshInterval: string;
|
minRefreshInterval: string;
|
||||||
@ -13,18 +13,18 @@ interface Args {
|
|||||||
// try to find the first refresh interval that matches the minRefreshInterval (min_refresh_interval in ini)
|
// try to find the first refresh interval that matches the minRefreshInterval (min_refresh_interval in ini)
|
||||||
// or just take the first interval.
|
// or just take the first interval.
|
||||||
export function getRefreshFromUrl({
|
export function getRefreshFromUrl({
|
||||||
params,
|
urlRefresh,
|
||||||
currentRefresh,
|
currentRefresh,
|
||||||
isAllowedIntervalFn,
|
isAllowedIntervalFn,
|
||||||
minRefreshInterval,
|
minRefreshInterval,
|
||||||
refreshIntervals = defaultIntervals,
|
refreshIntervals = defaultIntervals,
|
||||||
}: Args): string | boolean | undefined {
|
}: Args): string | boolean | undefined {
|
||||||
if (!params.refresh) {
|
if (!urlRefresh) {
|
||||||
return currentRefresh;
|
return currentRefresh;
|
||||||
}
|
}
|
||||||
|
|
||||||
const isAllowedInterval = isAllowedIntervalFn(params.refresh);
|
const isAllowedInterval = isAllowedIntervalFn(urlRefresh);
|
||||||
const isExistingInterval = refreshIntervals.find((interval) => interval === params.refresh);
|
const isExistingInterval = refreshIntervals.find((interval) => interval === urlRefresh);
|
||||||
|
|
||||||
if (!isAllowedInterval || !isExistingInterval) {
|
if (!isAllowedInterval || !isExistingInterval) {
|
||||||
const minRefreshIntervalInIntervals = minRefreshInterval
|
const minRefreshIntervalInIntervals = minRefreshInterval
|
||||||
@ -35,5 +35,5 @@ export function getRefreshFromUrl({
|
|||||||
return minRefreshIntervalInIntervals ?? lowestRefreshInterval ?? currentRefresh;
|
return minRefreshIntervalInIntervals ?? lowestRefreshInterval ?? currentRefresh;
|
||||||
}
|
}
|
||||||
|
|
||||||
return params.refresh || currentRefresh;
|
return urlRefresh || currentRefresh;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user