Skip to content

Commit

Permalink
Bugfix: interpolate interval on the backend (#327)
Browse files Browse the repository at this point in the history
  • Loading branch information
iwysiu authored Nov 14, 2024
1 parent 2ddda28 commit 4ef9904
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 9 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@

All notable changes to this project will be documented in this file.

## 2.9.11

- Bugfix: interpolate interval on the backend [#327](https://github.com/grafana/timestream-datasource/pull/327)

## 2.9.10

- Bugfix: Account for template variable being a number
- Chore: update dependabot config (#317)
- Dependency updates:
- Dependency updates:
- github.com/grafana/grafana-plugin-sdk-go from 0.251.0 to 0.258.0 in [#314](https://github.com/grafana/timestream-datasource/pull/314),[#315](https://github.com/grafana/timestream-datasource/pull/315), [#319](https://github.com/grafana/timestream-datasource/pull/319)
- github.com/aws/aws-sdk-go from 1.51.31 to 1.55.5 in [#319](https://github.com/grafana/timestream-datasource/pull/319)
- github.com/grafana/grafana-aws-sdk from 0.31.2 to 0.31.4 in [#319](https://github.com/grafana/timestream-datasource/pull/319)
Expand All @@ -32,6 +36,7 @@ All notable changes to this project will be documented in this file.
- Bump fast-loops from 1.1.3 to 1.1.4 in [#298](https://github.com/grafana/timestream-datasource/pull/298)

## 2.9.7

- feat: add errorsource [#296](https://github.com/grafana/timestream-datasource/pull/296)
- chore: refactor macros to avoid macro-length bug in [#295](https://github.com/grafana/timestream-datasource/pull/295)

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "grafana-timestream-datasource",
"version": "2.9.10",
"version": "2.9.11",
"description": "Load data timestream in grafana",
"scripts": {
"build": "webpack -c ./.config/webpack/webpack.config.ts --env production",
Expand Down
27 changes: 20 additions & 7 deletions src/DataSource.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ describe('DataSource', () => {
__interval: { value: 50000 },
};
// simplified version of getTemplateSrv().replace
const replaceMock = jest.fn().mockImplementation(
(target?: string, scopedVars?: ScopedVars, format?: string | Function) => {
const replaceMock = jest
.fn()
.mockImplementation((target?: string, scopedVars?: ScopedVars, format?: string | Function) => {
let res = target ?? '';
if (scopedVars && typeof format === 'function') {
Object.keys(scopedVars).forEach((v) => (res = res.replace(v, format(scopedVars[v]?.value))));
}
return res;
}
);
});
beforeEach(() => {
jest.spyOn(runtime, 'getTemplateSrv').mockImplementation(() => ({
getVariables: jest.fn(),
Expand All @@ -46,7 +46,8 @@ describe('DataSource', () => {
expect(res.rawQuery).toEqual(`select * from foo where var in ('foo','bar')`);
});

it('should return number variables', () => {
it('should replace __interval interpolated variables with their original string', () => {
replaceMock.mockClear();
mockDatasource.applyTemplateVariables(
{ ...mockQuery, rawQuery: 'select $__interval_ms, $__interval' },
{
Expand All @@ -55,8 +56,20 @@ describe('DataSource', () => {
}
);
// check rawQuery.replace is called with correct interval value
expect(replaceMock.mock.calls[3][1].__interval).toEqual({ value: 50000 });
expect(replaceMock.mock.calls[3][1].__interval_ms).toEqual({ value: 5000000 });
expect(replaceMock.mock.calls[3][1].__interval).toEqual({ value: '$__interval' });
expect(replaceMock.mock.calls[3][1].__interval_ms).toEqual({ value: '$__interval_ms' });
});

it('should return number variables', () => {
replaceMock.mockClear();
mockDatasource.applyTemplateVariables(
{ ...mockQuery, rawQuery: 'select $__from' },
{
__from: { value: 3000 },
}
);
// check rawQuery.replace is called with correct interval value
expect(replaceMock.mock.calls[3][1].__from).toEqual({ value: 3000 });
});
});
});
10 changes: 10 additions & 0 deletions src/DataSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,16 @@ export class DataSource extends DataSourceWithBackend<TimestreamQuery, Timestrea
const variables = { ...scopedVars };

const templateSrv = getTemplateSrv();

// We want to interpolate these variables on backend.
// The pre-calculated values are replaced with the variable strings.
variables.__interval = {
value: '$__interval',
};
variables.__interval_ms = {
value: '$__interval_ms',
};

return {
...query,
database: templateSrv.replace(query.database || '', scopedVars),
Expand Down

0 comments on commit 4ef9904

Please sign in to comment.