Browse Source

Refactoring

activeAddress
Matias Pando 10 years ago
parent
commit
d23075cadc
  1. 82
      lib/stats.js

82
lib/stats.js

@ -1,5 +1,8 @@
#!/usr/bin/env node #!/usr/bin/env node
'use strict';
var _ = require('lodash');
var mongodb = require('mongodb'); var mongodb = require('mongodb');
var moment = require('moment'); var moment = require('moment');
var async = require('async'); var async = require('async');
@ -14,17 +17,22 @@ try {
} }
var Today = otherDate || moment(); var Today = otherDate || moment();
var TotalTxLivenet = 0;
var TotalAmountLivenet = 0;
var TotalNewWalletsLivenet = 0;
var TotalTxTestnet = 0; var stats = {
var TotalAmountTestnet = 0; 'livenet': {
var TotalNewWalletsTestnet = 0; totalTx: 0,
totalAmount: 0,
totalNewWallets: 0
},
'testnet': {
totalTx: 0,
totalAmount: 0,
totalNewWallets: 0
}
}
var wallets = {}; var wallets = {};
var IsToday = function(date) { var IsToday = function(date) {
if (!date) return false; if (!date) return false;
var date = moment(date * 1000); var date = moment(date * 1000);
@ -34,61 +42,44 @@ var IsToday = function(date) {
var TotalTxpForToday = function(data) { var TotalTxpForToday = function(data) {
if (!data) return; if (!data) return;
if (!wallets[data.walletId]) {
console.log('Walletid not found! ', data.walletId);
return;
}
if (!IsToday(data.createdOn)) return; if (!IsToday(data.createdOn)) return;
if (wallets[data.walletId] == 'livenet') { var network = wallets[data.walletId];
TotalTxLivenet++; stats[network].totalTx++;
TotalAmountLivenet = TotalAmountLivenet + data.amount; stats[network].totalAmount += data.amount;
} else if (wallets[data.walletId] == 'testnet') { };
TotalTxTestnet++;
TotalAmountTestnet = TotalAmountTestnet + data.amount;
} else { var AddingWalletToCache = function(data) {
console.log('Invalid network ', wallets[data.walletId]); if (!data) return;
} wallets[data.id] = data.network;
}; };
var TotalNewWalletForToday = function(data) { var TotalNewWalletForToday = function(data) {
if (!data) return; if (!data) return;
if (!wallets[data.id]) {
wallets[data.id] = data.network;
}
if (!IsToday(data.createdOn)) return; 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() { var PrintStats = function() {
console.log('Stats for date : ', Today.format("YYYY-MM-DD")); console.log('Stats for date : ', Today.format("YYYY-MM-DD"));
console.log(' '); console.log(' ');
console.log('Livenet stats--------------------------------- ')
console.log('New wallets : ', TotalNewWalletsLivenet); for (var s in stats) {
console.log('Total tx : ', TotalTxLivenet); console.log(' ');
console.log('Total amount in tx (BTC) : ', TotalAmountLivenet * 1 / 1e8); console.log(s + ' stats--------------------------------- ')
console.log(' '); console.log('New wallets : ', stats[s].totalNewWallets);
console.log('Testnet stats--------------------------------- ') console.log('Total tx : ', stats[s].totalTx);
console.log('New wallets : ', TotalNewWalletsTestnet); console.log('Total amount in tx (BTC) : ', stats[s].totalAmount * 1 / 1e8);
console.log('Total tx : ', TotalTxTestnet); }
console.log('Total amount in tx (BTC) : ', TotalAmountTestnet * 1 / 1e8);
}; };
var ProcessData = function(DB, cb) { var ProcessData = function(DB, cb) {
ProccesWallets(DB, function() { ProccesWallets(DB, function() {
ProccesTxs(DB, cb); ProccesTxs(DB, cb);
}); });
}; };
var ProccesWallets = function(DB, cb) { var ProccesWallets = function(DB, cb) {
@ -96,6 +87,7 @@ var ProccesWallets = function(DB, cb) {
collection.find({}).toArray(function(err, items) { collection.find({}).toArray(function(err, items) {
for (var i = 0; i < items.length; i++) { for (var i = 0; i < items.length; i++) {
AddingWalletToCache(items[i]);
TotalNewWalletForToday(items[i]); TotalNewWalletForToday(items[i]);
}; };
cb(); cb();
@ -118,16 +110,16 @@ var url = 'mongodb://localhost:27017/bws';
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);
process.exit(code = 1); process.exit(1);
} }
console.log('Connection established to ', url); console.log('Connection established to ', url);
ProcessData(db, function(err) { ProcessData(db, function(err) {
if (err) { if (err) {
console.log('error ', err); console.log('error ', err);
process.exit(code = 1); process.exit(1);
} }
PrintStats(); PrintStats();
db.close(); db.close();
process.exit(code = 0); process.exit(0);
}); });
}); });

Loading…
Cancel
Save