Commit f813b4c5 by Torkel Ödegaard

feat(plugins): converted graphite plugin to new format

parent c5635f9c
...@@ -12,6 +12,7 @@ define([ ...@@ -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;
......
declare var Datasource: any;
export {Datasource};
...@@ -12,20 +12,16 @@ define([ ...@@ -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) {
module.factory('GraphiteDatasource', function($q, backendSrv, templateSrv) { this.basicAuth = instanceSettings.basicAuth;
this.url = instanceSettings.url;
function GraphiteDatasource(datasource) { this.name = instanceSettings.name;
this.basicAuth = datasource.basicAuth; this.cacheTimeout = instanceSettings.cacheTimeout;
this.url = datasource.url; this.withCredentials = instanceSettings.withCredentials;
this.name = datasource.name; this.render_method = instanceSettings.render_method || 'POST';
this.cacheTimeout = datasource.cacheTimeout;
this.withCredentials = datasource.withCredentials; this.query = function(options) {
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) { ...@@ -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) { ...@@ -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);
...@@ -109,8 +105,7 @@ function (angular, _, $, config, dateMath) { ...@@ -109,8 +105,7 @@ function (angular, _, $, config, dateMath) {
// 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];
...@@ -128,7 +123,7 @@ function (angular, _, $, config, dateMath) { ...@@ -128,7 +123,7 @@ function (angular, _, $, config, dateMath) {
} }
}; };
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) { ...@@ -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) { ...@@ -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) { ...@@ -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) { ...@@ -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) { ...@@ -296,12 +291,9 @@ function (angular, _, $, config, dateMath) {
return clean_options; return clean_options;
}; };
}
return GraphiteDatasource;
});
return { return {
serviceName: "GraphiteDatasource" Datasource: GraphiteDatasource
}; };
}); });
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() {
......
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