es-datasource.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. define([
  2. 'angular',
  3. 'lodash',
  4. 'jquery',
  5. 'config',
  6. 'kbn',
  7. 'moment'
  8. ],
  9. function (angular, _, $, config, kbn, moment) {
  10. 'use strict';
  11. var module = angular.module('grafana.services');
  12. module.factory('ElasticDatasource', function($q, $http, templateSrv) {
  13. function ElasticDatasource(datasource) {
  14. this.type = 'elastic';
  15. this.basicAuth = datasource.basicAuth;
  16. this.url = datasource.url;
  17. this.name = datasource.name;
  18. this.index = datasource.index;
  19. this.grafanaDB = datasource.grafanaDB;
  20. this.searchMaxResults = config.search.max_results || 20;
  21. this.saveTemp = _.isUndefined(datasource.save_temp) ? true : datasource.save_temp;
  22. this.saveTempTTL = _.isUndefined(datasource.save_temp_ttl) ? '30d' : datasource.save_temp_ttl;
  23. this.annotationEditorSrc = 'app/partials/elasticsearch/annotation_editor.html';
  24. this.supportAnnotations = true;
  25. this.supportMetrics = false;
  26. }
  27. ElasticDatasource.prototype._request = function(method, url, index, data) {
  28. var options = {
  29. url: this.url + "/" + index + url,
  30. method: method,
  31. data: data
  32. };
  33. if (this.basicAuth) {
  34. options.headers = {
  35. "Authorization": "Basic " + this.basicAuth
  36. };
  37. }
  38. return $http(options);
  39. };
  40. ElasticDatasource.prototype._get = function(url) {
  41. return this._request('GET', url, this.index)
  42. .then(function(results) {
  43. return results.data;
  44. });
  45. };
  46. ElasticDatasource.prototype._post = function(url, data) {
  47. return this._request('POST', url, this.index, data)
  48. .then(function(results) {
  49. return results.data;
  50. });
  51. };
  52. ElasticDatasource.prototype.annotationQuery = function(annotation, rangeUnparsed) {
  53. var range = {};
  54. var timeField = annotation.timeField || '@timestamp';
  55. var queryString = annotation.query || '*';
  56. var tagsField = annotation.tagsField || 'tags';
  57. var titleField = annotation.titleField || 'desc';
  58. var textField = annotation.textField || null;
  59. range[annotation.timeField]= {
  60. from: rangeUnparsed.from,
  61. to: rangeUnparsed.to,
  62. };
  63. var queryInterpolated = templateSrv.replace(queryString);
  64. var filter = { "bool": { "must": [{ "range": range }] } };
  65. var query = { "bool": { "should": [{ "query_string": { "query": queryInterpolated } }] } };
  66. var data = {
  67. "fields": [timeField, "_source"],
  68. "query" : { "filtered": { "query" : query, "filter": filter } },
  69. "size": 100
  70. };
  71. return this._request('POST', '/_search', annotation.index, data).then(function(results) {
  72. var list = [];
  73. var hits = results.data.hits.hits;
  74. for (var i = 0; i < hits.length; i++) {
  75. var source = hits[i]._source;
  76. var fields = hits[i].fields;
  77. var time = source[timeField];
  78. if (_.isString(fields[timeField]) || _.isNumber(fields[timeField])) {
  79. time = fields[timeField];
  80. }
  81. var event = {
  82. annotation: annotation,
  83. time: moment.utc(time).valueOf(),
  84. title: source[titleField],
  85. };
  86. if (source[tagsField]) {
  87. if (_.isArray(source[tagsField])) {
  88. event.tags = source[tagsField].join(', ');
  89. }
  90. else {
  91. event.tags = source[tagsField];
  92. }
  93. }
  94. if (textField && source[textField]) {
  95. event.text = source[textField];
  96. }
  97. list.push(event);
  98. }
  99. return list;
  100. });
  101. };
  102. ElasticDatasource.prototype.getDashboard = function(id, isTemp) {
  103. var url = '/dashboard/' + id;
  104. if (isTemp) {
  105. url = '/temp/' + id;
  106. }
  107. return this._get(url)
  108. .then(function(result) {
  109. if (result._source && result._source.dashboard) {
  110. return angular.fromJson(result._source.dashboard);
  111. } else {
  112. return false;
  113. }
  114. }, function(data) {
  115. if(data.status === 0) {
  116. throw "Could not contact Elasticsearch. Please ensure that Elasticsearch is reachable from your browser.";
  117. } else {
  118. throw "Could not find dashboard " + id;
  119. }
  120. });
  121. };
  122. ElasticDatasource.prototype.saveDashboard = function(dashboard) {
  123. var title = dashboard.title;
  124. var temp = dashboard.temp;
  125. if (temp) { delete dashboard.temp; }
  126. var data = {
  127. user: 'guest',
  128. group: 'guest',
  129. title: title,
  130. tags: dashboard.tags,
  131. dashboard: angular.toJson(dashboard)
  132. };
  133. if (temp) {
  134. return this._saveTempDashboard(data);
  135. }
  136. else {
  137. return this._request('PUT', '/dashboard/' + encodeURIComponent(title), this.index, data)
  138. .then(function() {
  139. return { title: title, url: '/dashboard/db/' + title };
  140. }, function(err) {
  141. throw 'Failed to save to elasticsearch ' + err.data;
  142. });
  143. }
  144. };
  145. ElasticDatasource.prototype._saveTempDashboard = function(data) {
  146. return this._request('POST', '/temp/?ttl=' + this.saveTempTTL, this.index, data)
  147. .then(function(result) {
  148. var baseUrl = window.location.href.replace(window.location.hash,'');
  149. var url = baseUrl + "#dashboard/temp/" + result.data._id;
  150. return { title: data.title, url: url };
  151. }, function(err) {
  152. throw "Failed to save to temp dashboard to elasticsearch " + err.data;
  153. });
  154. };
  155. ElasticDatasource.prototype.deleteDashboard = function(id) {
  156. return this._request('DELETE', '/dashboard/' + id, this.index)
  157. .then(function(result) {
  158. return result.data._id;
  159. }, function(err) {
  160. throw err.data;
  161. });
  162. };
  163. ElasticDatasource.prototype.searchDashboards = function(queryString) {
  164. queryString = queryString.toLowerCase().replace(' and ', ' AND ');
  165. var tagsOnly = queryString.indexOf('tags!:') === 0;
  166. if (tagsOnly) {
  167. var tagsQuery = queryString.substring(6, queryString.length);
  168. queryString = 'tags:' + tagsQuery + '*';
  169. }
  170. else {
  171. if (queryString.length === 0) {
  172. queryString = 'title:';
  173. }
  174. if (queryString[queryString.length - 1] !== '*') {
  175. queryString += '*';
  176. }
  177. }
  178. var query = {
  179. query: { query_string: { query: queryString } },
  180. facets: { tags: { terms: { field: "tags", order: "term", size: 50 } } },
  181. size: this.searchMaxResults,
  182. sort: ["_uid"]
  183. };
  184. return this._post('/dashboard/_search', query)
  185. .then(function(results) {
  186. if(_.isUndefined(results.hits)) {
  187. return { dashboards: [], tags: [] };
  188. }
  189. var hits = { dashboards: [], tags: results.facets.tags.terms || [] };
  190. for (var i = 0; i < results.hits.hits.length; i++) {
  191. hits.dashboards.push({
  192. id: results.hits.hits[i]._id,
  193. title: results.hits.hits[i]._id,
  194. tags: results.hits.hits[i]._source.tags
  195. });
  196. }
  197. hits.tagsOnly = tagsOnly;
  198. return hits;
  199. });
  200. };
  201. return ElasticDatasource;
  202. });
  203. });