datasource.ts 10 KB

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