Просмотр исходного кода

Merge pull request #1814 from masaori335/kairosdb-test

Add a basic test of KairosDBDatasource
Torkel Ödegaard 10 лет назад
Родитель
Сommit
0d8e024c18

+ 11 - 14
public/app/plugins/datasource/kairosdb/datasource.js

@@ -44,7 +44,7 @@ function (angular, _, kbn) {
         d.resolve({ data: [] });
         d.resolve({ data: [] });
         return d.promise;
         return d.promise;
       }
       }
-      return this.performTimeSeriesQuery(queries, start, end).then(handleKairosDBQueryResponseAlias,handleQueryError);
+      return this.performTimeSeriesQuery(queries, start, end).then(handleKairosDBQueryResponseAlias, handleQueryError);
     };
     };
 
 
     ///////////////////////////////////////////////////////////////////////
     ///////////////////////////////////////////////////////////////////////
@@ -56,8 +56,8 @@ function (angular, _, kbn) {
         metrics: queries
         metrics: queries
       };
       };
       reqBody.cache_time = 0;
       reqBody.cache_time = 0;
-      convertToKairosTime(start,reqBody,'start');
-      convertToKairosTime(end,reqBody,'end');
+      convertToKairosTime(start, reqBody, 'start');
+      convertToKairosTime(end, reqBody, 'end');
       var options = {
       var options = {
         method: 'POST',
         method: 'POST',
         url: '/api/v1/datapoints/query',
         url: '/api/v1/datapoints/query',
@@ -86,10 +86,10 @@ function (angular, _, kbn) {
 
 
     };
     };
 
 
-    KairosDBDatasource.prototype.performTagSuggestQuery = function(metricname,range,type,keyValue) {
+    KairosDBDatasource.prototype.performTagSuggestQuery = function(metricname, range, type, keyValue) {
       if (tagList && (metricname === tagList.metricName) && (range.from === tagList.range.from) &&
       if (tagList && (metricname === tagList.metricName) && (range.from === tagList.range.from) &&
         (range.to === tagList.range.to)) {
         (range.to === tagList.range.to)) {
-        return getTagListFromResponse(tagList.results,type,keyValue);
+        return getTagListFromResponse(tagList.results, type, keyValue);
       }
       }
       tagList = {
       tagList = {
         metricName:metricname,
         metricName:metricname,
@@ -98,8 +98,8 @@ function (angular, _, kbn) {
       var body = {
       var body = {
         metrics : [{name : metricname}]
         metrics : [{name : metricname}]
       };
       };
-      convertToKairosTime(range.from,body,'start');
-      convertToKairosTime(range.to,body,'end');
+      convertToKairosTime(range.from, body, 'start');
+      convertToKairosTime(range.to, body, 'end');
       var options = {
       var options = {
         url : this.url + '/api/v1/datapoints/query/tags',
         url : this.url + '/api/v1/datapoints/query/tags',
         method : 'POST',
         method : 'POST',
@@ -107,7 +107,7 @@ function (angular, _, kbn) {
       };
       };
       return $http(options).then(function(results) {
       return $http(options).then(function(results) {
         tagList.results = results;
         tagList.results = results;
-        return getTagListFromResponse(results,type,keyValue);
+        return getTagListFromResponse(results, type, keyValue);
       });
       });
 
 
     };
     };
@@ -116,14 +116,14 @@ function (angular, _, kbn) {
     /// Formatting methods
     /// Formatting methods
     ////////////////////////////////////////////////////////////////////////
     ////////////////////////////////////////////////////////////////////////
 
 
-    function getTagListFromResponse(results,type,keyValue) {
+    function getTagListFromResponse(results, type, keyValue) {
       if (!results.data) {
       if (!results.data) {
         return [];
         return [];
       }
       }
       if (type === "key") {
       if (type === "key") {
         return _.keys(results.data.queries[0].results[0].tags);
         return _.keys(results.data.queries[0].results[0].tags);
       }
       }
-      else if (type === "value" && _.has(results.data.queries[0].results[0].tags,keyValue)) {
+      else if (type === "value" && _.has(results.data.queries[0].results[0].tags, keyValue)) {
         return results.data.queries[0].results[0].tags[keyValue];
         return results.data.queries[0].results[0].tags[keyValue];
       }
       }
       return [];
       return [];
@@ -150,9 +150,6 @@ function (angular, _, kbn) {
       var output = [];
       var output = [];
       var index = 0;
       var index = 0;
       _.each(results.data.queries, function(series) {
       _.each(results.data.queries, function(series) {
-        var sample_size = series.sample_size;
-        console.log("sample_size:" + sample_size + " samples");
-
         _.each(series.results, function(result) {
         _.each(series.results, function(result) {
 
 
           //var target = result.name;
           //var target = result.name;
@@ -194,7 +191,7 @@ function (angular, _, kbn) {
       return output2;
       return output2;
     }
     }
 
 
-    function convertTargetToQuery(options,target) {
+    function convertTargetToQuery(options, target) {
       if (!target.metric || target.hide) {
       if (!target.metric || target.hide) {
         return null;
         return null;
       }
       }

+ 2 - 2
public/app/plugins/datasource/kairosdb/partials/query.editor.html

@@ -2,7 +2,7 @@
 	<div ng-repeat="target in panel.targets"
 	<div ng-repeat="target in panel.targets"
 		class="tight-form-container"
 		class="tight-form-container"
 		ng-class="{'tight-form-disabled': target.hide}"
 		ng-class="{'tight-form-disabled': target.hide}"
-		ng-controller="KairosDBTargetCtrl"
+		ng-controller="KairosDBQueryCtrl"
 		ng-init="init()">
 		ng-init="init()">
 
 
 		<div class="tight-form">
 		<div class="tight-form">
@@ -345,7 +345,7 @@
 	</div>
 	</div>
 </div>
 </div>
 
 
-<section class="grafana-metric-options" ng-controller="KairosDBTargetCtrl">
+<section class="grafana-metric-options" ng-controller="KairosDBQueryCtrl">
 	<div class="tight-form">
 	<div class="tight-form">
 		<ul class="tight-form-list">
 		<ul class="tight-form-list">
 			<li class="tight-form-item tight-form-item-icon">
 			<li class="tight-form-item tight-form-item-icon">

+ 4 - 4
public/app/plugins/datasource/kairosdb/queryCtrl.js

@@ -9,7 +9,7 @@ function (angular, _) {
   var metricList = null;
   var metricList = null;
   var targetLetters = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O'];
   var targetLetters = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O'];
 
 
-  module.controller('KairosDBTargetCtrl', function($scope) {
+  module.controller('KairosDBQueryCtrl', function($scope) {
 
 
     $scope.init = function() {
     $scope.init = function() {
       $scope.metric = {
       $scope.metric = {
@@ -72,12 +72,12 @@ function (angular, _) {
     };
     };
 
 
     $scope.suggestTagKeys = function(query, callback) {
     $scope.suggestTagKeys = function(query, callback) {
-      callback($scope.datasource.performTagSuggestQuery($scope.target.metric, $scope.rangeUnparsed, 'key',''));
+      callback($scope.datasource.performTagSuggestQuery($scope.target.metric, $scope.rangeUnparsed, 'key', ''));
     };
     };
 
 
     $scope.suggestTagValues = function(query, callback) {
     $scope.suggestTagValues = function(query, callback) {
       callback($scope.datasource
       callback($scope.datasource
-        .performTagSuggestQuery($scope.target.metric,$scope.rangeUnparsed, 'value',$scope.target.currentTagKey));
+        .performTagSuggestQuery($scope.target.metric, $scope.rangeUnparsed, 'value', $scope.target.currentTagKey));
     };
     };
 
 
     // Filter metric by tag
     // Filter metric by tag
@@ -94,7 +94,7 @@ function (angular, _) {
 
 
       $scope.validateFilterTag();
       $scope.validateFilterTag();
       if (!$scope.target.errors.tags) {
       if (!$scope.target.errors.tags) {
-        if (!_.has($scope.target.tags,$scope.target.currentTagKey)) {
+        if (!_.has($scope.target.tags, $scope.target.currentTagKey)) {
           $scope.target.tags[$scope.target.currentTagKey] = [];
           $scope.target.tags[$scope.target.currentTagKey] = [];
         }
         }
         $scope.target.tags[$scope.target.currentTagKey].push($scope.target.currentTagValue);
         $scope.target.tags[$scope.target.currentTagKey].push($scope.target.currentTagValue);

+ 63 - 0
public/test/specs/kairosdb-datasource-specs.js

@@ -0,0 +1,63 @@
+define([
+  'helpers',
+  'plugins/datasource/kairosdb/datasource'
+], function(helpers) {
+  'use strict';
+
+  describe('KairosDBDatasource', function() {
+    var ctx = new helpers.ServiceTestContext();
+
+    beforeEach(module('grafana.services'));
+    beforeEach(ctx.providePhase(['templateSrv']));
+    beforeEach(ctx.createService('KairosDBDatasource'));
+    beforeEach(function() {
+      ctx.ds = new ctx.service({ url: ''});
+    });
+
+    describe('When querying kairosdb with one target using query editor target spec', function() {
+      var results;
+      var urlExpected = "/api/v1/datapoints/query";
+      var bodyExpected = {
+        metrics: [{ name: "test" }],
+        cache_time: 0,
+        start_relative: {
+          value: "1",
+          unit: "hours"
+        }
+      };
+
+      var query = {
+        range: { from: 'now-1h', to: 'now' },
+        targets: [{ metric: 'test', downsampling: '(NONE)'}]
+      };
+
+      var response = {
+        queries: [{
+          sample_size: 60,
+          results: [{
+            name: "test",
+            values: [[1420070400000, 1]]
+          }]
+        }]
+      };
+
+      beforeEach(function() {
+        ctx.$httpBackend.expect('POST', urlExpected, bodyExpected).respond(response);
+        ctx.ds.query(query).then(function(data) { results = data; });
+        ctx.$httpBackend.flush();
+      });
+
+      it('should generate the correct query', function() {
+        ctx.$httpBackend.verifyNoOutstandingExpectation();
+      });
+
+      it('should return series list', function() {
+        expect(results.data.length).to.be(1);
+        expect(results.data[0].target).to.be('test');
+      });
+
+    });
+
+  });
+
+});

+ 1 - 1
public/test/test-main.js

@@ -128,6 +128,7 @@ require([
     'specs/influxQueryBuilder-specs',
     'specs/influxQueryBuilder-specs',
     'specs/influx09-querybuilder-specs',
     'specs/influx09-querybuilder-specs',
     'specs/influxdb-datasource-specs',
     'specs/influxdb-datasource-specs',
+    'specs/kairosdb-datasource-specs',
     'specs/graph-ctrl-specs',
     'specs/graph-ctrl-specs',
     'specs/graph-specs',
     'specs/graph-specs',
     'specs/graph-tooltip-specs',
     'specs/graph-tooltip-specs',
@@ -150,4 +151,3 @@ require([
     window.__karma__.start();
     window.__karma__.start();
   });
   });
 });
 });
-