Skip to content

Commit

Permalink
move TxStats() to routes/stats.js
Browse files Browse the repository at this point in the history
 * TxStat table added
  • Loading branch information
hackmod committed Dec 13, 2018
1 parent 7338151 commit 1898ecf
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 49 deletions.
8 changes: 8 additions & 0 deletions db.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@ var BlockStat = new Schema(
"uncleCount": Number
});

var TxStat = new Schema(
{
"timestamp": {type: Number, index: {unique: true}},
"txns": Number
}, {collection: "TxStat"});

// create indices
Transaction.index({timestamp:-1});
Transaction.index({blockNumber:-1});
Expand All @@ -86,11 +92,13 @@ Block.index({miner:1});
Block.index({miner:1, blockNumber:-1});

mongoose.model('BlockStat', BlockStat);
mongoose.model('TxStat', TxStat);
mongoose.model('Block', Block);
mongoose.model('Account', Account);
mongoose.model('Contract', Contract);
mongoose.model('Transaction', Transaction);
module.exports.BlockStat = mongoose.model('BlockStat');
module.exports.TxStat = mongoose.model('TxStat');
module.exports.Block = mongoose.model('Block');
module.exports.Contract = mongoose.model('Contract');
module.exports.Transaction = mongoose.model('Transaction');
Expand Down
61 changes: 12 additions & 49 deletions routes/stats.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var mongoose = require( 'mongoose' );
var Block = mongoose.model( 'Block' );
var BlockStat = mongoose.model( 'BlockStat' );
var TxStat = mongoose.model( 'TxStat' );
var Transaction = mongoose.model( 'Transaction' );
var filters = require('./filters');

Expand Down Expand Up @@ -104,58 +105,20 @@ var getTxStats = function(req, res) {
}
}

// select mod
var rngs = [ 60*60, 2*60*60, 4*60*60, 6*60*60, 12*60*60,
24*60*60, 7*24*60*60, 14*24*60*60, 30*24*60*60, 60*24*60*60
];
var mods = [ 30*60, 30*60, 60*60, 60*60, 60*60,
60*60, 24*60*60, 24*60*60, 24*60*60, 24*60*60,
24*60*60
];
var i = 0;
rngs.forEach(function(r) {
if (range > r) {
i++;
}
return;
});
var mod = mods[i];

var timebefore = parseInt((new Date()).getTime() / 1000) - range;
timebefore -= timebefore % mod;
Transaction.aggregate([{
$match: {
timestamp: {
$gte: timebefore

TxStat.find({ timestamp: { $gte: timebefore } }, "timestamp txns")
.lean(true).sort('-timestamp').limit(100)
.exec(function (err, docs) {
if (err || !docs) {
console.error(err);
res.status(500).send();
res.end();
return;
}
}
}, {
$group: {
_id: {
timestamp: {
$subtract: [ '$timestamp', { $mod: [ '$timestamp', mod ] } ]
}
},
timestamp: { $min: '$timestamp' },
txns: { $sum: 1 },
amount: { $sum: '$value' }
}
}, {
$project: {
"_id": 0,
"timestamp": 1,
"txns": 1,
"amount": 1
}
}]).sort('timestamp').exec(function(err, result) {
if (err || !result) {
console.error(err);
res.status(500).send();
} else {
res.write(JSON.stringify(result));
res.write(JSON.stringify(docs));
res.end();
}
});
});
}

/**
Expand Down
108 changes: 108 additions & 0 deletions tools/stats.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ var Web3 = require('web3');

var mongoose = require( 'mongoose' );
var BlockStat = require( '../db.js' ).BlockStat;
var TxStat = require( '../db.js' ).TxStat;
var Transaction = require( '../db.js' ).Transaction;

var updateStats = function(range, interval, rescan) {
var latestBlock = web3.eth.blockNumber;
Expand Down Expand Up @@ -58,6 +60,80 @@ var getStats = function(web3, blockNumber, nextBlock, endNumber, interval, resca
}
}

/**
* Aggregate transaction stats
*/
var updateTxStats = function(settings, days) {
var txnDays = settings.stats && settings.stats.txnDays || 3;

var defaultRange = 24*txnDays*60*60;
// check validity of range
var range = 24*days*60*60;
if (range && range < 60 * 60 * 24 * 7) {
if (range < 3600) { // minimal 1 hour
range = 3600;
}
} else {
range = defaultRange;
}

// select mod
var rngs = [ 60*60, 2*60*60, 4*60*60, 6*60*60, 12*60*60,
24*60*60, 7*24*60*60, 14*24*60*60, 30*24*60*60, 60*24*60*60
];
var mods = [ 30*60, 30*60, 60*60, 60*60, 60*60,
60*60, 24*60*60, 24*60*60, 24*60*60, 24*60*60,
24*60*60
];
var i = 0;
rngs.forEach(function(r) {
if (range > r) {
i++;
}
return;
});
var mod = mods[i];

var timebefore = parseInt((new Date()).getTime() / 1000) - range;
timebefore -= timebefore % mod;
Transaction.aggregate([{
$match: {
timestamp: {
$gte: timebefore
}
}
}, {
$group: {
_id: {
timestamp: {
$subtract: [ '$timestamp', { $mod: [ '$timestamp', mod ] } ]
}
},
timestamp: { $min: '$timestamp' },
txns: { $sum: 1 }
}
}, {
$project: {
"_id": 0,
"timestamp": 1,
"txns": 1
}
}]).sort('timestamp').exec(function(err, results) {
if (err || !results) {
console.error(err);
} else {
results.forEach(function(result) {
var txstat = {
"timestamp": result.timestamp,
"txns": result.txns
}
console.log(' - txstat ' + result.timestamp + ' / txns = ' + result.txns);
TxStat.collection.update({ timestamp: result.timestamp }, { $set: txstat }, { upsert: true });
});
}
});
}

/**
* Checks if the a record exists for the block number
* if record exists: abort
Expand Down Expand Up @@ -142,6 +218,29 @@ if (process.env.RESCAN) {
rescan = true;
}

// tx stats
var notxstats = false;
// tx days: aggregate range
var txndays = 14; /* 14 days */
// interval
var txStatInterval = 10 * 60 * 1000;

/**
* Usage:
* - to execute updateTxStats() only once.
* NOTXSTATS=1 tools/stats.js
*/
if (process.env.NOTXSTATS) {
notxstats = true;
}

if (process.env.TXNDAYS) {
txndays = Number(process.env.TXNDAYS);
if (txndays < 1) {
txndays = 1;
}
}

// load config.json
var config = { nodeAddr: 'localhost', gethPort: 8545, bulkSize: 100 };
try {
Expand Down Expand Up @@ -171,3 +270,12 @@ if (!rescan) {
updateStats(range, interval);
}, statInterval);
}

// update TxStats
updateTxStats(config.settings, txndays);

if (!notxstats) {
setInterval(function() {
updateTxStats(config.settings, txndays);
}, txStatInterval);
}

0 comments on commit 1898ecf

Please sign in to comment.