From 6ae373b8c7150aa661457b0df9935e3bc8af3eea Mon Sep 17 00:00:00 2001 From: Sven Grossmann Date: Mon, 20 Mar 2023 10:04:41 +0100 Subject: [PATCH] QueryStats: Fix comparison of timeranges (#65025) * fix timerange comparison * remove import --- .../datasource/loki/components/stats.test.ts | 16 +++++++++------- .../plugins/datasource/loki/components/stats.ts | 6 +----- 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/public/app/plugins/datasource/loki/components/stats.test.ts b/public/app/plugins/datasource/loki/components/stats.test.ts index f631e611877..a72efc7dc8f 100644 --- a/public/app/plugins/datasource/loki/components/stats.test.ts +++ b/public/app/plugins/datasource/loki/components/stats.test.ts @@ -1,4 +1,4 @@ -import { TimeRange } from '@grafana/data'; +import { dateTime, getDefaultTimeRange } from '@grafana/data'; import { createLokiDatasource } from '../mocks'; @@ -8,23 +8,25 @@ describe('shouldUpdateStats', () => { it('should return true if the query has changed', () => { const query = '{job="grafana"}'; const prevQuery = '{job="not-grafana"}'; - const timerange = { raw: { from: 'now-1h', to: 'now' } } as TimeRange; - const prevTimerange = { raw: { from: 'now-1h', to: 'now' } } as TimeRange; + const timerange = getDefaultTimeRange(); + const prevTimerange = timerange; expect(shouldUpdateStats(query, prevQuery, timerange, prevTimerange)).toBe(true); }); + it('should return true if the timerange has changed', () => { const query = '{job="grafana"}'; const prevQuery = '{job="grafana"}'; - const timerange = { raw: { from: 'now-1h', to: 'now' } } as TimeRange; - const prevTimerange = { raw: { from: 'now-2h', to: 'now' } } as TimeRange; + const timerange = getDefaultTimeRange(); + timerange.from = dateTime(Date.now() - 1000000); + const prevTimerange = getDefaultTimeRange(); expect(shouldUpdateStats(query, prevQuery, timerange, prevTimerange)).toBe(true); }); it('should return false if the query and timerange have not changed', () => { const query = '{job="grafana"}'; const prevQuery = '{job="grafana"}'; - const timerange = { raw: { from: 'now-1h', to: 'now' } } as TimeRange; - const prevTimerange = { raw: { from: 'now-1h', to: 'now' } } as TimeRange; + const timerange = getDefaultTimeRange(); + const prevTimerange = timerange; expect(shouldUpdateStats(query, prevQuery, timerange, prevTimerange)).toBe(false); }); }); diff --git a/public/app/plugins/datasource/loki/components/stats.ts b/public/app/plugins/datasource/loki/components/stats.ts index 24088d33f60..64ad8833731 100644 --- a/public/app/plugins/datasource/loki/components/stats.ts +++ b/public/app/plugins/datasource/loki/components/stats.ts @@ -18,11 +18,7 @@ export function shouldUpdateStats( timerange: TimeRange, prevTimerange: TimeRange | undefined ): boolean { - if ( - query === prevQuery && - timerange.raw.from === prevTimerange?.raw.from && - timerange.raw.to === prevTimerange?.raw.to - ) { + if (query === prevQuery && timerange.from.isSame(prevTimerange?.from) && timerange.to.isSame(prevTimerange?.to)) { return false; }