2019-12-05 17:04:03 +08:00
|
|
|
import angular from 'angular';
|
2020-02-13 23:00:01 +08:00
|
|
|
import { CoreApp, DataQueryRequest, dateMath, Field } from '@grafana/data';
|
2017-12-20 19:33:33 +08:00
|
|
|
import _ from 'lodash';
|
2019-12-12 00:40:56 +08:00
|
|
|
import { ElasticDatasource } from './datasource';
|
2019-07-06 14:05:53 +08:00
|
|
|
import { toUtc, dateTime } from '@grafana/data';
|
2020-01-21 17:08:07 +08:00
|
|
|
import { backendSrv } from 'app/core/services/backend_srv'; // will use the version in __mocks__
|
2019-06-25 04:15:03 +08:00
|
|
|
import { TimeSrv } from 'app/features/dashboard/services/TimeSrv';
|
|
|
|
|
import { TemplateSrv } from 'app/features/templating/template_srv';
|
2019-10-31 17:48:05 +08:00
|
|
|
import { DataSourceInstanceSettings } from '@grafana/data';
|
2020-02-13 23:00:01 +08:00
|
|
|
import { ElasticsearchOptions, ElasticsearchQuery } from './types';
|
2018-07-04 16:43:36 +08:00
|
|
|
|
2020-01-21 17:08:07 +08:00
|
|
|
jest.mock('@grafana/runtime', () => ({
|
|
|
|
|
...jest.requireActual('@grafana/runtime'),
|
|
|
|
|
getBackendSrv: () => backendSrv,
|
|
|
|
|
}));
|
|
|
|
|
|
2018-08-30 16:49:18 +08:00
|
|
|
describe('ElasticDatasource', function(this: any) {
|
2020-01-21 17:08:07 +08:00
|
|
|
const datasourceRequestMock = jest.spyOn(backendSrv, 'datasourceRequest');
|
|
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
|
jest.clearAllMocks();
|
|
|
|
|
});
|
2018-07-03 16:12:07 +08:00
|
|
|
|
2019-10-14 16:27:47 +08:00
|
|
|
const $rootScope = {
|
2018-07-03 16:12:07 +08:00
|
|
|
$on: jest.fn(),
|
|
|
|
|
appEvent: jest.fn(),
|
|
|
|
|
};
|
|
|
|
|
|
2019-06-25 04:15:03 +08:00
|
|
|
const templateSrv: any = {
|
2019-01-09 14:46:20 +08:00
|
|
|
replace: jest.fn(text => {
|
2019-02-13 18:14:53 +08:00
|
|
|
if (text.startsWith('$')) {
|
2019-01-09 14:46:20 +08:00
|
|
|
return `resolvedVariable`;
|
|
|
|
|
} else {
|
|
|
|
|
return text;
|
|
|
|
|
}
|
|
|
|
|
}),
|
2018-07-04 16:43:36 +08:00
|
|
|
getAdhocFilters: jest.fn(() => []),
|
|
|
|
|
};
|
2018-07-03 16:47:50 +08:00
|
|
|
|
2019-06-25 04:15:03 +08:00
|
|
|
const timeSrv: any = {
|
2018-07-04 16:43:36 +08:00
|
|
|
time: { from: 'now-1h', to: 'now' },
|
2018-07-04 17:23:12 +08:00
|
|
|
timeRange: jest.fn(() => {
|
2018-07-04 16:43:36 +08:00
|
|
|
return {
|
2019-06-25 04:15:03 +08:00
|
|
|
from: dateMath.parse(timeSrv.time.from, false),
|
|
|
|
|
to: dateMath.parse(timeSrv.time.to, true),
|
2018-07-04 16:43:36 +08:00
|
|
|
};
|
|
|
|
|
}),
|
|
|
|
|
setTime: jest.fn(time => {
|
|
|
|
|
this.time = time;
|
|
|
|
|
}),
|
|
|
|
|
};
|
2018-07-03 16:12:07 +08:00
|
|
|
|
2018-09-03 17:30:44 +08:00
|
|
|
const ctx = {
|
2018-07-03 16:12:07 +08:00
|
|
|
$rootScope,
|
2018-09-03 17:30:44 +08:00
|
|
|
} as any;
|
2016-01-09 20:21:16 +08:00
|
|
|
|
2019-06-25 04:15:03 +08:00
|
|
|
function createDatasource(instanceSettings: DataSourceInstanceSettings<ElasticsearchOptions>) {
|
|
|
|
|
instanceSettings.jsonData = instanceSettings.jsonData || ({} as ElasticsearchOptions);
|
2020-01-21 17:08:07 +08:00
|
|
|
ctx.ds = new ElasticDatasource(instanceSettings, templateSrv as TemplateSrv, timeSrv as TimeSrv);
|
2016-01-09 20:21:16 +08:00
|
|
|
}
|
2015-09-28 22:28:19 +08:00
|
|
|
|
2018-09-04 21:55:41 +08:00
|
|
|
describe('When testing datasource with index pattern', () => {
|
|
|
|
|
beforeEach(() => {
|
2017-12-19 23:06:54 +08:00
|
|
|
createDatasource({
|
2017-12-20 19:33:33 +08:00
|
|
|
url: 'http://es.com',
|
2019-06-25 04:15:03 +08:00
|
|
|
database: '[asd-]YYYY.MM.DD',
|
|
|
|
|
jsonData: { interval: 'Daily', esVersion: 2 } as ElasticsearchOptions,
|
|
|
|
|
} as DataSourceInstanceSettings<ElasticsearchOptions>);
|
2015-09-28 22:28:19 +08:00
|
|
|
});
|
|
|
|
|
|
2018-09-04 21:55:41 +08:00
|
|
|
it('should translate index pattern to current day', () => {
|
2019-07-11 23:05:45 +08:00
|
|
|
let requestOptions: any;
|
2020-01-21 17:08:07 +08:00
|
|
|
datasourceRequestMock.mockImplementation(options => {
|
2015-09-28 22:28:19 +08:00
|
|
|
requestOptions = options;
|
2018-07-03 16:12:07 +08:00
|
|
|
return Promise.resolve({ data: {} });
|
|
|
|
|
});
|
2015-09-28 22:28:19 +08:00
|
|
|
|
|
|
|
|
ctx.ds.testDatasource();
|
|
|
|
|
|
2019-05-08 19:51:44 +08:00
|
|
|
const today = toUtc().format('YYYY.MM.DD');
|
2018-07-03 16:47:50 +08:00
|
|
|
expect(requestOptions.url).toBe('http://es.com/asd-' + today + '/_mapping');
|
2015-09-28 22:28:19 +08:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2018-09-04 21:55:41 +08:00
|
|
|
describe('When issuing metric query with interval pattern', () => {
|
2019-07-11 23:05:45 +08:00
|
|
|
let requestOptions: any, parts: any, header: any, query: any, result: any;
|
2015-10-23 04:23:21 +08:00
|
|
|
|
2019-03-26 23:15:23 +08:00
|
|
|
beforeEach(async () => {
|
2017-12-19 23:06:54 +08:00
|
|
|
createDatasource({
|
2017-12-20 19:33:33 +08:00
|
|
|
url: 'http://es.com',
|
2019-06-25 04:15:03 +08:00
|
|
|
database: '[asd-]YYYY.MM.DD',
|
|
|
|
|
jsonData: { interval: 'Daily', esVersion: 2 } as ElasticsearchOptions,
|
|
|
|
|
} as DataSourceInstanceSettings<ElasticsearchOptions>);
|
2015-09-28 22:28:19 +08:00
|
|
|
|
2020-01-21 17:08:07 +08:00
|
|
|
datasourceRequestMock.mockImplementation(options => {
|
2015-09-28 22:28:19 +08:00
|
|
|
requestOptions = options;
|
2019-03-26 23:15:23 +08:00
|
|
|
return Promise.resolve({
|
|
|
|
|
data: {
|
|
|
|
|
responses: [
|
|
|
|
|
{
|
|
|
|
|
aggregations: {
|
|
|
|
|
'1': {
|
|
|
|
|
buckets: [
|
|
|
|
|
{
|
|
|
|
|
doc_count: 10,
|
|
|
|
|
key: 1000,
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
});
|
2018-07-03 16:12:07 +08:00
|
|
|
});
|
2015-09-28 22:28:19 +08:00
|
|
|
|
2019-01-09 14:46:20 +08:00
|
|
|
query = {
|
2015-09-28 22:28:19 +08:00
|
|
|
range: {
|
2019-05-08 19:51:44 +08:00
|
|
|
from: toUtc([2015, 4, 30, 10]),
|
|
|
|
|
to: toUtc([2015, 5, 1, 10]),
|
2015-09-28 22:28:19 +08:00
|
|
|
},
|
2017-12-19 23:06:54 +08:00
|
|
|
targets: [
|
|
|
|
|
{
|
2019-02-13 18:14:53 +08:00
|
|
|
alias: '$varAlias',
|
2019-03-26 23:15:23 +08:00
|
|
|
bucketAggs: [{ type: 'date_histogram', field: '@timestamp', id: '1' }],
|
|
|
|
|
metrics: [{ type: 'count', id: '1' }],
|
2017-12-20 19:33:33 +08:00
|
|
|
query: 'escape\\:test',
|
|
|
|
|
},
|
|
|
|
|
],
|
2019-01-09 14:46:20 +08:00
|
|
|
};
|
|
|
|
|
|
2019-03-26 23:15:23 +08:00
|
|
|
result = await ctx.ds.query(query);
|
2015-09-28 22:28:19 +08:00
|
|
|
|
2017-12-20 19:33:33 +08:00
|
|
|
parts = requestOptions.data.split('\n');
|
2015-10-23 04:23:21 +08:00
|
|
|
header = angular.fromJson(parts[0]);
|
|
|
|
|
});
|
|
|
|
|
|
2018-09-04 21:55:41 +08:00
|
|
|
it('should translate index pattern to current day', () => {
|
2018-07-03 16:12:07 +08:00
|
|
|
expect(header.index).toEqual(['asd-2015.05.30', 'asd-2015.05.31', 'asd-2015.06.01']);
|
2015-09-28 22:28:19 +08:00
|
|
|
});
|
2015-10-23 04:23:21 +08:00
|
|
|
|
2019-03-26 23:15:23 +08:00
|
|
|
it('should not resolve the variable in the original alias field in the query', () => {
|
|
|
|
|
expect(query.targets[0].alias).toEqual('$varAlias');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should resolve the alias variable for the alias/target in the result', () => {
|
|
|
|
|
expect(result.data[0].target).toEqual('resolvedVariable');
|
2019-01-09 14:46:20 +08:00
|
|
|
});
|
|
|
|
|
|
2018-09-04 21:55:41 +08:00
|
|
|
it('should json escape lucene query', () => {
|
2018-08-27 00:43:07 +08:00
|
|
|
const body = angular.fromJson(parts[1]);
|
2018-07-03 16:12:07 +08:00
|
|
|
expect(body.query.bool.filter[1].query_string.query).toBe('escape\\:test');
|
2015-10-23 04:23:21 +08:00
|
|
|
});
|
2015-09-28 22:28:19 +08:00
|
|
|
});
|
2015-11-05 15:36:51 +08:00
|
|
|
|
2019-06-25 04:15:03 +08:00
|
|
|
describe('When issuing logs query with interval pattern', () => {
|
2019-12-12 00:40:56 +08:00
|
|
|
async function setupDataSource(jsonData?: Partial<ElasticsearchOptions>) {
|
2019-06-25 04:15:03 +08:00
|
|
|
createDatasource({
|
|
|
|
|
url: 'http://es.com',
|
|
|
|
|
database: 'mock-index',
|
2019-12-12 00:40:56 +08:00
|
|
|
jsonData: {
|
|
|
|
|
interval: 'Daily',
|
|
|
|
|
esVersion: 2,
|
|
|
|
|
timeField: '@timestamp',
|
|
|
|
|
...(jsonData || {}),
|
|
|
|
|
} as ElasticsearchOptions,
|
2019-06-25 04:15:03 +08:00
|
|
|
} as DataSourceInstanceSettings<ElasticsearchOptions>);
|
|
|
|
|
|
2020-01-21 17:08:07 +08:00
|
|
|
datasourceRequestMock.mockImplementation(options => {
|
2019-12-12 00:40:56 +08:00
|
|
|
return Promise.resolve(logsResponse);
|
2019-06-25 04:15:03 +08:00
|
|
|
});
|
|
|
|
|
|
2019-12-12 00:40:56 +08:00
|
|
|
const query = {
|
2019-06-25 04:15:03 +08:00
|
|
|
range: {
|
|
|
|
|
from: toUtc([2015, 4, 30, 10]),
|
|
|
|
|
to: toUtc([2019, 7, 1, 10]),
|
|
|
|
|
},
|
|
|
|
|
targets: [
|
|
|
|
|
{
|
|
|
|
|
alias: '$varAlias',
|
|
|
|
|
refId: 'A',
|
|
|
|
|
bucketAggs: [{ type: 'date_histogram', settings: { interval: 'auto' }, id: '2' }],
|
|
|
|
|
metrics: [{ type: 'count', id: '1' }],
|
|
|
|
|
query: 'escape\\:test',
|
|
|
|
|
interval: '10s',
|
|
|
|
|
isLogsQuery: true,
|
|
|
|
|
timeField: '@timestamp',
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
};
|
|
|
|
|
|
2019-12-12 00:40:56 +08:00
|
|
|
const queryBuilderSpy = jest.spyOn(ctx.ds.queryBuilder, 'getLogsQuery');
|
|
|
|
|
const response = await ctx.ds.query(query);
|
|
|
|
|
return { queryBuilderSpy, response };
|
|
|
|
|
}
|
2019-06-25 04:15:03 +08:00
|
|
|
|
2019-12-12 00:40:56 +08:00
|
|
|
it('should call getLogsQuery()', async () => {
|
|
|
|
|
const { queryBuilderSpy } = await setupDataSource();
|
2019-06-25 04:15:03 +08:00
|
|
|
expect(queryBuilderSpy).toHaveBeenCalled();
|
|
|
|
|
});
|
2019-12-12 00:40:56 +08:00
|
|
|
|
|
|
|
|
it('should enhance fields with links', async () => {
|
|
|
|
|
const { response } = await setupDataSource({
|
|
|
|
|
dataLinks: [
|
|
|
|
|
{
|
|
|
|
|
field: 'host',
|
|
|
|
|
url: 'http://localhost:3000/${__value.raw}',
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
});
|
|
|
|
|
// 1 for logs and 1 for counts.
|
|
|
|
|
expect(response.data.length).toBe(2);
|
|
|
|
|
const links = response.data[0].fields.find((field: Field) => field.name === 'host').config.links;
|
|
|
|
|
expect(links.length).toBe(1);
|
|
|
|
|
expect(links[0].url).toBe('http://localhost:3000/${__value.raw}');
|
|
|
|
|
});
|
2019-06-25 04:15:03 +08:00
|
|
|
});
|
|
|
|
|
|
2018-09-04 21:55:41 +08:00
|
|
|
describe('When issuing document query', () => {
|
2019-07-11 23:05:45 +08:00
|
|
|
let requestOptions: any, parts: any, header: any;
|
2015-11-05 15:36:51 +08:00
|
|
|
|
2018-09-04 21:55:41 +08:00
|
|
|
beforeEach(() => {
|
2017-12-19 23:06:54 +08:00
|
|
|
createDatasource({
|
2017-12-20 19:33:33 +08:00
|
|
|
url: 'http://es.com',
|
2019-06-25 04:15:03 +08:00
|
|
|
database: 'test',
|
|
|
|
|
jsonData: { esVersion: 2 } as ElasticsearchOptions,
|
|
|
|
|
} as DataSourceInstanceSettings<ElasticsearchOptions>);
|
2015-11-05 15:36:51 +08:00
|
|
|
|
2020-01-21 17:08:07 +08:00
|
|
|
datasourceRequestMock.mockImplementation(options => {
|
2015-11-05 15:36:51 +08:00
|
|
|
requestOptions = options;
|
2018-07-03 16:12:07 +08:00
|
|
|
return Promise.resolve({ data: { responses: [] } });
|
|
|
|
|
});
|
2015-11-05 15:36:51 +08:00
|
|
|
|
|
|
|
|
ctx.ds.query({
|
2017-12-19 23:06:54 +08:00
|
|
|
range: {
|
2019-05-08 19:51:44 +08:00
|
|
|
from: dateTime([2015, 4, 30, 10]),
|
|
|
|
|
to: dateTime([2015, 5, 1, 10]),
|
2017-12-19 23:06:54 +08:00
|
|
|
},
|
|
|
|
|
targets: [
|
|
|
|
|
{
|
|
|
|
|
bucketAggs: [],
|
2017-12-20 19:33:33 +08:00
|
|
|
metrics: [{ type: 'raw_document' }],
|
|
|
|
|
query: 'test',
|
|
|
|
|
},
|
|
|
|
|
],
|
2015-11-05 15:36:51 +08:00
|
|
|
});
|
|
|
|
|
|
2017-12-20 19:33:33 +08:00
|
|
|
parts = requestOptions.data.split('\n');
|
2015-11-05 15:36:51 +08:00
|
|
|
header = angular.fromJson(parts[0]);
|
|
|
|
|
});
|
|
|
|
|
|
2018-09-04 21:55:41 +08:00
|
|
|
it('should set search type to query_then_fetch', () => {
|
2018-07-03 16:12:07 +08:00
|
|
|
expect(header.search_type).toEqual('query_then_fetch');
|
2015-11-05 15:36:51 +08:00
|
|
|
});
|
|
|
|
|
|
2018-09-04 21:55:41 +08:00
|
|
|
it('should set size', () => {
|
2018-08-27 00:43:07 +08:00
|
|
|
const body = angular.fromJson(parts[1]);
|
2018-07-03 16:12:07 +08:00
|
|
|
expect(body.size).toBe(500);
|
2015-11-05 15:36:51 +08:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2018-09-04 21:55:41 +08:00
|
|
|
describe('When getting fields', () => {
|
2018-07-03 16:47:50 +08:00
|
|
|
beforeEach(() => {
|
2019-06-25 04:15:03 +08:00
|
|
|
createDatasource({
|
|
|
|
|
url: 'http://es.com',
|
|
|
|
|
database: 'metricbeat',
|
|
|
|
|
jsonData: { esVersion: 50 } as ElasticsearchOptions,
|
|
|
|
|
} as DataSourceInstanceSettings<ElasticsearchOptions>);
|
2016-09-15 13:30:08 +08:00
|
|
|
|
2020-01-21 17:08:07 +08:00
|
|
|
datasourceRequestMock.mockImplementation(options => {
|
2018-07-03 16:12:07 +08:00
|
|
|
return Promise.resolve({
|
2017-12-19 23:06:54 +08:00
|
|
|
data: {
|
|
|
|
|
metricbeat: {
|
|
|
|
|
mappings: {
|
|
|
|
|
metricsets: {
|
|
|
|
|
_all: {},
|
|
|
|
|
properties: {
|
2017-12-20 19:33:33 +08:00
|
|
|
'@timestamp': { type: 'date' },
|
2017-12-19 23:06:54 +08:00
|
|
|
beat: {
|
|
|
|
|
properties: {
|
|
|
|
|
name: {
|
2017-12-20 19:33:33 +08:00
|
|
|
fields: { raw: { type: 'keyword' } },
|
|
|
|
|
type: 'string',
|
2017-12-19 23:06:54 +08:00
|
|
|
},
|
2017-12-20 19:33:33 +08:00
|
|
|
hostname: { type: 'string' },
|
|
|
|
|
},
|
2017-12-19 23:06:54 +08:00
|
|
|
},
|
|
|
|
|
system: {
|
|
|
|
|
properties: {
|
|
|
|
|
cpu: {
|
|
|
|
|
properties: {
|
2017-12-20 19:33:33 +08:00
|
|
|
system: { type: 'float' },
|
|
|
|
|
user: { type: 'float' },
|
|
|
|
|
},
|
2017-12-19 23:06:54 +08:00
|
|
|
},
|
|
|
|
|
process: {
|
|
|
|
|
properties: {
|
|
|
|
|
cpu: {
|
|
|
|
|
properties: {
|
2017-12-20 19:33:33 +08:00
|
|
|
total: { type: 'float' },
|
|
|
|
|
},
|
2017-12-19 23:06:54 +08:00
|
|
|
},
|
2017-12-20 19:33:33 +08:00
|
|
|
name: { type: 'string' },
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
2017-12-19 23:06:54 +08:00
|
|
|
});
|
2018-07-03 16:47:50 +08:00
|
|
|
});
|
2016-09-15 13:30:08 +08:00
|
|
|
});
|
|
|
|
|
|
2019-04-25 15:41:13 +08:00
|
|
|
it('should return nested fields', async () => {
|
|
|
|
|
const fieldObjects = await ctx.ds.getFields({
|
|
|
|
|
find: 'fields',
|
|
|
|
|
query: '*',
|
|
|
|
|
});
|
|
|
|
|
const fields = _.map(fieldObjects, 'text');
|
|
|
|
|
expect(fields).toEqual([
|
|
|
|
|
'@timestamp',
|
|
|
|
|
'beat.name.raw',
|
|
|
|
|
'beat.name',
|
|
|
|
|
'beat.hostname',
|
|
|
|
|
'system.cpu.system',
|
|
|
|
|
'system.cpu.user',
|
|
|
|
|
'system.process.cpu.total',
|
|
|
|
|
'system.process.name',
|
|
|
|
|
]);
|
2016-09-15 13:30:08 +08:00
|
|
|
});
|
|
|
|
|
|
2019-04-25 15:41:13 +08:00
|
|
|
it('should return number fields', async () => {
|
|
|
|
|
const fieldObjects = await ctx.ds.getFields({
|
|
|
|
|
find: 'fields',
|
|
|
|
|
query: '*',
|
|
|
|
|
type: 'number',
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const fields = _.map(fieldObjects, 'text');
|
|
|
|
|
expect(fields).toEqual(['system.cpu.system', 'system.cpu.user', 'system.process.cpu.total']);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should return date fields', async () => {
|
|
|
|
|
const fieldObjects = await ctx.ds.getFields({
|
|
|
|
|
find: 'fields',
|
|
|
|
|
query: '*',
|
|
|
|
|
type: 'date',
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const fields = _.map(fieldObjects, 'text');
|
|
|
|
|
expect(fields).toEqual(['@timestamp']);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('When getting fields from ES 7.0', () => {
|
|
|
|
|
beforeEach(() => {
|
2019-06-25 04:15:03 +08:00
|
|
|
createDatasource({
|
|
|
|
|
url: 'http://es.com',
|
|
|
|
|
database: 'genuine.es7._mapping.response',
|
|
|
|
|
jsonData: { esVersion: 70 } as ElasticsearchOptions,
|
|
|
|
|
} as DataSourceInstanceSettings<ElasticsearchOptions>);
|
2016-09-15 13:30:08 +08:00
|
|
|
|
2020-01-21 17:08:07 +08:00
|
|
|
datasourceRequestMock.mockImplementation(options => {
|
2019-04-25 15:41:13 +08:00
|
|
|
return Promise.resolve({
|
|
|
|
|
data: {
|
|
|
|
|
'genuine.es7._mapping.response': {
|
|
|
|
|
mappings: {
|
|
|
|
|
properties: {
|
|
|
|
|
'@timestamp_millis': {
|
|
|
|
|
type: 'date',
|
|
|
|
|
format: 'epoch_millis',
|
|
|
|
|
},
|
|
|
|
|
classification_terms: {
|
|
|
|
|
type: 'keyword',
|
|
|
|
|
},
|
|
|
|
|
domains: {
|
|
|
|
|
type: 'keyword',
|
|
|
|
|
},
|
|
|
|
|
ip_address: {
|
|
|
|
|
type: 'ip',
|
|
|
|
|
},
|
|
|
|
|
justification_blob: {
|
|
|
|
|
properties: {
|
|
|
|
|
criterion: {
|
|
|
|
|
type: 'text',
|
|
|
|
|
fields: {
|
|
|
|
|
keyword: {
|
|
|
|
|
type: 'keyword',
|
|
|
|
|
ignore_above: 256,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
overall_vote_score: {
|
|
|
|
|
type: 'float',
|
|
|
|
|
},
|
|
|
|
|
shallow: {
|
|
|
|
|
properties: {
|
|
|
|
|
jsi: {
|
|
|
|
|
properties: {
|
|
|
|
|
sdb: {
|
|
|
|
|
properties: {
|
|
|
|
|
dsel2: {
|
|
|
|
|
properties: {
|
|
|
|
|
'bootlegged-gille': {
|
|
|
|
|
properties: {
|
|
|
|
|
botness: {
|
|
|
|
|
type: 'float',
|
|
|
|
|
},
|
|
|
|
|
general_algorithm_score: {
|
|
|
|
|
type: 'float',
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
'uncombed-boris': {
|
|
|
|
|
properties: {
|
|
|
|
|
botness: {
|
|
|
|
|
type: 'float',
|
|
|
|
|
},
|
|
|
|
|
general_algorithm_score: {
|
|
|
|
|
type: 'float',
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
overall_vote_score: {
|
|
|
|
|
type: 'float',
|
|
|
|
|
},
|
|
|
|
|
ua_terms_long: {
|
|
|
|
|
type: 'keyword',
|
|
|
|
|
},
|
|
|
|
|
ua_terms_short: {
|
|
|
|
|
type: 'keyword',
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
2017-12-19 23:06:54 +08:00
|
|
|
});
|
2019-04-25 15:41:13 +08:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should return nested fields', async () => {
|
|
|
|
|
const fieldObjects = await ctx.ds.getFields({
|
|
|
|
|
find: 'fields',
|
|
|
|
|
query: '*',
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const fields = _.map(fieldObjects, 'text');
|
|
|
|
|
expect(fields).toEqual([
|
|
|
|
|
'@timestamp_millis',
|
|
|
|
|
'classification_terms',
|
|
|
|
|
'domains',
|
|
|
|
|
'ip_address',
|
|
|
|
|
'justification_blob.criterion.keyword',
|
|
|
|
|
'justification_blob.criterion',
|
|
|
|
|
'justification_blob.overall_vote_score',
|
|
|
|
|
'justification_blob.shallow.jsi.sdb.dsel2.bootlegged-gille.botness',
|
|
|
|
|
'justification_blob.shallow.jsi.sdb.dsel2.bootlegged-gille.general_algorithm_score',
|
|
|
|
|
'justification_blob.shallow.jsi.sdb.dsel2.uncombed-boris.botness',
|
|
|
|
|
'justification_blob.shallow.jsi.sdb.dsel2.uncombed-boris.general_algorithm_score',
|
|
|
|
|
'overall_vote_score',
|
|
|
|
|
'ua_terms_long',
|
|
|
|
|
'ua_terms_short',
|
|
|
|
|
]);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should return number fields', async () => {
|
|
|
|
|
const fieldObjects = await ctx.ds.getFields({
|
|
|
|
|
find: 'fields',
|
|
|
|
|
query: '*',
|
|
|
|
|
type: 'number',
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const fields = _.map(fieldObjects, 'text');
|
|
|
|
|
expect(fields).toEqual([
|
|
|
|
|
'justification_blob.overall_vote_score',
|
|
|
|
|
'justification_blob.shallow.jsi.sdb.dsel2.bootlegged-gille.botness',
|
|
|
|
|
'justification_blob.shallow.jsi.sdb.dsel2.bootlegged-gille.general_algorithm_score',
|
|
|
|
|
'justification_blob.shallow.jsi.sdb.dsel2.uncombed-boris.botness',
|
|
|
|
|
'justification_blob.shallow.jsi.sdb.dsel2.uncombed-boris.general_algorithm_score',
|
|
|
|
|
'overall_vote_score',
|
|
|
|
|
]);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should return date fields', async () => {
|
|
|
|
|
const fieldObjects = await ctx.ds.getFields({
|
|
|
|
|
find: 'fields',
|
|
|
|
|
query: '*',
|
|
|
|
|
type: 'date',
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const fields = _.map(fieldObjects, 'text');
|
|
|
|
|
expect(fields).toEqual(['@timestamp_millis']);
|
2016-09-15 13:30:08 +08:00
|
|
|
});
|
|
|
|
|
});
|
2016-09-20 02:10:45 +08:00
|
|
|
|
2018-09-04 21:55:41 +08:00
|
|
|
describe('When issuing aggregation query on es5.x', () => {
|
2019-07-11 23:05:45 +08:00
|
|
|
let requestOptions: any, parts: any, header: any;
|
2016-06-20 06:40:16 +08:00
|
|
|
|
2018-09-04 21:55:41 +08:00
|
|
|
beforeEach(() => {
|
2017-12-19 23:06:54 +08:00
|
|
|
createDatasource({
|
2017-12-20 19:33:33 +08:00
|
|
|
url: 'http://es.com',
|
2019-06-25 04:15:03 +08:00
|
|
|
database: 'test',
|
|
|
|
|
jsonData: { esVersion: 5 } as ElasticsearchOptions,
|
|
|
|
|
} as DataSourceInstanceSettings<ElasticsearchOptions>);
|
2016-06-20 06:40:16 +08:00
|
|
|
|
2020-01-21 17:08:07 +08:00
|
|
|
datasourceRequestMock.mockImplementation(options => {
|
2016-06-20 06:40:16 +08:00
|
|
|
requestOptions = options;
|
2018-07-03 16:47:50 +08:00
|
|
|
return Promise.resolve({ data: { responses: [] } });
|
|
|
|
|
});
|
2016-06-20 06:40:16 +08:00
|
|
|
|
|
|
|
|
ctx.ds.query({
|
2017-12-19 23:06:54 +08:00
|
|
|
range: {
|
2019-05-08 19:51:44 +08:00
|
|
|
from: dateTime([2015, 4, 30, 10]),
|
|
|
|
|
to: dateTime([2015, 5, 1, 10]),
|
2017-12-19 23:06:54 +08:00
|
|
|
},
|
|
|
|
|
targets: [
|
|
|
|
|
{
|
2017-12-21 15:39:31 +08:00
|
|
|
bucketAggs: [{ type: 'date_histogram', field: '@timestamp', id: '2' }],
|
2017-12-20 19:33:33 +08:00
|
|
|
metrics: [{ type: 'count' }],
|
|
|
|
|
query: 'test',
|
|
|
|
|
},
|
|
|
|
|
],
|
2016-06-20 06:40:16 +08:00
|
|
|
});
|
|
|
|
|
|
2017-12-20 19:33:33 +08:00
|
|
|
parts = requestOptions.data.split('\n');
|
2016-06-20 06:40:16 +08:00
|
|
|
header = angular.fromJson(parts[0]);
|
|
|
|
|
});
|
|
|
|
|
|
2018-09-04 21:55:41 +08:00
|
|
|
it('should not set search type to count', () => {
|
2018-07-03 16:12:07 +08:00
|
|
|
expect(header.search_type).not.toEqual('count');
|
2016-06-20 06:40:16 +08:00
|
|
|
});
|
|
|
|
|
|
2018-09-04 21:55:41 +08:00
|
|
|
it('should set size to 0', () => {
|
2018-08-27 00:43:07 +08:00
|
|
|
const body = angular.fromJson(parts[1]);
|
2018-07-03 16:12:07 +08:00
|
|
|
expect(body.size).toBe(0);
|
2016-06-20 06:40:16 +08:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2018-09-04 21:55:41 +08:00
|
|
|
describe('When issuing metricFind query on es5.x', () => {
|
2019-07-11 23:05:45 +08:00
|
|
|
let requestOptions: any, parts, header: any, body: any, results: any;
|
2016-06-20 06:40:16 +08:00
|
|
|
|
2018-07-03 16:47:50 +08:00
|
|
|
beforeEach(() => {
|
2017-12-19 23:06:54 +08:00
|
|
|
createDatasource({
|
2017-12-20 19:33:33 +08:00
|
|
|
url: 'http://es.com',
|
2019-06-25 04:15:03 +08:00
|
|
|
database: 'test',
|
|
|
|
|
jsonData: { esVersion: 5 } as ElasticsearchOptions,
|
|
|
|
|
} as DataSourceInstanceSettings<ElasticsearchOptions>);
|
2016-06-20 06:40:16 +08:00
|
|
|
|
2020-01-21 17:08:07 +08:00
|
|
|
datasourceRequestMock.mockImplementation(options => {
|
2016-06-20 06:40:16 +08:00
|
|
|
requestOptions = options;
|
2018-07-03 16:47:50 +08:00
|
|
|
return Promise.resolve({
|
2017-07-12 13:43:12 +08:00
|
|
|
data: {
|
|
|
|
|
responses: [
|
|
|
|
|
{
|
|
|
|
|
aggregations: {
|
2017-12-20 19:33:33 +08:00
|
|
|
'1': {
|
2017-07-12 13:43:12 +08:00
|
|
|
buckets: [
|
2017-12-20 19:33:33 +08:00
|
|
|
{ doc_count: 1, key: 'test' },
|
2017-12-19 23:06:54 +08:00
|
|
|
{
|
|
|
|
|
doc_count: 2,
|
2017-12-20 19:33:33 +08:00
|
|
|
key: 'test2',
|
|
|
|
|
key_as_string: 'test2_as_string',
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
},
|
2016-06-20 06:40:16 +08:00
|
|
|
});
|
2018-07-03 16:47:50 +08:00
|
|
|
});
|
2016-06-20 06:40:16 +08:00
|
|
|
|
2019-07-11 23:05:45 +08:00
|
|
|
ctx.ds.metricFindQuery('{"find": "terms", "field": "test"}').then((res: any) => {
|
2017-07-12 13:43:12 +08:00
|
|
|
results = res;
|
|
|
|
|
});
|
|
|
|
|
|
2017-12-20 19:33:33 +08:00
|
|
|
parts = requestOptions.data.split('\n');
|
2016-06-20 06:40:16 +08:00
|
|
|
header = angular.fromJson(parts[0]);
|
2016-07-27 04:05:49 +08:00
|
|
|
body = angular.fromJson(parts[1]);
|
2016-06-20 06:40:16 +08:00
|
|
|
});
|
|
|
|
|
|
2018-07-03 16:47:50 +08:00
|
|
|
it('should get results', () => {
|
2018-07-03 16:12:07 +08:00
|
|
|
expect(results.length).toEqual(2);
|
2017-07-12 13:43:12 +08:00
|
|
|
});
|
|
|
|
|
|
2018-07-03 16:47:50 +08:00
|
|
|
it('should use key or key_as_string', () => {
|
2018-07-03 16:12:07 +08:00
|
|
|
expect(results[0].text).toEqual('test');
|
|
|
|
|
expect(results[1].text).toEqual('test2_as_string');
|
2017-07-12 13:43:12 +08:00
|
|
|
});
|
|
|
|
|
|
2018-07-03 16:47:50 +08:00
|
|
|
it('should not set search type to count', () => {
|
2018-07-03 16:12:07 +08:00
|
|
|
expect(header.search_type).not.toEqual('count');
|
2016-06-20 06:40:16 +08:00
|
|
|
});
|
|
|
|
|
|
2018-07-03 16:47:50 +08:00
|
|
|
it('should set size to 0', () => {
|
2018-07-03 16:12:07 +08:00
|
|
|
expect(body.size).toBe(0);
|
2016-06-20 06:40:16 +08:00
|
|
|
});
|
|
|
|
|
|
2018-07-03 16:47:50 +08:00
|
|
|
it('should not set terms aggregation size to 0', () => {
|
2018-07-03 16:12:07 +08:00
|
|
|
expect(body['aggs']['1']['terms'].size).not.toBe(0);
|
2016-10-02 22:59:25 +08:00
|
|
|
});
|
2016-06-20 06:40:16 +08:00
|
|
|
});
|
2020-02-13 23:00:01 +08:00
|
|
|
|
|
|
|
|
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');
|
|
|
|
|
});
|
|
|
|
|
});
|
2015-09-28 22:28:19 +08:00
|
|
|
});
|
2019-12-12 00:40:56 +08:00
|
|
|
|
2020-02-13 23:00:01 +08:00
|
|
|
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',
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
2019-12-12 00:40:56 +08:00
|
|
|
const logsResponse = {
|
|
|
|
|
data: {
|
|
|
|
|
responses: [
|
|
|
|
|
{
|
|
|
|
|
aggregations: {
|
|
|
|
|
'2': {
|
|
|
|
|
buckets: [
|
|
|
|
|
{
|
|
|
|
|
doc_count: 10,
|
|
|
|
|
key: 1000,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
doc_count: 15,
|
|
|
|
|
key: 2000,
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
hits: {
|
|
|
|
|
hits: [
|
|
|
|
|
{
|
|
|
|
|
'@timestamp': ['2019-06-24T09:51:19.765Z'],
|
|
|
|
|
_id: 'fdsfs',
|
|
|
|
|
_type: '_doc',
|
|
|
|
|
_index: 'mock-index',
|
|
|
|
|
_source: {
|
|
|
|
|
'@timestamp': '2019-06-24T09:51:19.765Z',
|
|
|
|
|
host: 'djisaodjsoad',
|
|
|
|
|
message: 'hello, i am a message',
|
|
|
|
|
},
|
|
|
|
|
fields: {
|
|
|
|
|
'@timestamp': ['2019-06-24T09:51:19.765Z'],
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
'@timestamp': ['2019-06-24T09:52:19.765Z'],
|
|
|
|
|
_id: 'kdospaidopa',
|
|
|
|
|
_type: '_doc',
|
|
|
|
|
_index: 'mock-index',
|
|
|
|
|
_source: {
|
|
|
|
|
'@timestamp': '2019-06-24T09:52:19.765Z',
|
|
|
|
|
host: 'dsalkdakdop',
|
|
|
|
|
message: 'hello, i am also message',
|
|
|
|
|
},
|
|
|
|
|
fields: {
|
|
|
|
|
'@timestamp': ['2019-06-24T09:52:19.765Z'],
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
};
|