Commit 991b6eaf by bergquist

tech(influxdb): convert datasource to TS class

parent 5b75eea8
...@@ -8,35 +8,49 @@ import InfluxSeries from './influx_series'; ...@@ -8,35 +8,49 @@ import InfluxSeries from './influx_series';
import InfluxQuery from './influx_query'; import InfluxQuery from './influx_query';
import ResponseParser from './response_parser'; import ResponseParser from './response_parser';
/** @ngInject */ export default class InfluxDatasource {
export function InfluxDatasource(instanceSettings, $q, backendSrv, templateSrv) { type: string;
this.type = 'influxdb'; urls: any;
this.urls = _.map(instanceSettings.url.split(','), function(url) { username: string;
return url.trim(); password: string;
}); name: string;
database: any;
this.username = instanceSettings.username; basicAuth: any;
this.password = instanceSettings.password; interval: any;
this.name = instanceSettings.name; supportAnnotations: boolean;
this.database = instanceSettings.database; supportMetrics: boolean;
this.basicAuth = instanceSettings.basicAuth; responseParser: any;
this.interval = (instanceSettings.jsonData || {}).timeInterval;
this.supportAnnotations = true; /** @ngInject */
this.supportMetrics = true; constructor(instanceSettings, private $q, private backendSrv, private templateSrv) {
this.responseParser = new ResponseParser(); this.type = 'influxdb';
this.urls = _.map(instanceSettings.url.split(','), function(url) {
this.query = function(options) { return url.trim();
var timeFilter = getTimeFilter(options); });
this.username = instanceSettings.username;
this.password = instanceSettings.password;
this.name = instanceSettings.name;
this.database = instanceSettings.database;
this.basicAuth = instanceSettings.basicAuth;
this.interval = (instanceSettings.jsonData || {}).timeInterval;
this.supportAnnotations = true;
this.supportMetrics = true;
this.responseParser = new ResponseParser();
}
query(options) {
var timeFilter = this.getTimeFilter(options);
var queryTargets = []; var queryTargets = [];
var i, y; var i, y;
var allQueries = _.map(options.targets, function(target) { var allQueries = _.map(options.targets, (target) => {
if (target.hide) { return []; } if (target.hide) { return []; }
queryTargets.push(target); queryTargets.push(target);
// build query // build query
var queryModel = new InfluxQuery(target, templateSrv, options.scopedVars); var queryModel = new InfluxQuery(target, this.templateSrv, options.scopedVars);
var query = queryModel.render(true); var query = queryModel.render(true);
query = query.replace(/\$interval/g, (target.interval || options.interval)); query = query.replace(/\$interval/g, (target.interval || options.interval));
return query; return query;
...@@ -47,9 +61,9 @@ export function InfluxDatasource(instanceSettings, $q, backendSrv, templateSrv) ...@@ -47,9 +61,9 @@ export function InfluxDatasource(instanceSettings, $q, backendSrv, templateSrv)
allQueries = allQueries.replace(/\$timeFilter/g, timeFilter); allQueries = allQueries.replace(/\$timeFilter/g, timeFilter);
// replace templated variables // replace templated variables
allQueries = templateSrv.replace(allQueries, options.scopedVars); allQueries = this.templateSrv.replace(allQueries, options.scopedVars);
return this._seriesQuery(allQueries).then(function(data): any { return this._seriesQuery(allQueries).then((data): any => {
if (!data || !data.results) { if (!data || !data.results) {
return []; return [];
} }
...@@ -62,7 +76,7 @@ export function InfluxDatasource(instanceSettings, $q, backendSrv, templateSrv) ...@@ -62,7 +76,7 @@ export function InfluxDatasource(instanceSettings, $q, backendSrv, templateSrv)
var target = queryTargets[i]; var target = queryTargets[i];
var alias = target.alias; var alias = target.alias;
if (alias) { if (alias) {
alias = templateSrv.replace(target.alias, options.scopedVars); alias = this.templateSrv.replace(target.alias, options.scopedVars);
} }
var influxSeries = new InfluxSeries({ series: data.results[i].series, alias: alias }); var influxSeries = new InfluxSeries({ series: data.results[i].series, alias: alias });
...@@ -86,16 +100,16 @@ export function InfluxDatasource(instanceSettings, $q, backendSrv, templateSrv) ...@@ -86,16 +100,16 @@ export function InfluxDatasource(instanceSettings, $q, backendSrv, templateSrv)
}); });
}; };
this.annotationQuery = function(options) { annotationQuery(options) {
if (!options.annotation.query) { if (!options.annotation.query) {
return $q.reject({message: 'Query missing in annotation definition'}); return this.$q.reject({message: 'Query missing in annotation definition'});
} }
var timeFilter = getTimeFilter({rangeRaw: options.rangeRaw}); var timeFilter = this.getTimeFilter({rangeRaw: options.rangeRaw});
var query = options.annotation.query.replace('$timeFilter', timeFilter); var query = options.annotation.query.replace('$timeFilter', timeFilter);
query = templateSrv.replace(query); query = this.templateSrv.replace(query);
return this._seriesQuery(query).then(function(data) { return this._seriesQuery(query).then(data => {
if (!data || !data.results || !data.results[0]) { if (!data || !data.results || !data.results[0]) {
throw { message: 'No results in response from InfluxDB' }; throw { message: 'No results in response from InfluxDB' };
} }
...@@ -103,29 +117,29 @@ export function InfluxDatasource(instanceSettings, $q, backendSrv, templateSrv) ...@@ -103,29 +117,29 @@ export function InfluxDatasource(instanceSettings, $q, backendSrv, templateSrv)
}); });
}; };
this.metricFindQuery = (query) => { metricFindQuery(query) {
var interpolated; var interpolated;
try { try {
interpolated = templateSrv.replace(query, null, 'regex'); interpolated = this.templateSrv.replace(query, null, 'regex');
} catch (err) { } catch (err) {
return $q.reject(err); return this.$q.reject(err);
} }
return this._seriesQuery(interpolated) return this._seriesQuery(interpolated)
.then(_.curry(this.responseParser.parse)(query)); .then(_.curry(this.responseParser.parse)(query));
}; };
this._seriesQuery = function(query) { _seriesQuery(query) {
return this._influxRequest('GET', '/query', {q: query, epoch: 'ms'}); return this._influxRequest('GET', '/query', {q: query, epoch: 'ms'});
}; }
this.testDatasource = function() { testDatasource() {
return this.metricFindQuery('SHOW MEASUREMENTS LIMIT 1').then(function () { return this.metricFindQuery('SHOW MEASUREMENTS LIMIT 1').then(() => {
return { status: "success", message: "Data source is working", title: "Success" }; return { status: "success", message: "Data source is working", title: "Success" };
}); });
}; }
this._influxRequest = function(method, url, data) { _influxRequest(method, url, data) {
var self = this; var self = this;
var currentUrl = self.urls.shift(); var currentUrl = self.urls.shift();
...@@ -159,7 +173,7 @@ export function InfluxDatasource(instanceSettings, $q, backendSrv, templateSrv) ...@@ -159,7 +173,7 @@ export function InfluxDatasource(instanceSettings, $q, backendSrv, templateSrv)
options.headers.Authorization = self.basicAuth; options.headers.Authorization = self.basicAuth;
} }
return backendSrv.datasourceRequest(options).then(function(result) { return this.backendSrv.datasourceRequest(options).then(result => {
return result.data; return result.data;
}, function(err) { }, function(err) {
if (err.status !== 0 || err.status >= 300) { if (err.status !== 0 || err.status >= 300) {
...@@ -172,9 +186,9 @@ export function InfluxDatasource(instanceSettings, $q, backendSrv, templateSrv) ...@@ -172,9 +186,9 @@ export function InfluxDatasource(instanceSettings, $q, backendSrv, templateSrv)
}); });
}; };
function getTimeFilter(options) { getTimeFilter(options) {
var from = getInfluxTime(options.rangeRaw.from, false); var from = this.getInfluxTime(options.rangeRaw.from, false);
var until = getInfluxTime(options.rangeRaw.to, true); var until = this.getInfluxTime(options.rangeRaw.to, true);
var fromIsAbsolute = from[from.length-1] === 's'; var fromIsAbsolute = from[from.length-1] === 's';
if (until === 'now()' && !fromIsAbsolute) { if (until === 'now()' && !fromIsAbsolute) {
...@@ -184,7 +198,7 @@ export function InfluxDatasource(instanceSettings, $q, backendSrv, templateSrv) ...@@ -184,7 +198,7 @@ export function InfluxDatasource(instanceSettings, $q, backendSrv, templateSrv)
return 'time > ' + from + ' and time < ' + until; return 'time > ' + from + ' and time < ' + until;
} }
function getInfluxTime(date, roundUp) { getInfluxTime(date, roundUp) {
if (_.isString(date)) { if (_.isString(date)) {
if (date === 'now') { if (date === 'now') {
return 'now()'; return 'now()';
......
...@@ -11,6 +11,7 @@ export default class InfluxQuery { ...@@ -11,6 +11,7 @@ export default class InfluxQuery {
templateSrv: any; templateSrv: any;
scopedVars: any; scopedVars: any;
/** @ngInject */
constructor(target, templateSrv?, scopedVars?) { constructor(target, templateSrv?, scopedVars?) {
this.target = target; this.target = target;
this.templateSrv = templateSrv; this.templateSrv = templateSrv;
......
import {InfluxDatasource} from './datasource'; import InfluxDatasource from './datasource';
import {InfluxQueryCtrl} from './query_ctrl'; import {InfluxQueryCtrl} from './query_ctrl';
class InfluxConfigCtrl { class InfluxConfigCtrl {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment