浏览代码

feat(plugins): upgraded opentsdb

Torkel Ödegaard 10 年之前
父节点
当前提交
35f40b7312

+ 3 - 3
public/app/core/routes/all.js

@@ -42,17 +42,17 @@ define([
         controller : 'DashboardImportCtrl',
       })
       .when('/datasources', {
-        templateUrl: 'app/features/org/partials/datasources.html',
+        templateUrl: 'app/features/datasources/partials/list.html',
         controller : 'DataSourcesCtrl',
         resolve: loadOrgBundle,
       })
       .when('/datasources/edit/:id', {
-        templateUrl: 'app/features/org/partials/datasourceEdit.html',
+        templateUrl: 'app/features/datasources/partials/edit.html',
         controller : 'DataSourceEditCtrl',
         resolve: loadOrgBundle,
       })
       .when('/datasources/new', {
-        templateUrl: 'app/features/org/partials/datasourceEdit.html',
+        templateUrl: 'app/features/datasources/partials/edit.html',
         controller : 'DataSourceEditCtrl',
         resolve: loadOrgBundle,
       })

+ 3 - 0
public/app/plugins/datasource/opentsdb/datasource.d.ts

@@ -0,0 +1,3 @@
+declare var Datasource: any;
+export default Datasource;
+

+ 16 - 22
public/app/plugins/datasource/opentsdb/datasource.js

@@ -3,25 +3,19 @@ define([
   'lodash',
   'app/core/utils/datemath',
   'moment',
-  './directives',
   './queryCtrl',
 ],
 function (angular, _, dateMath) {
   'use strict';
 
-  var module = angular.module('grafana.services');
-
-  module.factory('OpenTSDBDatasource', function($q, backendSrv, templateSrv) {
-
-    function OpenTSDBDatasource(datasource) {
-      this.type = 'opentsdb';
-      this.url = datasource.url;
-      this.name = datasource.name;
-      this.supportMetrics = true;
-    }
+  function OpenTSDBDatasource(instanceSettings, $q, backendSrv, templateSrv) {
+    this.type = 'opentsdb';
+    this.url = instanceSettings.url;
+    this.name = instanceSettings.name;
+    this.supportMetrics = true;
 
     // Called once per panel (graph)
-    OpenTSDBDatasource.prototype.query = function(options) {
+    this.query = function(options) {
       var start = convertToTSDBTime(options.rangeRaw.from, false);
       var end = convertToTSDBTime(options.rangeRaw.to, true);
       var qs = [];
@@ -60,7 +54,7 @@ function (angular, _, dateMath) {
       });
     };
 
-    OpenTSDBDatasource.prototype.performTimeSeriesQuery = function(queries, start, end) {
+    this.performTimeSeriesQuery = function(queries, start, end) {
       var reqBody = {
         start: start,
         queries: queries
@@ -80,13 +74,13 @@ function (angular, _, dateMath) {
       return backendSrv.datasourceRequest(options);
     };
 
-    OpenTSDBDatasource.prototype._performSuggestQuery = function(query, type) {
+    this._performSuggestQuery = function(query, type) {
       return this._get('/api/suggest', {type: type, q: query, max: 1000}).then(function(result) {
         return result.data;
       });
     };
 
-    OpenTSDBDatasource.prototype._performMetricKeyValueLookup = function(metric, key) {
+    this._performMetricKeyValueLookup = function(metric, key) {
       if(!metric || !key) {
         return $q.when([]);
       }
@@ -105,7 +99,7 @@ function (angular, _, dateMath) {
       });
     };
 
-    OpenTSDBDatasource.prototype._performMetricKeyLookup = function(metric) {
+    this._performMetricKeyLookup = function(metric) {
       if(!metric) { return $q.when([]); }
 
       return this._get('/api/search/lookup', {m: metric, limit: 1000}).then(function(result) {
@@ -122,7 +116,7 @@ function (angular, _, dateMath) {
       });
     };
 
-    OpenTSDBDatasource.prototype._get = function(relativeUrl, params) {
+    this._get = function(relativeUrl, params) {
       return backendSrv.datasourceRequest({
         method: 'GET',
         url: this.url + relativeUrl,
@@ -130,7 +124,7 @@ function (angular, _, dateMath) {
       });
     };
 
-    OpenTSDBDatasource.prototype.metricFindQuery = function(query) {
+    this.metricFindQuery = function(query) {
       if (!query) { return $q.when([]); }
 
       var interpolated;
@@ -181,14 +175,14 @@ function (angular, _, dateMath) {
       return $q.when([]);
     };
 
-    OpenTSDBDatasource.prototype.testDatasource = function() {
+    this.testDatasource = function() {
       return this._performSuggestQuery('cpu', 'metrics').then(function () {
         return { status: "success", message: "Data source is working", title: "Success" };
       });
     };
 
     var aggregatorsPromise = null;
-    OpenTSDBDatasource.prototype.getAggregators = function() {
+    this.getAggregators = function() {
       if (aggregatorsPromise) { return aggregatorsPromise; }
 
       aggregatorsPromise =  this._get('/api/aggregators').then(function(result) {
@@ -311,7 +305,7 @@ function (angular, _, dateMath) {
       return date.valueOf();
     }
 
-    return OpenTSDBDatasource;
-  });
+  }
 
+  return OpenTSDBDatasource;
 });

+ 9 - 1
public/app/plugins/datasource/opentsdb/directives.js → public/app/plugins/datasource/opentsdb/module.js

@@ -1,7 +1,8 @@
 define([
   'angular',
+  './datasource',
 ],
-function (angular) {
+function (angular, OpenTsDatasource) {
   'use strict';
 
   var module = angular.module('grafana.directives');
@@ -13,4 +14,11 @@ function (angular) {
     };
   });
 
+  module.directive('datasourceCustomSettingsViewOpentsdb', function() {
+    return {templateUrl: 'app/plugins/datasource/opentsdb/partials/config.html'};
+  });
+
+  return {
+    Datasource: OpenTsDatasource
+  };
 });

+ 1 - 6
public/app/plugins/datasource/opentsdb/plugin.json

@@ -3,12 +3,7 @@
   "name": "OpenTSDB",
   "id": "opentsdb",
 
-  "serviceName": "OpenTSDBDatasource",
-  "module": "app/plugins/datasource/opentsdb/datasource",
-
-  "partials": {
-    "config": "app/plugins/datasource/opentsdb/partials/config.html"
-  },
+  "module": "app/plugins/datasource/opentsdb/module",
 
   "metrics": true,
   "defaultMatchFormat": "pipe"

+ 71 - 0
public/app/plugins/datasource/opentsdb/specs/datasource-specs.ts

@@ -0,0 +1,71 @@
+import {describe, beforeEach, it, sinon, expect, angularMocks} from 'test/lib/common';
+import helpers from 'test/specs/helpers';
+import Datasource from "../datasource";
+
+describe('opentsdb', function() {
+  var ctx = new helpers.ServiceTestContext();
+  var instanceSettings = {url: '' };
+
+  beforeEach(angularMocks.module('grafana.core'));
+  beforeEach(angularMocks.module('grafana.services'));
+  beforeEach(ctx.providePhase(['backendSrv']));
+
+  beforeEach(angularMocks.inject(function($q, $rootScope, $httpBackend, $injector) {
+    ctx.$q = $q;
+    ctx.$httpBackend =  $httpBackend;
+    ctx.$rootScope = $rootScope;
+    ctx.ds = $injector.instantiate(Datasource, {instanceSettings: instanceSettings});
+  }));
+
+  describe('When performing metricFindQuery', function() {
+    var results;
+    var requestOptions;
+
+    beforeEach(function() {
+      ctx.backendSrv.datasourceRequest = function(options) {
+        requestOptions = options;
+        return ctx.$q.when({data: [{ target: 'prod1.count', datapoints: [[10, 1], [12,1]] }]});
+      };
+    });
+
+    it('metrics() should generate api suggest query', function() {
+      ctx.ds.metricFindQuery('metrics(pew)').then(function(data) { results = data; });
+      ctx.$rootScope.$apply();
+      expect(requestOptions.url).to.be('/api/suggest');
+      expect(requestOptions.params.type).to.be('metrics');
+      expect(requestOptions.params.q).to.be('pew');
+    });
+
+    it('tag_names(cpu) should generate looku  query', function() {
+      ctx.ds.metricFindQuery('tag_names(cpu)').then(function(data) { results = data; });
+      ctx.$rootScope.$apply();
+      expect(requestOptions.url).to.be('/api/search/lookup');
+      expect(requestOptions.params.m).to.be('cpu');
+    });
+
+    it('tag_values(cpu, test) should generate looku  query', function() {
+      ctx.ds.metricFindQuery('tag_values(cpu, hostname)').then(function(data) { results = data; });
+      ctx.$rootScope.$apply();
+      expect(requestOptions.url).to.be('/api/search/lookup');
+      expect(requestOptions.params.m).to.be('cpu{hostname=*}');
+    });
+
+    it('suggest_tagk() should generate api suggest query', function() {
+      ctx.ds.metricFindQuery('suggest_tagk(foo)').then(function(data) { results = data; });
+      ctx.$rootScope.$apply();
+      expect(requestOptions.url).to.be('/api/suggest');
+      expect(requestOptions.params.type).to.be('tagk');
+      expect(requestOptions.params.q).to.be('foo');
+    });
+
+    it('suggest_tagv() should generate api suggest query', function() {
+      ctx.ds.metricFindQuery('suggest_tagv(bar)').then(function(data) { results = data; });
+      ctx.$rootScope.$apply();
+      expect(requestOptions.url).to.be('/api/suggest');
+      expect(requestOptions.params.type).to.be('tagv');
+      expect(requestOptions.params.q).to.be('bar');
+    });
+  });
+
+});
+

+ 0 - 71
public/test/specs/opentsdbDatasource-specs.js

@@ -1,71 +0,0 @@
-define([
-  './helpers',
-  'app/plugins/datasource/opentsdb/datasource'
-], function(helpers) {
-  'use strict';
-
-  describe('opentsdb', function() {
-    var ctx = new helpers.ServiceTestContext();
-
-    beforeEach(module('grafana.core'));
-    beforeEach(module('grafana.services'));
-    beforeEach(ctx.providePhase(['backendSrv']));
-
-    beforeEach(ctx.createService('OpenTSDBDatasource'));
-    beforeEach(function() {
-      ctx.ds = new ctx.service({ url: [''] });
-    });
-
-    describe('When performing metricFindQuery', function() {
-      var results;
-      var requestOptions;
-
-      beforeEach(function() {
-        ctx.backendSrv.datasourceRequest = function(options) {
-          requestOptions = options;
-          return ctx.$q.when({data: [{ target: 'prod1.count', datapoints: [[10, 1], [12,1]] }]});
-        };
-      });
-
-      it('metrics() should generate api suggest query', function() {
-        ctx.ds.metricFindQuery('metrics(pew)').then(function(data) { results = data; });
-        ctx.$rootScope.$apply();
-        expect(requestOptions.url).to.be('/api/suggest');
-        expect(requestOptions.params.type).to.be('metrics');
-        expect(requestOptions.params.q).to.be('pew');
-      });
-
-      it('tag_names(cpu) should generate looku  query', function() {
-        ctx.ds.metricFindQuery('tag_names(cpu)').then(function(data) { results = data; });
-        ctx.$rootScope.$apply();
-        expect(requestOptions.url).to.be('/api/search/lookup');
-        expect(requestOptions.params.m).to.be('cpu');
-      });
-
-      it('tag_values(cpu, test) should generate looku  query', function() {
-        ctx.ds.metricFindQuery('tag_values(cpu, hostname)').then(function(data) { results = data; });
-        ctx.$rootScope.$apply();
-        expect(requestOptions.url).to.be('/api/search/lookup');
-        expect(requestOptions.params.m).to.be('cpu{hostname=*}');
-      });
-
-      it('suggest_tagk() should generate api suggest query', function() {
-        ctx.ds.metricFindQuery('suggest_tagk(foo)').then(function(data) { results = data; });
-        ctx.$rootScope.$apply();
-        expect(requestOptions.url).to.be('/api/suggest');
-        expect(requestOptions.params.type).to.be('tagk');
-        expect(requestOptions.params.q).to.be('foo');
-      });
-
-      it('suggest_tagv() should generate api suggest query', function() {
-        ctx.ds.metricFindQuery('suggest_tagv(bar)').then(function(data) { results = data; });
-        ctx.$rootScope.$apply();
-        expect(requestOptions.url).to.be('/api/suggest');
-        expect(requestOptions.params.type).to.be('tagv');
-        expect(requestOptions.params.q).to.be('bar');
-      });
-
-    });
-  });
-});
-