mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
feat(plugins): converted graphite plugin to new format
This commit is contained in:
@@ -12,6 +12,7 @@ define([
|
|||||||
function (angular, _, moment, kbn, ElasticQueryBuilder, IndexPattern, ElasticResponse) {
|
function (angular, _, moment, kbn, ElasticQueryBuilder, IndexPattern, ElasticResponse) {
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
|
/** @ngInject */
|
||||||
function ElasticDatasource(instanceSettings, $q, backendSrv, templateSrv, timeSrv) {
|
function ElasticDatasource(instanceSettings, $q, backendSrv, templateSrv, timeSrv) {
|
||||||
this.basicAuth = instanceSettings.basicAuth;
|
this.basicAuth = instanceSettings.basicAuth;
|
||||||
this.withCredentials = instanceSettings.withCredentials;
|
this.withCredentials = instanceSettings.withCredentials;
|
||||||
|
|||||||
3
public/app/plugins/datasource/graphite/datasource.d.ts
vendored
Normal file
3
public/app/plugins/datasource/graphite/datasource.d.ts
vendored
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
declare var Datasource: any;
|
||||||
|
export {Datasource};
|
||||||
|
|
||||||
@@ -12,20 +12,16 @@ define([
|
|||||||
function (angular, _, $, config, dateMath) {
|
function (angular, _, $, config, dateMath) {
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
var module = angular.module('grafana.services');
|
/** @ngInject */
|
||||||
|
function GraphiteDatasource(instanceSettings, $q, backendSrv, templateSrv) {
|
||||||
|
this.basicAuth = instanceSettings.basicAuth;
|
||||||
|
this.url = instanceSettings.url;
|
||||||
|
this.name = instanceSettings.name;
|
||||||
|
this.cacheTimeout = instanceSettings.cacheTimeout;
|
||||||
|
this.withCredentials = instanceSettings.withCredentials;
|
||||||
|
this.render_method = instanceSettings.render_method || 'POST';
|
||||||
|
|
||||||
module.factory('GraphiteDatasource', function($q, backendSrv, templateSrv) {
|
this.query = function(options) {
|
||||||
|
|
||||||
function GraphiteDatasource(datasource) {
|
|
||||||
this.basicAuth = datasource.basicAuth;
|
|
||||||
this.url = datasource.url;
|
|
||||||
this.name = datasource.name;
|
|
||||||
this.cacheTimeout = datasource.cacheTimeout;
|
|
||||||
this.withCredentials = datasource.withCredentials;
|
|
||||||
this.render_method = datasource.render_method || 'POST';
|
|
||||||
}
|
|
||||||
|
|
||||||
GraphiteDatasource.prototype.query = function(options) {
|
|
||||||
try {
|
try {
|
||||||
var graphOptions = {
|
var graphOptions = {
|
||||||
from: this.translateTime(options.rangeRaw.from, false),
|
from: this.translateTime(options.rangeRaw.from, false),
|
||||||
@@ -62,7 +58,7 @@ function (angular, _, $, config, dateMath) {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
GraphiteDatasource.prototype.convertDataPointsToMs = function(result) {
|
this.convertDataPointsToMs = function(result) {
|
||||||
if (!result || !result.data) { return []; }
|
if (!result || !result.data) { return []; }
|
||||||
for (var i = 0; i < result.data.length; i++) {
|
for (var i = 0; i < result.data.length; i++) {
|
||||||
var series = result.data[i];
|
var series = result.data[i];
|
||||||
@@ -73,7 +69,7 @@ function (angular, _, $, config, dateMath) {
|
|||||||
return result;
|
return result;
|
||||||
};
|
};
|
||||||
|
|
||||||
GraphiteDatasource.prototype.annotationQuery = function(options) {
|
this.annotationQuery = function(options) {
|
||||||
// Graphite metric as annotation
|
// Graphite metric as annotation
|
||||||
if (options.annotation.target) {
|
if (options.annotation.target) {
|
||||||
var target = templateSrv.replace(options.annotation.target);
|
var target = templateSrv.replace(options.annotation.target);
|
||||||
@@ -85,50 +81,49 @@ function (angular, _, $, config, dateMath) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
return this.query(graphiteQuery)
|
return this.query(graphiteQuery)
|
||||||
.then(function(result) {
|
.then(function(result) {
|
||||||
var list = [];
|
var list = [];
|
||||||
|
|
||||||
for (var i = 0; i < result.data.length; i++) {
|
for (var i = 0; i < result.data.length; i++) {
|
||||||
var target = result.data[i];
|
var target = result.data[i];
|
||||||
|
|
||||||
for (var y = 0; y < target.datapoints.length; y++) {
|
for (var y = 0; y < target.datapoints.length; y++) {
|
||||||
var datapoint = target.datapoints[y];
|
var datapoint = target.datapoints[y];
|
||||||
if (!datapoint[0]) { continue; }
|
if (!datapoint[0]) { continue; }
|
||||||
|
|
||||||
list.push({
|
list.push({
|
||||||
annotation: options.annotation,
|
annotation: options.annotation,
|
||||||
time: datapoint[1],
|
time: datapoint[1],
|
||||||
title: target.target
|
title: target.target
|
||||||
});
|
});
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return list;
|
return list;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
// Graphite event as annotation
|
// Graphite event as annotation
|
||||||
else {
|
else {
|
||||||
var tags = templateSrv.replace(options.annotation.tags);
|
var tags = templateSrv.replace(options.annotation.tags);
|
||||||
return this.events({range: options.rangeRaw, tags: tags})
|
return this.events({range: options.rangeRaw, tags: tags}).then(function(results) {
|
||||||
.then(function(results) {
|
var list = [];
|
||||||
var list = [];
|
for (var i = 0; i < results.data.length; i++) {
|
||||||
for (var i = 0; i < results.data.length; i++) {
|
var e = results.data[i];
|
||||||
var e = results.data[i];
|
|
||||||
|
|
||||||
list.push({
|
list.push({
|
||||||
annotation: options.annotation,
|
annotation: options.annotation,
|
||||||
time: e.when * 1000,
|
time: e.when * 1000,
|
||||||
title: e.what,
|
title: e.what,
|
||||||
tags: e.tags,
|
tags: e.tags,
|
||||||
text: e.data
|
text: e.data
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
return list;
|
return list;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
GraphiteDatasource.prototype.events = function(options) {
|
this.events = function(options) {
|
||||||
try {
|
try {
|
||||||
var tags = '';
|
var tags = '';
|
||||||
if (options.tags) {
|
if (options.tags) {
|
||||||
@@ -146,7 +141,7 @@ function (angular, _, $, config, dateMath) {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
GraphiteDatasource.prototype.translateTime = function(date, roundUp) {
|
this.translateTime = function(date, roundUp) {
|
||||||
if (_.isString(date)) {
|
if (_.isString(date)) {
|
||||||
if (date === 'now') {
|
if (date === 'now') {
|
||||||
return 'now';
|
return 'now';
|
||||||
@@ -178,7 +173,7 @@ function (angular, _, $, config, dateMath) {
|
|||||||
return date.unix();
|
return date.unix();
|
||||||
};
|
};
|
||||||
|
|
||||||
GraphiteDatasource.prototype.metricFindQuery = function(query) {
|
this.metricFindQuery = function(query) {
|
||||||
var interpolated;
|
var interpolated;
|
||||||
try {
|
try {
|
||||||
interpolated = encodeURIComponent(templateSrv.replace(query));
|
interpolated = encodeURIComponent(templateSrv.replace(query));
|
||||||
@@ -198,24 +193,24 @@ function (angular, _, $, config, dateMath) {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
GraphiteDatasource.prototype.testDatasource = function() {
|
this.testDatasource = function() {
|
||||||
return this.metricFindQuery('*').then(function () {
|
return this.metricFindQuery('*').then(function () {
|
||||||
return { status: "success", message: "Data source is working", title: "Success" };
|
return { status: "success", message: "Data source is working", title: "Success" };
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
GraphiteDatasource.prototype.listDashboards = function(query) {
|
this.listDashboards = function(query) {
|
||||||
return this.doGraphiteRequest({ method: 'GET', url: '/dashboard/find/', params: {query: query || ''} })
|
return this.doGraphiteRequest({ method: 'GET', url: '/dashboard/find/', params: {query: query || ''} })
|
||||||
.then(function(results) {
|
.then(function(results) {
|
||||||
return results.data.dashboards;
|
return results.data.dashboards;
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
GraphiteDatasource.prototype.loadDashboard = function(dashName) {
|
this.loadDashboard = function(dashName) {
|
||||||
return this.doGraphiteRequest({method: 'GET', url: '/dashboard/load/' + encodeURIComponent(dashName) });
|
return this.doGraphiteRequest({method: 'GET', url: '/dashboard/load/' + encodeURIComponent(dashName) });
|
||||||
};
|
};
|
||||||
|
|
||||||
GraphiteDatasource.prototype.doGraphiteRequest = function(options) {
|
this.doGraphiteRequest = function(options) {
|
||||||
if (this.basicAuth || this.withCredentials) {
|
if (this.basicAuth || this.withCredentials) {
|
||||||
options.withCredentials = true;
|
options.withCredentials = true;
|
||||||
}
|
}
|
||||||
@@ -230,9 +225,9 @@ function (angular, _, $, config, dateMath) {
|
|||||||
return backendSrv.datasourceRequest(options);
|
return backendSrv.datasourceRequest(options);
|
||||||
};
|
};
|
||||||
|
|
||||||
GraphiteDatasource.prototype._seriesRefLetters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
this._seriesRefLetters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
||||||
|
|
||||||
GraphiteDatasource.prototype.buildGraphiteParams = function(options, scopedVars) {
|
this.buildGraphiteParams = function(options, scopedVars) {
|
||||||
var graphite_options = ['from', 'until', 'rawData', 'format', 'maxDataPoints', 'cacheTimeout'];
|
var graphite_options = ['from', 'until', 'rawData', 'format', 'maxDataPoints', 'cacheTimeout'];
|
||||||
var clean_options = [], targets = {};
|
var clean_options = [], targets = {};
|
||||||
var target, targetValue, i;
|
var target, targetValue, i;
|
||||||
@@ -296,12 +291,9 @@ function (angular, _, $, config, dateMath) {
|
|||||||
|
|
||||||
return clean_options;
|
return clean_options;
|
||||||
};
|
};
|
||||||
|
}
|
||||||
return GraphiteDatasource;
|
|
||||||
|
|
||||||
});
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
serviceName: "GraphiteDatasource"
|
Datasource: GraphiteDatasource
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,19 +1,24 @@
|
|||||||
|
|
||||||
import "../datasource";
|
|
||||||
import {describe, beforeEach, it, sinon, expect, angularMocks} from 'test/lib/common';
|
import {describe, beforeEach, it, sinon, expect, angularMocks} from 'test/lib/common';
|
||||||
import helpers from 'test/specs/helpers';
|
import helpers from 'test/specs/helpers';
|
||||||
|
import {Datasource} from "../datasource";
|
||||||
|
|
||||||
describe('graphiteDatasource', function() {
|
describe('graphiteDatasource', function() {
|
||||||
var ctx = new helpers.ServiceTestContext();
|
var ctx = new helpers.ServiceTestContext();
|
||||||
|
var instanceSettings: any = {url:['']};
|
||||||
|
|
||||||
beforeEach(angularMocks.module('grafana.core'));
|
beforeEach(angularMocks.module('grafana.core'));
|
||||||
beforeEach(angularMocks.module('grafana.services'));
|
beforeEach(angularMocks.module('grafana.services'));
|
||||||
|
|
||||||
beforeEach(ctx.providePhase(['backendSrv']));
|
beforeEach(ctx.providePhase(['backendSrv']));
|
||||||
beforeEach(ctx.createService('GraphiteDatasource'));
|
beforeEach(angularMocks.inject(function($q, $rootScope, $httpBackend, $injector) {
|
||||||
|
ctx.$q = $q;
|
||||||
|
ctx.$httpBackend = $httpBackend;
|
||||||
|
ctx.$rootScope = $rootScope;
|
||||||
|
ctx.$injector = $injector;
|
||||||
|
}));
|
||||||
|
|
||||||
beforeEach(function() {
|
beforeEach(function() {
|
||||||
ctx.ds = new ctx.service({ url: [''] });
|
ctx.ds = ctx.$injector.instantiate(Datasource, {instanceSettings: instanceSettings});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('When querying influxdb with one target using query editor target spec', function() {
|
describe('When querying influxdb with one target using query editor target spec', function() {
|
||||||
|
|||||||
Reference in New Issue
Block a user