datasource.ts 9.3 KB

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