Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds Usage API along with tests and bumps version.
- Loading branch information
Showing
5 changed files
with
289 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,98 @@ | |||
var request = require('superagent'); | |||
var config = require('../config'); | |||
|
|||
var Usage = function(key) { | |||
if (!key || !key.length) { | |||
throw new TypeError('You must provide an API Key to ' + | |||
'create an instance of this class.'); | |||
} | |||
this.apikey = key; | |||
this.url = config.baseurl + '/usage'; | |||
}; | |||
|
|||
(function() { | |||
var apikeyHeader = config.apikey_header; | |||
|
|||
/** | |||
* Returns detailed or totalized information | |||
* about sent and received messages. | |||
* @param {Object} filter An optinal filter object. | |||
* @param {Function} callback The callback function. | |||
* | |||
* The filter object should have the following properties: | |||
* { | |||
* from: A String with an ISO8601 Date, indicating the initial | |||
* date from where you want to make the query. | |||
* to: A String with an ISO8601 Date indicating the end | |||
* date. | |||
* resource_id: A String with a resource id. | |||
* swarm_id: A String with a swarm id. | |||
* totalized: A Boolean indicating whether you want to get the | |||
* total usage or not. | |||
* page: A Number with the result page you want to get. | |||
* count: The number of results you would like to get. | |||
* orderby: A string with the name of the field by which you would like to sort the result. | |||
* sort: A string with 'asc' or desc' that determines the order | |||
of the result. | |||
* | |||
* } | |||
* | |||
* @public | |||
**/ | |||
this.get = function() { | |||
var filter, callback; | |||
var arglen = arguments.length; | |||
|
|||
if (arglen > 2 || arglen < 1) { | |||
var arglen = arguments.length; | |||
throw new TypeError('Wrong number of arguments. ' + | |||
'In order to invoke this function you need ' + | |||
'to provide one or two arguments.'); | |||
} | |||
|
|||
if (arglen == 1) { | |||
callback = arguments[0]; | |||
if (typeof callback !== 'function') { | |||
throw new TypeError('When invoking with one ' + | |||
'argument, a callback function is expected.'); | |||
|
|||
} | |||
} else if (arglen == 2) { | |||
filter = arguments[0]; | |||
callback = arguments[1]; | |||
|
|||
if (typeof filter !== 'object' || | |||
typeof callback !== 'function') { | |||
throw new TypeError('When invoking with two ' + | |||
'arguments, an object is expected as the first ' + | |||
'argument and a callback function as the second argument.'); | |||
} | |||
|
|||
/** | |||
* Workaround for superagent given that it | |||
* doesn't handle boolean values properly when | |||
* sending data through the query string. | |||
**/ | |||
filter.totalized = filter.totalized ? 'true' : 'false'; | |||
} | |||
|
|||
var url = this.url; | |||
|
|||
request | |||
.get(url + '/messages') | |||
.send(filter) | |||
.set(apikeyHeader, this.apikey) | |||
.end(function(res) { | |||
var err; | |||
if (res.status == 200) { | |||
callback(err, res.body); | |||
} else { | |||
err = res.body || res.text; | |||
callback(err); | |||
} | |||
}); | |||
} | |||
}).call(Usage.prototype); | |||
|
|||
module.exports = Usage; | |||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,186 @@ | |||
var SwarmService = require('../lib/swarm'); | |||
var ResourceService = require('../lib/resource'); | |||
var ApiKeyService = require('../lib/apikey'); | |||
var UsageService = require('../lib/usage'); | |||
|
|||
var bugswarmprt = require('bugswarm-prt'); | |||
var Prosumer = bugswarmprt.Swarm; | |||
|
|||
describe('Usage service', function() { | |||
var swarmId; | |||
var prosumerId; | |||
var cfgKey; | |||
var prtKey; | |||
|
|||
var lastRecord = {}; | |||
|
|||
var apikeyService; | |||
var swarmService; | |||
var resourceService; | |||
var usageService; | |||
|
|||
before(function(done) { | |||
apikeyService = new ApiKeyService('librarytest', 'test123'); | |||
apikeyService.generate(function(err, keys) { | |||
for(var i in keys) { | |||
if(keys[i].type == 'configuration') { | |||
cfgKey = keys[i].key; | |||
} else { | |||
prtKey = keys[i].key; | |||
} | |||
} | |||
swarmService = new SwarmService(cfgKey); | |||
resourceService = new ResourceService(cfgKey); | |||
usageService = new UsageService(cfgKey); | |||
done(); | |||
}); | |||
}); | |||
|
|||
it('should prepare the environment for this test suite', function(done) { | |||
var data = { | |||
name: 'my swarm', | |||
public: false, | |||
description: 'my swarm description' | |||
}; | |||
|
|||
//creates swarm | |||
swarmService.create(data, | |||
function(err, swarm) { | |||
swarmId = swarm.id; | |||
|
|||
var data = { | |||
name: 'my resource', | |||
description: 'my description', | |||
machine_type: 'bug' | |||
}; | |||
|
|||
//creates resource | |||
resourceService.create(data, function(err, resource) { | |||
var options = { | |||
swarm_id: swarmId, | |||
resource_id: resource.id, | |||
resource_type: 'consumer' | |||
}; | |||
|
|||
prosumerId = resource.id; | |||
//adds resource as consumer | |||
swarmService.addResource(options, function(err) { | |||
options.resource_type = 'producer'; | |||
//adds resource as producer | |||
swarmService.addResource(options, function(err) { | |||
done(); | |||
}); | |||
}); | |||
}); | |||
}); | |||
}); | |||
|
|||
it('should generate some messages', function(done) { | |||
var options = { | |||
apikey: prtKey, | |||
resource: prosumerId, | |||
swarms: swarmId | |||
}; | |||
|
|||
var prosumer = new Prosumer(options); | |||
prosumer.on('error', function(err) { | |||
assert.isUndefined(err); | |||
prosumer.disconnect(); | |||
}); | |||
|
|||
prosumer.on('presence', function(presence) { | |||
if (presence.from.swarm) { | |||
presence.from.swarm.should.equal(swarmId); | |||
for (var i = 0; i < 3; i++) { | |||
prosumer.send('hey!'); | |||
} | |||
} | |||
}); | |||
|
|||
var j = 1; | |||
prosumer.on('message', function(message) { | |||
if (j == 3) { | |||
prosumer.disconnect(); | |||
} | |||
j++; | |||
}); | |||
|
|||
prosumer.on('disconnect', function() { | |||
done(); | |||
}); | |||
|
|||
prosumer.connect(); | |||
}); | |||
|
|||
it('should return the total of sent and received messages between a given period of time.', function(done) { | |||
var from = new Date(); | |||
from.setMinutes(0); | |||
from.setSeconds(0); | |||
from.setMilliseconds(0); | |||
from = from.toISOString(); | |||
|
|||
var to = new Date(); | |||
to.setMinutes(59); | |||
to.setSeconds(59); | |||
to = to.toISOString(); | |||
|
|||
|
|||
var filter = { | |||
totalized: true, | |||
resource_id: prosumerId, | |||
from: from, | |||
to: to | |||
}; | |||
|
|||
usageService.get(filter, function(error, data) { | |||
Array.isArray(data).should.be.false; | |||
Object.keys(data).should.not.be.empty; | |||
data.should.have.property('messages'); | |||
data.messages.should.have.property('sent', 3); | |||
data.messages.should.have.property('received', 0); | |||
done(); | |||
}); | |||
}); | |||
|
|||
it('should return the user\'s usage detail', function(done) { | |||
usageService.get(function(error, data) { | |||
Array.isArray(data).should.be.true; | |||
data.length.should.be.above(0); | |||
for(var i in data) { | |||
data[i].user_id.should.eql('librarytest'); | |||
} | |||
|
|||
['resource_id', 'swarm_id', | |||
'timestamp', 'user_id', | |||
'bytes', 'messages'].forEach(function(p) { | |||
data[0].should.have.property(p); | |||
}); | |||
|
|||
done(); | |||
}); | |||
}); | |||
|
|||
it('should return detailed information of the current month if no dates are provided', function(done) { | |||
usageService.get(function(error, data) { | |||
Array.isArray(data).should.be.true; | |||
data.length.should.be.above(0); | |||
|
|||
var now = new Date(); | |||
var cyear = now.getFullYear(); | |||
var cmonth = now.getMonth(); | |||
var cday = now.getDate(); | |||
var chour = now.getHours(); | |||
|
|||
var currentMonth = new Date(cyear, cmonth).getTime(); | |||
|
|||
for(var i in data) { | |||
var timestamp = new Date(data[i].timestamp); | |||
var mtime = new Date(timestamp.getFullYear(), | |||
timestamp.getMonth()).getTime(); | |||
|
|||
currentMonth.should.eql(mtime); | |||
} | |||
done(); | |||
}); | |||
}); | |||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters