| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347 |
- import _ from 'lodash';
- import { dateMath } from '@grafana/data';
- import InfluxSeries from './influx_series';
- import InfluxQueryModel from './influx_query_model';
- import ResponseParser from './response_parser';
- import { InfluxQueryBuilder } from './query_builder';
- import { DataSourceApi, DataSourceInstanceSettings } from '@grafana/ui';
- import { InfluxQuery, InfluxOptions } from './types';
- import { BackendSrv } from 'app/core/services/backend_srv';
- import { TemplateSrv } from 'app/features/templating/template_srv';
- import { IQService } from 'angular';
- export default class InfluxDatasource extends DataSourceApi<InfluxQuery, InfluxOptions> {
- type: string;
- urls: any;
- username: string;
- password: string;
- name: string;
- database: any;
- basicAuth: any;
- withCredentials: any;
- interval: any;
- responseParser: any;
- httpMode: string;
- /** @ngInject */
- constructor(
- instanceSettings: DataSourceInstanceSettings<InfluxOptions>,
- private $q: IQService,
- private backendSrv: BackendSrv,
- private templateSrv: TemplateSrv
- ) {
- super(instanceSettings);
- this.type = 'influxdb';
- this.urls = _.map(instanceSettings.url.split(','), url => {
- return url.trim();
- });
- this.username = instanceSettings.username;
- this.password = instanceSettings.password;
- this.name = instanceSettings.name;
- this.database = instanceSettings.database;
- this.basicAuth = instanceSettings.basicAuth;
- this.withCredentials = instanceSettings.withCredentials;
- const settingsData = instanceSettings.jsonData || ({} as InfluxOptions);
- this.interval = settingsData.timeInterval;
- this.httpMode = settingsData.httpMode || 'GET';
- this.responseParser = new ResponseParser();
- }
- query(options: any) {
- let timeFilter = this.getTimeFilter(options);
- const scopedVars = options.scopedVars;
- const targets = _.cloneDeep(options.targets);
- const queryTargets: any[] = [];
- let queryModel: InfluxQueryModel;
- let i, y;
- let allQueries = _.map(targets, target => {
- if (target.hide) {
- return '';
- }
- queryTargets.push(target);
- // backward compatibility
- scopedVars.interval = scopedVars.__interval;
- queryModel = new InfluxQueryModel(target, this.templateSrv, scopedVars);
- return queryModel.render(true);
- }).reduce((acc, current) => {
- if (current !== '') {
- acc += ';' + current;
- }
- return acc;
- });
- if (allQueries === '') {
- return this.$q.when({ data: [] });
- }
- // add global adhoc filters to timeFilter
- const adhocFilters = this.templateSrv.getAdhocFilters(this.name);
- if (adhocFilters.length > 0) {
- timeFilter += ' AND ' + queryModel.renderAdhocFilters(adhocFilters);
- }
- // replace grafana variables
- scopedVars.timeFilter = { value: timeFilter };
- // replace templated variables
- allQueries = this.templateSrv.replace(allQueries, scopedVars);
- return this._seriesQuery(allQueries, options).then(
- (data: any): any => {
- if (!data || !data.results) {
- return [];
- }
- const seriesList = [];
- for (i = 0; i < data.results.length; i++) {
- const result = data.results[i];
- if (!result || !result.series) {
- continue;
- }
- const target = queryTargets[i];
- let alias = target.alias;
- if (alias) {
- alias = this.templateSrv.replace(target.alias, options.scopedVars);
- }
- const influxSeries = new InfluxSeries({
- series: data.results[i].series,
- alias: alias,
- });
- switch (target.resultFormat) {
- case 'table': {
- seriesList.push(influxSeries.getTable());
- break;
- }
- default: {
- const timeSeries = influxSeries.getTimeSeries();
- for (y = 0; y < timeSeries.length; y++) {
- seriesList.push(timeSeries[y]);
- }
- break;
- }
- }
- }
- return { data: seriesList };
- }
- );
- }
- annotationQuery(options: any) {
- if (!options.annotation.query) {
- return this.$q.reject({
- message: 'Query missing in annotation definition',
- });
- }
- const timeFilter = this.getTimeFilter({ rangeRaw: options.rangeRaw, timezone: options.timezone });
- let query = options.annotation.query.replace('$timeFilter', timeFilter);
- query = this.templateSrv.replace(query, null, 'regex');
- return this._seriesQuery(query, options).then((data: any) => {
- if (!data || !data.results || !data.results[0]) {
- throw { message: 'No results in response from InfluxDB' };
- }
- return new InfluxSeries({
- series: data.results[0].series,
- annotation: options.annotation,
- }).getAnnotations();
- });
- }
- targetContainsTemplate(target: any) {
- for (const group of target.groupBy) {
- for (const param of group.params) {
- if (this.templateSrv.variableExists(param)) {
- return true;
- }
- }
- }
- for (const i in target.tags) {
- if (this.templateSrv.variableExists(target.tags[i].value)) {
- return true;
- }
- }
- return false;
- }
- metricFindQuery(query: string, options?: any) {
- const interpolated = this.templateSrv.replace(query, null, 'regex');
- return this._seriesQuery(interpolated, options).then(_.curry(this.responseParser.parse)(query));
- }
- getTagKeys(options: any = {}) {
- const queryBuilder = new InfluxQueryBuilder({ measurement: options.measurement || '', tags: [] }, this.database);
- const query = queryBuilder.buildExploreQuery('TAG_KEYS');
- return this.metricFindQuery(query, options);
- }
- getTagValues(options: any = {}) {
- const queryBuilder = new InfluxQueryBuilder({ measurement: options.measurement || '', tags: [] }, this.database);
- const query = queryBuilder.buildExploreQuery('TAG_VALUES', options.key);
- return this.metricFindQuery(query, options);
- }
- _seriesQuery(query: string, options?: any) {
- if (!query) {
- return this.$q.when({ results: [] });
- }
- if (options && options.range) {
- const timeFilter = this.getTimeFilter({ rangeRaw: options.range, timezone: options.timezone });
- query = query.replace('$timeFilter', timeFilter);
- }
- return this._influxRequest(this.httpMode, '/query', { q: query, epoch: 'ms' }, options);
- }
- serializeParams(params: any) {
- if (!params) {
- return '';
- }
- return _.reduce(
- params,
- (memo, value, key) => {
- if (value === null || value === undefined) {
- return memo;
- }
- memo.push(encodeURIComponent(key) + '=' + encodeURIComponent(value));
- return memo;
- },
- []
- ).join('&');
- }
- testDatasource() {
- const queryBuilder = new InfluxQueryBuilder({ measurement: '', tags: [] }, this.database);
- const query = queryBuilder.buildExploreQuery('RETENTION POLICIES');
- return this._seriesQuery(query)
- .then((res: any) => {
- const error = _.get(res, 'results[0].error');
- if (error) {
- return { status: 'error', message: error };
- }
- return { status: 'success', message: 'Data source is working' };
- })
- .catch((err: any) => {
- return { status: 'error', message: err.message };
- });
- }
- _influxRequest(method: string, url: string, data: any, options?: any) {
- const currentUrl = this.urls.shift();
- this.urls.push(currentUrl);
- const params: any = {};
- if (this.username) {
- params.u = this.username;
- params.p = this.password;
- }
- if (options && options.database) {
- params.db = options.database;
- } else if (this.database) {
- params.db = this.database;
- }
- if (method === 'POST' && _.has(data, 'q')) {
- // verb is POST and 'q' param is defined
- _.extend(params, _.omit(data, ['q']));
- data = this.serializeParams(_.pick(data, ['q']));
- } else if (method === 'GET' || method === 'POST') {
- // verb is GET, or POST without 'q' param
- _.extend(params, data);
- data = null;
- }
- const req: any = {
- method: method,
- url: currentUrl + url,
- params: params,
- data: data,
- precision: 'ms',
- inspect: { type: 'influxdb' },
- paramSerializer: this.serializeParams,
- };
- req.headers = req.headers || {};
- if (this.basicAuth || this.withCredentials) {
- req.withCredentials = true;
- }
- if (this.basicAuth) {
- req.headers.Authorization = this.basicAuth;
- }
- if (method === 'POST') {
- req.headers['Content-type'] = 'application/x-www-form-urlencoded';
- }
- return this.backendSrv.datasourceRequest(req).then(
- (result: any) => {
- return result.data;
- },
- (err: any) => {
- if (err.status !== 0 || err.status >= 300) {
- if (err.data && err.data.error) {
- throw {
- message: 'InfluxDB Error: ' + err.data.error,
- data: err.data,
- config: err.config,
- };
- } else {
- throw {
- message: 'Network Error: ' + err.statusText + '(' + err.status + ')',
- data: err.data,
- config: err.config,
- };
- }
- }
- }
- );
- }
- getTimeFilter(options: any) {
- const from = this.getInfluxTime(options.rangeRaw.from, false, options.timezone);
- const until = this.getInfluxTime(options.rangeRaw.to, true, options.timezone);
- const fromIsAbsolute = from[from.length - 1] === 'ms';
- if (until === 'now()' && !fromIsAbsolute) {
- return 'time >= ' + from;
- }
- return 'time >= ' + from + ' and time <= ' + until;
- }
- getInfluxTime(date: any, roundUp: any, timezone: any) {
- if (_.isString(date)) {
- if (date === 'now') {
- return 'now()';
- }
- const parts = /^now-(\d+)([dhms])$/.exec(date);
- if (parts) {
- const amount = parseInt(parts[1], 10);
- const unit = parts[2];
- return 'now() - ' + amount + unit;
- }
- date = dateMath.parse(date, roundUp, timezone);
- }
- return date.valueOf() + 'ms';
- }
- }
|