es-datasource.js 6.9 KB

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