2014-07-29 11:26:05 +02:00
|
|
|
define([
|
|
|
|
|
'angular',
|
2014-08-07 14:35:19 +02:00
|
|
|
'lodash',
|
2014-07-29 11:26:05 +02:00
|
|
|
'config',
|
|
|
|
|
'kbn',
|
|
|
|
|
'moment'
|
|
|
|
|
],
|
2014-09-09 13:04:07 +02:00
|
|
|
function (angular, _, config, kbn, moment) {
|
2014-07-29 11:26:05 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
var module = angular.module('grafana.services');
|
|
|
|
|
|
2014-08-28 16:03:13 +02:00
|
|
|
module.factory('ElasticDatasource', function($q, $http, templateSrv) {
|
2014-07-29 11:26:05 +02:00
|
|
|
|
|
|
|
|
function ElasticDatasource(datasource) {
|
2015-01-17 10:39:01 +01:00
|
|
|
this.type = 'elasticsearch';
|
2014-07-29 11:26:05 +02:00
|
|
|
this.basicAuth = datasource.basicAuth;
|
|
|
|
|
this.url = datasource.url;
|
|
|
|
|
this.name = datasource.name;
|
2014-07-29 17:24:42 +02:00
|
|
|
this.index = datasource.index;
|
2014-07-31 09:17:37 +02:00
|
|
|
this.searchMaxResults = config.search.max_results || 20;
|
2014-08-03 12:07:50 +02:00
|
|
|
|
|
|
|
|
this.saveTemp = _.isUndefined(datasource.save_temp) ? true : datasource.save_temp;
|
|
|
|
|
this.saveTempTTL = _.isUndefined(datasource.save_temp_ttl) ? '30d' : datasource.save_temp_ttl;
|
2014-07-29 11:26:05 +02:00
|
|
|
}
|
|
|
|
|
|
2014-07-30 10:52:02 +02:00
|
|
|
ElasticDatasource.prototype._request = function(method, url, index, data) {
|
2014-07-29 17:24:42 +02:00
|
|
|
var options = {
|
2014-07-30 10:52:02 +02:00
|
|
|
url: this.url + "/" + index + url,
|
2014-07-29 17:24:42 +02:00
|
|
|
method: method,
|
|
|
|
|
data: data
|
|
|
|
|
};
|
2014-07-30 11:34:09 +02:00
|
|
|
|
|
|
|
|
if (this.basicAuth) {
|
2014-09-18 17:50:59 +06:00
|
|
|
options.withCredentials = true;
|
2014-07-29 17:24:42 +02:00
|
|
|
options.headers = {
|
2014-07-30 11:34:09 +02:00
|
|
|
"Authorization": "Basic " + this.basicAuth
|
2014-07-29 17:24:42 +02:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $http(options);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
ElasticDatasource.prototype._get = function(url) {
|
2014-07-30 10:52:02 +02:00
|
|
|
return this._request('GET', url, this.index)
|
2014-07-29 17:24:42 +02:00
|
|
|
.then(function(results) {
|
|
|
|
|
return results.data;
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
ElasticDatasource.prototype._post = function(url, data) {
|
2014-07-30 10:52:02 +02:00
|
|
|
return this._request('POST', url, this.index, data)
|
2014-07-29 17:24:42 +02:00
|
|
|
.then(function(results) {
|
|
|
|
|
return results.data;
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2014-08-28 16:03:13 +02:00
|
|
|
ElasticDatasource.prototype.annotationQuery = function(annotation, rangeUnparsed) {
|
2014-07-29 17:24:42 +02: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 13:34:12 +01:00
|
|
|
range[timeField]= {
|
2014-07-29 17:24:42 +02:00
|
|
|
from: rangeUnparsed.from,
|
|
|
|
|
to: rangeUnparsed.to,
|
|
|
|
|
};
|
|
|
|
|
|
2014-08-28 16:03:13 +02:00
|
|
|
var queryInterpolated = templateSrv.replace(queryString);
|
2014-07-29 17:24:42 +02:00
|
|
|
var filter = { "bool": { "must": [{ "range": range }] } };
|
2014-08-08 07:19:03 +02:00
|
|
|
var query = { "bool": { "should": [{ "query_string": { "query": queryInterpolated } }] } };
|
2014-09-09 08:50:01 +02:00
|
|
|
var data = {
|
|
|
|
|
"fields": [timeField, "_source"],
|
|
|
|
|
"query" : { "filtered": { "query" : query, "filter": filter } },
|
|
|
|
|
"size": 100
|
|
|
|
|
};
|
2014-07-29 17:24:42 +02:00
|
|
|
|
2014-07-30 10:52:02 +02:00
|
|
|
return this._request('POST', '/_search', annotation.index, data).then(function(results) {
|
2014-07-29 17:24:42 +02:00
|
|
|
var list = [];
|
|
|
|
|
var hits = results.data.hits.hits;
|
|
|
|
|
|
2014-09-19 14:03:08 +01:00
|
|
|
var getFieldFromSource = function(source, fieldName) {
|
2014-09-20 08:30:59 +02: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 09:03:04 +02:00
|
|
|
if (!fieldValue) {
|
|
|
|
|
console.log('could not find field in annotatation: ', fieldName);
|
|
|
|
|
return '';
|
|
|
|
|
}
|
2014-09-20 08:30:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (_.isArray(fieldValue)) {
|
|
|
|
|
fieldValue = fieldValue.join(', ');
|
2014-09-19 14:03:08 +01:00
|
|
|
}
|
|
|
|
|
return fieldValue;
|
|
|
|
|
};
|
|
|
|
|
|
2014-07-29 17:24:42 +02:00
|
|
|
for (var i = 0; i < hits.length; i++) {
|
|
|
|
|
var source = hits[i]._source;
|
2014-09-09 08:50:01 +02:00
|
|
|
var fields = hits[i].fields;
|
|
|
|
|
var time = source[timeField];
|
|
|
|
|
|
|
|
|
|
if (_.isString(fields[timeField]) || _.isNumber(fields[timeField])) {
|
|
|
|
|
time = fields[timeField];
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-29 17:24:42 +02:00
|
|
|
var event = {
|
|
|
|
|
annotation: annotation,
|
2014-09-09 08:50:01 +02:00
|
|
|
time: moment.utc(time).valueOf(),
|
2014-09-19 14:03:08 +01:00
|
|
|
title: getFieldFromSource(source, titleField),
|
|
|
|
|
tags: getFieldFromSource(source, tagsField),
|
|
|
|
|
text: getFieldFromSource(source, textField)
|
2014-07-29 17:24:42 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
list.push(event);
|
|
|
|
|
}
|
|
|
|
|
return list;
|
|
|
|
|
});
|
2014-07-29 11:26:05 +02:00
|
|
|
};
|
|
|
|
|
|
2014-09-09 13:04:07 +02:00
|
|
|
ElasticDatasource.prototype._getDashboardWithSlug = function(id) {
|
|
|
|
|
return this._get('/dashboard/' + kbn.slugifyForUrl(id))
|
|
|
|
|
.then(function(result) {
|
|
|
|
|
return angular.fromJson(result._source.dashboard);
|
|
|
|
|
}, function() {
|
|
|
|
|
throw "Dashboard not found";
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2014-08-03 12:07:50 +02:00
|
|
|
ElasticDatasource.prototype.getDashboard = function(id, isTemp) {
|
2014-07-30 10:52:02 +02:00
|
|
|
var url = '/dashboard/' + id;
|
2014-09-09 13:04:07 +02:00
|
|
|
if (isTemp) { url = '/temp/' + id; }
|
2014-07-30 10:52:02 +02:00
|
|
|
|
2014-09-09 13:04:07 +02:00
|
|
|
var self = this;
|
2014-07-30 10:52:02 +02:00
|
|
|
return this._get(url)
|
|
|
|
|
.then(function(result) {
|
2014-09-09 13:04:07 +02:00
|
|
|
return angular.fromJson(result._source.dashboard);
|
2014-07-30 11:34:09 +02:00
|
|
|
}, function(data) {
|
|
|
|
|
if(data.status === 0) {
|
2014-07-30 10:52:02 +02:00
|
|
|
throw "Could not contact Elasticsearch. Please ensure that Elasticsearch is reachable from your browser.";
|
|
|
|
|
} else {
|
2014-09-09 13:04:07 +02:00
|
|
|
// backward compatible fallback
|
|
|
|
|
return self._getDashboardWithSlug(id);
|
2014-07-30 10:52:02 +02:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2014-08-03 12:07:50 +02:00
|
|
|
ElasticDatasource.prototype.saveDashboard = function(dashboard) {
|
|
|
|
|
var title = dashboard.title;
|
|
|
|
|
var temp = dashboard.temp;
|
|
|
|
|
if (temp) { delete dashboard.temp; }
|
2014-07-30 10:52:02 +02:00
|
|
|
|
|
|
|
|
var data = {
|
|
|
|
|
user: 'guest',
|
|
|
|
|
group: 'guest',
|
|
|
|
|
title: title,
|
|
|
|
|
tags: dashboard.tags,
|
|
|
|
|
dashboard: angular.toJson(dashboard)
|
|
|
|
|
};
|
|
|
|
|
|
2014-08-03 12:07:50 +02:00
|
|
|
if (temp) {
|
|
|
|
|
return this._saveTempDashboard(data);
|
|
|
|
|
}
|
|
|
|
|
else {
|
2014-09-09 13:04:07 +02:00
|
|
|
|
|
|
|
|
var id = encodeURIComponent(kbn.slugifyForUrl(title));
|
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
|
|
return this._request('PUT', '/dashboard/' + id, this.index, data)
|
|
|
|
|
.then(function(results) {
|
2014-09-19 10:37:28 +02:00
|
|
|
self._removeUnslugifiedDashboard(results, title, id);
|
2014-09-09 13:04:07 +02:00
|
|
|
return { title: title, url: '/dashboard/db/' + id };
|
2014-09-11 16:00:59 +02:00
|
|
|
}, function() {
|
|
|
|
|
throw 'Failed to save to elasticsearch';
|
2014-08-03 12:07:50 +02:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
};
|
2014-07-30 10:52:02 +02:00
|
|
|
|
2014-09-19 10:37:28 +02:00
|
|
|
ElasticDatasource.prototype._removeUnslugifiedDashboard = function(saveResult, title, id) {
|
2014-09-09 13:04:07 +02:00
|
|
|
if (saveResult.statusText !== 'Created') { return; }
|
2014-09-19 10:37:28 +02:00
|
|
|
if (title === id) { return; }
|
2014-09-09 13:04:07 +02:00
|
|
|
|
|
|
|
|
var self = this;
|
|
|
|
|
this._get('/dashboard/' + title).then(function() {
|
|
|
|
|
self.deleteDashboard(title);
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2014-08-03 12:07:50 +02:00
|
|
|
ElasticDatasource.prototype._saveTempDashboard = function(data) {
|
|
|
|
|
return this._request('POST', '/temp/?ttl=' + this.saveTempTTL, this.index, data)
|
2014-07-30 10:52:02 +02:00
|
|
|
.then(function(result) {
|
|
|
|
|
|
|
|
|
|
var baseUrl = window.location.href.replace(window.location.hash,'');
|
|
|
|
|
var url = baseUrl + "#dashboard/temp/" + result.data._id;
|
|
|
|
|
|
2014-08-03 12:07:50 +02:00
|
|
|
return { title: data.title, url: url };
|
2014-07-30 10:52:02 +02:00
|
|
|
|
|
|
|
|
}, function(err) {
|
|
|
|
|
throw "Failed to save to temp dashboard to elasticsearch " + err.data;
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
ElasticDatasource.prototype.deleteDashboard = function(id) {
|
|
|
|
|
return this._request('DELETE', '/dashboard/' + id, this.index)
|
|
|
|
|
.then(function(result) {
|
|
|
|
|
return result.data._id;
|
|
|
|
|
}, function(err) {
|
|
|
|
|
throw err.data;
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
ElasticDatasource.prototype.searchDashboards = function(queryString) {
|
2014-09-09 15:17:13 -04:00
|
|
|
var endsInOpen = function(string, opener, closer) {
|
|
|
|
|
var character;
|
|
|
|
|
var count = 0;
|
2014-10-05 23:24:41 +01:00
|
|
|
for (var i = 0, len = string.length; i < len; i++) {
|
2014-09-09 15:17:13 -04:00
|
|
|
character = string[i];
|
|
|
|
|
|
2014-09-09 15:54:05 -04:00
|
|
|
if (character === opener) {
|
2014-09-09 15:17:13 -04:00
|
|
|
count++;
|
2014-09-09 15:54:05 -04:00
|
|
|
} else if (character === closer) {
|
2014-09-09 15:17:13 -04:00
|
|
|
count--;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return count > 0;
|
2014-09-09 15:54:05 -04:00
|
|
|
};
|
2014-07-31 10:36:45 +02:00
|
|
|
|
2014-07-30 10:52:02 +02:00
|
|
|
var tagsOnly = queryString.indexOf('tags!:') === 0;
|
|
|
|
|
if (tagsOnly) {
|
|
|
|
|
var tagsQuery = queryString.substring(6, queryString.length);
|
|
|
|
|
queryString = 'tags:' + tagsQuery + '*';
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
if (queryString.length === 0) {
|
|
|
|
|
queryString = 'title:';
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-09 15:17:13 -04:00
|
|
|
// make this a partial search if we're not in some reserved portion of the language, comments on conditionals, in order:
|
|
|
|
|
// 1. ends in reserved character, boosting, boolean operator ( -foo)
|
|
|
|
|
// 2. typing a reserved word like AND, OR, NOT
|
|
|
|
|
// 3. open parens (groupiing)
|
|
|
|
|
// 4. open " (term phrase)
|
|
|
|
|
// 5. open [ (range)
|
|
|
|
|
// 6. open { (range)
|
|
|
|
|
// see http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html#query-string-syntax
|
|
|
|
|
if (!queryString.match(/(\*|\]|}|~|\)|"|^\d+|\s[\-+]\w+)$/) &&
|
|
|
|
|
!queryString.match(/[A-Z]$/) &&
|
|
|
|
|
!endsInOpen(queryString, '(', ')') &&
|
|
|
|
|
!endsInOpen(queryString, '"', '"') &&
|
|
|
|
|
!endsInOpen(queryString, '[', ']') && !endsInOpen(queryString, '[', '}') &&
|
|
|
|
|
!endsInOpen(queryString, '{', ']') && !endsInOpen(queryString, '{', '}')
|
|
|
|
|
){
|
2014-07-30 10:52:02 +02:00
|
|
|
queryString += '*';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var query = {
|
|
|
|
|
query: { query_string: { query: queryString } },
|
|
|
|
|
facets: { tags: { terms: { field: "tags", order: "term", size: 50 } } },
|
2014-07-31 09:17:37 +02:00
|
|
|
size: this.searchMaxResults,
|
2014-07-30 10:52:02 +02:00
|
|
|
sort: ["_uid"]
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return this._post('/dashboard/_search', query)
|
|
|
|
|
.then(function(results) {
|
|
|
|
|
if(_.isUndefined(results.hits)) {
|
|
|
|
|
return { dashboards: [], tags: [] };
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-09 12:31:51 -04:00
|
|
|
var resultsHits = results.hits.hits;
|
2014-10-05 23:24:41 +01:00
|
|
|
var displayHits = { dashboards: [], tags: results.facets.tags.terms || [] };
|
2014-07-31 10:54:36 +02:00
|
|
|
|
2014-10-09 12:31:51 -04:00
|
|
|
for (var i = 0, len = resultsHits.length; i < len; i++) {
|
|
|
|
|
var hit = resultsHits[i];
|
2014-10-05 23:24:41 +01:00
|
|
|
displayHits.dashboards.push({
|
|
|
|
|
id: hit._id,
|
|
|
|
|
title: hit._source.title,
|
|
|
|
|
tags: hit._source.tags
|
2014-07-31 10:54:36 +02:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-05 23:24:41 +01:00
|
|
|
displayHits.tagsOnly = tagsOnly;
|
|
|
|
|
return displayHits;
|
2014-07-30 10:52:02 +02:00
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2014-07-29 11:26:05 +02:00
|
|
|
return ElasticDatasource;
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
});
|