From 2873fd8e406e20716b6623ce21150d41160effb4 Mon Sep 17 00:00:00 2001 From: Matias Pando Date: Thu, 7 May 2015 17:07:29 -0300 Subject: [PATCH] Splitting stats by date --- lib/expressapp.js | 7 ++- scripts/stats.js | 155 +++++++++++++++++++++++++++------------------- 2 files changed, 95 insertions(+), 67 deletions(-) diff --git a/lib/expressapp.js b/lib/expressapp.js index 7e88863..fa553f7 100644 --- a/lib/expressapp.js +++ b/lib/expressapp.js @@ -316,8 +316,11 @@ ExpressApp.prototype.start = function(opts, cb) { }); }); - router.get('/v1/stats/', function(req, res) { - var data = stats.getStats(function(err, data) { + router.get('/v1/stats/:from/:to/', function(req, res) { + 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); res.json(data); res.end(); diff --git a/scripts/stats.js b/scripts/stats.js index edfc192..04cdf39 100644 --- a/scripts/stats.js +++ b/scripts/stats.js @@ -8,53 +8,26 @@ var moment = require('moment'); var async = require('async'); 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 url = 'mongodb://' + (c.host || 'localhost') + ':' + (c.port ||  27017) + '/bws'; -var Today = otherDate || moment(); - -var stats = { - 'livenet': { - totalTx: 0, - totalAmount: 0, - totalNewWallets: 0 - }, - 'testnet': { - totalTx: 0, - totalAmount: 0, - totalNewWallets: 0 - } -} +var startDate = moment(); +var endDate = moment(); + +var stats = {}; var wallets = {}; var bwsStats = {}; -bwsStats.IsToday = function(date) { - if (!date) return false; - var date = moment(date * 1000); - return (date >= Today.startOf('day') && date <= Today.endOf('day')); +bwsStats.cleanUp = function() { + stats = { + 'livenet': {}, + '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) { if (!data) return; @@ -63,55 +36,107 @@ bwsStats.AddingWalletToCache = function(data) { bwsStats.TotalNewWalletForToday = function(data) { if (!data) return; - - if (!bwsStats.IsToday(data.createdOn)) return; - stats[data.network].totalNewWallets++; - + var day = moment(data.createdOn * 1000).format('YYYYMMDD'); + if (!stats[data.network][day]) { + stats[data.network][day] = { + totalTx: 0, + totalAmount: 0, + totalNewWallets: 0 + }; + } + stats[data.network][day].totalNewWallets++; }; -bwsStats.PrintStats = function() { - console.log('Stats for date : ', Today.format("YYYY-MM-DD")); - console.log(' '); - - for (var s in stats) { - console.log(' '); - console.log(s + ' stats--------------------------------- ') - console.log('New wallets : ', stats[s].totalNewWallets); - console.log('Total tx : ', stats[s].totalTx); - console.log('Total amount in tx (BTC) : ', stats[s].totalAmount * 1 / 1e8); +bwsStats.TotalTxpForToday = function(data) { + if (!data) return; + var day = moment(data.createdOn * 1000).format('YYYYMMDD'); + var network = wallets[data.walletId]; + if (!stats[network][day]) { + stats[network][day] = { + totalTx: 0, + totalAmount: 0, + totalNewWallets: 0 + }; } + stats[network][day].totalTx++; + stats[network][day].totalAmount += data.amount; }; + bwsStats.ProcessData = function(DB, cb) { bwsStats.ProccesWallets(DB, function() { - bwsStats.ProccesTxs(DB, cb); + bwsStats.ProccesNewWallets(DB, function() { + bwsStats.ProccesTxs(DB, cb); + }); }); }; + bwsStats.ProccesWallets = function(DB, cb) { var collection = DB.collection('wallets'); - collection.find({}).toArray(function(err, items) { - for (var i = 0; i < items.length; i++) { - bwsStats.AddingWalletToCache(items[i]); - bwsStats.TotalNewWalletForToday(items[i]); - }; + if (err) { + console.log('Error.', err); + } + + items.forEach(function(it) { + bwsStats.AddingWalletToCache(it); + }); cb(); }); }; -bwsStats.ProccesTxs = function(DB, cb) { - var collection = DB.collection('txs'); +bwsStats.ProccesNewWallets = function(DB, cb) { + 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) { - for (var i = 0; i < items.length; i++) { - bwsStats.TotalTxpForToday(items[i]); - }; + collection.find({ + createdOn: { + $gt: start, + $lt: end + } + }).toArray(function(err, items) { + if (err) { + console.log('Error.', err); + } + items.forEach(function(it) { + bwsStats.TotalNewWalletForToday(it); + }); 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) { if (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); bwsStats.ProcessData(db, function(err) { - db.close(); if (err) { + console.log('Error.', err); cb(err, null); return; } - cb(null, stats) + cb(null, stats); }); }); }