Commit 17b5f48c by Tobias Skarhed Committed by Torkel Ödegaard

Datasource: Refactor Graphite to class (#17942)

* Rewrite Graphite Datasources to class

* Graphite: removed unused prop
parent e6b8a152
...@@ -2,21 +2,44 @@ import _ from 'lodash'; ...@@ -2,21 +2,44 @@ import _ from 'lodash';
import * as dateMath from '@grafana/ui/src/utils/datemath'; import * as dateMath from '@grafana/ui/src/utils/datemath';
import { isVersionGtOrEq, SemVersion } from 'app/core/utils/version'; import { isVersionGtOrEq, SemVersion } from 'app/core/utils/version';
import gfunc from './gfunc'; import gfunc from './gfunc';
import { IQService } from 'angular';
/** @ngInject */ import { BackendSrv } from 'app/core/services/backend_srv';
export function GraphiteDatasource(this: any, instanceSettings, $q, backendSrv, templateSrv) { import { TemplateSrv } from 'app/features/templating/template_srv';
this.basicAuth = instanceSettings.basicAuth; import { ScopedVars } from '@grafana/ui';
this.url = instanceSettings.url;
this.name = instanceSettings.name; export class GraphiteDatasource {
this.graphiteVersion = instanceSettings.jsonData.graphiteVersion || '0.9'; basicAuth: string;
this.supportsTags = supportsTags(this.graphiteVersion); url: string;
this.cacheTimeout = instanceSettings.cacheTimeout; name: string;
this.withCredentials = instanceSettings.withCredentials; graphiteVersion: any;
this.render_method = instanceSettings.render_method || 'POST'; supportsTags: boolean;
this.funcDefs = null; cacheTimeout: any;
this.funcDefsPromise = null; withCredentials: boolean;
funcDefs = null;
this.getQueryOptionsInfo = () => { funcDefsPromise = null;
_seriesRefLetters: string;
/** @ngInject */
constructor(
instanceSettings: any,
private $q: IQService,
private backendSrv: BackendSrv,
private templateSrv: TemplateSrv
) {
this.basicAuth = instanceSettings.basicAuth;
this.url = instanceSettings.url;
this.name = instanceSettings.name;
this.graphiteVersion = instanceSettings.jsonData.graphiteVersion || '0.9';
this.supportsTags = supportsTags(this.graphiteVersion);
this.cacheTimeout = instanceSettings.cacheTimeout;
this.withCredentials = instanceSettings.withCredentials;
this.funcDefs = null;
this.funcDefsPromise = null;
this._seriesRefLetters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
}
getQueryOptionsInfo() {
return { return {
maxDataPoints: true, maxDataPoints: true,
cacheTimeout: true, cacheTimeout: true,
...@@ -27,9 +50,9 @@ export function GraphiteDatasource(this: any, instanceSettings, $q, backendSrv, ...@@ -27,9 +50,9 @@ export function GraphiteDatasource(this: any, instanceSettings, $q, backendSrv,
}, },
], ],
}; };
}; }
this.query = function(options) { query(options: any) {
const graphOptions = { const graphOptions = {
from: this.translateTime(options.rangeRaw.from, false, options.timezone), from: this.translateTime(options.rangeRaw.from, false, options.timezone),
until: this.translateTime(options.rangeRaw.to, true, options.timezone), until: this.translateTime(options.rangeRaw.to, true, options.timezone),
...@@ -41,7 +64,7 @@ export function GraphiteDatasource(this: any, instanceSettings, $q, backendSrv, ...@@ -41,7 +64,7 @@ export function GraphiteDatasource(this: any, instanceSettings, $q, backendSrv,
const params = this.buildGraphiteParams(graphOptions, options.scopedVars); const params = this.buildGraphiteParams(graphOptions, options.scopedVars);
if (params.length === 0) { if (params.length === 0) {
return $q.when({ data: [] }); return this.$q.when({ data: [] });
} }
const httpOptions: any = { const httpOptions: any = {
...@@ -60,17 +83,17 @@ export function GraphiteDatasource(this: any, instanceSettings, $q, backendSrv, ...@@ -60,17 +83,17 @@ export function GraphiteDatasource(this: any, instanceSettings, $q, backendSrv,
} }
return this.doGraphiteRequest(httpOptions).then(this.convertDataPointsToMs); return this.doGraphiteRequest(httpOptions).then(this.convertDataPointsToMs);
}; }
this.addTracingHeaders = function(httpOptions, options) { addTracingHeaders(httpOptions: { headers: any }, options: { dashboardId: any; panelId: any }) {
const proxyMode = !this.url.match(/^http/); const proxyMode = !this.url.match(/^http/);
if (proxyMode) { if (proxyMode) {
httpOptions.headers['X-Dashboard-Id'] = options.dashboardId; httpOptions.headers['X-Dashboard-Id'] = options.dashboardId;
httpOptions.headers['X-Panel-Id'] = options.panelId; httpOptions.headers['X-Panel-Id'] = options.panelId;
} }
}; }
this.convertDataPointsToMs = result => { convertDataPointsToMs(result: any) {
if (!result || !result.data) { if (!result || !result.data) {
return []; return [];
} }
...@@ -81,9 +104,9 @@ export function GraphiteDatasource(this: any, instanceSettings, $q, backendSrv, ...@@ -81,9 +104,9 @@ export function GraphiteDatasource(this: any, instanceSettings, $q, backendSrv,
} }
} }
return result; return result;
}; }
this.parseTags = tagString => { parseTags(tagString: string) {
let tags = []; let tags = [];
tags = tagString.split(','); tags = tagString.split(',');
if (tags.length === 1) { if (tags.length === 1) {
...@@ -93,12 +116,12 @@ export function GraphiteDatasource(this: any, instanceSettings, $q, backendSrv, ...@@ -93,12 +116,12 @@ export function GraphiteDatasource(this: any, instanceSettings, $q, backendSrv,
} }
} }
return tags; return tags;
}; }
this.annotationQuery = function(options) { annotationQuery(options: { annotation: { target: string; tags: string }; rangeRaw: any }) {
// Graphite metric as annotation // Graphite metric as annotation
if (options.annotation.target) { if (options.annotation.target) {
const target = templateSrv.replace(options.annotation.target, {}, 'glob'); const target = this.templateSrv.replace(options.annotation.target, {}, 'glob');
const graphiteQuery = { const graphiteQuery = {
rangeRaw: options.rangeRaw, rangeRaw: options.rangeRaw,
targets: [{ target: target }], targets: [{ target: target }],
...@@ -106,7 +129,7 @@ export function GraphiteDatasource(this: any, instanceSettings, $q, backendSrv, ...@@ -106,7 +129,7 @@ export function GraphiteDatasource(this: any, instanceSettings, $q, backendSrv,
maxDataPoints: 100, maxDataPoints: 100,
}; };
return this.query(graphiteQuery).then(result => { return this.query(graphiteQuery).then((result: { data: any[] }) => {
const list = []; const list = [];
for (let i = 0; i < result.data.length; i++) { for (let i = 0; i < result.data.length; i++) {
...@@ -130,7 +153,7 @@ export function GraphiteDatasource(this: any, instanceSettings, $q, backendSrv, ...@@ -130,7 +153,7 @@ export function GraphiteDatasource(this: any, instanceSettings, $q, backendSrv,
}); });
} else { } else {
// Graphite event as annotation // Graphite event as annotation
const tags = templateSrv.replace(options.annotation.tags); const tags = this.templateSrv.replace(options.annotation.tags);
return this.events({ range: options.rangeRaw, tags: tags }).then(results => { return this.events({ range: options.rangeRaw, tags: tags }).then(results => {
const list = []; const list = [];
for (let i = 0; i < results.data.length; i++) { for (let i = 0; i < results.data.length; i++) {
...@@ -153,9 +176,9 @@ export function GraphiteDatasource(this: any, instanceSettings, $q, backendSrv, ...@@ -153,9 +176,9 @@ export function GraphiteDatasource(this: any, instanceSettings, $q, backendSrv,
return list; return list;
}); });
} }
}; }
this.events = function(options) { events(options: { range: any; tags: any; timezone?: any }) {
try { try {
let tags = ''; let tags = '';
if (options.tags) { if (options.tags) {
...@@ -171,15 +194,15 @@ export function GraphiteDatasource(this: any, instanceSettings, $q, backendSrv, ...@@ -171,15 +194,15 @@ export function GraphiteDatasource(this: any, instanceSettings, $q, backendSrv,
tags, tags,
}); });
} catch (err) { } catch (err) {
return $q.reject(err); return this.$q.reject(err);
} }
}; }
this.targetContainsTemplate = target => { targetContainsTemplate(target: { target: any }) {
return templateSrv.variableExists(target.target); return this.templateSrv.variableExists(target.target);
}; }
this.translateTime = (date, roundUp, timezone) => { translateTime(date: any, roundUp: any, timezone: any) {
if (_.isString(date)) { if (_.isString(date)) {
if (date === 'now') { if (date === 'now') {
return 'now'; return 'now';
...@@ -207,11 +230,11 @@ export function GraphiteDatasource(this: any, instanceSettings, $q, backendSrv, ...@@ -207,11 +230,11 @@ export function GraphiteDatasource(this: any, instanceSettings, $q, backendSrv,
} }
return date.unix(); return date.unix();
}; }
this.metricFindQuery = function(query, optionalOptions) { metricFindQuery(query: string, optionalOptions: any) {
const options = optionalOptions || {}; const options: any = optionalOptions || {};
const interpolatedQuery = templateSrv.replace(query); const interpolatedQuery = this.templateSrv.replace(query);
// special handling for tag_values(<tag>[,<expression>]*), this is used for template variables // special handling for tag_values(<tag>[,<expression>]*), this is used for template variables
let matches = interpolatedQuery.match(/^tag_values\(([^,]+)((, *[^,]+)*)\)$/); let matches = interpolatedQuery.match(/^tag_values\(([^,]+)((, *[^,]+)*)\)$/);
...@@ -261,7 +284,7 @@ export function GraphiteDatasource(this: any, instanceSettings, $q, backendSrv, ...@@ -261,7 +284,7 @@ export function GraphiteDatasource(this: any, instanceSettings, $q, backendSrv,
httpOptions.params.until = this.translateTime(options.range.to, true, options.timezone); httpOptions.params.until = this.translateTime(options.range.to, true, options.timezone);
} }
return this.doGraphiteRequest(httpOptions).then(results => { return this.doGraphiteRequest(httpOptions).then((results: any) => {
return _.map(results.data, metric => { return _.map(results.data, metric => {
return { return {
text: metric.text, text: metric.text,
...@@ -269,9 +292,9 @@ export function GraphiteDatasource(this: any, instanceSettings, $q, backendSrv, ...@@ -269,9 +292,9 @@ export function GraphiteDatasource(this: any, instanceSettings, $q, backendSrv,
}; };
}); });
}); });
}; }
this.getTags = function(optionalOptions) { getTags(optionalOptions: any) {
const options = optionalOptions || {}; const options = optionalOptions || {};
const httpOptions: any = { const httpOptions: any = {
...@@ -294,14 +317,14 @@ export function GraphiteDatasource(this: any, instanceSettings, $q, backendSrv, ...@@ -294,14 +317,14 @@ export function GraphiteDatasource(this: any, instanceSettings, $q, backendSrv,
}; };
}); });
}); });
}; }
this.getTagValues = function(tag, optionalOptions) { getTagValues(tag: string, optionalOptions: any) {
const options = optionalOptions || {}; const options = optionalOptions || {};
const httpOptions: any = { const httpOptions: any = {
method: 'GET', method: 'GET',
url: '/tags/' + templateSrv.replace(tag), url: '/tags/' + this.templateSrv.replace(tag),
// for cancellations // for cancellations
requestId: options.requestId, requestId: options.requestId,
}; };
...@@ -311,7 +334,7 @@ export function GraphiteDatasource(this: any, instanceSettings, $q, backendSrv, ...@@ -311,7 +334,7 @@ export function GraphiteDatasource(this: any, instanceSettings, $q, backendSrv,
httpOptions.params.until = this.translateTime(options.range.to, true, options.timezone); httpOptions.params.until = this.translateTime(options.range.to, true, options.timezone);
} }
return this.doGraphiteRequest(httpOptions).then(results => { return this.doGraphiteRequest(httpOptions).then((results: any) => {
if (results.data && results.data.values) { if (results.data && results.data.values) {
return _.map(results.data.values, value => { return _.map(results.data.values, value => {
return { return {
...@@ -323,16 +346,16 @@ export function GraphiteDatasource(this: any, instanceSettings, $q, backendSrv, ...@@ -323,16 +346,16 @@ export function GraphiteDatasource(this: any, instanceSettings, $q, backendSrv,
return []; return [];
} }
}); });
}; }
this.getTagsAutoComplete = (expressions, tagPrefix, optionalOptions) => { getTagsAutoComplete(expressions: any[], tagPrefix: any, optionalOptions: any) {
const options = optionalOptions || {}; const options = optionalOptions || {};
const httpOptions: any = { const httpOptions: any = {
method: 'GET', method: 'GET',
url: '/tags/autoComplete/tags', url: '/tags/autoComplete/tags',
params: { params: {
expr: _.map(expressions, expression => templateSrv.replace((expression || '').trim())), expr: _.map(expressions, expression => this.templateSrv.replace((expression || '').trim())),
}, },
// for cancellations // for cancellations
requestId: options.requestId, requestId: options.requestId,
...@@ -358,17 +381,17 @@ export function GraphiteDatasource(this: any, instanceSettings, $q, backendSrv, ...@@ -358,17 +381,17 @@ export function GraphiteDatasource(this: any, instanceSettings, $q, backendSrv,
return []; return [];
} }
}); });
}; }
this.getTagValuesAutoComplete = (expressions, tag, valuePrefix, optionalOptions) => { getTagValuesAutoComplete(expressions: any[], tag: any, valuePrefix: any, optionalOptions: any) {
const options = optionalOptions || {}; const options = optionalOptions || {};
const httpOptions: any = { const httpOptions: any = {
method: 'GET', method: 'GET',
url: '/tags/autoComplete/values', url: '/tags/autoComplete/values',
params: { params: {
expr: _.map(expressions, expression => templateSrv.replace((expression || '').trim())), expr: _.map(expressions, expression => this.templateSrv.replace((expression || '').trim())),
tag: templateSrv.replace((tag || '').trim()), tag: this.templateSrv.replace((tag || '').trim()),
}, },
// for cancellations // for cancellations
requestId: options.requestId, requestId: options.requestId,
...@@ -385,7 +408,7 @@ export function GraphiteDatasource(this: any, instanceSettings, $q, backendSrv, ...@@ -385,7 +408,7 @@ export function GraphiteDatasource(this: any, instanceSettings, $q, backendSrv,
httpOptions.params.until = this.translateTime(options.range.to, true, options.timezone); httpOptions.params.until = this.translateTime(options.range.to, true, options.timezone);
} }
return this.doGraphiteRequest(httpOptions).then(results => { return this.doGraphiteRequest(httpOptions).then((results: any) => {
if (results.data) { if (results.data) {
return _.map(results.data, value => { return _.map(results.data, value => {
return { text: value }; return { text: value };
...@@ -394,9 +417,9 @@ export function GraphiteDatasource(this: any, instanceSettings, $q, backendSrv, ...@@ -394,9 +417,9 @@ export function GraphiteDatasource(this: any, instanceSettings, $q, backendSrv,
return []; return [];
} }
}); });
}; }
this.getVersion = function(optionalOptions) { getVersion(optionalOptions: any) {
const options = optionalOptions || {}; const options = optionalOptions || {};
const httpOptions = { const httpOptions = {
...@@ -406,7 +429,7 @@ export function GraphiteDatasource(this: any, instanceSettings, $q, backendSrv, ...@@ -406,7 +429,7 @@ export function GraphiteDatasource(this: any, instanceSettings, $q, backendSrv,
}; };
return this.doGraphiteRequest(httpOptions) return this.doGraphiteRequest(httpOptions)
.then(results => { .then((results: any) => {
if (results.data) { if (results.data) {
const semver = new SemVersion(results.data); const semver = new SemVersion(results.data);
return semver.isValid() ? results.data : ''; return semver.isValid() ? results.data : '';
...@@ -416,21 +439,21 @@ export function GraphiteDatasource(this: any, instanceSettings, $q, backendSrv, ...@@ -416,21 +439,21 @@ export function GraphiteDatasource(this: any, instanceSettings, $q, backendSrv,
.catch(() => { .catch(() => {
return ''; return '';
}); });
}; }
this.createFuncInstance = function(funcDef, options?) { createFuncInstance(funcDef: any, options?: any) {
return gfunc.createFuncInstance(funcDef, options, this.funcDefs); return gfunc.createFuncInstance(funcDef, options, this.funcDefs);
}; }
this.getFuncDef = function(name) { getFuncDef(name: string) {
return gfunc.getFuncDef(name, this.funcDefs); return gfunc.getFuncDef(name, this.funcDefs);
}; }
this.waitForFuncDefsLoaded = function() { waitForFuncDefsLoaded() {
return this.getFuncDefs(); return this.getFuncDefs();
}; }
this.getFuncDefs = function() { getFuncDefs() {
if (this.funcDefsPromise !== null) { if (this.funcDefsPromise !== null) {
return this.funcDefsPromise; return this.funcDefsPromise;
} }
...@@ -447,7 +470,7 @@ export function GraphiteDatasource(this: any, instanceSettings, $q, backendSrv, ...@@ -447,7 +470,7 @@ export function GraphiteDatasource(this: any, instanceSettings, $q, backendSrv,
}; };
this.funcDefsPromise = this.doGraphiteRequest(httpOptions) this.funcDefsPromise = this.doGraphiteRequest(httpOptions)
.then(results => { .then((results: any) => {
if (results.status !== 200 || typeof results.data !== 'object') { if (results.status !== 200 || typeof results.data !== 'object') {
this.funcDefs = gfunc.getFuncDefs(this.graphiteVersion); this.funcDefs = gfunc.getFuncDefs(this.graphiteVersion);
} else { } else {
...@@ -462,9 +485,9 @@ export function GraphiteDatasource(this: any, instanceSettings, $q, backendSrv, ...@@ -462,9 +485,9 @@ export function GraphiteDatasource(this: any, instanceSettings, $q, backendSrv,
}); });
return this.funcDefsPromise; return this.funcDefsPromise;
}; }
this.testDatasource = function() { testDatasource() {
const query = { const query = {
panelId: 3, panelId: 3,
rangeRaw: { from: 'now-1h', to: 'now' }, rangeRaw: { from: 'now-1h', to: 'now' },
...@@ -474,9 +497,16 @@ export function GraphiteDatasource(this: any, instanceSettings, $q, backendSrv, ...@@ -474,9 +497,16 @@ export function GraphiteDatasource(this: any, instanceSettings, $q, backendSrv,
return this.query(query).then(() => { return this.query(query).then(() => {
return { status: 'success', message: 'Data source is working' }; return { status: 'success', message: 'Data source is working' };
}); });
}; }
this.doGraphiteRequest = function(options) { doGraphiteRequest(options: {
method?: string;
url: any;
requestId?: any;
withCredentials?: any;
headers?: any;
inspect?: any;
}) {
if (this.basicAuth || this.withCredentials) { if (this.basicAuth || this.withCredentials) {
options.withCredentials = true; options.withCredentials = true;
} }
...@@ -488,12 +518,10 @@ export function GraphiteDatasource(this: any, instanceSettings, $q, backendSrv, ...@@ -488,12 +518,10 @@ export function GraphiteDatasource(this: any, instanceSettings, $q, backendSrv,
options.url = this.url + options.url; options.url = this.url + options.url;
options.inspect = { type: 'graphite' }; options.inspect = { type: 'graphite' };
return backendSrv.datasourceRequest(options); return this.backendSrv.datasourceRequest(options);
}; }
this._seriesRefLetters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
this.buildGraphiteParams = function(options, scopedVars) { buildGraphiteParams(options: any, scopedVars: ScopedVars) {
const graphiteOptions = ['from', 'until', 'rawData', 'format', 'maxDataPoints', 'cacheTimeout']; const graphiteOptions = ['from', 'until', 'rawData', 'format', 'maxDataPoints', 'cacheTimeout'];
const cleanOptions = [], const cleanOptions = [],
targets = {}; targets = {};
...@@ -504,7 +532,7 @@ export function GraphiteDatasource(this: any, instanceSettings, $q, backendSrv, ...@@ -504,7 +532,7 @@ export function GraphiteDatasource(this: any, instanceSettings, $q, backendSrv,
options['format'] = 'json'; options['format'] = 'json';
function fixIntervalFormat(match) { function fixIntervalFormat(match: any) {
return match.replace('m', 'min').replace('M', 'mon'); return match.replace('m', 'min').replace('M', 'mon');
} }
...@@ -518,12 +546,12 @@ export function GraphiteDatasource(this: any, instanceSettings, $q, backendSrv, ...@@ -518,12 +546,12 @@ export function GraphiteDatasource(this: any, instanceSettings, $q, backendSrv,
target.refId = this._seriesRefLetters[i]; target.refId = this._seriesRefLetters[i];
} }
targetValue = templateSrv.replace(target.target, scopedVars); targetValue = this.templateSrv.replace(target.target, scopedVars);
targetValue = targetValue.replace(intervalFormatFixRegex, fixIntervalFormat); targetValue = targetValue.replace(intervalFormatFixRegex, fixIntervalFormat);
targets[target.refId] = targetValue; targets[target.refId] = targetValue;
} }
function nestedSeriesRegexReplacer(match, g1) { function nestedSeriesRegexReplacer(match: any, g1: string | number) {
return targets[g1] || match; return targets[g1] || match;
} }
...@@ -557,7 +585,7 @@ export function GraphiteDatasource(this: any, instanceSettings, $q, backendSrv, ...@@ -557,7 +585,7 @@ export function GraphiteDatasource(this: any, instanceSettings, $q, backendSrv,
} }
return cleanOptions; return cleanOptions;
}; }
} }
function supportsTags(version: string): boolean { function supportsTags(version: string): boolean {
......
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