Skip to content

Commit

Permalink
Add a Stats type, so values exist for all members even when populated…
Browse files Browse the repository at this point in the history
… by a sparse result
  • Loading branch information
paddybyers committed Apr 3, 2015
1 parent 4b909a3 commit 16c1515
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 1 deletion.
1 change: 1 addition & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ module.exports = function (grunt) {
'<%= dirs.common %>/lib/types/message.js',
'<%= dirs.common %>/lib/types/presencemessage.js',
'<%= dirs.common %>/lib/types/protocolmessage.js',
'<%= dirs.common %>/lib/types/stats.js',

'<%= dirs.common %>/lib/transport/connectionerror.js',
'<%= dirs.common %>/lib/transport/connectionmanager.js',
Expand Down
4 changes: 3 additions & 1 deletion common/lib/client/rest.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ var Rest = (function() {
Utils.mixin(headers, this.options.headers);

(new PaginatedResource(this, '/stats', headers, params, envelope, function(body, headers, unpacked) {
return unpacked ? body : JSON.parse(body);
var statsValues = (unpacked ? body : JSON.parse(body));
for(var i = 0; i < statsValues.length; i++) statsValues[i] = Stats.fromValues(statsValues[i]);
return statsValues;
})).get(callback);
};

Expand Down
59 changes: 59 additions & 0 deletions common/lib/types/stats.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
var Stats = (function() {

function MessageCount(values) {
this.count = (values && values.count) || 0;
this.data = (values && values.data) || 0;
}

function ResourceCount(values) {
this.peak = (values && values.peak) || 0;
this.min = (values && values.min) || 0;
this.mean = (values && values.mean) || 0;
this.opened = (values && values.opened) || 0;
this.refused = (values && values.refused) || 0;
}

function RequestCount(values) {
this.succeeded = (values && values.succeeded) || 0;
this.failed = (values && values.failed) || 0;
this.refused = (values && values.refused) || 0;
}

function ConnectionTypes(values) {
this.plain = new ResourceCount(values && values.plain);
this.tls = new ResourceCount(values && values.tls);
this.all = new ResourceCount(values && values.all);
}

function MessageTypes(values) {
this.messages = new MessageCount(values && values.messages);
this.presence = new MessageCount(values && values.presence);
this.all = new MessageCount(values && values.all);
}

function MessageTraffic(values) {
this.realtime = new MessageTypes(values && values.realtime);
this.rest = new MessageTypes(values && values.rest);
this.webhook = new MessageTypes(values && values.webhook);
this.all = new MessageTypes(values && values.all);
}

function Stats(values) {
this.all = new MessageTypes(values && values.all);
this.inbound = new MessageTraffic(values && values.inbound);
this.outbound = new MessageTraffic(values && values.outbound);
this.persisted = new MessageTypes(values && values.persisted);
this.connections = new ConnectionTypes(values && values.connections);
this.channels = new ResourceCount(values && values.channels);
this.apiRequests = new RequestCount(values && values.apiRequests);
this.tokenRequests = new RequestCount(values && values.tokenRequests);
this.inProgress = (values && values.inProgress) || undefined;
this.intervalId = (values && values.intervalId) || undefined;
}

Stats.fromValues = function(values) {
return new Stats(values);
};

return Stats;
})();
1 change: 1 addition & 0 deletions nodejs/realtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ includeScript('../common/lib/types/errorinfo.js');
includeScript('../common/lib/types/message.js');
includeScript('../common/lib/types/presencemessage.js');
includeScript('../common/lib/types/protocolmessage.js');
includeScript('../common/lib/types/stats.js');
includeScript('../common/lib/transport/connectionerror.js');
includeScript('../common/lib/transport/connectionmanager.js');
includeScript('../common/lib/transport/transport.js');
Expand Down
1 change: 1 addition & 0 deletions nodejs/rest.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ includeScript('./lib/util/crypto.js');
includeScript('../common/lib/types/errorinfo.js');
includeScript('../common/lib/types/message.js');
includeScript('../common/lib/types/presencemessage.js');
includeScript('../common/lib/types/stats.js');
includeScript('../common/lib/client/resource.js');
includeScript('../common/lib/client/paginatedresource.js');
includeScript('../common/lib/client/auth.js');
Expand Down

1 comment on commit 16c1515

@mattheworiordan
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, thanks @paddybyers

Please sign in to comment.