From 9bf7a75b91318e8b722e5c5ce3ab10a2d693dfba Mon Sep 17 00:00:00 2001 From: Nick Wei Date: Wed, 27 Aug 2014 14:42:02 -0700 Subject: [PATCH 01/12] creates DEVICES object for device lookup, and listDevices() --- examples/index.js | 17 ++++++++++++-- lib/index.js | 56 ++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 66 insertions(+), 7 deletions(-) diff --git a/examples/index.js b/examples/index.js index f257742..52950cf 100644 --- a/examples/index.js +++ b/examples/index.js @@ -50,7 +50,7 @@ */ // Check if a metric exists: - console.log( metrics.exists( 'mem.utilization' ) ); + console.log( metrics.exists( 'mem.swapSpaceUtilization' ) ); /** * Returns: @@ -65,13 +65,26 @@ */ // Get a metric specification: - console.log( metrics.get( 'mem.utilization' ) ); + console.log( metrics.get( 'mem.swapSpaceUtilization' ) ); /** * Returns: * [object] */ + // Get the list of devices: + console.log( metrics.listDevices() ); + + /** + * Returns: + * [ + * 'device0', + * 'device1', + * ... + * 'deviceN' + * ] + */ + })(); diff --git a/lib/index.js b/lib/index.js index e01086b..027b56e 100644 --- a/lib/index.js +++ b/lib/index.js @@ -29,11 +29,6 @@ (function() { 'use strict'; - // MODULES // - - var os = require( 'os' ); - - // SPECIFICATIONS // var METRICS = require( './../specs/memory.json' ); @@ -44,6 +39,47 @@ var NAMES = Object.keys( METRICS ); + // DEVICES // + + var DEVICES = {}, + DEVICENAMES, + key, + i, + deviceList; + + /** Iterates through METRICS and creates DEVICES with the following structure: + *{ + * deviceName1: { + * metricNameA: {}, + * metricNameB: {}, + * ... + * }, + * deviceName2: { + * metricNameH: {}, + * metricNameI: {}, + * ... + * }, + * ... + *} + */ + for ( key in METRICS ) { + deviceList = METRICS[ key ][ 'device' ]; + + if ( deviceList !== null ) { + + for ( i = 0; i < deviceList.length; i++) { + + if ( !DEVICES[ deviceList[ i ] ] ) { + DEVICES[ deviceList[ i ] ] = {}; + } + DEVICES[ deviceList[ i ] ][ key ] = METRICS[ key ]; + } + } + } + + DEVICENAMES = Object.keys( DEVICES ); + + // METRICS // /** @@ -119,6 +155,16 @@ return null; }; // end METHOD source() + /** + * METHOD: listDevices() + * Lists all devices associated with these metrics. + * + * @returns {Array} list of device names. + */ + Metrics.prototype.listDevices = function() { + return DEVICENAMES; + }; // end METHOD listDevices() + // EXPORTS // From c1b37bd8c1f0ad272e3f5cdc0255696593dfb667 Mon Sep 17 00:00:00 2001 From: Nick Wei Date: Wed, 27 Aug 2014 15:00:50 -0700 Subject: [PATCH 02/12] implements getDevice() --- examples/index.js | 8 ++++++++ lib/index.js | 17 +++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/examples/index.js b/examples/index.js index 52950cf..955a277 100644 --- a/examples/index.js +++ b/examples/index.js @@ -85,6 +85,14 @@ * ] */ + // Get a list of metric specifications by device name: + console.log( metrics.getDevice( 'disk' ) ); + + /** + * Returns: + * [object] + */ + })(); diff --git a/lib/index.js b/lib/index.js index 027b56e..78a8e8f 100644 --- a/lib/index.js +++ b/lib/index.js @@ -165,6 +165,23 @@ return DEVICENAMES; }; // end METHOD listDevices() + /** + * METHOD: getDevice( name ) + * Returns a list of metric specifications associated with the device. If a device does not have any associated metric specifications, returns null. + * + * @param {String} name - device name + * @returns {Object|null} list of metric specifications or null + */ + Metrics.prototype.getDevice = function( name ) { + if ( typeof name !== 'string' ) { + throw new TypeError( 'getDevice()::invalid input argument. Device `name` must be a string.' ); + } + if ( DEVICENAMES.indexOf( name ) === -1 ) { + return null; + } + return JSON.parse( JSON.stringify( DEVICES[ name ] ) ); + }; // end METHOD getDevice() + // EXPORTS // From 884d64af7750c4fbbc642d100cc4132885e75a03 Mon Sep 17 00:00:00 2001 From: Nick Wei Date: Wed, 27 Aug 2014 15:50:55 -0700 Subject: [PATCH 03/12] implements case independent lookup for exists(), get(), and getDevice() --- examples/index.js | 6 +++--- lib/index.js | 38 +++++++++++++++++++++++++------------- 2 files changed, 28 insertions(+), 16 deletions(-) diff --git a/examples/index.js b/examples/index.js index 955a277..3533de9 100644 --- a/examples/index.js +++ b/examples/index.js @@ -50,7 +50,7 @@ */ // Check if a metric exists: - console.log( metrics.exists( 'mem.swapSpaceUtilization' ) ); + console.log( metrics.exists( 'mem.swapspaceutilization' ) ); /** * Returns: @@ -65,7 +65,7 @@ */ // Get a metric specification: - console.log( metrics.get( 'mem.swapSpaceUtilization' ) ); + console.log( metrics.get( 'mem.swapspaceutilization' ) ); /** * Returns: @@ -86,7 +86,7 @@ */ // Get a list of metric specifications by device name: - console.log( metrics.getDevice( 'disk' ) ); + console.log( metrics.getDevice( 'DISK' ) ); /** * Returns: diff --git a/lib/index.js b/lib/index.js index 78a8e8f..3ddacfb 100644 --- a/lib/index.js +++ b/lib/index.js @@ -34,15 +34,9 @@ var METRICS = require( './../specs/memory.json' ); - // VARIABLES // - - var NAMES = Object.keys( METRICS ); - - // DEVICES // var DEVICES = {}, - DEVICENAMES, key, i, deviceList; @@ -67,7 +61,7 @@ if ( deviceList !== null ) { - for ( i = 0; i < deviceList.length; i++) { + for ( i = 0; i < deviceList.length; i++ ) { if ( !DEVICES[ deviceList[ i ] ] ) { DEVICES[ deviceList[ i ] ] = {}; @@ -77,7 +71,21 @@ } } - DEVICENAMES = Object.keys( DEVICES ); + + // VARIABLES // + + var NAMES = Object.keys( METRICS ), + DEVICENAMES = Object.keys( DEVICES ), + NAMESLOWER = NAMES.slice(), + DEVICENAMESLOWER = DEVICENAMES.slice(); + + for ( i = 0; i < NAMESLOWER.length; i++ ) { + NAMESLOWER[i] = NAMESLOWER[i].toLowerCase(); + } + + for ( i = 0; i < DEVICENAMESLOWER.length; i++ ) { + DEVICENAMESLOWER[i] = DEVICENAMESLOWER[i].toLowerCase(); + } // METRICS // @@ -114,7 +122,7 @@ if ( typeof name !== 'string' ) { throw new TypeError( 'exists()::invalid input argument. Metric `name` must be a string.' ); } - if ( NAMES.indexOf( name ) === -1 ) { + if ( NAMESLOWER.indexOf( name.toLowerCase() ) === -1 ) { return false; } return true; @@ -128,13 +136,15 @@ * @returns {Object|null} metric specification or null */ Metrics.prototype.get = function( name ) { + var indexOfName; if ( typeof name !== 'string' ) { throw new TypeError( 'get()::invalid input argument. Metric `name` must be a string.' ); } - if ( NAMES.indexOf( name ) === -1 ) { + indexOfName = NAMESLOWER.indexOf( name.toLowerCase() ); + if ( indexOfName === -1 ) { return null; } - return JSON.parse( JSON.stringify( METRICS[ name ] ) ); + return JSON.parse( JSON.stringify( METRICS[ NAMES[ indexOfName ] ] ) ); }; // end METHOD get() /** @@ -173,13 +183,15 @@ * @returns {Object|null} list of metric specifications or null */ Metrics.prototype.getDevice = function( name ) { + var indexOfName; if ( typeof name !== 'string' ) { throw new TypeError( 'getDevice()::invalid input argument. Device `name` must be a string.' ); } - if ( DEVICENAMES.indexOf( name ) === -1 ) { + indexOfName = DEVICENAMESLOWER.indexOf( name.toLowerCase() ); + if ( indexOfName === -1 ) { return null; } - return JSON.parse( JSON.stringify( DEVICES[ name ] ) ); + return JSON.parse( JSON.stringify( DEVICES[ DEVICENAMES[ indexOfName ] ] ) ); }; // end METHOD getDevice() From e16a80bc1d1f536de0ed11dc97dec4f101da3acf Mon Sep 17 00:00:00 2001 From: Nick Wei Date: Wed, 27 Aug 2014 16:57:08 -0700 Subject: [PATCH 04/12] implements default behavior - returns all for get() and getDevice() --- lib/index.js | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/lib/index.js b/lib/index.js index 3ddacfb..f5302e2 100644 --- a/lib/index.js +++ b/lib/index.js @@ -130,13 +130,16 @@ /** * METHOD: get( name ) - * Returns a metric's specification. If a metric does not have a specification, returns null. + * Returns a metric's specification. If a metric does not have a specification, returns null. If invoked with null for metric name, returns all metric specifications. * * @param {String} name - metric name - * @returns {Object|null} metric specification or null + * @returns {Object|null} one or all metric specification(s), or null */ Metrics.prototype.get = function( name ) { var indexOfName; + if ( name === undefined ) { + return JSON.parse( JSON.stringify( METRICS ) ); + } if ( typeof name !== 'string' ) { throw new TypeError( 'get()::invalid input argument. Metric `name` must be a string.' ); } @@ -177,13 +180,16 @@ /** * METHOD: getDevice( name ) - * Returns a list of metric specifications associated with the device. If a device does not have any associated metric specifications, returns null. + * Returns a list of metric specifications associated with the device. If a device does not have any associated metric specifications, returns null. If invoked with null for device name, returns all devices and their corresponding metric specifications. * * @param {String} name - device name - * @returns {Object|null} list of metric specifications or null + * @returns {Object|null} list of metric specifications, or list of devices, or null */ Metrics.prototype.getDevice = function( name ) { var indexOfName; + if ( name === undefined ) { + return JSON.parse( JSON.stringify( DEVICES ) ); + } if ( typeof name !== 'string' ) { throw new TypeError( 'getDevice()::invalid input argument. Device `name` must be a string.' ); } From 2bd73342a3532afe2cd467027ef87465d822d743 Mon Sep 17 00:00:00 2001 From: Nick Wei Date: Wed, 27 Aug 2014 17:18:15 -0700 Subject: [PATCH 05/12] adds postupdate and get-specs scripts to package.json --- package.json | 2 ++ 1 file changed, 2 insertions(+) diff --git a/package.json b/package.json index 790d3c0..01c1544 100644 --- a/package.json +++ b/package.json @@ -14,6 +14,8 @@ ], "scripts": { "postinstall": "./scripts/specs.js", + "postupdate": "./scripts/specs.js", + "get-specs": "./scripts/specs.js", "test": "./node_modules/.bin/mocha", "test-cov": "./node_modules/.bin/istanbul cover ./node_modules/.bin/_mocha --dir ./reports/coverage -- -R spec", "coveralls": "./node_modules/.bin/istanbul cover ./node_modules/.bin/_mocha --dir ./reports/coveralls/coverage --report lcovonly -- -R spec && cat ./reports/coveralls/coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./reports/coveralls" From be900314e3838eddf73e08ecf5aa46e586fbb32a Mon Sep 17 00:00:00 2001 From: Nick Wei Date: Wed, 27 Aug 2014 17:48:14 -0700 Subject: [PATCH 06/12] fixes crash caused by remaking an existing directory --- scripts/specs.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/scripts/specs.js b/scripts/specs.js index 70a77df..56523cd 100755 --- a/scripts/specs.js +++ b/scripts/specs.js @@ -58,7 +58,14 @@ function getSpecs() { var keys = Object.keys( resources ); - fs.mkdirSync( filepath ); + try { + fs.mkdirSync( filepath ); + } catch ( err ) { + // Ignores the error that is thrown when the directory already exists. + if ( err[ 'code' ] !== 'EEXIST') { + throw new Error( err ); + } + } for ( var i = 0; i < keys.length; i++ ) { request({ From 9b099850975710f91ae7af9a2c5385e34745be63 Mon Sep 17 00:00:00 2001 From: Nick Wei Date: Thu, 28 Aug 2014 11:18:31 -0700 Subject: [PATCH 07/12] adds tests --- lib/index.js | 18 +++++++----------- package.json | 7 ++++--- scripts/specs.js | 4 +--- test/test.js | 35 +++++++++++++++++++++++++++++++---- 4 files changed, 43 insertions(+), 21 deletions(-) diff --git a/lib/index.js b/lib/index.js index f5302e2..5e0fcd7 100644 --- a/lib/index.js +++ b/lib/index.js @@ -36,8 +36,8 @@ // DEVICES // - var DEVICES = {}, - key, + var DEVICES = {}; + var key, i, deviceList; @@ -58,15 +58,12 @@ */ for ( key in METRICS ) { deviceList = METRICS[ key ][ 'device' ]; - if ( deviceList !== null ) { - for ( i = 0; i < deviceList.length; i++ ) { - - if ( !DEVICES[ deviceList[ i ] ] ) { - DEVICES[ deviceList[ i ] ] = {}; + if ( !DEVICES[ deviceList[i] ] ) { + DEVICES[ deviceList[i] ] = {}; } - DEVICES[ deviceList[ i ] ][ key ] = METRICS[ key ]; + DEVICES[ deviceList[i] ][ key ] = METRICS[ key ]; } } } @@ -75,14 +72,13 @@ // VARIABLES // var NAMES = Object.keys( METRICS ), - DEVICENAMES = Object.keys( DEVICES ), - NAMESLOWER = NAMES.slice(), + DEVICENAMES = Object.keys( DEVICES ); + var NAMESLOWER = NAMES.slice(), DEVICENAMESLOWER = DEVICENAMES.slice(); for ( i = 0; i < NAMESLOWER.length; i++ ) { NAMESLOWER[i] = NAMESLOWER[i].toLowerCase(); } - for ( i = 0; i < DEVICENAMESLOWER.length; i++ ) { DEVICENAMESLOWER[i] = DEVICENAMESLOWER[i].toLowerCase(); } diff --git a/package.json b/package.json index 01c1544..53df8c7 100644 --- a/package.json +++ b/package.json @@ -16,7 +16,7 @@ "postinstall": "./scripts/specs.js", "postupdate": "./scripts/specs.js", "get-specs": "./scripts/specs.js", - "test": "./node_modules/.bin/mocha", + "test": "./node_modules/.bin/mocha -R nyan", "test-cov": "./node_modules/.bin/istanbul cover ./node_modules/.bin/_mocha --dir ./reports/coverage -- -R spec", "coveralls": "./node_modules/.bin/istanbul cover ./node_modules/.bin/_mocha --dir ./reports/coveralls/coverage --report lcovonly -- -R spec && cat ./reports/coveralls/coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./reports/coveralls" }, @@ -36,9 +36,10 @@ }, "devDependencies": { "chai": "1.x.x", - "mocha": "1.x.x", "coveralls": "^2.11.1", - "istanbul": "^0.3.0" + "istanbul": "^0.3.0", + "mocha": "1.x.x", + "underscore": "^1.7.0" }, "licenses": [ { diff --git a/scripts/specs.js b/scripts/specs.js index 56523cd..ef40eed 100755 --- a/scripts/specs.js +++ b/scripts/specs.js @@ -57,16 +57,14 @@ */ function getSpecs() { var keys = Object.keys( resources ); - try { fs.mkdirSync( filepath ); } catch ( err ) { // Ignores the error that is thrown when the directory already exists. - if ( err[ 'code' ] !== 'EEXIST') { + if ( err.errno !== 47 & err.code !== 'EEXIST' ) { throw new Error( err ); } } - for ( var i = 0; i < keys.length; i++ ) { request({ 'method': 'GET', diff --git a/test/test.js b/test/test.js index 44220d6..eac582f 100644 --- a/test/test.js +++ b/test/test.js @@ -8,7 +8,10 @@ var // Expectation library: METRICS = require( './../specs/memory.json' ), // Module to be tested: - metrics = require( './../lib' ); + metrics = require( './../lib' ), + + // Metric specifications by device: + DEVICES = {}; // VARIABLES // @@ -24,7 +27,8 @@ describe( 'doc-metrix-memory', function tests() { // SETUP // - var NAMES = Object.keys( METRICS ); + var NAMES = Object.keys( METRICS ), + DEVICENAMES = Object.keys( DEVICES ); // TESTS // @@ -82,6 +86,10 @@ describe( 'doc-metrix-memory', function tests() { assert.ok( metrics.exists( NAMES[0] ), true ); }); + it( 'should return true for a metric which has a specification regardless of input name case sensitivity', function test() { + assert.ok( metrics.exists( NAMES[0].toUpperCase() ), true ); + }); + }); describe( 'get', function tests() { @@ -90,14 +98,13 @@ describe( 'doc-metrix-memory', function tests() { expect( metrics.get ).to.be.a( 'function' ); }); - it( 'should not allow a non-string metric name', function test() { + it( 'should not allow a non-undefined or non-string metric name', function test() { var values = [ 5, [], {}, true, null, - undefined, NaN, function(){} ]; @@ -121,6 +128,14 @@ describe( 'doc-metrix-memory', function tests() { assert.deepEqual( metrics.get( NAMES[0] ), METRICS[ NAMES[0] ] ); }); + it( 'should return all metric specifications if called without a metric name', function test() { + assert.deepEqual( metrics.get(), METRICS ); + }); + + it( 'should return a metric specification regardless of input name case sensitivity', function test() { + assert.deepEqual( metrics.get( NAMES[0].toUpperCase() ), METRICS[ NAMES[0] ] ); + }); + }); describe( 'source', function tests() { @@ -129,4 +144,16 @@ describe( 'doc-metrix-memory', function tests() { }); + xdescribe( 'listDevices', function tests() { + + it( 'should provide a method to list all devices associated with the metrics', function test() { + expect( metrics.listDevices ).to.be.a( 'function' ); + }); + + it( 'should list all devices associated with the metrics', function test() { + assert.deepEqual( metrics.listDevices(), DEVICENAMES ); + }); + + }); + }); \ No newline at end of file From fc33ff3e5769e29e645cb98973d55658bb300209 Mon Sep 17 00:00:00 2001 From: Nick Wei Date: Thu, 28 Aug 2014 13:35:07 -0700 Subject: [PATCH 08/12] adds tests --- test/test.js | 61 +++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 58 insertions(+), 3 deletions(-) diff --git a/test/test.js b/test/test.js index eac582f..17d091b 100644 --- a/test/test.js +++ b/test/test.js @@ -3,6 +3,7 @@ var // Expectation library: chai = require( 'chai' ), + _ = require( 'underscore' ), // Metrics specification: METRICS = require( './../specs/memory.json' ), @@ -13,6 +14,14 @@ var // Expectation library: // Metric specifications by device: DEVICES = {}; +_.each( METRICS, function( metric, metricName ) { + if ( metric.device !== null ) { + _.each( metric.device, function( device ) { + DEVICES[ device ] = ( DEVICES[ device ] ? DEVICES[ device ] : {} ); + DEVICES[ device ] [ metricName ] = metric; + } ); + } +} ); // VARIABLES // @@ -79,7 +88,7 @@ describe( 'doc-metrix-memory', function tests() { }); it( 'should return false for a metric which does not have a specification', function test() { - assert.notOk( metrics.exists( 'cpu.utilization' ) ); + assert.notOk( metrics.exists( 'invalid.classification.utilization' ) ); }); it( 'should return true for a metric which has a specification', function test() { @@ -121,7 +130,7 @@ describe( 'doc-metrix-memory', function tests() { }); it( 'should return null for a metric which does not have a specification', function test() { - assert.isNull( metrics.get( 'cpu.utilization' ) ); + assert.isNull( metrics.get( 'invalid.classification.utilization' ) ); }); it( 'should return a metric specification', function test() { @@ -144,7 +153,7 @@ describe( 'doc-metrix-memory', function tests() { }); - xdescribe( 'listDevices', function tests() { + describe( 'listDevices', function tests() { it( 'should provide a method to list all devices associated with the metrics', function test() { expect( metrics.listDevices ).to.be.a( 'function' ); @@ -156,4 +165,50 @@ describe( 'doc-metrix-memory', function tests() { }); + describe( 'getDevice', function tests() { + + it( 'should provide a method to get metric specifications associated with a device', function test() { + expect( metrics.getDevice ).to.be.a( 'function' ); + }); + + it( 'should not allow a non-undefined or non-string metric name', function test() { + var values = [ + 5, + [], + {}, + true, + null, + NaN, + function(){} + ]; + + for ( var i = 0; i < values.length; i++ ) { + expect( badValue( values[i] ) ).to.throw( Error ); + } + + function badValue( value ) { + return function() { + metrics.getDevice( value ); + }; + } + }); + + it( 'should return null for a device which does not have any associated metric specification', function test() { + assert.isNull( metrics.getDevice( 'no such device' ) ); + }); + + it( 'should return metric specification(s) associated with the device', function test() { + assert.deepEqual( metrics.getDevice( DEVICENAMES[0] ), DEVICES[ DEVICENAMES[0] ] ); + }); + + it( 'should return metric specifications associated with all devices if called without a device name', function test() { + assert.deepEqual( metrics.getDevice(), DEVICES ); + }); + + it( 'should return metric specification(s) associated with the device regardless of input device case sensitivity', function test() { + assert.deepEqual( metrics.getDevice( DEVICENAMES[0].toUpperCase() ), DEVICES[ DEVICENAMES[0] ] ); + }); + + }); + }); \ No newline at end of file From 7bcb784c0404036f256160bd19b0cf2bb1acaff1 Mon Sep 17 00:00:00 2001 From: Nick Wei Date: Thu, 28 Aug 2014 14:04:47 -0700 Subject: [PATCH 09/12] removes source(), and renames getDevice() to device() --- lib/index.js | 26 ++++---------------------- test/test.js | 20 +++++++------------- 2 files changed, 11 insertions(+), 35 deletions(-) diff --git a/lib/index.js b/lib/index.js index 5e0fcd7..2693e0a 100644 --- a/lib/index.js +++ b/lib/index.js @@ -146,24 +146,6 @@ return JSON.parse( JSON.stringify( METRICS[ NAMES[ indexOfName ] ] ) ); }; // end METHOD get() - /** - * METHOD: source( name ) - * Returns a metric's name. The source depends on the OS. Check the relevant specification for the return value and its use. If no source is found, returns null. - * - * @param {String} name - metric name - * @returns {String|null} metric source or null - */ - Metrics.prototype.source = function( name ) { - var spec; - if ( typeof name !== 'string' ) { - throw new TypeError( 'source()::invalid input argument. Metric `name` must be a string.' ); - } - if ( NAMES.indexOf( name ) === -1 ) { - return null; - } - return null; - }; // end METHOD source() - /** * METHOD: listDevices() * Lists all devices associated with these metrics. @@ -175,26 +157,26 @@ }; // end METHOD listDevices() /** - * METHOD: getDevice( name ) + * METHOD: device( name ) * Returns a list of metric specifications associated with the device. If a device does not have any associated metric specifications, returns null. If invoked with null for device name, returns all devices and their corresponding metric specifications. * * @param {String} name - device name * @returns {Object|null} list of metric specifications, or list of devices, or null */ - Metrics.prototype.getDevice = function( name ) { + Metrics.prototype.device = function( name ) { var indexOfName; if ( name === undefined ) { return JSON.parse( JSON.stringify( DEVICES ) ); } if ( typeof name !== 'string' ) { - throw new TypeError( 'getDevice()::invalid input argument. Device `name` must be a string.' ); + throw new TypeError( 'device()::invalid input argument. Device `name` must be a string.' ); } indexOfName = DEVICENAMESLOWER.indexOf( name.toLowerCase() ); if ( indexOfName === -1 ) { return null; } return JSON.parse( JSON.stringify( DEVICES[ DEVICENAMES[ indexOfName ] ] ) ); - }; // end METHOD getDevice() + }; // end METHOD device() // EXPORTS // diff --git a/test/test.js b/test/test.js index 17d091b..af7610f 100644 --- a/test/test.js +++ b/test/test.js @@ -147,12 +147,6 @@ describe( 'doc-metrix-memory', function tests() { }); - describe( 'source', function tests() { - - it( 'should do something' ); - - }); - describe( 'listDevices', function tests() { it( 'should provide a method to list all devices associated with the metrics', function test() { @@ -165,10 +159,10 @@ describe( 'doc-metrix-memory', function tests() { }); - describe( 'getDevice', function tests() { + describe( 'device', function tests() { it( 'should provide a method to get metric specifications associated with a device', function test() { - expect( metrics.getDevice ).to.be.a( 'function' ); + expect( metrics.device ).to.be.a( 'function' ); }); it( 'should not allow a non-undefined or non-string metric name', function test() { @@ -188,25 +182,25 @@ describe( 'doc-metrix-memory', function tests() { function badValue( value ) { return function() { - metrics.getDevice( value ); + metrics.device( value ); }; } }); it( 'should return null for a device which does not have any associated metric specification', function test() { - assert.isNull( metrics.getDevice( 'no such device' ) ); + assert.isNull( metrics.device( 'no such device' ) ); }); it( 'should return metric specification(s) associated with the device', function test() { - assert.deepEqual( metrics.getDevice( DEVICENAMES[0] ), DEVICES[ DEVICENAMES[0] ] ); + assert.deepEqual( metrics.device( DEVICENAMES[0] ), DEVICES[ DEVICENAMES[0] ] ); }); it( 'should return metric specifications associated with all devices if called without a device name', function test() { - assert.deepEqual( metrics.getDevice(), DEVICES ); + assert.deepEqual( metrics.device(), DEVICES ); }); it( 'should return metric specification(s) associated with the device regardless of input device case sensitivity', function test() { - assert.deepEqual( metrics.getDevice( DEVICENAMES[0].toUpperCase() ), DEVICES[ DEVICENAMES[0] ] ); + assert.deepEqual( metrics.device( DEVICENAMES[0].toUpperCase() ), DEVICES[ DEVICENAMES[0] ] ); }); }); From 8597a9f2cb1fde36cfd81bbc487a359ffa4c379d Mon Sep 17 00:00:00 2001 From: Nick Wei Date: Thu, 28 Aug 2014 14:12:06 -0700 Subject: [PATCH 10/12] fixes logic to check for length of arguments instead of input type in get() and device() --- lib/index.js | 4 ++-- test/test.js | 6 ++++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/index.js b/lib/index.js index 2693e0a..d621399 100644 --- a/lib/index.js +++ b/lib/index.js @@ -133,7 +133,7 @@ */ Metrics.prototype.get = function( name ) { var indexOfName; - if ( name === undefined ) { + if ( arguments.length === 0 ) { return JSON.parse( JSON.stringify( METRICS ) ); } if ( typeof name !== 'string' ) { @@ -165,7 +165,7 @@ */ Metrics.prototype.device = function( name ) { var indexOfName; - if ( name === undefined ) { + if ( arguments.length === 0 ) { return JSON.parse( JSON.stringify( DEVICES ) ); } if ( typeof name !== 'string' ) { diff --git a/test/test.js b/test/test.js index af7610f..6e5b817 100644 --- a/test/test.js +++ b/test/test.js @@ -107,12 +107,13 @@ describe( 'doc-metrix-memory', function tests() { expect( metrics.get ).to.be.a( 'function' ); }); - it( 'should not allow a non-undefined or non-string metric name', function test() { + it( 'should not allow a non-string metric name', function test() { var values = [ 5, [], {}, true, + undefined, null, NaN, function(){} @@ -165,12 +166,13 @@ describe( 'doc-metrix-memory', function tests() { expect( metrics.device ).to.be.a( 'function' ); }); - it( 'should not allow a non-undefined or non-string metric name', function test() { + it( 'should not allow a non-string metric name', function test() { var values = [ 5, [], {}, true, + undefined, null, NaN, function(){} From d4d5c709e1e2d62bd6fd8ca64560e4a92c4aa174 Mon Sep 17 00:00:00 2001 From: Nick Wei Date: Thu, 28 Aug 2014 14:20:36 -0700 Subject: [PATCH 11/12] changes checking of directory existence to use fs.exists instead of try catch statement --- scripts/specs.js | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/scripts/specs.js b/scripts/specs.js index ef40eed..07c20a5 100755 --- a/scripts/specs.js +++ b/scripts/specs.js @@ -57,13 +57,8 @@ */ function getSpecs() { var keys = Object.keys( resources ); - try { + if ( !fs.existsSync( filepath ) ) { fs.mkdirSync( filepath ); - } catch ( err ) { - // Ignores the error that is thrown when the directory already exists. - if ( err.errno !== 47 & err.code !== 'EEXIST' ) { - throw new Error( err ); - } } for ( var i = 0; i < keys.length; i++ ) { request({ From 92d92adf15b214eed2c8ae6612751cfe4655e9fa Mon Sep 17 00:00:00 2001 From: Nick Wei Date: Thu, 28 Aug 2014 15:28:34 -0700 Subject: [PATCH 12/12] updates tests to not use underscore --- lib/index.js | 20 +++++++++----------- package.json | 3 +-- test/test.js | 26 ++++++++++++++++++-------- 3 files changed, 28 insertions(+), 21 deletions(-) diff --git a/lib/index.js b/lib/index.js index d621399..9a89016 100644 --- a/lib/index.js +++ b/lib/index.js @@ -37,9 +37,9 @@ // DEVICES // var DEVICES = {}; - var key, - i, - deviceList; + var metricName, + devices, + i; /** Iterates through METRICS and creates DEVICES with the following structure: *{ @@ -56,14 +56,12 @@ * ... *} */ - for ( key in METRICS ) { - deviceList = METRICS[ key ][ 'device' ]; - if ( deviceList !== null ) { - for ( i = 0; i < deviceList.length; i++ ) { - if ( !DEVICES[ deviceList[i] ] ) { - DEVICES[ deviceList[i] ] = {}; - } - DEVICES[ deviceList[i] ][ key ] = METRICS[ key ]; + for ( metricName in METRICS ) { + devices = METRICS[ metricName ][ 'device' ]; + if ( devices !== null ) { + for ( i = 0; i < devices.length; i++ ) { + DEVICES[ devices[i] ] = ( DEVICES[ devices[i] ] ? DEVICES[ devices[i] ] : {} ); + DEVICES[ devices[i] ][ metricName ] = METRICS[ metricName ]; } } } diff --git a/package.json b/package.json index 53df8c7..1d5286e 100644 --- a/package.json +++ b/package.json @@ -38,8 +38,7 @@ "chai": "1.x.x", "coveralls": "^2.11.1", "istanbul": "^0.3.0", - "mocha": "1.x.x", - "underscore": "^1.7.0" + "mocha": "1.x.x" }, "licenses": [ { diff --git a/test/test.js b/test/test.js index 6e5b817..21be883 100644 --- a/test/test.js +++ b/test/test.js @@ -3,7 +3,6 @@ var // Expectation library: chai = require( 'chai' ), - _ = require( 'underscore' ), // Metrics specification: METRICS = require( './../specs/memory.json' ), @@ -14,14 +13,25 @@ var // Expectation library: // Metric specifications by device: DEVICES = {}; -_.each( METRICS, function( metric, metricName ) { - if ( metric.device !== null ) { - _.each( metric.device, function( device ) { - DEVICES[ device ] = ( DEVICES[ device ] ? DEVICES[ device ] : {} ); - DEVICES[ device ] [ metricName ] = metric; - } ); +var metricName, + deviceName, + i; + +for ( metricName in METRICS ) { + if ( METRICS[ metricName ].device !== null ) { + for ( i = 0; i < METRICS[ metricName ].device.length; i++ ) { + DEVICES[ METRICS[ metricName ].device[i] ] = ( DEVICES[ METRICS[ metricName ].device[i] ] ? DEVICES[ METRICS[ metricName ].device[i] ] : {} ); + } + } +} +for ( deviceName in DEVICES ) { + for ( metricName in METRICS ) { + if ( METRICS[ metricName ].device.indexOf( deviceName ) !== -1 ) { + DEVICES[ deviceName ][ metricName ] = METRICS[ metricName ]; + } } -} ); +} + // VARIABLES //