From d7c4e407dd1c163af5899cc33eb630fd3e025af3 Mon Sep 17 00:00:00 2001 From: MGJamJam Date: Wed, 24 Jul 2024 16:26:46 -0300 Subject: [PATCH] fix mutating of query start time for multiple queries --- CHANGELOG.md | 3 +++ src/datasource.ts | 10 ++++++---- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4486992..6a3b1ac 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,9 @@ ## Development +### Fix +- different query start times with multiple queries due to mutating of grafana start time + ## 1.1.1 ### Added diff --git a/src/datasource.ts b/src/datasource.ts index 6cc15d3..3082e55 100644 --- a/src/datasource.ts +++ b/src/datasource.ts @@ -11,6 +11,8 @@ import { } from '@grafana/data'; import { getBackendSrv } from '@grafana/runtime'; import { filter, isEmpty } from 'lodash'; +// eslint-disable-next-line no-restricted-imports +import moment from 'moment'; import { catchError, lastValueFrom, map, Observable, of } from 'rxjs'; import { @@ -101,7 +103,9 @@ export class DataSource extends DataSourceApi< ? calculateQueryInterval(target.interval, range!.from.valueOf(), range!.to.valueOf()) : undefined; - let queryFrom = range!.from; + // create new moment object to not mutate the original grafana object with startOf() to not change + // the grafana graph as this would change the timeframe for all the following queries + let queryFrom = moment(range!.from.valueOf()); const queryTo = range!.to; // floor the query start time to improve cache hitting @@ -113,9 +117,7 @@ export class DataSource extends DataSourceApi< } const momentTimeUnit = getMomentTimeUnitForQueryInterval(flooringInterval); if (momentTimeUnit != null) { - // range from is a moment and startOf is mutating moment object so this has a side effect to also change the - // grafana selected timeframe value which will adapt the grafana graph as well - queryFrom = range!.from.startOf(momentTimeUnit); + queryFrom.startOf(momentTimeUnit); } }