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

fix mutating of query start time for multiple queries #96

Merged
merged 5 commits into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Development

### Fix

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

## 1.1.1

### Added
Expand Down
18 changes: 13 additions & 5 deletions src/datasource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
} 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 @@ -55,7 +57,7 @@
};

export class DataSource extends DataSourceApi<
BitmovinAnalyticsDataQuery | OldBitmovinAnalyticsDataQuery,

Check warning on line 60 in src/datasource.ts

View workflow job for this annotation

GitHub Actions / build

'OldBitmovinAnalyticsDataQuery' is deprecated. These are the options query options of the old Angular based plugin
BitmovinDataSourceOptions
> {
baseUrl: string;
Expand Down Expand Up @@ -88,6 +90,7 @@
async query(options: DataQueryRequest<BitmovinAnalyticsDataQuery>): Promise<DataQueryResponse> {
const { range } = options;
const isRelativeRangeFrom = this.isRelativeRangeFrom(range.raw);
let momentTimeUnit = undefined;

//filter disabled queries
const enabledQueries = (options.targets = filter(options.targets, (t) => !t.hide));
Expand All @@ -101,7 +104,9 @@
? 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 at this point 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 @@ -111,11 +116,9 @@
// to allow higher granularity if interval is selected by user
flooringInterval = getSmallerInterval(interval, flooringInterval);
}
const momentTimeUnit = getMomentTimeUnitForQueryInterval(flooringInterval);
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 Expand Up @@ -203,6 +206,11 @@
});
});

// round down grafana start time to adjust the grafana graph and show first data point
if (momentTimeUnit != null) {
range.from.startOf(momentTimeUnit);
}

return Promise.all(promises).then((data) => ({ data }));
}

Expand Down
Loading