2019-06-25 04:15:03 +08:00
|
|
|
import angular, { IQService } from 'angular';
|
2017-12-20 19:33:33 +08:00
|
|
|
import _ from 'lodash';
|
2019-06-25 04:15:03 +08:00
|
|
|
import { DataSourceApi, DataSourceInstanceSettings, DataQueryRequest, DataQueryResponse } from '@grafana/ui';
|
2017-12-20 19:33:33 +08:00
|
|
|
import { ElasticResponse } from './elastic_response';
|
2019-03-26 23:15:23 +08:00
|
|
|
import { IndexPattern } from './index_pattern';
|
|
|
|
|
import { ElasticQueryBuilder } from './query_builder';
|
2019-07-06 14:05:53 +08:00
|
|
|
import { toUtc } from '@grafana/data';
|
2019-06-25 04:15:03 +08:00
|
|
|
import * as queryDef from './query_def';
|
|
|
|
|
import { BackendSrv } from 'app/core/services/backend_srv';
|
|
|
|
|
import { TemplateSrv } from 'app/features/templating/template_srv';
|
|
|
|
|
import { TimeSrv } from 'app/features/dashboard/services/TimeSrv';
|
|
|
|
|
import { ElasticsearchOptions, ElasticsearchQuery } from './types';
|
2017-09-28 18:52:39 +08:00
|
|
|
|
2019-06-25 04:15:03 +08:00
|
|
|
export class ElasticDatasource extends DataSourceApi<ElasticsearchQuery, ElasticsearchOptions> {
|
2017-09-28 18:52:39 +08:00
|
|
|
basicAuth: string;
|
|
|
|
|
withCredentials: boolean;
|
|
|
|
|
url: string;
|
|
|
|
|
name: string;
|
|
|
|
|
index: string;
|
|
|
|
|
timeField: string;
|
|
|
|
|
esVersion: number;
|
|
|
|
|
interval: string;
|
2017-11-21 17:56:34 +08:00
|
|
|
maxConcurrentShardRequests: number;
|
2017-09-28 18:52:39 +08:00
|
|
|
queryBuilder: ElasticQueryBuilder;
|
|
|
|
|
indexPattern: IndexPattern;
|
2019-06-25 04:15:03 +08:00
|
|
|
logMessageField?: string;
|
|
|
|
|
logLevelField?: string;
|
2017-09-28 18:52:39 +08:00
|
|
|
|
|
|
|
|
/** @ngInject */
|
2019-06-25 04:15:03 +08:00
|
|
|
constructor(
|
|
|
|
|
instanceSettings: DataSourceInstanceSettings<ElasticsearchOptions>,
|
|
|
|
|
private $q: IQService,
|
|
|
|
|
private backendSrv: BackendSrv,
|
|
|
|
|
private templateSrv: TemplateSrv,
|
|
|
|
|
private timeSrv: TimeSrv
|
|
|
|
|
) {
|
|
|
|
|
super(instanceSettings);
|
2017-09-28 18:52:39 +08:00
|
|
|
this.basicAuth = instanceSettings.basicAuth;
|
|
|
|
|
this.withCredentials = instanceSettings.withCredentials;
|
|
|
|
|
this.url = instanceSettings.url;
|
|
|
|
|
this.name = instanceSettings.name;
|
2019-06-25 04:15:03 +08:00
|
|
|
this.index = instanceSettings.database;
|
|
|
|
|
const settingsData = instanceSettings.jsonData || ({} as ElasticsearchOptions);
|
|
|
|
|
|
|
|
|
|
this.timeField = settingsData.timeField;
|
|
|
|
|
this.esVersion = settingsData.esVersion;
|
|
|
|
|
this.indexPattern = new IndexPattern(this.index, settingsData.interval);
|
|
|
|
|
this.interval = settingsData.timeInterval;
|
|
|
|
|
this.maxConcurrentShardRequests = settingsData.maxConcurrentShardRequests;
|
2017-09-28 18:52:39 +08:00
|
|
|
this.queryBuilder = new ElasticQueryBuilder({
|
|
|
|
|
timeField: this.timeField,
|
2017-12-20 19:33:33 +08:00
|
|
|
esVersion: this.esVersion,
|
2017-09-28 18:52:39 +08:00
|
|
|
});
|
2019-06-25 04:15:03 +08:00
|
|
|
this.logMessageField = settingsData.logMessageField || '';
|
|
|
|
|
this.logLevelField = settingsData.logLevelField || '';
|
|
|
|
|
|
|
|
|
|
if (this.logMessageField === '') {
|
|
|
|
|
this.logMessageField = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (this.logLevelField === '') {
|
|
|
|
|
this.logLevelField = null;
|
|
|
|
|
}
|
2017-09-28 18:52:39 +08:00
|
|
|
}
|
|
|
|
|
|
2019-07-11 23:05:45 +08:00
|
|
|
private request(method: string, url: string, data?: undefined) {
|
2018-08-29 20:27:29 +08:00
|
|
|
const options: any = {
|
2017-12-20 19:33:33 +08:00
|
|
|
url: this.url + '/' + url,
|
2017-09-28 18:52:39 +08:00
|
|
|
method: method,
|
2017-12-20 19:33:33 +08:00
|
|
|
data: data,
|
2017-09-28 18:52:39 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (this.basicAuth || this.withCredentials) {
|
|
|
|
|
options.withCredentials = true;
|
|
|
|
|
}
|
|
|
|
|
if (this.basicAuth) {
|
|
|
|
|
options.headers = {
|
2017-12-20 19:33:33 +08:00
|
|
|
Authorization: this.basicAuth,
|
2017-09-28 18:52:39 +08:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return this.backendSrv.datasourceRequest(options);
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-11 23:05:45 +08:00
|
|
|
private get(url: string) {
|
2018-08-29 20:27:29 +08:00
|
|
|
const range = this.timeSrv.timeRange();
|
2018-09-03 17:00:46 +08:00
|
|
|
const indexList = this.indexPattern.getIndexList(range.from.valueOf(), range.to.valueOf());
|
|
|
|
|
if (_.isArray(indexList) && indexList.length) {
|
2019-07-11 23:05:45 +08:00
|
|
|
return this.request('GET', indexList[0] + url).then((results: any) => {
|
2017-09-28 18:52:39 +08:00
|
|
|
results.data.$$config = results.config;
|
|
|
|
|
return results.data;
|
|
|
|
|
});
|
|
|
|
|
} else {
|
2019-07-11 23:05:45 +08:00
|
|
|
return this.request('GET', this.indexPattern.getIndexForToday() + url).then((results: any) => {
|
2017-09-28 18:52:39 +08:00
|
|
|
results.data.$$config = results.config;
|
|
|
|
|
return results.data;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-11 23:05:45 +08:00
|
|
|
private post(url: string, data: any) {
|
2017-12-20 19:33:33 +08:00
|
|
|
return this.request('POST', url, data)
|
2019-07-11 23:05:45 +08:00
|
|
|
.then((results: any) => {
|
2017-12-19 23:06:54 +08:00
|
|
|
results.data.$$config = results.config;
|
|
|
|
|
return results.data;
|
|
|
|
|
})
|
2019-07-11 23:05:45 +08:00
|
|
|
.catch((err: any) => {
|
2017-12-19 23:06:54 +08:00
|
|
|
if (err.data && err.data.error) {
|
|
|
|
|
throw {
|
2017-12-20 19:33:33 +08:00
|
|
|
message: 'Elasticsearch error: ' + err.data.error.reason,
|
|
|
|
|
error: err.data.error,
|
2017-12-19 23:06:54 +08:00
|
|
|
};
|
|
|
|
|
}
|
2017-10-18 18:02:57 +08:00
|
|
|
|
2017-12-19 23:06:54 +08:00
|
|
|
throw err;
|
|
|
|
|
});
|
2017-09-28 18:52:39 +08:00
|
|
|
}
|
|
|
|
|
|
2019-07-11 23:05:45 +08:00
|
|
|
annotationQuery(options: any) {
|
2018-08-29 20:27:29 +08:00
|
|
|
const annotation = options.annotation;
|
|
|
|
|
const timeField = annotation.timeField || '@timestamp';
|
|
|
|
|
const queryString = annotation.query || '*';
|
|
|
|
|
const tagsField = annotation.tagsField || 'tags';
|
|
|
|
|
const textField = annotation.textField || null;
|
2017-09-28 18:52:39 +08:00
|
|
|
|
2019-07-11 23:05:45 +08:00
|
|
|
const range: any = {};
|
2017-12-19 23:06:54 +08:00
|
|
|
range[timeField] = {
|
2017-09-28 18:52:39 +08:00
|
|
|
from: options.range.from.valueOf(),
|
|
|
|
|
to: options.range.to.valueOf(),
|
2017-12-20 19:33:33 +08:00
|
|
|
format: 'epoch_millis',
|
2017-09-28 18:52:39 +08:00
|
|
|
};
|
|
|
|
|
|
2018-08-29 20:27:29 +08:00
|
|
|
const queryInterpolated = this.templateSrv.replace(queryString, {}, 'lucene');
|
|
|
|
|
const query = {
|
2017-12-19 23:06:54 +08:00
|
|
|
bool: {
|
|
|
|
|
filter: [
|
|
|
|
|
{ range: range },
|
2017-09-28 18:52:39 +08:00
|
|
|
{
|
2017-12-19 23:06:54 +08:00
|
|
|
query_string: {
|
2017-12-20 19:33:33 +08:00
|
|
|
query: queryInterpolated,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
},
|
2017-09-28 18:52:39 +08:00
|
|
|
};
|
|
|
|
|
|
2019-07-11 23:05:45 +08:00
|
|
|
const data: any = {
|
|
|
|
|
query,
|
2017-12-20 19:33:33 +08:00
|
|
|
size: 10000,
|
2017-09-28 18:52:39 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// fields field not supported on ES 5.x
|
|
|
|
|
if (this.esVersion < 5) {
|
2017-12-20 19:33:33 +08:00
|
|
|
data['fields'] = [timeField, '_source'];
|
2017-09-28 18:52:39 +08:00
|
|
|
}
|
|
|
|
|
|
2018-08-29 20:27:29 +08:00
|
|
|
const header: any = {
|
2017-12-20 19:33:33 +08:00
|
|
|
search_type: 'query_then_fetch',
|
|
|
|
|
ignore_unavailable: true,
|
2017-12-19 23:06:54 +08:00
|
|
|
};
|
2017-09-28 18:52:39 +08:00
|
|
|
|
|
|
|
|
// old elastic annotations had index specified on them
|
|
|
|
|
if (annotation.index) {
|
|
|
|
|
header.index = annotation.index;
|
|
|
|
|
} else {
|
2017-12-21 15:39:31 +08:00
|
|
|
header.index = this.indexPattern.getIndexList(options.range.from, options.range.to);
|
2017-09-28 18:52:39 +08:00
|
|
|
}
|
|
|
|
|
|
2018-08-29 20:27:29 +08:00
|
|
|
const payload = angular.toJson(header) + '\n' + angular.toJson(data) + '\n';
|
2017-09-28 18:52:39 +08:00
|
|
|
|
2019-07-11 23:05:45 +08:00
|
|
|
return this.post('_msearch', payload).then((res: any) => {
|
2018-08-29 20:27:29 +08:00
|
|
|
const list = [];
|
|
|
|
|
const hits = res.responses[0].hits.hits;
|
2017-09-28 18:52:39 +08:00
|
|
|
|
2019-07-11 23:05:45 +08:00
|
|
|
const getFieldFromSource = (source: any, fieldName: any) => {
|
2017-12-19 23:06:54 +08:00
|
|
|
if (!fieldName) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2017-09-28 18:52:39 +08:00
|
|
|
|
2018-08-29 20:27:29 +08:00
|
|
|
const fieldNames = fieldName.split('.');
|
2018-08-30 15:03:11 +08:00
|
|
|
let fieldValue = source;
|
2017-09-28 18:52:39 +08:00
|
|
|
|
2018-08-30 15:03:11 +08:00
|
|
|
for (let i = 0; i < fieldNames.length; i++) {
|
2017-09-28 18:52:39 +08:00
|
|
|
fieldValue = fieldValue[fieldNames[i]];
|
|
|
|
|
if (!fieldValue) {
|
2017-12-20 19:33:33 +08:00
|
|
|
console.log('could not find field in annotation: ', fieldName);
|
|
|
|
|
return '';
|
2017-09-28 18:52:39 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return fieldValue;
|
|
|
|
|
};
|
|
|
|
|
|
2018-08-30 15:03:11 +08:00
|
|
|
for (let i = 0; i < hits.length; i++) {
|
2018-08-29 20:27:29 +08:00
|
|
|
const source = hits[i]._source;
|
2018-08-30 15:03:11 +08:00
|
|
|
let time = getFieldFromSource(source, timeField);
|
2017-12-20 19:33:33 +08:00
|
|
|
if (typeof hits[i].fields !== 'undefined') {
|
2018-08-29 20:27:29 +08:00
|
|
|
const fields = hits[i].fields;
|
2017-09-28 18:52:39 +08:00
|
|
|
if (_.isString(fields[timeField]) || _.isNumber(fields[timeField])) {
|
|
|
|
|
time = fields[timeField];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-29 20:27:29 +08:00
|
|
|
const event = {
|
2017-09-28 18:52:39 +08:00
|
|
|
annotation: annotation,
|
2019-05-08 19:51:44 +08:00
|
|
|
time: toUtc(time).valueOf(),
|
2017-10-07 16:31:39 +08:00
|
|
|
text: getFieldFromSource(source, textField),
|
2017-12-20 19:33:33 +08:00
|
|
|
tags: getFieldFromSource(source, tagsField),
|
2017-09-28 18:52:39 +08:00
|
|
|
};
|
|
|
|
|
|
2017-10-07 16:31:39 +08:00
|
|
|
// legacy support for title tield
|
|
|
|
|
if (annotation.titleField) {
|
|
|
|
|
const title = getFieldFromSource(source, annotation.titleField);
|
|
|
|
|
if (title) {
|
2017-12-20 19:33:33 +08:00
|
|
|
event.text = title + '\n' + event.text;
|
2017-10-07 16:31:39 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-20 19:33:33 +08:00
|
|
|
if (typeof event.tags === 'string') {
|
|
|
|
|
event.tags = event.tags.split(',');
|
2017-10-07 16:31:39 +08:00
|
|
|
}
|
|
|
|
|
|
2017-09-28 18:52:39 +08:00
|
|
|
list.push(event);
|
|
|
|
|
}
|
|
|
|
|
return list;
|
|
|
|
|
});
|
2017-10-07 16:31:39 +08:00
|
|
|
}
|
2017-09-28 18:52:39 +08:00
|
|
|
|
|
|
|
|
testDatasource() {
|
|
|
|
|
// validate that the index exist and has date field
|
2017-12-20 19:33:33 +08:00
|
|
|
return this.getFields({ type: 'date' }).then(
|
2019-07-11 23:05:45 +08:00
|
|
|
(dateFields: any) => {
|
2019-04-15 18:11:52 +08:00
|
|
|
const timeField: any = _.find(dateFields, { text: this.timeField });
|
2017-12-19 23:06:54 +08:00
|
|
|
if (!timeField) {
|
|
|
|
|
return {
|
2017-12-20 19:33:33 +08:00
|
|
|
status: 'error',
|
|
|
|
|
message: 'No date field named ' + this.timeField + ' found',
|
2017-12-19 23:06:54 +08:00
|
|
|
};
|
|
|
|
|
}
|
2017-12-20 19:33:33 +08:00
|
|
|
return { status: 'success', message: 'Index OK. Time field name OK.' };
|
2018-08-30 16:49:18 +08:00
|
|
|
},
|
2019-07-11 23:05:45 +08:00
|
|
|
(err: any) => {
|
2017-12-19 23:06:54 +08:00
|
|
|
console.log(err);
|
|
|
|
|
if (err.data && err.data.error) {
|
2018-08-30 15:03:11 +08:00
|
|
|
let message = angular.toJson(err.data.error);
|
2017-12-19 23:06:54 +08:00
|
|
|
if (err.data.error.reason) {
|
|
|
|
|
message = err.data.error.reason;
|
|
|
|
|
}
|
2017-12-20 19:33:33 +08:00
|
|
|
return { status: 'error', message: message };
|
2017-12-19 23:06:54 +08:00
|
|
|
} else {
|
2017-12-20 19:33:33 +08:00
|
|
|
return { status: 'error', message: err.status };
|
2017-09-28 18:52:39 +08:00
|
|
|
}
|
|
|
|
|
}
|
2017-12-19 23:06:54 +08:00
|
|
|
);
|
2017-09-28 18:52:39 +08:00
|
|
|
}
|
|
|
|
|
|
2019-07-11 23:05:45 +08:00
|
|
|
getQueryHeader(searchType: any, timeFrom: any, timeTo: any) {
|
2018-09-03 17:00:46 +08:00
|
|
|
const queryHeader: any = {
|
2017-12-19 23:06:54 +08:00
|
|
|
search_type: searchType,
|
|
|
|
|
ignore_unavailable: true,
|
2017-12-20 19:33:33 +08:00
|
|
|
index: this.indexPattern.getIndexList(timeFrom, timeTo),
|
2017-12-19 23:06:54 +08:00
|
|
|
};
|
2019-04-25 15:41:13 +08:00
|
|
|
if (this.esVersion >= 56 && this.esVersion < 70) {
|
2018-09-03 17:00:46 +08:00
|
|
|
queryHeader['max_concurrent_shard_requests'] = this.maxConcurrentShardRequests;
|
2017-11-21 17:56:34 +08:00
|
|
|
}
|
2018-09-03 17:00:46 +08:00
|
|
|
return angular.toJson(queryHeader);
|
2017-09-28 18:52:39 +08:00
|
|
|
}
|
|
|
|
|
|
2019-06-25 04:15:03 +08:00
|
|
|
query(options: DataQueryRequest<ElasticsearchQuery>): Promise<DataQueryResponse> {
|
2018-08-30 15:03:11 +08:00
|
|
|
let payload = '';
|
2019-03-26 23:15:23 +08:00
|
|
|
const targets = _.cloneDeep(options.targets);
|
2019-06-25 04:15:03 +08:00
|
|
|
const sentTargets: ElasticsearchQuery[] = [];
|
2017-09-28 18:52:39 +08:00
|
|
|
|
|
|
|
|
// add global adhoc filters to timeFilter
|
2018-08-29 20:27:29 +08:00
|
|
|
const adhocFilters = this.templateSrv.getAdhocFilters(this.name);
|
2017-09-28 18:52:39 +08:00
|
|
|
|
2019-03-26 23:15:23 +08:00
|
|
|
for (const target of targets) {
|
2017-12-19 23:06:54 +08:00
|
|
|
if (target.hide) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2017-09-28 18:52:39 +08:00
|
|
|
|
2019-06-13 21:45:31 +08:00
|
|
|
let queryString = this.templateSrv.replace(target.query, options.scopedVars, 'lucene');
|
|
|
|
|
// Elasticsearch queryString should always be '*' if empty string
|
|
|
|
|
if (!queryString || queryString === '') {
|
|
|
|
|
queryString = '*';
|
|
|
|
|
}
|
2019-06-25 04:15:03 +08:00
|
|
|
|
|
|
|
|
let queryObj;
|
|
|
|
|
if (target.isLogsQuery) {
|
|
|
|
|
target.bucketAggs = [queryDef.defaultBucketAgg()];
|
|
|
|
|
target.metrics = [queryDef.defaultMetricAgg()];
|
|
|
|
|
queryObj = this.queryBuilder.getLogsQuery(target, queryString);
|
|
|
|
|
} else {
|
|
|
|
|
if (target.alias) {
|
|
|
|
|
target.alias = this.templateSrv.replace(target.alias, options.scopedVars, 'lucene');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
queryObj = this.queryBuilder.build(target, adhocFilters, queryString);
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-29 20:27:29 +08:00
|
|
|
const esQuery = angular.toJson(queryObj);
|
2017-09-28 18:52:39 +08:00
|
|
|
|
2018-08-29 20:27:29 +08:00
|
|
|
const searchType = queryObj.size === 0 && this.esVersion < 5 ? 'count' : 'query_then_fetch';
|
|
|
|
|
const header = this.getQueryHeader(searchType, options.range.from, options.range.to);
|
2017-12-20 19:33:33 +08:00
|
|
|
payload += header + '\n';
|
2017-12-19 23:06:54 +08:00
|
|
|
|
2017-12-20 19:33:33 +08:00
|
|
|
payload += esQuery + '\n';
|
2019-06-25 04:15:03 +08:00
|
|
|
|
2017-09-28 18:52:39 +08:00
|
|
|
sentTargets.push(target);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (sentTargets.length === 0) {
|
2019-06-25 04:15:03 +08:00
|
|
|
return Promise.resolve({ data: [] });
|
2017-09-28 18:52:39 +08:00
|
|
|
}
|
|
|
|
|
|
2019-06-25 04:15:03 +08:00
|
|
|
payload = payload.replace(/\$timeFrom/g, options.range.from.valueOf().toString());
|
|
|
|
|
payload = payload.replace(/\$timeTo/g, options.range.to.valueOf().toString());
|
2017-09-28 18:52:39 +08:00
|
|
|
payload = this.templateSrv.replace(payload, options.scopedVars);
|
|
|
|
|
|
2019-04-25 15:41:13 +08:00
|
|
|
const url = this.getMultiSearchUrl();
|
|
|
|
|
|
2019-07-11 23:05:45 +08:00
|
|
|
return this.post(url, payload).then((res: any) => {
|
2019-06-25 04:15:03 +08:00
|
|
|
const er = new ElasticResponse(sentTargets, res);
|
|
|
|
|
if (sentTargets.some(target => target.isLogsQuery)) {
|
|
|
|
|
return er.getLogs(this.logMessageField, this.logLevelField);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return er.getTimeSeries();
|
2017-09-28 18:52:39 +08:00
|
|
|
});
|
2017-10-07 16:31:39 +08:00
|
|
|
}
|
2017-09-28 18:52:39 +08:00
|
|
|
|
2019-07-11 23:05:45 +08:00
|
|
|
getFields(query: any) {
|
2019-04-25 15:41:13 +08:00
|
|
|
const configuredEsVersion = this.esVersion;
|
2019-07-11 23:05:45 +08:00
|
|
|
return this.get('/_mapping').then((result: any) => {
|
|
|
|
|
const typeMap: any = {
|
2017-12-20 19:33:33 +08:00
|
|
|
float: 'number',
|
|
|
|
|
double: 'number',
|
|
|
|
|
integer: 'number',
|
|
|
|
|
long: 'number',
|
|
|
|
|
date: 'date',
|
|
|
|
|
string: 'string',
|
|
|
|
|
text: 'string',
|
|
|
|
|
scaled_float: 'number',
|
|
|
|
|
nested: 'nested',
|
2017-09-28 18:52:39 +08:00
|
|
|
};
|
|
|
|
|
|
2019-07-11 23:05:45 +08:00
|
|
|
function shouldAddField(obj: any, key: any, query: any) {
|
2017-12-20 19:33:33 +08:00
|
|
|
if (key[0] === '_') {
|
2017-09-28 18:52:39 +08:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!query.type) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// equal query type filter, or via typemap translation
|
|
|
|
|
return query.type === obj.type || query.type === typeMap[obj.type];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Store subfield names: [system, process, cpu, total] -> system.process.cpu.total
|
2019-07-11 23:05:45 +08:00
|
|
|
const fieldNameParts: any = [];
|
|
|
|
|
const fields: any = {};
|
2017-09-28 18:52:39 +08:00
|
|
|
|
2019-07-11 23:05:45 +08:00
|
|
|
function getFieldsRecursively(obj: any) {
|
2018-08-29 20:27:29 +08:00
|
|
|
for (const key in obj) {
|
|
|
|
|
const subObj = obj[key];
|
2017-09-28 18:52:39 +08:00
|
|
|
|
|
|
|
|
// Check mapping field for nested fields
|
|
|
|
|
if (_.isObject(subObj.properties)) {
|
|
|
|
|
fieldNameParts.push(key);
|
|
|
|
|
getFieldsRecursively(subObj.properties);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (_.isObject(subObj.fields)) {
|
|
|
|
|
fieldNameParts.push(key);
|
|
|
|
|
getFieldsRecursively(subObj.fields);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (_.isString(subObj.type)) {
|
2018-08-29 20:27:29 +08:00
|
|
|
const fieldName = fieldNameParts.concat(key).join('.');
|
2017-09-28 18:52:39 +08:00
|
|
|
|
|
|
|
|
// Hide meta-fields and check field type
|
|
|
|
|
if (shouldAddField(subObj, key, query)) {
|
|
|
|
|
fields[fieldName] = {
|
|
|
|
|
text: fieldName,
|
2017-12-20 19:33:33 +08:00
|
|
|
type: subObj.type,
|
2017-09-28 18:52:39 +08:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
fieldNameParts.pop();
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-29 20:27:29 +08:00
|
|
|
for (const indexName in result) {
|
|
|
|
|
const index = result[indexName];
|
2017-09-28 18:52:39 +08:00
|
|
|
if (index && index.mappings) {
|
2018-08-29 20:27:29 +08:00
|
|
|
const mappings = index.mappings;
|
2019-04-25 15:41:13 +08:00
|
|
|
|
|
|
|
|
if (configuredEsVersion < 70) {
|
|
|
|
|
for (const typeName in mappings) {
|
|
|
|
|
const properties = mappings[typeName].properties;
|
|
|
|
|
getFieldsRecursively(properties);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
const properties = mappings.properties;
|
2017-09-28 18:52:39 +08:00
|
|
|
getFieldsRecursively(properties);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// transform to array
|
2018-09-04 20:27:03 +08:00
|
|
|
return _.map(fields, value => {
|
2017-09-28 18:52:39 +08:00
|
|
|
return value;
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-11 23:05:45 +08:00
|
|
|
getTerms(queryDef: any) {
|
2018-08-29 20:27:29 +08:00
|
|
|
const range = this.timeSrv.timeRange();
|
|
|
|
|
const searchType = this.esVersion >= 5 ? 'query_then_fetch' : 'count';
|
|
|
|
|
const header = this.getQueryHeader(searchType, range.from, range.to);
|
2018-08-30 15:03:11 +08:00
|
|
|
let esQuery = angular.toJson(this.queryBuilder.getTermsQuery(queryDef));
|
2017-09-28 18:52:39 +08:00
|
|
|
|
2019-06-25 04:15:03 +08:00
|
|
|
esQuery = esQuery.replace(/\$timeFrom/g, range.from.valueOf().toString());
|
|
|
|
|
esQuery = esQuery.replace(/\$timeTo/g, range.to.valueOf().toString());
|
2017-12-20 19:33:33 +08:00
|
|
|
esQuery = header + '\n' + esQuery + '\n';
|
2017-09-28 18:52:39 +08:00
|
|
|
|
2019-04-25 15:41:13 +08:00
|
|
|
const url = this.getMultiSearchUrl();
|
|
|
|
|
|
2019-07-11 23:05:45 +08:00
|
|
|
return this.post(url, esQuery).then((res: any) => {
|
2017-12-21 15:39:31 +08:00
|
|
|
if (!res.responses[0].aggregations) {
|
|
|
|
|
return [];
|
2017-12-19 23:06:54 +08:00
|
|
|
}
|
2017-12-21 15:39:31 +08:00
|
|
|
|
2018-08-29 20:27:29 +08:00
|
|
|
const buckets = res.responses[0].aggregations['1'].buckets;
|
2018-09-04 20:27:03 +08:00
|
|
|
return _.map(buckets, bucket => {
|
2017-12-21 15:39:31 +08:00
|
|
|
return {
|
|
|
|
|
text: bucket.key_as_string || bucket.key,
|
|
|
|
|
value: bucket.key,
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
});
|
2017-09-28 18:52:39 +08:00
|
|
|
}
|
|
|
|
|
|
2019-04-25 15:41:13 +08:00
|
|
|
getMultiSearchUrl() {
|
|
|
|
|
if (this.esVersion >= 70 && this.maxConcurrentShardRequests) {
|
|
|
|
|
return `_msearch?max_concurrent_shard_requests=${this.maxConcurrentShardRequests}`;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return '_msearch';
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-11 23:05:45 +08:00
|
|
|
metricFindQuery(query: any) {
|
2017-09-28 18:52:39 +08:00
|
|
|
query = angular.fromJson(query);
|
|
|
|
|
if (!query) {
|
|
|
|
|
return this.$q.when([]);
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-20 19:33:33 +08:00
|
|
|
if (query.find === 'fields') {
|
|
|
|
|
query.field = this.templateSrv.replace(query.field, {}, 'lucene');
|
2017-09-28 18:52:39 +08:00
|
|
|
return this.getFields(query);
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-20 19:33:33 +08:00
|
|
|
if (query.find === 'terms') {
|
2018-05-08 21:17:16 +08:00
|
|
|
query.field = this.templateSrv.replace(query.field, {}, 'lucene');
|
2017-12-20 19:33:33 +08:00
|
|
|
query.query = this.templateSrv.replace(query.query || '*', {}, 'lucene');
|
2017-09-28 18:52:39 +08:00
|
|
|
return this.getTerms(query);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getTagKeys() {
|
|
|
|
|
return this.getFields({});
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-11 23:05:45 +08:00
|
|
|
getTagValues(options: any) {
|
2017-12-20 19:33:33 +08:00
|
|
|
return this.getTerms({ field: options.key, query: '*' });
|
2017-09-28 18:52:39 +08:00
|
|
|
}
|
2018-05-29 01:45:18 +08:00
|
|
|
|
2019-07-11 23:05:45 +08:00
|
|
|
targetContainsTemplate(target: any) {
|
2018-05-29 01:45:18 +08:00
|
|
|
if (this.templateSrv.variableExists(target.query) || this.templateSrv.variableExists(target.alias)) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-26 23:14:40 +08:00
|
|
|
for (const bucketAgg of target.bucketAggs) {
|
2018-05-29 01:45:18 +08:00
|
|
|
if (this.templateSrv.variableExists(bucketAgg.field) || this.objectContainsTemplate(bucketAgg.settings)) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-26 23:14:40 +08:00
|
|
|
for (const metric of target.metrics) {
|
2018-05-29 01:45:18 +08:00
|
|
|
if (
|
|
|
|
|
this.templateSrv.variableExists(metric.field) ||
|
|
|
|
|
this.objectContainsTemplate(metric.settings) ||
|
|
|
|
|
this.objectContainsTemplate(metric.meta)
|
|
|
|
|
) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-11 23:05:45 +08:00
|
|
|
private isPrimitive(obj: any) {
|
2018-05-29 01:45:18 +08:00
|
|
|
if (obj === null || obj === undefined) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
if (['string', 'number', 'boolean'].some(type => type === typeof true)) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-11 23:05:45 +08:00
|
|
|
private objectContainsTemplate(obj: any) {
|
2018-05-29 01:45:18 +08:00
|
|
|
if (!obj) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-26 23:14:40 +08:00
|
|
|
for (const key of Object.keys(obj)) {
|
2018-05-29 01:45:18 +08:00
|
|
|
if (this.isPrimitive(obj[key])) {
|
|
|
|
|
if (this.templateSrv.variableExists(obj[key])) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
} else if (Array.isArray(obj[key])) {
|
2018-08-26 23:14:40 +08:00
|
|
|
for (const item of obj[key]) {
|
2018-05-29 01:45:18 +08:00
|
|
|
if (this.objectContainsTemplate(item)) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if (this.objectContainsTemplate(obj[key])) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2017-09-28 18:52:39 +08:00
|
|
|
}
|
2019-06-29 00:38:16 +08:00
|
|
|
|
|
|
|
|
export function getMaxConcurrenShardRequestOrDefault(options: ElasticsearchOptions): number {
|
|
|
|
|
if (options.maxConcurrentShardRequests === 5 && options.esVersion < 70) {
|
|
|
|
|
return 256;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (options.maxConcurrentShardRequests === 256 && options.esVersion >= 70) {
|
|
|
|
|
return 5;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const defaultMaxConcurrentShardRequests = options.esVersion >= 70 ? 5 : 256;
|
|
|
|
|
return options.maxConcurrentShardRequests || defaultMaxConcurrentShardRequests;
|
|
|
|
|
}
|