You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@usergrid.apache.org by sn...@apache.org on 2014/01/28 21:22:42 UTC
[71/79] [abbrv] git commit: Adding Counters to the NodeJS SDK
Adding Counters to the NodeJS SDK
Project: http://git-wip-us.apache.org/repos/asf/incubator-usergrid/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-usergrid/commit/68b3b3c6
Tree: http://git-wip-us.apache.org/repos/asf/incubator-usergrid/tree/68b3b3c6
Diff: http://git-wip-us.apache.org/repos/asf/incubator-usergrid/diff/68b3b3c6
Branch: refs/pull/29/merge
Commit: 68b3b3c6765d10eb5eb0ac6af789ff959bbb92de
Parents: 80c4ddd
Author: ryan bridges <rb...@apigee.com>
Authored: Mon Jan 27 18:22:39 2014 -0500
Committer: ryan bridges <rb...@apigee.com>
Committed: Mon Jan 27 21:16:14 2014 -0500
----------------------------------------------------------------------
sdks/nodejs/lib/usergrid.js | 187 +++++++++++++++++++++++++++++++++++++++
sdks/nodejs/test.js | 94 ++++++++++++++++----
2 files changed, 264 insertions(+), 17 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/68b3b3c6/sdks/nodejs/lib/usergrid.js
----------------------------------------------------------------------
diff --git a/sdks/nodejs/lib/usergrid.js b/sdks/nodejs/lib/usergrid.js
index 3be7c77..59cb843 100755
--- a/sdks/nodejs/lib/usergrid.js
+++ b/sdks/nodejs/lib/usergrid.js
@@ -2207,11 +2207,198 @@ var AUTH_NONE = 'NONE';
}
return tail.join("&");
}
+/*
+ * A class to model a Usergrid event.
+ *
+ * @constructor
+ * @param {object} options {timestamp:0, category:'value', counters:{name : value}}
+ * @returns {callback} callback(err, event)
+ */
+Usergrid.Counter = function(options, callback) {
+ var self=this;
+ this._client = options.client;
+ this._data = options.data || {};
+ this._data.category = options.category||"UNKNOWN";
+ this._data.timestamp = options.timestamp||0;
+ this._data.type = "events";
+ this._data.counters=options.counters||{};
+ if(typeof(callback) === 'function') {
+ callback.call(self, false, self);
+ }
+ //this.save(callback);
+};
+var COUNTER_RESOLUTIONS=[
+ 'all', 'minute', 'five_minutes', 'half_hour',
+ 'hour', 'six_day', 'day', 'week', 'month'
+];
+/*
+ * Inherit from Usergrid.Entity.
+ * Note: This only accounts for data on the group object itself.
+ * You need to use add and remove to manipulate group membership.
+ */
+Usergrid.Counter.prototype = new Usergrid.Entity();
+
+/*
+ * overrides Entity.prototype.fetch. Returns all data for counters
+ * associated with the object as specified in the constructor
+ *
+ * @public
+ * @method increment
+ * @param {function} callback
+ * @returns {callback} callback(err, event)
+ */
+Usergrid.Counter.prototype.fetch=function(callback){
+ this.getData({}, callback);
+}
+/*
+ * increments the counter for a specific event
+ *
+ * options object: {name: counter_name}
+ *
+ * @public
+ * @method increment
+ * @params {object} options
+ * @param {function} callback
+ * @returns {callback} callback(err, event)
+ */
+Usergrid.Counter.prototype.increment=function(options, callback){
+ var self=this,
+ name=options.name,
+ value=options.value;
+ if(!name){
+ if(typeof(callback) === 'function') {
+ return callback.call(self, true, "'value' for increment, decrement must be a number");
+ }
+ }else if(isNaN(value)){
+ if(typeof(callback) === 'function') {
+ return callback.call(self, true, "'value' for increment, decrement must be a number");
+ }
+ }else{
+ self._data.counters[name]=(parseInt(value))||1;
+ return self.save(callback);
+ }
+};
+/*
+ * decrements the counter for a specific event
+ *
+ * options object: {name: counter_name}
+ *
+ * @public
+ * @method decrement
+ * @params {object} options
+ * @param {function} callback
+ * @returns {callback} callback(err, event)
+ */
+
+Usergrid.Counter.prototype.decrement=function(options, callback){
+ var self=this,
+ name=options.name,
+ value=options.value;
+ self.increment({name:name, value:-((parseInt(value))||1)}, callback);
+};
+/*
+ * resets the counter for a specific event
+ *
+ * options object: {name: counter_name}
+ *
+ * @public
+ * @method reset
+ * @params {object} options
+ * @param {function} callback
+ * @returns {callback} callback(err, event)
+ */
+
+Usergrid.Counter.prototype.reset=function(options, callback){
+ var self=this,
+ name=options.name;
+ self.increment({name:name, value:0}, callback);
+};
+
+/*
+ * gets data for one or more counters over a given
+ * time period at a specified resolution
+ *
+ * options object: {
+ * counters: ['counter1', 'counter2', ...],
+ * start: epoch timestamp or ISO date string,
+ * end: epoch timestamp or ISO date string,
+ * resolution: one of ('all', 'minute', 'five_minutes', 'half_hour', 'hour', 'six_day', 'day', 'week', or 'month')
+ * }
+ *
+ * @public
+ * @method getData
+ * @params {object} options
+ * @param {function} callback
+ * @returns {callback} callback(err, event)
+ */
+Usergrid.Counter.prototype.getData=function(options, callback){
+ var start_time,
+ end_time,
+ start=options.start||0,
+ end=options.end||Date.now(),
+ resolution=(options.resolution||'all').toLowerCase(),
+ counters=options.counters||Object.keys(this._data.counters),
+ res=(resolution||'all').toLowerCase();
+ if(COUNTER_RESOLUTIONS.indexOf(res)===-1){
+ res='all';
+ }
+ if(start){
+ switch(typeof start){
+ case "undefined":
+ start_time=0;
+ break;
+ case "number":
+ start_time=start;
+ break;
+ case "string":
+ start_time=(isNaN(start))?Date.parse(start):parseInt(start);
+ break;
+ default:
+ start_time=Date.parse(start.toString());
+ }
+ }
+ if(end){
+ switch(typeof end){
+ case "undefined":
+ end_time=Date.now();
+ break;
+ case "number":
+ end_time=end;
+ break;
+ case "string":
+ end_time=(isNaN(end))?Date.parse(end):parseInt(end);
+ break;
+ default:
+ end_time=Date.parse(end.toString());
+ }
+ }
+ var self=this;
+ //https://api.usergrid.com/yourorgname/sandbox/counters?counter=test_counter
+ var params=Object.keys(counters).map(function(counter){
+ return ["counter", encodeURIComponent(counters[counter])].join('=');
+ });
+ params.push('resolution='+res)
+ params.push('start_time='+String(start_time))
+ params.push('end_time='+String(end_time))
+
+ var endpoint="counters?"+params.join('&');
+ this._client.request({endpoint:endpoint}, function(err, data){
+ if(data.counters && data.counters.length){
+ data.counters.forEach(function(counter){
+ self._data.counters[counter.name]=counter.value||counter.values;
+ })
+ }
+ if(typeof(callback) === 'function') {
+ callback.call(self, err, data);
+ }
+ })
+};
exports.client = Usergrid.Client;
exports.entity = Usergrid.Entity;
exports.collection = Usergrid.Collection;
exports.group = Usergrid.Group;
+exports.counter = Usergrid.Counter;
exports.AUTH_CLIENT_ID = AUTH_CLIENT_ID;
exports.AUTH_APP_USER = AUTH_APP_USER;
exports.AUTH_NONE = AUTH_NONE;
http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/68b3b3c6/sdks/nodejs/test.js
----------------------------------------------------------------------
diff --git a/sdks/nodejs/test.js b/sdks/nodejs/test.js
index c93754a..fabb27e 100755
--- a/sdks/nodejs/test.js
+++ b/sdks/nodejs/test.js
@@ -23,23 +23,12 @@ var _email = 'marty'+_unique+'@timetravel.com';
var _password = 'password2';
var _newpassword = 'password3';
- var client = new usergrid.client({
- orgName:'yourorgname',
- appName:'yourappname',
- authType:usergrid.AUTH_CLIENT_ID,
- clientId:'<your client id>',
- clientSecret:'<your client secret>',
- logging: false, //optional - turn on logging, off by default
- buildCurl: false //optional - turn on curl commands, off by default
- });
-
-
- var client = new usergrid.client({
- orgName:'yourorgname',
- appName:'sandbox',
- logging: true, //optional - turn on logging, off by default
- buildCurl: true //optional - turn on curl commands, off by default
- });
+var client = new usergrid.client({
+ orgName:'yourorgname',
+ appName:'sandbox',
+ logging: true, //optional - turn on logging, off by default
+ buildCurl: true //optional - turn on curl commands, off by default
+});
//call the runner function to start the process
@@ -186,6 +175,26 @@ function runner(step, arg, arg2){
notice('-----running step '+step+': clean up dogs');
cleanUpDogs(step, arg);
break;
+ case 35:
+ notice('-----running step '+step+': create counter');
+ counterCreate(step, arg);
+ break;
+ case 36:
+ notice('-----running step '+step+': reset counter');
+ counterReset(step, arg);
+ break;
+ case 37:
+ notice('-----running step '+step+': increment counter');
+ counterIncrement(step, arg);
+ break;
+ case 38:
+ notice('-----running step '+step+': decrement counter');
+ counterDecrement(step, arg);
+ break;
+ case 34:
+ notice('-----running step '+step+': fetch counter data');
+ counterFetch(step, arg);
+ break;
default:
notice('-----test complete!-----');
notice('Success count= ' + successCount);
@@ -998,3 +1007,54 @@ function cleanUpDogs(step){
}
});
}
+//var counter;
+function counterCreate(step){
+ var counter = new usergrid.counter({client:client, data:{category:'mocha_test', timestamp:0, name:"test", counters:{test:0,test_counter:0}}}, function(err, data){
+ if (err) {
+ error('counter not removed');
+ } else {
+ success('counter created');
+ }
+ });
+ runner(step, counter);
+}
+function counterReset(step, counter){
+ counter.reset({name:'test'}, function(err, data){
+ if (err) {
+ error('counter not reset');
+ } else {
+ success('counter reset');
+ }
+ runner(step, counter);
+ });
+}
+function counterIncrement(step, counter){
+ counter.increment({name:'test', value:1}, function(err, data){
+ if (err) {
+ error('counter not incremented');
+ } else {
+ success('counter incremented');
+ }
+ runner(step, counter);
+ });
+}
+function counterDecrement(step, counter){
+ counter.decrement({name:'test', value:1}, function(err, data){
+ if (err) {
+ error('counter not decremented');
+ } else {
+ success('counter decremented');
+ }
+ runner(step, counter);
+ });
+}
+function counterFetch(step, counter){
+ counter.getData({resolution:'all', counters:['test', 'test_counter']}, function(err, data){
+ if (err) {
+ error('counter not fetched');
+ } else {
+ success('counter fetched');
+ }
+ runner(step, counter);
+ });
+}