Commit 9c55500c by Andrej Ocenas Committed by GitHub

Elastic: Replace range as number not string (#22173)

parent e60b1240
import angular from 'angular'; import angular from 'angular';
import { dateMath, Field } from '@grafana/data'; import { CoreApp, DataQueryRequest, dateMath, Field } from '@grafana/data';
import _ from 'lodash'; import _ from 'lodash';
import { ElasticDatasource } from './datasource'; import { ElasticDatasource } from './datasource';
import { toUtc, dateTime } from '@grafana/data'; import { toUtc, dateTime } from '@grafana/data';
...@@ -7,7 +7,7 @@ import { backendSrv } from 'app/core/services/backend_srv'; // will use the vers ...@@ -7,7 +7,7 @@ import { backendSrv } from 'app/core/services/backend_srv'; // will use the vers
import { TimeSrv } from 'app/features/dashboard/services/TimeSrv'; import { TimeSrv } from 'app/features/dashboard/services/TimeSrv';
import { TemplateSrv } from 'app/features/templating/template_srv'; import { TemplateSrv } from 'app/features/templating/template_srv';
import { DataSourceInstanceSettings } from '@grafana/data'; import { DataSourceInstanceSettings } from '@grafana/data';
import { ElasticsearchOptions } from './types'; import { ElasticsearchOptions, ElasticsearchQuery } from './types';
jest.mock('@grafana/runtime', () => ({ jest.mock('@grafana/runtime', () => ({
...jest.requireActual('@grafana/runtime'), ...jest.requireActual('@grafana/runtime'),
...@@ -613,8 +613,57 @@ describe('ElasticDatasource', function(this: any) { ...@@ -613,8 +613,57 @@ describe('ElasticDatasource', function(this: any) {
expect(body['aggs']['1']['terms'].size).not.toBe(0); expect(body['aggs']['1']['terms'].size).not.toBe(0);
}); });
}); });
describe('query', () => {
it('should replace range as integer not string', () => {
const dataSource = new ElasticDatasource(
{
url: 'http://es.com',
database: '[asd-]YYYY.MM.DD',
jsonData: {
interval: 'Daily',
esVersion: 2,
timeField: '@time',
},
} as DataSourceInstanceSettings<ElasticsearchOptions>,
templateSrv as TemplateSrv,
timeSrv as TimeSrv
);
(dataSource as any).post = jest.fn(() => Promise.resolve({ responses: [] }));
dataSource.query(createElasticQuery());
const query = ((dataSource as any).post as jest.Mock).mock.calls[0][1];
expect(typeof JSON.parse(query.split('\n')[1]).query.bool.filter[0].range['@time'].gte).toBe('number');
});
});
}); });
const createElasticQuery = (): DataQueryRequest<ElasticsearchQuery> => {
return {
requestId: '',
dashboardId: 0,
interval: '',
panelId: 0,
scopedVars: {},
timezone: '',
app: CoreApp.Dashboard,
startTime: 0,
range: {
from: dateTime([2015, 4, 30, 10]),
to: dateTime([2015, 5, 1, 10]),
} as any,
targets: [
{
refId: '',
isLogsQuery: false,
bucketAggs: [{ type: 'date_histogram', field: '@timestamp', id: '2' }],
metrics: [{ type: 'count', id: '' }],
query: 'test',
},
],
};
};
const logsResponse = { const logsResponse = {
data: { data: {
responses: [ responses: [
......
...@@ -368,8 +368,12 @@ export class ElasticDatasource extends DataSourceApi<ElasticsearchQuery, Elastic ...@@ -368,8 +368,12 @@ export class ElasticDatasource extends DataSourceApi<ElasticsearchQuery, Elastic
return Promise.resolve({ data: [] }); return Promise.resolve({ data: [] });
} }
payload = payload.replace(/\$timeFrom/g, options.range.from.valueOf().toString()); // We replace the range here for actual values. We need to replace it together with enclosing "" so that we replace
payload = payload.replace(/\$timeTo/g, options.range.to.valueOf().toString()); // it as an integer not as string with digits. This is because elastic will convert the string only if the time
// field is specified as type date (which probably should) but can also be specified as integer (millisecond epoch)
// and then sending string will error out.
payload = payload.replace(/"\$timeFrom"/g, options.range.from.valueOf().toString());
payload = payload.replace(/"\$timeTo"/g, options.range.to.valueOf().toString());
payload = this.templateSrv.replace(payload, options.scopedVars); payload = this.templateSrv.replace(payload, options.scopedVars);
const url = this.getMultiSearchUrl(); const url = this.getMultiSearchUrl();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment