Skip to content

Commit

Permalink
fix mutating of query start time for multiple queries
Browse files Browse the repository at this point in the history
  • Loading branch information
MGJamJam committed Jul 24, 2024
1 parent 4cedeb6 commit d7c4e40
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## Development

### Fix
- different query start times with multiple queries due to mutating of grafana start time

## 1.1.1

### Added
Expand Down
10 changes: 6 additions & 4 deletions src/datasource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -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);
}
}

Expand Down

0 comments on commit d7c4e40

Please sign in to comment.