es-datasource.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. define([
  2. 'angular',
  3. 'underscore',
  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) {
  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, filterSrv, 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 filter = { "bool": { "must": [{ "range": range }] } };
  64. var query = { "bool": { "should": [{ "query_string": { "query": queryString } }] } };
  65. var data = { "query" : { "filtered": { "query" : query, "filter": filter } }, "size": 100 };
  66. return this._request('POST', '/_search', annotation.index, data).then(function(results) {
  67. var list = [];
  68. var hits = results.data.hits.hits;
  69. for (var i = 0; i < hits.length; i++) {
  70. var source = hits[i]._source;
  71. var event = {
  72. annotation: annotation,
  73. time: moment.utc(source[timeField]).valueOf(),
  74. title: source[titleField],
  75. };
  76. if (source[tagsField]) {
  77. if (_.isArray(source[tagsField])) {
  78. event.tags = source[tagsField].join(', ');
  79. }
  80. else {
  81. event.tags = source[tagsField];
  82. }
  83. }
  84. if (textField && source[textField]) {
  85. event.text = source[textField];
  86. }
  87. list.push(event);
  88. }
  89. return list;
  90. });
  91. };
  92. ElasticDatasource.prototype.getDashboard = function(id, isTemp) {
  93. var url = '/dashboard/' + id;
  94. if (isTemp) {
  95. url = '/temp/' + id;
  96. }
  97. return this._get(url)
  98. .then(function(result) {
  99. if (result._source && result._source.dashboard) {
  100. return angular.fromJson(result._source.dashboard);
  101. } else {
  102. return false;
  103. }
  104. }, function(data) {
  105. if(data.status === 0) {
  106. throw "Could not contact Elasticsearch. Please ensure that Elasticsearch is reachable from your browser.";
  107. } else {
  108. throw "Could not find dashboard " + id;
  109. }
  110. });
  111. };
  112. ElasticDatasource.prototype.saveDashboard = function(dashboard) {
  113. var title = dashboard.title;
  114. var temp = dashboard.temp;
  115. if (temp) { delete dashboard.temp; }
  116. var data = {
  117. user: 'guest',
  118. group: 'guest',
  119. title: title,
  120. tags: dashboard.tags,
  121. dashboard: angular.toJson(dashboard)
  122. };
  123. if (temp) {
  124. return this._saveTempDashboard(data);
  125. }
  126. else {
  127. return this._request('PUT', '/dashboard/' + encodeURIComponent(title), this.index, data)
  128. .then(function() {
  129. return { title: title, url: '/dashboard/db/' + title };
  130. }, function(err) {
  131. throw 'Failed to save to elasticsearch ' + err.data;
  132. });
  133. }
  134. };
  135. ElasticDatasource.prototype._saveTempDashboard = function(data) {
  136. return this._request('POST', '/temp/?ttl=' + this.saveTempTTL, this.index, data)
  137. .then(function(result) {
  138. var baseUrl = window.location.href.replace(window.location.hash,'');
  139. var url = baseUrl + "#dashboard/temp/" + result.data._id;
  140. return { title: data.title, url: url };
  141. }, function(err) {
  142. throw "Failed to save to temp dashboard to elasticsearch " + err.data;
  143. });
  144. };
  145. ElasticDatasource.prototype.deleteDashboard = function(id) {
  146. return this._request('DELETE', '/dashboard/' + id, this.index)
  147. .then(function(result) {
  148. return result.data._id;
  149. }, function(err) {
  150. throw err.data;
  151. });
  152. };
  153. ElasticDatasource.prototype.searchDashboards = function(queryString) {
  154. queryString = queryString.toLowerCase().replace(' and ', ' AND ');
  155. var tagsOnly = queryString.indexOf('tags!:') === 0;
  156. if (tagsOnly) {
  157. var tagsQuery = queryString.substring(6, queryString.length);
  158. queryString = 'tags:' + tagsQuery + '*';
  159. }
  160. else {
  161. if (queryString.length === 0) {
  162. queryString = 'title:';
  163. }
  164. if (queryString[queryString.length - 1] !== '*') {
  165. queryString += '*';
  166. }
  167. }
  168. var query = {
  169. query: { query_string: { query: queryString } },
  170. facets: { tags: { terms: { field: "tags", order: "term", size: 50 } } },
  171. size: this.searchMaxResults,
  172. sort: ["_uid"]
  173. };
  174. return this._post('/dashboard/_search', query)
  175. .then(function(results) {
  176. if(_.isUndefined(results.hits)) {
  177. return { dashboards: [], tags: [] };
  178. }
  179. var hits = { dashboards: [], tags: results.facets.tags.terms || [] };
  180. for (var i = 0; i < results.hits.hits.length; i++) {
  181. hits.dashboards.push({
  182. id: results.hits.hits[i]._id,
  183. tags: results.hits.hits[i]._source.tags
  184. });
  185. }
  186. hits.tagsOnly = tagsOnly;
  187. return hits;
  188. });
  189. };
  190. return ElasticDatasource;
  191. });
  192. });