Browse Source

Splitting stats by date

activeAddress
Matias Pando 10 years ago
parent
commit
2873fd8e40
  1. 7
      lib/expressapp.js
  2. 155
      scripts/stats.js

7
lib/expressapp.js

@ -316,8 +316,11 @@ ExpressApp.prototype.start = function(opts, cb) {
}); });
}); });
router.get('/v1/stats/', function(req, res) { router.get('/v1/stats/:from/:to/', function(req, res) {
var data = stats.getStats(function(err, data) { var opts = {};
opts.from = req.params['from'];
opts.to = req.params['to'];
var data = stats.getStats(opts, function(err, data) {
if (err) return returnError(err, res, req); if (err) return returnError(err, res, req);
res.json(data); res.json(data);
res.end(); res.end();

155
scripts/stats.js

@ -8,53 +8,26 @@ var moment = require('moment');
var async = require('async'); var async = require('async');
var config = require('../config'); var config = require('../config');
var otherDate;
//trying to parse optional parameter to get stats on any given date
try {
otherDate = process.argv[2] && moment(process.argv[2]).isValid() ? moment(process.argv[2]) : null;
} catch (e) {
console.log('Enter the date in the format YYYY-MM-DD.');
}
var c = config.storageOpts.mongoDb; var c = config.storageOpts.mongoDb;
var url = 'mongodb://' + (c.host || 'localhost') + ':' + (c.port ||  27017) + '/bws'; var url = 'mongodb://' + (c.host || 'localhost') + ':' + (c.port ||  27017) + '/bws';
var Today = otherDate || moment(); var startDate = moment();
var endDate = moment();
var stats = {
'livenet': { var stats = {};
totalTx: 0,
totalAmount: 0,
totalNewWallets: 0
},
'testnet': {
totalTx: 0,
totalAmount: 0,
totalNewWallets: 0
}
}
var wallets = {}; var wallets = {};
var bwsStats = {}; var bwsStats = {};
bwsStats.IsToday = function(date) { bwsStats.cleanUp = function() {
if (!date) return false; stats = {
var date = moment(date * 1000); 'livenet': {},
return (date >= Today.startOf('day') && date <= Today.endOf('day')); 'testnet': {}
};
} }
bwsStats.TotalTxpForToday = function(data) {
if (!data) return;
if (!bwsStats.IsToday(data.createdOn)) return;
var network = wallets[data.walletId];
stats[network].totalTx++;
stats[network].totalAmount += data.amount;
};
bwsStats.AddingWalletToCache = function(data) { bwsStats.AddingWalletToCache = function(data) {
if (!data) return; if (!data) return;
@ -63,55 +36,107 @@ bwsStats.AddingWalletToCache = function(data) {
bwsStats.TotalNewWalletForToday = function(data) { bwsStats.TotalNewWalletForToday = function(data) {
if (!data) return; if (!data) return;
var day = moment(data.createdOn * 1000).format('YYYYMMDD');
if (!bwsStats.IsToday(data.createdOn)) return; if (!stats[data.network][day]) {
stats[data.network].totalNewWallets++; stats[data.network][day] = {
totalTx: 0,
totalAmount: 0,
totalNewWallets: 0
};
}
stats[data.network][day].totalNewWallets++;
}; };
bwsStats.PrintStats = function() { bwsStats.TotalTxpForToday = function(data) {
console.log('Stats for date : ', Today.format("YYYY-MM-DD")); if (!data) return;
console.log(' '); var day = moment(data.createdOn * 1000).format('YYYYMMDD');
var network = wallets[data.walletId];
for (var s in stats) { if (!stats[network][day]) {
console.log(' '); stats[network][day] = {
console.log(s + ' stats--------------------------------- ') totalTx: 0,
console.log('New wallets : ', stats[s].totalNewWallets); totalAmount: 0,
console.log('Total tx : ', stats[s].totalTx); totalNewWallets: 0
console.log('Total amount in tx (BTC) : ', stats[s].totalAmount * 1 / 1e8); };
} }
stats[network][day].totalTx++;
stats[network][day].totalAmount += data.amount;
}; };
bwsStats.ProcessData = function(DB, cb) { bwsStats.ProcessData = function(DB, cb) {
bwsStats.ProccesWallets(DB, function() { bwsStats.ProccesWallets(DB, function() {
bwsStats.ProccesTxs(DB, cb); bwsStats.ProccesNewWallets(DB, function() {
bwsStats.ProccesTxs(DB, cb);
});
}); });
}; };
bwsStats.ProccesWallets = function(DB, cb) { bwsStats.ProccesWallets = function(DB, cb) {
var collection = DB.collection('wallets'); var collection = DB.collection('wallets');
collection.find({}).toArray(function(err, items) { collection.find({}).toArray(function(err, items) {
for (var i = 0; i < items.length; i++) { if (err) {
bwsStats.AddingWalletToCache(items[i]); console.log('Error.', err);
bwsStats.TotalNewWalletForToday(items[i]); }
};
items.forEach(function(it) {
bwsStats.AddingWalletToCache(it);
});
cb(); cb();
}); });
}; };
bwsStats.ProccesTxs = function(DB, cb) { bwsStats.ProccesNewWallets = function(DB, cb) {
var collection = DB.collection('txs'); var collection = DB.collection('wallets');
var start = Math.floor(startDate.startOf('day').valueOf() / 1000);
var end = Math.floor(endDate.endOf('day').valueOf() / 1000);
collection.find({}).toArray(function(err, items) { collection.find({
for (var i = 0; i < items.length; i++) { createdOn: {
bwsStats.TotalTxpForToday(items[i]); $gt: start,
}; $lt: end
}
}).toArray(function(err, items) {
if (err) {
console.log('Error.', err);
}
items.forEach(function(it) {
bwsStats.TotalNewWalletForToday(it);
});
cb(); cb();
}); });
}; };
bwsStats.getStats = function(cb) { bwsStats.ProccesTxs = function(DB, cb) {
var collection = DB.collection('txs');
var start = Math.floor(startDate.startOf('day').valueOf() / 1000);
var end = Math.floor(endDate.endOf('day').valueOf() / 1000);
collection.find({
createdOn: {
$gt: start,
$lt: end
}
},
function(err, items) {
if (err || !items) {
console.log("No items found.");
} else {
items.forEach(function(it) {
bwsStats.TotalTxpForToday(it);
});
}
cb();
});
};
bwsStats.getStats = function(opts, cb) {
if (opts) {
startDate = moment(opts.from);
endDate = moment(opts.to);
}
bwsStats.cleanUp();
mongodb.MongoClient.connect(url, function(err, db) { mongodb.MongoClient.connect(url, function(err, db) {
if (err) { if (err) {
console.log('Unable to connect to the mongoDB server. Error:', err); console.log('Unable to connect to the mongoDB server. Error:', err);
@ -119,12 +144,12 @@ bwsStats.getStats = function(cb) {
} }
console.log('Connection established to ', url); console.log('Connection established to ', url);
bwsStats.ProcessData(db, function(err) { bwsStats.ProcessData(db, function(err) {
db.close();
if (err) { if (err) {
console.log('Error.', err);
cb(err, null); cb(err, null);
return; return;
} }
cb(null, stats) cb(null, stats);
}); });
}); });
} }

Loading…
Cancel
Save