datasource.js 12 KB

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