influxdbDatasource.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. define([
  2. 'angular',
  3. 'lodash',
  4. 'kbn',
  5. './influxSeries'
  6. ],
  7. function (angular, _, kbn, InfluxSeries) {
  8. 'use strict';
  9. var module = angular.module('grafana.services');
  10. module.factory('InfluxDatasource', function($q, $http) {
  11. function InfluxDatasource(datasource) {
  12. this.type = 'influxDB';
  13. this.editorSrc = 'app/partials/influxdb/editor.html';
  14. this.urls = datasource.urls;
  15. this.username = datasource.username;
  16. this.password = datasource.password;
  17. this.name = datasource.name;
  18. this.templateSettings = {
  19. interpolate : /\[\[([\s\S]+?)\]\]/g,
  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.grafanaDB = datasource.grafanaDB;
  24. this.supportAnnotations = true;
  25. this.supportMetrics = true;
  26. this.annotationEditorSrc = 'app/partials/influxdb/annotation_editor.html';
  27. }
  28. InfluxDatasource.prototype.query = function(filterSrv, options) {
  29. var promises = _.map(options.targets, function(target) {
  30. var query;
  31. var alias = '';
  32. if (target.hide || !((target.series && target.column) || target.query)) {
  33. return [];
  34. }
  35. var timeFilter = getTimeFilter(options);
  36. var groupByField;
  37. if (target.rawQuery) {
  38. query = target.query;
  39. query = query.replace(";", "");
  40. var queryElements = query.split(" ");
  41. var lowerCaseQueryElements = query.toLowerCase().split(" ");
  42. var whereIndex = lowerCaseQueryElements.indexOf("where");
  43. var groupByIndex = lowerCaseQueryElements.indexOf("group");
  44. var orderIndex = lowerCaseQueryElements.indexOf("order");
  45. if (lowerCaseQueryElements[1].indexOf(',') !== -1) {
  46. groupByField = lowerCaseQueryElements[1].replace(',', '');
  47. }
  48. if (whereIndex !== -1) {
  49. queryElements.splice(whereIndex + 1, 0, timeFilter, "and");
  50. }
  51. else {
  52. if (groupByIndex !== -1) {
  53. queryElements.splice(groupByIndex, 0, "where", timeFilter);
  54. }
  55. else if (orderIndex !== -1) {
  56. queryElements.splice(orderIndex, 0, "where", timeFilter);
  57. }
  58. else {
  59. queryElements.push("where");
  60. queryElements.push(timeFilter);
  61. }
  62. }
  63. query = queryElements.join(" ");
  64. query = filterSrv.applyTemplateToTarget(query);
  65. }
  66. else {
  67. var template = "select [[group]][[group_comma]] [[func]]([[column]]) from [[series]] " +
  68. "where [[timeFilter]] [[condition_add]] [[condition_key]] [[condition_op]] [[condition_value]] " +
  69. "group by time([[interval]])[[group_comma]] [[group]] order asc";
  70. var templateData = {
  71. series: target.series,
  72. column: target.column,
  73. func: target.function,
  74. timeFilter: timeFilter,
  75. interval: target.interval || options.interval,
  76. condition_add: target.condition_filter ? 'and' : '',
  77. condition_key: target.condition_filter ? target.condition_key : '',
  78. condition_op: target.condition_filter ? target.condition_op : '',
  79. condition_value: target.condition_filter ? target.condition_value : '',
  80. group_comma: target.groupby_field_add && target.groupby_field ? ',' : '',
  81. group: target.groupby_field_add ? target.groupby_field : '',
  82. };
  83. if(!templateData.series.match('^/.*/')) {
  84. templateData.series = '"' + templateData.series + '"';
  85. }
  86. query = _.template(template, templateData, this.templateSettings);
  87. query = filterSrv.applyTemplateToTarget(query);
  88. if (target.groupby_field_add) {
  89. groupByField = target.groupby_field;
  90. }
  91. target.query = query;
  92. }
  93. if (target.alias) {
  94. alias = filterSrv.applyTemplateToTarget(target.alias);
  95. }
  96. var handleResponse = _.partial(handleInfluxQueryResponse, alias, groupByField);
  97. return this._seriesQuery(query).then(handleResponse);
  98. }, this);
  99. return $q.all(promises).then(function(results) {
  100. return { data: _.flatten(results) };
  101. });
  102. };
  103. InfluxDatasource.prototype.annotationQuery = function(annotation, filterSrv, rangeUnparsed) {
  104. var timeFilter = getTimeFilter({ range: rangeUnparsed });
  105. var query = _.template(annotation.query, { timeFilter: timeFilter }, this.templateSettings);
  106. return this._seriesQuery(query).then(function(results) {
  107. return new InfluxSeries({ seriesList: results, annotation: annotation }).getAnnotations();
  108. });
  109. };
  110. InfluxDatasource.prototype.listColumns = function(seriesName) {
  111. return this._seriesQuery('select * from /' + seriesName + '/ limit 1').then(function(data) {
  112. if (!data) {
  113. return [];
  114. }
  115. return data[0].columns;
  116. });
  117. };
  118. InfluxDatasource.prototype.listSeries = function() {
  119. return this._seriesQuery('list series').then(function(data) {
  120. if (!data || data.length === 0) {
  121. return [];
  122. }
  123. // influxdb >= 1.8
  124. if (data[0].points.length > 0) {
  125. return _.map(data[0].points, function(point) {
  126. return point[1];
  127. });
  128. }
  129. else { // influxdb <= 1.7
  130. return _.map(data, function(series) {
  131. return series.name; // influxdb < 1.7
  132. });
  133. }
  134. });
  135. };
  136. InfluxDatasource.prototype.metricFindQuery = function (filterSrv, query) {
  137. var interpolated;
  138. try {
  139. interpolated = filterSrv.applyTemplateToTarget(query);
  140. }
  141. catch (err) {
  142. return $q.reject(err);
  143. }
  144. return this._seriesQuery(interpolated)
  145. .then(function (results) {
  146. return _.map(results[0].points, function (metric) {
  147. return {
  148. text: metric[1],
  149. expandable: false
  150. };
  151. });
  152. });
  153. };
  154. function retry(deferred, callback, delay) {
  155. return callback().then(undefined, function(reason) {
  156. if (reason.status !== 0 || reason.status >= 300) {
  157. reason.message = 'InfluxDB Error: <br/>' + reason.data;
  158. deferred.reject(reason);
  159. }
  160. else {
  161. setTimeout(function() {
  162. return retry(deferred, callback, Math.min(delay * 2, 30000));
  163. }, delay);
  164. }
  165. });
  166. }
  167. InfluxDatasource.prototype._seriesQuery = function(query) {
  168. return this._influxRequest('GET', '/series', {
  169. q: query,
  170. time_precision: 's',
  171. });
  172. };
  173. InfluxDatasource.prototype._influxRequest = function(method, url, data) {
  174. var _this = this;
  175. var deferred = $q.defer();
  176. retry(deferred, function() {
  177. var currentUrl = _this.urls.shift();
  178. _this.urls.push(currentUrl);
  179. var params = {
  180. u: _this.username,
  181. p: _this.password,
  182. };
  183. if (method === 'GET') {
  184. _.extend(params, data);
  185. data = null;
  186. }
  187. var options = {
  188. method: method,
  189. url: currentUrl + url,
  190. params: params,
  191. data: data,
  192. inspect: { type: 'influxdb' },
  193. };
  194. return $http(options).success(function (data) {
  195. deferred.resolve(data);
  196. });
  197. }, 10);
  198. return deferred.promise;
  199. };
  200. InfluxDatasource.prototype.saveDashboard = function(dashboard) {
  201. var tags = dashboard.tags.join(',');
  202. var title = dashboard.title;
  203. var temp = dashboard.temp;
  204. if (temp) { delete dashboard.temp; }
  205. var data = [{
  206. name: 'grafana.dashboard_' + btoa(title),
  207. columns: ['time', 'sequence_number', 'title', 'tags', 'dashboard'],
  208. points: [[1000000000000, 1, title, tags, angular.toJson(dashboard)]]
  209. }];
  210. if (temp) {
  211. return this._saveDashboardTemp(data, title);
  212. }
  213. else {
  214. return this._influxRequest('POST', '/series', data).then(function() {
  215. return { title: title, url: '/dashboard/db/' + title };
  216. }, function(err) {
  217. throw 'Failed to save dashboard to InfluxDB: ' + err.data;
  218. });
  219. }
  220. };
  221. InfluxDatasource.prototype._saveDashboardTemp = function(data, title) {
  222. data[0].name = 'grafana.temp_dashboard_' + btoa(title);
  223. data[0].columns.push('expires');
  224. data[0].points[0].push(this._getTempDashboardExpiresDate());
  225. return this._influxRequest('POST', '/series', data).then(function() {
  226. var baseUrl = window.location.href.replace(window.location.hash,'');
  227. var url = baseUrl + "#dashboard/temp/" + title;
  228. return { title: title, url: url };
  229. }, function(err) {
  230. throw 'Failed to save shared dashboard to InfluxDB: ' + err.data;
  231. });
  232. };
  233. InfluxDatasource.prototype._getTempDashboardExpiresDate = function() {
  234. var ttlLength = this.saveTempTTL.substring(0, this.saveTempTTL.length - 1);
  235. var ttlTerm = this.saveTempTTL.substring(this.saveTempTTL.length - 1, this.saveTempTTL.length).toLowerCase();
  236. var expires = Date.now();
  237. switch(ttlTerm) {
  238. case "m":
  239. expires += ttlLength * 60000;
  240. break;
  241. case "d":
  242. expires += ttlLength * 86400000;
  243. break;
  244. case "w":
  245. expires += ttlLength * 604800000;
  246. break;
  247. default:
  248. throw "Unknown ttl duration format";
  249. }
  250. return expires;
  251. };
  252. InfluxDatasource.prototype.getDashboard = function(id, isTemp) {
  253. var queryString = 'select dashboard from "grafana.dashboard_' + btoa(id) + '"';
  254. if (isTemp) {
  255. queryString = 'select dashboard from "grafana.temp_dashboard_' + btoa(id) + '"';
  256. }
  257. return this._seriesQuery(queryString).then(function(results) {
  258. if (!results || !results.length) {
  259. throw "Dashboard not found";
  260. }
  261. var dashCol = _.indexOf(results[0].columns, 'dashboard');
  262. var dashJson = results[0].points[0][dashCol];
  263. return angular.fromJson(dashJson);
  264. }, function(err) {
  265. return "Could not load dashboard, " + err.data;
  266. });
  267. };
  268. InfluxDatasource.prototype.deleteDashboard = function(id) {
  269. return this._seriesQuery('drop series "grafana.dashboard_' + btoa(id) + '"').then(function(results) {
  270. if (!results) {
  271. throw "Could not delete dashboard";
  272. }
  273. return id;
  274. }, function(err) {
  275. return "Could not delete dashboard, " + err.data;
  276. });
  277. };
  278. InfluxDatasource.prototype.searchDashboards = function(queryString) {
  279. var influxQuery = 'select title, tags from /grafana.dashboard_.*/ where ';
  280. var tagsOnly = queryString.indexOf('tags!:') === 0;
  281. if (tagsOnly) {
  282. var tagsQuery = queryString.substring(6, queryString.length);
  283. influxQuery = influxQuery + 'tags =~ /.*' + tagsQuery + '.*/i';
  284. }
  285. else {
  286. var titleOnly = queryString.indexOf('title:') === 0;
  287. if (titleOnly) {
  288. var titleQuery = queryString.substring(6, queryString.length);
  289. influxQuery = influxQuery + ' title =~ /.*' + titleQuery + '.*/i';
  290. }
  291. else {
  292. influxQuery = influxQuery + '(tags =~ /.*' + queryString + '.*/i or title =~ /.*' + queryString + '.*/i)';
  293. }
  294. }
  295. return this._seriesQuery(influxQuery).then(function(results) {
  296. var hits = { dashboards: [], tags: [], tagsOnly: false };
  297. if (!results || !results.length) {
  298. return hits;
  299. }
  300. var dashCol = _.indexOf(results[0].columns, 'title');
  301. var tagsCol = _.indexOf(results[0].columns, 'tags');
  302. for (var i = 0; i < results.length; i++) {
  303. var hit = {
  304. id: results[i].points[0][dashCol],
  305. title: results[i].points[0][dashCol],
  306. tags: results[i].points[0][tagsCol].split(",")
  307. };
  308. hit.tags = hit.tags[0] ? hit.tags : [];
  309. hits.dashboards.push(hit);
  310. }
  311. return hits;
  312. });
  313. };
  314. function handleInfluxQueryResponse(alias, groupByField, seriesList) {
  315. var influxSeries = new InfluxSeries({
  316. seriesList: seriesList,
  317. alias: alias,
  318. groupByField: groupByField
  319. });
  320. return influxSeries.getTimeSeries();
  321. }
  322. function getTimeFilter(options) {
  323. var from = getInfluxTime(options.range.from);
  324. var until = getInfluxTime(options.range.to);
  325. if (until === 'now()') {
  326. return 'time > now() - ' + from;
  327. }
  328. return 'time > ' + from + ' and time < ' + until;
  329. }
  330. function getInfluxTime(date) {
  331. if (_.isString(date)) {
  332. if (date === 'now') {
  333. return 'now()';
  334. }
  335. else if (date.indexOf('now') >= 0) {
  336. return date.substring(4);
  337. }
  338. date = kbn.parseDate(date);
  339. }
  340. return to_utc_epoch_seconds(date);
  341. }
  342. function to_utc_epoch_seconds(date) {
  343. return (date.getTime() / 1000).toFixed(0) + 's';
  344. }
  345. return InfluxDatasource;
  346. });
  347. });