From 4c6febb4bfebf27bf98e4617755d40d8dcab1861 Mon Sep 17 00:00:00 2001 From: Richard Wall Date: Sat, 10 Sep 2011 11:52:52 +0100 Subject: #846377 Pass transformer function to the RrdQuery and add a unit test --- docs/examples/jarmon_example_recipes.js | 11 +++ jarmon/jarmon.js | 2 +- jarmon/jarmon.test.js | 137 +++++++------------------------- 3 files changed, 40 insertions(+), 110 deletions(-) diff --git a/docs/examples/jarmon_example_recipes.js b/docs/examples/jarmon_example_recipes.js index aa42d6f..7acfa8d 100644 --- a/docs/examples/jarmon_example_recipes.js +++ b/docs/examples/jarmon_example_recipes.js @@ -58,5 +58,16 @@ jarmon.CHART_RECIPES_COLLECTD = { ['data/interface/if_octets-wlan0.rrd', 'rx', 'Receive', 'bit/s', function (v) { return v*8; }] ], options: jQuery.extend(true, {}, jarmon.Chart.BASE_OPTIONS) + }, + + 'droprate': { + title: 'Ping Droprate', + data: [ + ['data/ping/ping_droprate-google.com.rrd', 0, + 'google.com', '%', function (v) { return v*100; }], + ['data/ping/ping_droprate-softlayer.com.rrd', 0, + 'softlayer.com', '%', function (v) { return v*100; }] + ], + options: jQuery.extend(true, {}, jarmon.Chart.BASE_OPTIONS) } }; diff --git a/jarmon/jarmon.js b/jarmon/jarmon.js index d8cf305..32fce6d 100644 --- a/jarmon/jarmon.js +++ b/jarmon/jarmon.js @@ -529,7 +529,7 @@ jarmon.RrdQueryRemote.prototype._callRemote = function(methodName, args) { var rrd = new RRDFile(res); self.lastUpdate = rrd.getLastUpdate(); - var rq = new jarmon.RrdQuery(rrd, self.unit); + var rq = new jarmon.RrdQuery(rrd, self.unit, self.transformer); try { ret.resolve(rq[methodName].apply(rq, args)); } catch(e) { diff --git a/jarmon/jarmon.test.js b/jarmon/jarmon.test.js index a2b36be..5f5989d 100644 --- a/jarmon/jarmon.test.js +++ b/jarmon/jarmon.test.js @@ -281,8 +281,36 @@ YUI({ logInclude: { TestRunner: true } }).use('console', 'test', function(Y) { }); }); this.wait(); + }, + + _x10transformer: function(v) { + return v * 10; + }, + + test_transformerFunction: function () { + /** + * RrdQuery can be passed a transformer function which may + * manipulate the values from the RRDFile + **/ + var self = this; + this.d.done( + function(rrd) { + self.resume(function() { + var rq = new jarmon.RrdQuery(rrd, '', self._x10transformer); + var data = rq.getData(RRD_STARTTIME, RRD_ENDTIME); + // Make sure that the transformer is the + // function we asked for + Y.Assert.areEqual(self._x10transformer, rq.transformer); + // Real data goes 0,1,2,3... + // should be transformed to 0,10,20... + Y.Assert.areEqual(0, data.data[0][1]); + Y.Assert.areEqual(10, data.data[1][1]); + }); + }); + this.wait(); } + })); @@ -388,115 +416,6 @@ YUI({ logInclude: { TestRunner: true } }).use('console', 'test', function(Y) { })); - Y.Test.Runner.add(new Y.Test.Case({ - name: "jarmon.Chart", - - test_draw: function () { - /** - * Test that a rendered chart has the correct dimensions, legend, - * axis, labels etc - **/ - var self = this; - var $tpl = $( - '
').appendTo($('body')); - var c = new jarmon.Chart($tpl, jarmon.Chart.BASE_OPTIONS); - // - c.options.xaxis.tzoffset = 0; - c.addData( - 'speed', - new jarmon.RrdQueryRemote('build/test.rrd', 'm/s'), - true); - var d = c.setTimeRange(RRD_STARTTIME, RRD_ENDTIME); - d.done( - function() { - self.resume(function() { - // TODO: write useful tests - }); - }); - this.wait(); - } - })); - - - Y.Test.Runner.add(new Y.Test.Case({ - name: "jarmon.RrdChooser", - - setUp: function() { - this.$tpl = $('
').appendTo($('body')); - var c = new jarmon.RrdChooser(this.$tpl); - c.drawRrdUrlForm(); - }, - - test_drawInitialForm: function () { - /** - * Test that the initial config form contains an rrd form field - **/ - Y.Assert.areEqual( - this.$tpl.find('form input[name=rrd_url]').size(), 1); - }, - - test_drawUrlErrorMessage: function () { - /** - * Test that submitting the form with an incorrect url results in - * an error message - **/ - var self = this; - this.$tpl.find('form input[name=rrd_url]').val('Foo/Bar').submit(); - this.wait( - function() { - Y.Assert.areEqual(self.$tpl.find('.error').size(), 1); - }, 1000); - }, - - test_drawUrlListDatasources: function () { - /** - * Test that submitting the form with an correct rrd url results in - * list of further DS label fields - **/ - var self = this; - this.$tpl.find( - 'form input[name=rrd_url]').val('build/test.rrd').submit(); - this.wait( - function() { - Y.Assert.areEqual( - self.$tpl.find('input[name=rrd_ds_label]').size(), 1); - }, 1000 - ); - } - })); - - - Y.Test.Runner.add(new Y.Test.Case({ - name: "jarmon.ChartEditor", - - setUp: function() { - this.$tpl = $('
').appendTo($('body')); - var c = new jarmon.ChartEditor( - this.$tpl, - { - title: 'Foo', - datasources: [ - ['data/cpu-0/cpu-wait.rrd', 0, 'CPU-0 Wait', '%'], - ['data/cpu-1/cpu-wait.rrd', 0, 'CPU-1 Wait', '%'], - ['data/cpu-0/cpu-system.rrd', 0, 'CPU-0 System', '%'], - ['data/cpu-1/cpu-system.rrd', 0, 'CPU-1 System', '%'], - ['data/cpu-0/cpu-user.rrd', 0, 'CPU-0 User', '%'], - ['data/cpu-1/cpu-user.rrd', 0, 'CPU-1 User', '%'] - ] - } - ); - c.draw(); - }, - - test_drawInitialForm: function () { - /** - * Test that the initial config form contains an rrd form field - **/ - Y.Assert.areEqual( - this.$tpl.find('form input[name=rrd_url]').size(), 1); - } - })); - //initialize the console var yconsole = new Y.Console({ -- cgit v1.2.3