Skip to content
Browse files

api.stats.getAll returns a nested hash rather than stringified keys

  • Loading branch information...
1 parent 24b1a1b commit c9e84bfe4920a05fa9271fccff58c158d916134d @evantahler committed Mar 19, 2013
Showing with 42 additions and 30 deletions.
  1. +12 −22 initializers/stats.js
  2. +25 −3 initializers/utils.js
  3. +2 −2 test/action_status.js
  4. +2 −2 test/client_socket.js
  5. +1 −1 test/core_tasks.js
View
34 initializers/stats.js
@@ -33,33 +33,23 @@ var stats = function(api, next){
api.stats.getAll = function(next){
api.redis.client.hgetall(api.stats.collections.global, function(err, globalStats){
api.redis.client.hgetall(api.stats.collections.local, function(err, localStats){
- var statNames = [];
if(globalStats == null){ globalStats = {}; }
if(localStats == null){ localStats = {}; }
- for(var i in localStats){
- statNames.push(i);
- localStats[i] = parseFloat(localStats[i]);
+
+ var results = {
+ global: {},
+ local: {},
}
- for(var i in globalStats){
- statNames.push(i);
- globalStats[i] = parseFloat(globalStats[i]);
+
+ for(var key in globalStats){
+ api.utils.hasifyNestedString(key, globalStats[key], results.global);
}
- api.utils.arrayUniqueify(statNames);
- statNames.sort();
- var result = {
- global: {},
- local: {}
- };
- for(var i in statNames){
- var name = statNames[i];
- if(globalStats[name] != null){
- result.global[name] = globalStats[name];
- }
- if(localStats[name] != null){
- result.local[name] = localStats[name];
- }
+
+ for(var key in localStats){
+ api.utils.hasifyNestedString(key, localStats[key], results.local);
}
- next(err, result);
+
+ next(err, results);
});
});
}
View
28 initializers/utils.js
@@ -137,10 +137,32 @@ var utils = function(api, next){
});
}
return cookies;
- };
+ }
+
+ ////////////////////////////////////////////////////////////////////////////
+ // convert strings of the form "a:b:c" into a nested hash with a value
+
+ api.utils.hasifyNestedString = function(string, value, hash, seperator){
+ if(hash == null){ hash = {}; }
+ if(seperator == null){ seperator = ":"; }
+ var parts = string.split(seperator);
+ var refHash = hash;
+ var depth = 0;
+ parts.forEach(function(part){
+ if(depth == parts.length - 1){
+ refHash[part] = value;
+ }else{
+ if(refHash[part] == null){ refHash[part] = {}; }
+ refHash = refHash[part];
+ }
+ depth++;
+ });
+ return hash;
+ }
- next()
-}
////////////////////////////////////////////////////////////////////////////
// EXPORT
+ next();
+}
+
exports.utils = utils;
View
4 test/action_status.js
@@ -18,8 +18,8 @@ describe('Action: status', function(){
response.body.uptime.should.be.above(0);
response.body.id.length.should.be.above(0);
- response.body.stats.local['actions:actionsCurrentlyProcessing'].should.be.above(0);
- response.body.stats.local['actions:totalProcessedActions'].should.be.above(0);
+ response.body.stats.local.actions.actionsCurrentlyProcessing.should.be.above(0);
+ response.body.stats.local.actions.totalProcessedActions.should.be.above(0);
done();
});
View
4 test/client_socket.js
@@ -77,15 +77,15 @@ describe('Client: Socket', function(){
it('single string message are treated as actions', function(done){
makeSocketRequest(client, "status", function(response){
response.should.be.an.instanceOf(Object)
- response.stats.local['connections:activeConnections:socket'].should.equal(3)
+ response.stats.local.connections.activeConnections.socket.should.equal(3)
done();
});
});
it('stringified JSON can also be sent as actions', function(done){
makeSocketRequest(client, JSON.stringify({action: 'status', params: {something: 'else'}}), function(response){
response.should.be.an.instanceOf(Object)
- response.stats.local['connections:activeConnections:socket'].should.equal(3)
+ response.stats.local.connections.activeConnections.socket.should.equal(3)
done();
});
});
View
2 test/core_tasks.js
@@ -393,7 +393,7 @@ describe('Core: Tasks', function(){
});
it('periodc tasks which return a failure will still be re-enqueued and tried again', function(done){
- this.timeout(5000);
+ this.timeout(10000);
var worker = new rawAPI.taskProcessor({id: 1});
var task = new rawAPI.task({name: 'busted_task'});
task.enqueue(function(err, resp){

0 comments on commit c9e84bf

Please sign in to comment.
Something went wrong with that request. Please try again.