datasource.ts 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. import _ from 'lodash';
  2. import * as dateMath from 'app/core/utils/datemath';
  3. import InfluxSeries from './influx_series';
  4. import InfluxQuery from './influx_query';
  5. import ResponseParser from './response_parser';
  6. import { InfluxQueryBuilder } from './query_builder';
  7. export default class InfluxDatasource {
  8. type: string;
  9. urls: any;
  10. username: string;
  11. password: string;
  12. name: string;
  13. database: any;
  14. basicAuth: any;
  15. withCredentials: any;
  16. interval: any;
  17. supportAnnotations: boolean;
  18. supportMetrics: boolean;
  19. responseParser: any;
  20. /** @ngInject */
  21. constructor(
  22. instanceSettings,
  23. private $q,
  24. private backendSrv,
  25. private templateSrv
  26. ) {
  27. this.type = 'influxdb';
  28. this.urls = _.map(instanceSettings.url.split(','), function(url) {
  29. return url.trim();
  30. });
  31. this.username = instanceSettings.username;
  32. this.password = instanceSettings.password;
  33. this.name = instanceSettings.name;
  34. this.database = instanceSettings.database;
  35. this.basicAuth = instanceSettings.basicAuth;
  36. this.withCredentials = instanceSettings.withCredentials;
  37. this.interval = (instanceSettings.jsonData || {}).timeInterval;
  38. this.supportAnnotations = true;
  39. this.supportMetrics = true;
  40. this.responseParser = new ResponseParser();
  41. }
  42. query(options) {
  43. var timeFilter = this.getTimeFilter(options);
  44. var scopedVars = options.scopedVars;
  45. var targets = _.cloneDeep(options.targets);
  46. var queryTargets = [];
  47. var queryModel;
  48. var i, y;
  49. var allQueries = _.map(targets, target => {
  50. if (target.hide) {
  51. return '';
  52. }
  53. queryTargets.push(target);
  54. // backward compatability
  55. scopedVars.interval = scopedVars.__interval;
  56. queryModel = new InfluxQuery(target, this.templateSrv, scopedVars);
  57. return queryModel.render(true);
  58. }).reduce((acc, current) => {
  59. if (current !== '') {
  60. acc += ';' + current;
  61. }
  62. return acc;
  63. });
  64. if (allQueries === '') {
  65. return this.$q.when({ data: [] });
  66. }
  67. // add global adhoc filters to timeFilter
  68. var adhocFilters = this.templateSrv.getAdhocFilters(this.name);
  69. if (adhocFilters.length > 0) {
  70. timeFilter += ' AND ' + queryModel.renderAdhocFilters(adhocFilters);
  71. }
  72. // replace grafana variables
  73. scopedVars.timeFilter = { value: timeFilter };
  74. // replace templated variables
  75. allQueries = this.templateSrv.replace(allQueries, scopedVars);
  76. return this._seriesQuery(allQueries).then((data): any => {
  77. if (!data || !data.results) {
  78. return [];
  79. }
  80. var seriesList = [];
  81. for (i = 0; i < data.results.length; i++) {
  82. var result = data.results[i];
  83. if (!result || !result.series) {
  84. continue;
  85. }
  86. var target = queryTargets[i];
  87. var alias = target.alias;
  88. if (alias) {
  89. alias = this.templateSrv.replace(target.alias, options.scopedVars);
  90. }
  91. var influxSeries = new InfluxSeries({
  92. series: data.results[i].series,
  93. alias: alias,
  94. });
  95. switch (target.resultFormat) {
  96. case 'table': {
  97. seriesList.push(influxSeries.getTable());
  98. break;
  99. }
  100. default: {
  101. var timeSeries = influxSeries.getTimeSeries();
  102. for (y = 0; y < timeSeries.length; y++) {
  103. seriesList.push(timeSeries[y]);
  104. }
  105. break;
  106. }
  107. }
  108. }
  109. return { data: seriesList };
  110. });
  111. }
  112. annotationQuery(options) {
  113. if (!options.annotation.query) {
  114. return this.$q.reject({
  115. message: 'Query missing in annotation definition',
  116. });
  117. }
  118. var timeFilter = this.getTimeFilter({ rangeRaw: options.rangeRaw });
  119. var query = options.annotation.query.replace('$timeFilter', timeFilter);
  120. query = this.templateSrv.replace(query, null, 'regex');
  121. return this._seriesQuery(query).then(data => {
  122. if (!data || !data.results || !data.results[0]) {
  123. throw { message: 'No results in response from InfluxDB' };
  124. }
  125. return new InfluxSeries({
  126. series: data.results[0].series,
  127. annotation: options.annotation,
  128. }).getAnnotations();
  129. });
  130. }
  131. targetContainsTemplate(target) {
  132. for (let group of target.groupBy) {
  133. for (let param of group.params) {
  134. if (this.templateSrv.variableExists(param)) {
  135. return true;
  136. }
  137. }
  138. }
  139. for (let i in target.tags) {
  140. if (this.templateSrv.variableExists(target.tags[i].value)) {
  141. return true;
  142. }
  143. }
  144. return false;
  145. }
  146. metricFindQuery(query) {
  147. var interpolated = this.templateSrv.replace(query, null, 'regex');
  148. return this._seriesQuery(interpolated).then(
  149. _.curry(this.responseParser.parse)(query)
  150. );
  151. }
  152. getTagKeys(options) {
  153. var queryBuilder = new InfluxQueryBuilder(
  154. { measurement: '', tags: [] },
  155. this.database
  156. );
  157. var query = queryBuilder.buildExploreQuery('TAG_KEYS');
  158. return this.metricFindQuery(query);
  159. }
  160. getTagValues(options) {
  161. var queryBuilder = new InfluxQueryBuilder(
  162. { measurement: '', tags: [] },
  163. this.database
  164. );
  165. var query = queryBuilder.buildExploreQuery('TAG_VALUES', options.key);
  166. return this.metricFindQuery(query);
  167. }
  168. _seriesQuery(query) {
  169. if (!query) {
  170. return this.$q.when({ results: [] });
  171. }
  172. return this._influxRequest('GET', '/query', { q: query, epoch: 'ms' });
  173. }
  174. serializeParams(params) {
  175. if (!params) {
  176. return '';
  177. }
  178. return _.reduce(
  179. params,
  180. (memo, value, key) => {
  181. if (value === null || value === undefined) {
  182. return memo;
  183. }
  184. memo.push(encodeURIComponent(key) + '=' + encodeURIComponent(value));
  185. return memo;
  186. },
  187. []
  188. ).join('&');
  189. }
  190. testDatasource() {
  191. var queryBuilder = new InfluxQueryBuilder(
  192. { measurement: '', tags: [] },
  193. this.database
  194. );
  195. var query = queryBuilder.buildExploreQuery('RETENTION POLICIES');
  196. return this._seriesQuery(query)
  197. .then(res => {
  198. let error = _.get(res, 'results[0].error');
  199. if (error) {
  200. return { status: 'error', message: error };
  201. }
  202. return { status: 'success', message: 'Data source is working' };
  203. })
  204. .catch(err => {
  205. return { status: 'error', message: err.message };
  206. });
  207. }
  208. _influxRequest(method, url, data) {
  209. var self = this;
  210. var currentUrl = self.urls.shift();
  211. self.urls.push(currentUrl);
  212. var params: any = {};
  213. if (self.username) {
  214. params.u = self.username;
  215. params.p = self.password;
  216. }
  217. if (self.database) {
  218. params.db = self.database;
  219. }
  220. if (method === 'GET') {
  221. _.extend(params, data);
  222. data = null;
  223. }
  224. var options: any = {
  225. method: method,
  226. url: currentUrl + url,
  227. params: params,
  228. data: data,
  229. precision: 'ms',
  230. inspect: { type: 'influxdb' },
  231. paramSerializer: this.serializeParams,
  232. };
  233. options.headers = options.headers || {};
  234. if (this.basicAuth || this.withCredentials) {
  235. options.withCredentials = true;
  236. }
  237. if (self.basicAuth) {
  238. options.headers.Authorization = self.basicAuth;
  239. }
  240. return this.backendSrv.datasourceRequest(options).then(
  241. result => {
  242. return result.data;
  243. },
  244. function(err) {
  245. if (err.status !== 0 || err.status >= 300) {
  246. if (err.data && err.data.error) {
  247. throw {
  248. message: 'InfluxDB Error: ' + err.data.error,
  249. data: err.data,
  250. config: err.config,
  251. };
  252. } else {
  253. throw {
  254. message:
  255. 'Network Error: ' + err.statusText + '(' + err.status + ')',
  256. data: err.data,
  257. config: err.config,
  258. };
  259. }
  260. }
  261. }
  262. );
  263. }
  264. getTimeFilter(options) {
  265. var from = this.getInfluxTime(options.rangeRaw.from, false);
  266. var until = this.getInfluxTime(options.rangeRaw.to, true);
  267. var fromIsAbsolute = from[from.length - 1] === 'ms';
  268. if (until === 'now()' && !fromIsAbsolute) {
  269. return 'time >= ' + from;
  270. }
  271. return 'time >= ' + from + ' and time <= ' + until;
  272. }
  273. getInfluxTime(date, roundUp) {
  274. if (_.isString(date)) {
  275. if (date === 'now') {
  276. return 'now()';
  277. }
  278. var parts = /^now-(\d+)([d|h|m|s])$/.exec(date);
  279. if (parts) {
  280. var amount = parseInt(parts[1]);
  281. var unit = parts[2];
  282. return 'now() - ' + amount + unit;
  283. }
  284. date = dateMath.parse(date, roundUp);
  285. }
  286. return date.valueOf() + 'ms';
  287. }
  288. }