Browse Source

refactor REST endpoint

activeAddress
Ivan Socolsky 10 years ago
parent
commit
7e4a5f966a
  1. 14
      lib/expressapp.js
  2. 11
      lib/stats.js

14
lib/expressapp.js

@ -9,7 +9,7 @@ var querystring = require('querystring');
var bodyParser = require('body-parser')
var WalletService = require('./server');
var stats = require('./stats');
var Stats = require('./stats');
log.disableColor();
log.debug = log.verbose;
@ -340,18 +340,20 @@ ExpressApp.prototype.start = function(opts, cb) {
});
});
router.get('/v1/stats/:from/:to/', function(req, res) {
router.get('/v1/stats/', function(req, res) {
var opts = {};
opts.from = req.params['from'];
opts.to = req.params['to'];
stats.getStats(opts, function(err, data) {
if (req.query.network) opts.network = req.query.network;
if (req.query.from) opts.from = req.query.from;
if (req.query.to) opts.to = req.query.to;
var stats = new Stats(opts);
stats.run(function(err, data) {
if (err) return returnError(err, res, req);
res.json(data);
res.end();
});
});
this.app.use(opts.basePath || '/bws/api', router);
WalletService.initialize(opts, cb);

11
lib/stats.js

@ -57,6 +57,7 @@ Stats.prototype._getStats = function(cb) {
if (err) return cb(err);
result.newWallets = results[0];
result.txProposals = results[1];
return cb(null, result);
});
};
@ -98,15 +99,21 @@ Stats.prototype._getNewWallets = function(cb) {
var data = _.map(wallets, function(wallet) {
return {
day: moment(wallet.createdOn * 1000).format('YYYYMMDD'),
type: wallet.m + '-of-' + wallet.n,
type: (wallet.m == 1 && wallet.n == 1) ? 'personal' : 'shared',
config: wallet.m + '-of-' + wallet.n,
};
});
var stats = {
byDay: self._countBy(data, 'day'),
byType: self._countBy(data, 'type'),
byConfig: self._countBy(data, 'config'),
};
stats.byTypeThenDay = _.groupBy(data, 'type');
_.each(stats.byTypeThenDay, function(v, k) {
stats.byTypeThenDay[k] = self._countBy(v, 'day');
});
return cb(null, stats);
});
};

Loading…
Cancel
Save