From d23075cadc8c5313acc1404fdf2296731daeb080 Mon Sep 17 00:00:00 2001 From: Matias Pando Date: Mon, 4 May 2015 14:22:56 -0300 Subject: [PATCH] Refactoring --- lib/stats.js | 82 ++++++++++++++++++++++++---------------------------- 1 file changed, 37 insertions(+), 45 deletions(-) diff --git a/lib/stats.js b/lib/stats.js index afed3bc..33d5351 100644 --- a/lib/stats.js +++ b/lib/stats.js @@ -1,5 +1,8 @@ #!/usr/bin/env node +'use strict'; + +var _ = require('lodash'); var mongodb = require('mongodb'); var moment = require('moment'); var async = require('async'); @@ -14,17 +17,22 @@ try { } var Today = otherDate || moment(); -var TotalTxLivenet = 0; -var TotalAmountLivenet = 0; -var TotalNewWalletsLivenet = 0; -var TotalTxTestnet = 0; -var TotalAmountTestnet = 0; -var TotalNewWalletsTestnet = 0; +var stats = { + 'livenet': { + totalTx: 0, + totalAmount: 0, + totalNewWallets: 0 + }, + 'testnet': { + totalTx: 0, + totalAmount: 0, + totalNewWallets: 0 + } +} var wallets = {}; - var IsToday = function(date) { if (!date) return false; var date = moment(date * 1000); @@ -34,61 +42,44 @@ var IsToday = function(date) { var TotalTxpForToday = function(data) { if (!data) return; - if (!wallets[data.walletId]) { - console.log('Walletid not found! ', data.walletId); - return; - } - if (!IsToday(data.createdOn)) return; - if (wallets[data.walletId] == 'livenet') { - TotalTxLivenet++; - TotalAmountLivenet = TotalAmountLivenet + data.amount; - } else if (wallets[data.walletId] == 'testnet') { - TotalTxTestnet++; - TotalAmountTestnet = TotalAmountTestnet + data.amount; - } else { - console.log('Invalid network ', wallets[data.walletId]); - } + var network = wallets[data.walletId]; + stats[network].totalTx++; + stats[network].totalAmount += data.amount; +}; + + +var AddingWalletToCache = function(data) { + if (!data) return; + wallets[data.id] = data.network; }; var TotalNewWalletForToday = function(data) { if (!data) return; - if (!wallets[data.id]) { - wallets[data.id] = data.network; - } if (!IsToday(data.createdOn)) return; + stats[data.network].totalNewWallets++; - if (data.network == 'livenet') { - TotalNewWalletsLivenet++; - } else if (data.network == 'testnet') { - TotalNewWalletsTestnet++; - } else { - console.log('Invalid data ', data); - console.log('Invalid network ', data.network); - } }; var PrintStats = function() { console.log('Stats for date : ', Today.format("YYYY-MM-DD")); console.log(' '); - console.log('Livenet stats--------------------------------- ') - console.log('New wallets : ', TotalNewWalletsLivenet); - console.log('Total tx : ', TotalTxLivenet); - console.log('Total amount in tx (BTC) : ', TotalAmountLivenet * 1 / 1e8); - console.log(' '); - console.log('Testnet stats--------------------------------- ') - console.log('New wallets : ', TotalNewWalletsTestnet); - console.log('Total tx : ', TotalTxTestnet); - console.log('Total amount in tx (BTC) : ', TotalAmountTestnet * 1 / 1e8); + + 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); + } }; var ProcessData = function(DB, cb) { ProccesWallets(DB, function() { ProccesTxs(DB, cb); }); - }; var ProccesWallets = function(DB, cb) { @@ -96,6 +87,7 @@ var ProccesWallets = function(DB, cb) { collection.find({}).toArray(function(err, items) { for (var i = 0; i < items.length; i++) { + AddingWalletToCache(items[i]); TotalNewWalletForToday(items[i]); }; cb(); @@ -118,16 +110,16 @@ 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(code = 1); + process.exit(1); } console.log('Connection established to ', url); ProcessData(db, function(err) { if (err) { console.log('error ', err); - process.exit(code = 1); + process.exit(1); } PrintStats(); db.close(); - process.exit(code = 0); + process.exit(0); }); });