opentsdb-query
Advanced tools
Comparing version 0.0.0 to 0.0.1
102
lib/index.js
@@ -7,3 +7,3 @@ /** | ||
* DESCRIPTION: | ||
* - Query constructor. | ||
* - Query base class for metric and TSUID queries. | ||
* | ||
@@ -33,2 +33,7 @@ * | ||
// VARIABLES // | ||
var DOWNSAMPLE = /^[0-9]{1,}(ms|[smhdwmny])\-[a-z]+$/; | ||
// QUERY // | ||
@@ -54,5 +59,2 @@ | ||
// Tags: | ||
this._tags = {}; | ||
return this; | ||
@@ -73,3 +75,3 @@ } // end FUNCTION Query() | ||
if ( typeof aggregator !== 'string' ) { | ||
throw new Error( 'aggregator()::invalid input argument. Aggregator must be a string.' ); | ||
throw new TypeError( 'aggregator()::invalid input argument. Aggregator must be a string.' ); | ||
} | ||
@@ -92,4 +94,7 @@ this._aggregator = aggregator; | ||
if ( downsample !== null && typeof downsample !== 'string' ) { | ||
throw new Error( 'downsample()::invalid input argument. Downsample function must be a string.' ); | ||
throw new TypeError( 'downsample()::invalid input argument. Downsample function must be a string.' ); | ||
} | ||
if ( downsample !== null && !DOWNSAMPLE.test( downsample ) ) { | ||
throw new Error( 'downsample()::invalid format. The downsample format follows the convention: {value}{units}-{operator}; e.g., `5m-avg`.' ); | ||
} | ||
this._downsample = downsample; | ||
@@ -111,3 +116,3 @@ return this; | ||
if ( typeof bool !== 'boolean' ) { | ||
throw new Error( 'rate()::invalid input argument. Rate flag must be a boolean.' ); | ||
throw new TypeError( 'rate()::invalid input argument. Rate flag must be a boolean.' ); | ||
} | ||
@@ -138,3 +143,3 @@ this._rate = bool; | ||
if ( typeof options !== 'object' || options === null || Array.isArray( options ) === true ) { | ||
throw new Error( 'rateOptions()::invalid input argument. Rate options must be an object.' ); | ||
throw new TypeError( 'rateOptions()::invalid input argument. Rate options must be an object.' ); | ||
} | ||
@@ -145,3 +150,3 @@ | ||
if ( typeof counter !== 'boolean' ) { | ||
throw new Error( 'rateOptions()::invalid input argument. Counter must be a boolean flag.' ); | ||
throw new TypeError( 'rateOptions()::invalid input argument. Counter must be a boolean flag.' ); | ||
} | ||
@@ -154,3 +159,3 @@ this._rateOptions.counter = counter; | ||
if ( counterMax !== null && (typeof counterMax !== 'number' || counterMax !== counterMax) ) { | ||
throw new Error( 'rateOptions()::invalid input argument. Counter max must be numeric.' ); | ||
throw new TypeError( 'rateOptions()::invalid input argument. Counter max must be numeric.' ); | ||
} | ||
@@ -163,3 +168,3 @@ this._rateOptions.counterMax = counterMax; | ||
if ( typeof resetValue !== 'number' || resetValue !== resetValue ) { | ||
throw new Error( 'rateOptions()::invalid input argument. ResetValue must be numeric.' ); | ||
throw new TypeError( 'rateOptions()::invalid input argument. ResetValue must be numeric.' ); | ||
} | ||
@@ -173,51 +178,40 @@ this._rateOptions.resetValue = resetValue; | ||
/** | ||
* METHOD: tags( tag, value ) | ||
* Tag value setter and getter. If a value is supplied, sets the tag value. If no value is supplied, gets the tag value. If the tag name has not been set, getting the tag value returns undefined. If no tag name-value pair is provided, returns all tags. NOTE: the returned object for all tags does not correspond to the internal object. Accordingly, the internal object should not be treated as mutable. | ||
* METHOD: toString() | ||
* Serializes a query instance. | ||
* | ||
* @param {String} [tag] - tag name | ||
* @param {String} [value] - tag value; e.g., 'foo', 'foo|bar', or '*', where '*' means to request data for all tag values for a given tag name | ||
* @returns {Query|Object|String|undefined} Query instance, all tags, or tag value | ||
* @returns {String} serialized query string | ||
*/ | ||
Query.prototype.tags = function( tag, value ) { | ||
var numArgs = arguments.length, | ||
keys = Object.keys( this._tags ), | ||
key, | ||
tags = {}; | ||
if ( !numArgs ) { | ||
for ( var i = 0; i < keys.length; i++ ) { | ||
key = keys[ i ]; | ||
tags[ key ] = this._tags[ key ]; | ||
Query.prototype.toString = function() { | ||
var query = ''; | ||
// Aggregator: | ||
query += this._aggregator; | ||
// Rate? | ||
if ( this._rate ) { | ||
query += ':'; | ||
query += 'rate{'; | ||
// Counter: | ||
query += this._rateOptions.counter; | ||
query += ','; | ||
// Counter Max: | ||
if ( typeof this._rateOptions.counterMax === 'number' ) { | ||
query += this._rateOptions.counterMax; | ||
} | ||
return tags; | ||
query += ','; | ||
// Reset Value: | ||
query += this._rateOptions.resetValue; | ||
query += '}'; | ||
} | ||
if ( typeof tag !== 'string' ) { | ||
throw new Error( 'tag()::invalid input argument. Tag name must be a string.' ); | ||
} | ||
if ( numArgs === 1 ) { | ||
return this._tags[ tag ]; | ||
} | ||
if ( typeof value !== 'string' ) { | ||
throw new Error( 'tag()::invalid input argument. Tag value must be a string.' ); | ||
} | ||
this._tags[ tag ] = value; | ||
return this; | ||
}; // end METHOD tags() | ||
/** | ||
* METHOD: dtag( tag ) | ||
* Removes a tag. | ||
* | ||
* @param {String} tag - tag name | ||
* @returns {Query} Query instance | ||
*/ | ||
Query.prototype.dtag = function( tag ) { | ||
if ( !arguments.length ) { | ||
throw new Error( 'dtag()::insufficient input arguments. Must provide a tag name' ); | ||
// Downsample: | ||
if ( this._downsample ) { | ||
query += ':'; | ||
query += this._downsample ; | ||
} | ||
if ( typeof tag !== 'string' ) { | ||
throw new Error( 'dtag()::invalid input argument. Tag name must be a string.' ); | ||
} | ||
delete this._tags[ tag ]; | ||
return this; | ||
}; // end METHOD tag() | ||
return query; | ||
}; | ||
@@ -224,0 +218,0 @@ |
{ | ||
"name": "opentsdb-query", | ||
"version": "0.0.0", | ||
"description": "Base class for OpenTSDB queries.", | ||
"version": "0.0.1", | ||
"description": "Base class for OpenTSDB metric and TSUID queries.", | ||
"author": { | ||
@@ -6,0 +6,0 @@ "name": "Athan Reines", |
@@ -5,3 +5,3 @@ Query | ||
> Base class for [OpenTSDB](http://opentsdb.net) queries. | ||
> Base class for [OpenTSDB](http://opentsdb.net) metric and TSUID queries. | ||
@@ -51,3 +51,3 @@ | ||
This method is a setter/getter. If no `downsample` function is provided, returns the configured `downsample` function. By default, downsampling is turned off. To specify a `downsample` function, | ||
This method is a setter/getter. If no `downsample` function is provided, returns the configured `downsample` function. By default, downsampling is turned off (i.e., set to `null`). To specify a `downsample` function, | ||
@@ -102,3 +102,4 @@ ``` javascript | ||
"resetValue": 0 | ||
}); | ||
}) | ||
.toString(); | ||
``` | ||
@@ -132,3 +133,3 @@ | ||
``` bash | ||
$ open reports/coverage/lcov-report/index.html | ||
$ make view-cov | ||
``` | ||
@@ -135,0 +136,0 @@ |
162
13257
186