From ee3e07eec72b91fac2f47529ebeeb5b586d3017e Mon Sep 17 00:00:00 2001 From: Matias Pando Date: Mon, 4 May 2015 17:36:49 -0300 Subject: [PATCH] Adding stats on bws --- lib/expressapp.js | 10 +++++++ scripts/stats.js | 67 ++++++++++++++++++++++++++--------------------- 2 files changed, 47 insertions(+), 30 deletions(-) diff --git a/lib/expressapp.js b/lib/expressapp.js index 4b7dad2..7e88863 100644 --- a/lib/expressapp.js +++ b/lib/expressapp.js @@ -9,6 +9,7 @@ var querystring = require('querystring'); var bodyParser = require('body-parser') var WalletService = require('./server'); +var stats = require('../scripts/stats'); log.disableColor(); log.debug = log.verbose; @@ -315,10 +316,19 @@ ExpressApp.prototype.start = function(opts, cb) { }); }); + router.get('/v1/stats/', function(req, res) { + var data = stats.getStats(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); + }; module.exports = ExpressApp; diff --git a/scripts/stats.js b/scripts/stats.js index 33d5351..edfc192 100644 --- a/scripts/stats.js +++ b/scripts/stats.js @@ -6,6 +6,7 @@ var _ = require('lodash'); var mongodb = require('mongodb'); var moment = require('moment'); var async = require('async'); +var config = require('../config'); var otherDate; @@ -16,6 +17,9 @@ try { 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 = { @@ -33,16 +37,18 @@ var stats = { var wallets = {}; -var IsToday = function(date) { +var bwsStats = {}; + +bwsStats.IsToday = function(date) { if (!date) return false; var date = moment(date * 1000); return (date >= Today.startOf('day') && date <= Today.endOf('day')); } -var TotalTxpForToday = function(data) { +bwsStats.TotalTxpForToday = function(data) { if (!data) return; - if (!IsToday(data.createdOn)) return; + if (!bwsStats.IsToday(data.createdOn)) return; var network = wallets[data.walletId]; stats[network].totalTx++; @@ -50,20 +56,20 @@ var TotalTxpForToday = function(data) { }; -var AddingWalletToCache = function(data) { +bwsStats.AddingWalletToCache = function(data) { if (!data) return; wallets[data.id] = data.network; }; -var TotalNewWalletForToday = function(data) { +bwsStats.TotalNewWalletForToday = function(data) { if (!data) return; - if (!IsToday(data.createdOn)) return; + if (!bwsStats.IsToday(data.createdOn)) return; stats[data.network].totalNewWallets++; }; -var PrintStats = function() { +bwsStats.PrintStats = function() { console.log('Stats for date : ', Today.format("YYYY-MM-DD")); console.log(' '); @@ -76,50 +82,51 @@ var PrintStats = function() { } }; -var ProcessData = function(DB, cb) { - ProccesWallets(DB, function() { - ProccesTxs(DB, cb); +bwsStats.ProcessData = function(DB, cb) { + bwsStats.ProccesWallets(DB, function() { + bwsStats.ProccesTxs(DB, cb); }); }; -var ProccesWallets = function(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++) { - AddingWalletToCache(items[i]); - TotalNewWalletForToday(items[i]); + bwsStats.AddingWalletToCache(items[i]); + bwsStats.TotalNewWalletForToday(items[i]); }; cb(); }); }; -var ProccesTxs = function(DB, cb) { +bwsStats.ProccesTxs = function(DB, cb) { var collection = DB.collection('txs'); collection.find({}).toArray(function(err, items) { for (var i = 0; i < items.length; i++) { - TotalTxpForToday(items[i]); + bwsStats.TotalTxpForToday(items[i]); }; cb(); }); }; - -var url = 'mongodb://localhost:27017/bws'; -mongodb.MongoClient.connect(url, function(err, db) { - if (err) { - console.log('Unable to connect to the mongoDB server. Error:', err); - process.exit(1); - } - console.log('Connection established to ', url); - ProcessData(db, function(err) { +bwsStats.getStats = function(cb) { + mongodb.MongoClient.connect(url, function(err, db) { if (err) { - console.log('error ', err); - process.exit(1); + console.log('Unable to connect to the mongoDB server. Error:', err); + return; } - PrintStats(); - db.close(); - process.exit(0); + console.log('Connection established to ', url); + bwsStats.ProcessData(db, function(err) { + db.close(); + if (err) { + cb(err, null); + return; + } + cb(null, stats) + }); }); -}); +} + +module.exports = bwsStats;