influxdbDatasource.js 13 KB

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