2014-07-29 17:26:05 +08:00
|
|
|
define([
|
|
|
|
|
'angular',
|
2014-08-07 20:35:19 +08:00
|
|
|
'lodash',
|
2015-08-18 13:57:58 +08:00
|
|
|
'moment',
|
2015-09-06 22:09:42 +08:00
|
|
|
'kbn',
|
2015-07-01 20:54:06 +08:00
|
|
|
'./queryBuilder',
|
2015-09-06 18:58:53 +08:00
|
|
|
'./indexPattern',
|
2015-09-07 19:13:19 +08:00
|
|
|
'./elasticResponse',
|
2015-07-02 02:45:55 +08:00
|
|
|
'./queryCtrl',
|
2015-08-18 13:57:58 +08:00
|
|
|
'./directives'
|
2014-07-29 17:26:05 +08:00
|
|
|
],
|
2015-09-07 19:13:19 +08:00
|
|
|
function (angular, _, moment, kbn, ElasticQueryBuilder, IndexPattern, ElasticResponse) {
|
2014-07-29 17:26:05 +08:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
var module = angular.module('grafana.services');
|
|
|
|
|
|
2015-09-06 20:45:12 +08:00
|
|
|
module.factory('ElasticDatasource', function($q, backendSrv, templateSrv) {
|
2014-07-29 17:26:05 +08:00
|
|
|
|
|
|
|
|
function ElasticDatasource(datasource) {
|
2015-01-17 17:39:01 +08:00
|
|
|
this.type = 'elasticsearch';
|
2014-07-29 17:26:05 +08:00
|
|
|
this.basicAuth = datasource.basicAuth;
|
|
|
|
|
this.url = datasource.url;
|
|
|
|
|
this.name = datasource.name;
|
2014-07-29 23:24:42 +08:00
|
|
|
this.index = datasource.index;
|
2015-09-07 14:57:46 +08:00
|
|
|
this.timeField = datasource.jsonData.timeField;
|
2015-09-06 20:45:12 +08:00
|
|
|
this.indexPattern = new IndexPattern(datasource.index, datasource.jsonData.interval);
|
2015-09-07 14:57:46 +08:00
|
|
|
this.queryBuilder = new ElasticQueryBuilder({
|
|
|
|
|
timeField: this.timeField
|
|
|
|
|
});
|
2014-07-29 17:26:05 +08:00
|
|
|
}
|
|
|
|
|
|
2015-09-06 22:09:42 +08:00
|
|
|
ElasticDatasource.prototype._request = function(method, url, data) {
|
2014-07-29 23:24:42 +08:00
|
|
|
var options = {
|
2015-09-06 22:09:42 +08:00
|
|
|
url: this.url + "/" + url,
|
2014-07-29 23:24:42 +08:00
|
|
|
method: method,
|
|
|
|
|
data: data
|
|
|
|
|
};
|
2014-07-30 17:34:09 +08:00
|
|
|
|
|
|
|
|
if (this.basicAuth) {
|
2014-09-18 19:50:59 +08:00
|
|
|
options.withCredentials = true;
|
2014-07-29 23:24:42 +08:00
|
|
|
options.headers = {
|
2015-03-02 16:58:35 +08:00
|
|
|
"Authorization": this.basicAuth
|
2014-07-29 23:24:42 +08:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-07 15:50:03 +08:00
|
|
|
return backendSrv.datasourceRequest(options);
|
2014-07-29 23:24:42 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
ElasticDatasource.prototype._get = function(url) {
|
2015-09-06 22:09:42 +08:00
|
|
|
return this._request('GET', this.indexPattern.getIndexForToday() + url)
|
2014-07-29 23:24:42 +08:00
|
|
|
.then(function(results) {
|
|
|
|
|
return results.data;
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
ElasticDatasource.prototype._post = function(url, data) {
|
2015-09-06 22:09:42 +08:00
|
|
|
return this._request('POST', url, data)
|
2014-07-29 23:24:42 +08:00
|
|
|
.then(function(results) {
|
|
|
|
|
return results.data;
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2014-08-28 22:03:13 +08:00
|
|
|
ElasticDatasource.prototype.annotationQuery = function(annotation, rangeUnparsed) {
|
2014-07-29 23:24:42 +08:00
|
|
|
var range = {};
|
|
|
|
|
var timeField = annotation.timeField || '@timestamp';
|
|
|
|
|
var queryString = annotation.query || '*';
|
|
|
|
|
var tagsField = annotation.tagsField || 'tags';
|
|
|
|
|
var titleField = annotation.titleField || 'desc';
|
|
|
|
|
var textField = annotation.textField || null;
|
|
|
|
|
|
2015-02-09 20:34:12 +08:00
|
|
|
range[timeField]= {
|
2014-07-29 23:24:42 +08:00
|
|
|
from: rangeUnparsed.from,
|
|
|
|
|
to: rangeUnparsed.to,
|
|
|
|
|
};
|
|
|
|
|
|
2014-08-28 22:03:13 +08:00
|
|
|
var queryInterpolated = templateSrv.replace(queryString);
|
2014-07-29 23:24:42 +08:00
|
|
|
var filter = { "bool": { "must": [{ "range": range }] } };
|
2014-08-08 13:19:03 +08:00
|
|
|
var query = { "bool": { "should": [{ "query_string": { "query": queryInterpolated } }] } };
|
2014-09-09 14:50:01 +08:00
|
|
|
var data = {
|
|
|
|
|
"fields": [timeField, "_source"],
|
|
|
|
|
"query" : { "filtered": { "query" : query, "filter": filter } },
|
2015-04-22 13:50:23 +08:00
|
|
|
"size": 10000
|
2014-09-09 14:50:01 +08:00
|
|
|
};
|
2014-07-29 23:24:42 +08:00
|
|
|
|
2015-09-06 22:09:42 +08:00
|
|
|
return this._request('POST', annotation.index + '/_search', data).then(function(results) {
|
2014-07-29 23:24:42 +08:00
|
|
|
var list = [];
|
|
|
|
|
var hits = results.data.hits.hits;
|
|
|
|
|
|
2014-09-19 21:03:08 +08:00
|
|
|
var getFieldFromSource = function(source, fieldName) {
|
2014-09-20 14:30:59 +08:00
|
|
|
if (!fieldName) { return; }
|
|
|
|
|
|
|
|
|
|
var fieldNames = fieldName.split('.');
|
|
|
|
|
var fieldValue = source;
|
|
|
|
|
|
|
|
|
|
for (var i = 0; i < fieldNames.length; i++) {
|
|
|
|
|
fieldValue = fieldValue[fieldNames[i]];
|
2014-09-24 15:03:04 +08:00
|
|
|
if (!fieldValue) {
|
|
|
|
|
console.log('could not find field in annotatation: ', fieldName);
|
|
|
|
|
return '';
|
|
|
|
|
}
|
2014-09-20 14:30:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (_.isArray(fieldValue)) {
|
|
|
|
|
fieldValue = fieldValue.join(', ');
|
2014-09-19 21:03:08 +08:00
|
|
|
}
|
|
|
|
|
return fieldValue;
|
|
|
|
|
};
|
|
|
|
|
|
2014-07-29 23:24:42 +08:00
|
|
|
for (var i = 0; i < hits.length; i++) {
|
|
|
|
|
var source = hits[i]._source;
|
2014-09-09 14:50:01 +08:00
|
|
|
var fields = hits[i].fields;
|
|
|
|
|
var time = source[timeField];
|
|
|
|
|
|
|
|
|
|
if (_.isString(fields[timeField]) || _.isNumber(fields[timeField])) {
|
|
|
|
|
time = fields[timeField];
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-29 23:24:42 +08:00
|
|
|
var event = {
|
|
|
|
|
annotation: annotation,
|
2014-09-09 14:50:01 +08:00
|
|
|
time: moment.utc(time).valueOf(),
|
2014-09-19 21:03:08 +08:00
|
|
|
title: getFieldFromSource(source, titleField),
|
|
|
|
|
tags: getFieldFromSource(source, tagsField),
|
|
|
|
|
text: getFieldFromSource(source, textField)
|
2014-07-29 23:24:42 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
list.push(event);
|
|
|
|
|
}
|
|
|
|
|
return list;
|
|
|
|
|
});
|
2014-07-29 17:26:05 +08:00
|
|
|
};
|
|
|
|
|
|
2015-08-28 22:07:54 +08:00
|
|
|
ElasticDatasource.prototype.testDatasource = function() {
|
2015-09-06 18:58:53 +08:00
|
|
|
return this._get('/_stats').then(function() {
|
2015-08-28 22:07:54 +08:00
|
|
|
return { status: "success", message: "Data source is working", title: "Success" };
|
2015-09-06 18:58:53 +08:00
|
|
|
}, function(err) {
|
|
|
|
|
if (err.data && err.data.error) {
|
|
|
|
|
return { status: "error", message: err.data.error, title: "Error" };
|
|
|
|
|
} else {
|
|
|
|
|
return { status: "error", message: err.status, title: "Error" };
|
|
|
|
|
}
|
2015-08-28 22:07:54 +08:00
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2015-09-17 17:21:38 +08:00
|
|
|
ElasticDatasource.prototype.getQueryHeader = function(timeFrom, timeTo) {
|
2015-09-06 22:09:42 +08:00
|
|
|
var header = {search_type: "count", "ignore_unavailable": true};
|
2015-09-17 17:21:38 +08:00
|
|
|
header.index = this.indexPattern.getIndexList(timeFrom, timeTo);
|
2015-09-06 22:09:42 +08:00
|
|
|
return angular.toJson(header);
|
|
|
|
|
};
|
|
|
|
|
|
2015-07-01 20:54:06 +08:00
|
|
|
ElasticDatasource.prototype.query = function(options) {
|
2015-09-04 15:41:23 +08:00
|
|
|
var payload = "";
|
2015-09-07 14:57:46 +08:00
|
|
|
var target;
|
2015-09-03 14:18:00 +08:00
|
|
|
var sentTargets = [];
|
2015-09-06 22:09:42 +08:00
|
|
|
|
2015-09-17 17:21:38 +08:00
|
|
|
var header = this.getQueryHeader(options.timeFrom, options.timeTo);
|
2015-09-03 14:18:00 +08:00
|
|
|
|
2015-09-07 14:57:46 +08:00
|
|
|
for (var i = 0; i < options.targets.length; i++) {
|
|
|
|
|
target = options.targets[i];
|
|
|
|
|
if (target.hide) {return;}
|
2015-09-03 14:18:00 +08:00
|
|
|
|
2015-09-17 17:21:38 +08:00
|
|
|
var esQuery = this.queryBuilder.build(target, options.timeFrom, options.timeTo);
|
2015-09-03 14:18:00 +08:00
|
|
|
payload += header + '\n';
|
2015-09-03 20:55:48 +08:00
|
|
|
payload += angular.toJson(esQuery) + '\n';
|
2015-09-03 14:18:00 +08:00
|
|
|
|
|
|
|
|
sentTargets.push(target);
|
2015-09-07 14:57:46 +08:00
|
|
|
}
|
2015-09-03 14:18:00 +08:00
|
|
|
|
|
|
|
|
payload = payload.replace(/\$interval/g, options.interval);
|
2015-09-17 17:21:38 +08:00
|
|
|
payload = payload.replace(/\$timeFrom/g, options.timeFrom);
|
|
|
|
|
payload = payload.replace(/\$timeTo/g, options.timeTo);
|
2015-09-03 14:18:00 +08:00
|
|
|
payload = payload.replace(/\$maxDataPoints/g, options.maxDataPoints);
|
|
|
|
|
payload = templateSrv.replace(payload, options.scopedVars);
|
|
|
|
|
|
2015-09-07 19:13:19 +08:00
|
|
|
return this._post('/_msearch?search_type=count', payload).then(function(res) {
|
|
|
|
|
return new ElasticResponse(sentTargets, res).getTimeSeries();
|
|
|
|
|
});
|
2015-07-01 20:54:06 +08:00
|
|
|
};
|
|
|
|
|
|
2015-09-04 15:41:23 +08:00
|
|
|
ElasticDatasource.prototype.metricFindQuery = function() {
|
2015-09-06 01:55:58 +08:00
|
|
|
return this._get('/_mapping').then(function(res) {
|
2015-09-03 17:14:25 +08:00
|
|
|
var fields = {};
|
2015-09-02 23:45:41 +08:00
|
|
|
|
2015-09-06 01:55:58 +08:00
|
|
|
for (var indexName in res) {
|
|
|
|
|
var index = res[indexName];
|
|
|
|
|
var mappings = index.mappings;
|
|
|
|
|
if (!mappings) { continue; }
|
|
|
|
|
for (var typeName in mappings) {
|
|
|
|
|
var properties = mappings[typeName].properties;
|
|
|
|
|
for (var field in properties) {
|
|
|
|
|
var prop = properties[field];
|
|
|
|
|
if (prop.type && field[0] !== '_') {
|
|
|
|
|
fields[field] = prop;
|
2015-09-03 17:14:25 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fields = _.map(_.keys(fields), function(field) {
|
|
|
|
|
return {text: field};
|
2015-09-04 15:41:23 +08:00
|
|
|
});
|
|
|
|
|
|
2015-09-03 17:14:25 +08:00
|
|
|
return fields;
|
|
|
|
|
});
|
2015-09-02 23:45:41 +08:00
|
|
|
};
|
|
|
|
|
|
2015-09-16 22:28:41 +08:00
|
|
|
ElasticDatasource.prototype.getDashboard = function(id) {
|
|
|
|
|
return this._get('/dashboard/' + id)
|
|
|
|
|
.then(function(result) {
|
|
|
|
|
return angular.fromJson(result._source.dashboard);
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
ElasticDatasource.prototype.searchDashboards = function() {
|
|
|
|
|
var query = {
|
|
|
|
|
query: { query_string: { query: '*' } },
|
|
|
|
|
size: 10000,
|
|
|
|
|
sort: ["_uid"],
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return this._post(this.index + '/dashboard/_search', query)
|
|
|
|
|
.then(function(results) {
|
|
|
|
|
if(_.isUndefined(results.hits)) {
|
|
|
|
|
return { dashboards: [], tags: [] };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var resultsHits = results.hits.hits;
|
|
|
|
|
var displayHits = { dashboards: [] };
|
|
|
|
|
|
|
|
|
|
for (var i = 0, len = resultsHits.length; i < len; i++) {
|
|
|
|
|
var hit = resultsHits[i];
|
|
|
|
|
displayHits.dashboards.push({
|
|
|
|
|
id: hit._id,
|
|
|
|
|
title: hit._source.title,
|
|
|
|
|
tags: hit._source.tags
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return displayHits;
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2015-07-01 20:54:06 +08:00
|
|
|
return ElasticDatasource;
|
2014-07-29 17:26:05 +08:00
|
|
|
});
|
|
|
|
|
});
|