2014-07-29 11:26:05 +02:00
|
|
|
define([
|
|
|
|
|
'angular',
|
|
|
|
|
'underscore',
|
|
|
|
|
'jquery',
|
|
|
|
|
'config',
|
|
|
|
|
'kbn',
|
|
|
|
|
'moment'
|
|
|
|
|
],
|
|
|
|
|
function (angular, _, $, config, kbn, moment) {
|
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
var module = angular.module('grafana.services');
|
|
|
|
|
|
|
|
|
|
module.factory('ElasticDatasource', function($q, $http) {
|
|
|
|
|
|
|
|
|
|
function ElasticDatasource(datasource) {
|
|
|
|
|
this.type = 'elastic';
|
|
|
|
|
this.basicAuth = datasource.basicAuth;
|
|
|
|
|
this.url = datasource.url;
|
|
|
|
|
this.name = datasource.name;
|
|
|
|
|
this.supportAnnotations = true;
|
2014-07-30 08:34:58 +02:00
|
|
|
this.supportMetrics = false;
|
2014-07-29 17:24:42 +02:00
|
|
|
this.index = datasource.index;
|
2014-07-30 10:52:02 +02:00
|
|
|
this.grafanaDB = datasource.grafanaDB;
|
2014-07-29 11:26:05 +02:00
|
|
|
this.annotationEditorSrc = 'app/partials/elasticsearch/annotation_editor.html';
|
2014-07-31 09:17:37 +02:00
|
|
|
this.searchMaxResults = config.search.max_results || 20;
|
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-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-07-29 11:26:05 +02:00
|
|
|
ElasticDatasource.prototype.annotationQuery = function(annotation, filterSrv, 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;
|
|
|
|
|
|
|
|
|
|
range[annotation.timeField]= {
|
|
|
|
|
from: rangeUnparsed.from,
|
|
|
|
|
to: rangeUnparsed.to,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var filter = { "bool": { "must": [{ "range": range }] } };
|
|
|
|
|
var query = { "bool": { "should": [{ "query_string": { "query": queryString } }] } };
|
|
|
|
|
var data = { "query" : { "filtered": { "query" : query, "filter": filter } }, "size": 100 };
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
for (var i = 0; i < hits.length; i++) {
|
|
|
|
|
var source = hits[i]._source;
|
|
|
|
|
var event = {
|
|
|
|
|
annotation: annotation,
|
|
|
|
|
time: moment.utc(source[timeField]).valueOf(),
|
|
|
|
|
title: source[titleField],
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (source[tagsField]) {
|
|
|
|
|
if (_.isArray(source[tagsField])) {
|
|
|
|
|
event.tags = source[tagsField].join(', ');
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
event.tags = source[tagsField];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (textField && source[textField]) {
|
|
|
|
|
event.text = source[textField];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
list.push(event);
|
|
|
|
|
}
|
|
|
|
|
return list;
|
|
|
|
|
});
|
2014-07-29 11:26:05 +02:00
|
|
|
};
|
|
|
|
|
|
2014-07-30 10:52:02 +02:00
|
|
|
ElasticDatasource.prototype.getDashboard = function(id) {
|
|
|
|
|
var url = '/dashboard/' + id;
|
|
|
|
|
|
|
|
|
|
// hack to check if it is a temp dashboard
|
|
|
|
|
if (window.location.href.indexOf('dashboard/temp') > 0) {
|
|
|
|
|
url = '/temp/' + id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return this._get(url)
|
|
|
|
|
.then(function(result) {
|
|
|
|
|
if (result._source && result._source.dashboard) {
|
|
|
|
|
return angular.fromJson(result._source.dashboard);
|
|
|
|
|
} else {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
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 {
|
|
|
|
|
throw "Could not find dashboard " + id;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
ElasticDatasource.prototype.saveDashboard = function(dashboard, title) {
|
|
|
|
|
var dashboardClone = angular.copy(dashboard);
|
|
|
|
|
title = dashboardClone.title = title ? title : dashboard.title;
|
|
|
|
|
|
|
|
|
|
var data = {
|
|
|
|
|
user: 'guest',
|
|
|
|
|
group: 'guest',
|
|
|
|
|
title: title,
|
|
|
|
|
tags: dashboardClone.tags,
|
|
|
|
|
dashboard: angular.toJson(dashboardClone)
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return this._request('PUT', '/dashboard/' + encodeURIComponent(title), this.index, data)
|
|
|
|
|
.then(function() {
|
2014-07-31 11:05:47 +02:00
|
|
|
return { title: title, url: '/dashboard/db/' + title };
|
2014-07-30 10:52:02 +02:00
|
|
|
}, function(err) {
|
|
|
|
|
throw 'Failed to save to elasticsearch ' + err.data;
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
ElasticDatasource.prototype.saveDashboardTemp = function(dashboard) {
|
|
|
|
|
var data = {
|
|
|
|
|
user: 'guest',
|
|
|
|
|
group: 'guest',
|
|
|
|
|
title: dashboard.title,
|
|
|
|
|
tags: dashboard.tags,
|
|
|
|
|
dashboard: angular.toJson(dashboard)
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var ttl = dashboard.loader.save_temp_ttl;
|
|
|
|
|
|
|
|
|
|
return this._request('POST', '/temp/?ttl=' + ttl, this.index, data)
|
|
|
|
|
.then(function(result) {
|
|
|
|
|
|
|
|
|
|
var baseUrl = window.location.href.replace(window.location.hash,'');
|
|
|
|
|
var url = baseUrl + "#dashboard/temp/" + result.data._id;
|
|
|
|
|
|
|
|
|
|
return { title: dashboard.title, url: url };
|
|
|
|
|
|
|
|
|
|
}, 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-07-31 10:36:45 +02:00
|
|
|
queryString = queryString.toLowerCase().replace(' and ', ' AND ');
|
|
|
|
|
|
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:';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (queryString[queryString.length - 1] !== '*') {
|
|
|
|
|
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-07-31 10:54:36 +02:00
|
|
|
var hits = { dashboards: [], tags: results.facets.tags.terms || [] };
|
|
|
|
|
|
|
|
|
|
for (var i = 0; i < results.hits.hits.length; i++) {
|
|
|
|
|
hits.dashboards.push({
|
|
|
|
|
id: results.hits.hits[i]._id,
|
|
|
|
|
tags: results.hits.hits[i]._source.tags
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
hits.tagsOnly = tagsOnly;
|
|
|
|
|
return hits;
|
2014-07-30 10:52:02 +02:00
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2014-07-29 11:26:05 +02:00
|
|
|
return ElasticDatasource;
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
});
|