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 1 commit
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
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 @@
} 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 @@ -101,7 +103,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 as this would change the timeframe for all the following queries
let queryFrom = moment(range!.from.valueOf());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wasn't there a reason, why we wanted to mutate original range.from ? I remember, we discuss that for half hour+ in one of techsyncs ... otherwise we would use local let queryFrom from beginning ....

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the reason was to adjust the timeframe of the graph through modifying the range.from with the startOf() function to be able to display the first datapoint, otherwise it would still be there but outside the graph's boundaries.

I added the range.from() modification again, but only after all the queries were fetched and data was transformed. This way we send the starttimes for all the queries correctly but also adapt the timeframe of the graph to display the first datapoint.

const queryTo = range!.to;

// floor the query start time to improve cache hitting
Expand All @@ -113,9 +117,7 @@
}
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
Loading