| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207 |
- define([
- 'angular',
- 'lodash',
- 'app/core/utils/datemath',
- './influx_series',
- './query_builder',
- './directives',
- './query_ctrl',
- ],
- function (angular, _, dateMath, InfluxSeries, InfluxQueryBuilder) {
- 'use strict';
- var module = angular.module('grafana.services');
- module.factory('InfluxDatasource', function($q, $http, templateSrv) {
- function InfluxDatasource(datasource) {
- this.type = 'influxdb';
- this.urls = _.map(datasource.url.split(','), function(url) {
- return url.trim();
- });
- this.username = datasource.username;
- this.password = datasource.password;
- this.name = datasource.name;
- this.database = datasource.database;
- this.basicAuth = datasource.basicAuth;
- this.supportAnnotations = true;
- this.supportMetrics = true;
- }
- InfluxDatasource.prototype.query = function(options) {
- var timeFilter = getTimeFilter(options);
- var queryTargets = [];
- var i, y;
- var allQueries = _.map(options.targets, function(target) {
- if (target.hide) { return []; }
- queryTargets.push(target);
- // build query
- var queryBuilder = new InfluxQueryBuilder(target);
- var query = queryBuilder.build();
- query = query.replace(/\$interval/g, (target.interval || options.interval));
- return query;
- }).join("\n");
- // replace grafana variables
- allQueries = allQueries.replace(/\$timeFilter/g, timeFilter);
- // replace templated variables
- allQueries = templateSrv.replace(allQueries, options.scopedVars);
- return this._seriesQuery(allQueries).then(function(data) {
- if (!data || !data.results) {
- return [];
- }
- var seriesList = [];
- for (i = 0; i < data.results.length; i++) {
- var result = data.results[i];
- if (!result || !result.series) { continue; }
- var alias = (queryTargets[i] || {}).alias;
- if (alias) {
- alias = templateSrv.replace(alias, options.scopedVars);
- }
- var targetSeries = new InfluxSeries({ series: data.results[i].series, alias: alias }).getTimeSeries();
- for (y = 0; y < targetSeries.length; y++) {
- seriesList.push(targetSeries[y]);
- }
- }
- return { data: seriesList };
- });
- };
- InfluxDatasource.prototype.annotationQuery = function(annotation, rangeUnparsed) {
- var timeFilter = getTimeFilter({ range: rangeUnparsed });
- var query = annotation.query.replace('$timeFilter', timeFilter);
- query = templateSrv.replace(query);
- return this._seriesQuery(query).then(function(data) {
- if (!data || !data.results || !data.results[0]) {
- throw { message: 'No results in response from InfluxDB' };
- }
- return new InfluxSeries({ series: data.results[0].series, annotation: annotation }).getAnnotations();
- });
- };
- InfluxDatasource.prototype.metricFindQuery = function (query) {
- var interpolated;
- try {
- interpolated = templateSrv.replace(query);
- }
- catch (err) {
- return $q.reject(err);
- }
- return this._seriesQuery(interpolated).then(function (results) {
- if (!results || results.results.length === 0) { return []; }
- var influxResults = results.results[0];
- if (!influxResults.series) {
- return [];
- }
- var series = influxResults.series[0];
- if (query.indexOf('SHOW MEASUREMENTS') === 0) {
- return _.map(series.values, function(value) { return { text: value[0], expandable: true }; });
- }
- var flattenedValues = _.flatten(series.values);
- return _.map(flattenedValues, function(value) { return { text: value, expandable: true }; });
- });
- };
- InfluxDatasource.prototype._seriesQuery = function(query) {
- return this._influxRequest('GET', '/query', {q: query, epoch: 'ms'});
- };
- InfluxDatasource.prototype.testDatasource = function() {
- return this.metricFindQuery('SHOW MEASUREMENTS LIMIT 1').then(function () {
- return { status: "success", message: "Data source is working", title: "Success" };
- });
- };
- InfluxDatasource.prototype._influxRequest = function(method, url, data) {
- var self = this;
- var currentUrl = self.urls.shift();
- self.urls.push(currentUrl);
- var params = {
- u: self.username,
- p: self.password,
- };
- if (self.database) {
- params.db = self.database;
- }
- if (method === 'GET') {
- _.extend(params, data);
- data = null;
- }
- var options = {
- method: method,
- url: currentUrl + url,
- params: params,
- data: data,
- precision: "ms",
- inspect: { type: 'influxdb' },
- };
- options.headers = options.headers || {};
- if (self.basicAuth) {
- options.headers.Authorization = self.basicAuth;
- }
- return $http(options).then(function(result) {
- return result.data;
- }, function(err) {
- if (err.status !== 0 || err.status >= 300) {
- if (err.data && err.data.error) {
- throw { message: 'InfluxDB Error Response: ' + err.data.error, data: err.data, config: err.config };
- }
- else {
- throw { messsage: 'InfluxDB Error: ' + err.message, data: err.data, config: err.config };
- }
- }
- });
- };
- function getTimeFilter(options) {
- var from = getInfluxTime(options.rangeRaw.from, false);
- var until = getInfluxTime(options.rangeRaw.to, true);
- var fromIsAbsolute = from[from.length-1] === 's';
- if (until === 'now()' && !fromIsAbsolute) {
- return 'time > ' + from;
- }
- return 'time > ' + from + ' and time < ' + until;
- }
- function getInfluxTime(date, roundUp) {
- if (_.isString(date)) {
- if (date === 'now') {
- return 'now()';
- }
- if (date.indexOf('now-') >= 0 && date.indexOf('/') === -1) {
- return date.replace('now', 'now()').replace('-', ' - ');
- }
- date = dateMath.parse(date, roundUp);
- }
- return (date.valueOf() / 1000).toFixed(0) + 's';
- }
- return InfluxDatasource;
- });
- });
|