fixed: blocklist by date with limit. when arrive new block, reload last 5 blocks on homepage. The same for new txs.

This commit is contained in:
Gustavo Cortez 2014-02-09 15:59:28 -03:00
parent bd3bfe6e37
commit c6d137f856
4 changed files with 27 additions and 49 deletions

View File

@ -72,7 +72,6 @@ console.log('[blocks.js.60]: could not get %s from RPC. Orphan? Error?', blockha
* List of blocks by date * List of blocks by date
*/ */
exports.list = function(req, res) { exports.list = function(req, res) {
var limit = req.query.limit || -1;
var isToday = false; var isToday = false;
//helper to convert timestamps to yyyy-mm-dd format //helper to convert timestamps to yyyy-mm-dd format
@ -103,14 +102,15 @@ exports.list = function(req, res) {
var prev = formatTimestamp(new Date((gte - 86400) * 1000)); var prev = formatTimestamp(new Date((gte - 86400) * 1000));
var next = formatTimestamp(new Date(lte * 1000)); var next = formatTimestamp(new Date(lte * 1000));
bdb.getBlocksByDate(gte, lte, limit, function(err, blocks) { bdb.getBlocksByDate(gte, lte, function(err, blocks) {
if (err) { if (err) {
res.status(500).send(err); res.status(500).send(err);
} }
else { else {
var blockshashList = []; var blockshashList = [];
for(var i=0;i<blocks.length;i++) { var limit = parseInt(req.query.limit || blocks.length);
blockshashList.unshift(blocks[i].hash); for(var i=0;i<limit;i++) {
blockshashList.push(blocks[i].hash);
} }
async.mapSeries(blockshashList, getBlock, function(err, allblocks) { async.mapSeries(blockshashList, getBlock, function(err, allblocks) {
res.jsonp({ res.jsonp({

View File

@ -148,13 +148,12 @@ function spec(b) {
}); });
}; };
BlockDb.prototype.getBlocksByDate = function(start_ts, end_ts, limit, cb) { BlockDb.prototype.getBlocksByDate = function(start_ts, end_ts, cb) {
var list = []; var list = [];
db.createReadStream({ db.createReadStream({
start: TIMESTAMP_PREFIX + start_ts, start: TIMESTAMP_PREFIX + start_ts,
end: TIMESTAMP_PREFIX + end_ts, end: TIMESTAMP_PREFIX + end_ts,
fillCache: true, fillCache: true
limit: parseInt(limit) // force to int
}) })
.on('data', function (data) { .on('data', function (data) {
list.push({ list.push({
@ -166,7 +165,7 @@ function spec(b) {
return cb(err); return cb(err);
}) })
.on('end', function () { .on('end', function () {
return cb(null, list); return cb(null, list.reverse());
}); });
}; };

View File

@ -4,22 +4,23 @@ var TRANSACTION_DISPLAYED = 5;
var BLOCKS_DISPLAYED = 5; var BLOCKS_DISPLAYED = 5;
angular.module('insight.system').controller('IndexController', angular.module('insight.system').controller('IndexController',
function($scope, $rootScope, Global, getSocket, Blocks, Block, Transactions, Transaction) { function($scope, $rootScope, Global, getSocket, Blocks, Transactions) {
$scope.global = Global; $scope.global = Global;
var _getTransaction = function(txid, cb) { var _getBlocks = function() {
Transaction.get({ Blocks.get({
txId: txid limit: BLOCKS_DISPLAYED
}, function(res) { }, function(res) {
cb(res); $scope.blocks = res.blocks;
$scope.blocksLength = res.lenght;
}); });
}; };
var _getBlock = function(hash) { var _getTransactions = function() {
Block.get({ Transactions.get({
blockHash: hash limit: TRANSACTION_DISPLAYED
}, function(res) { }, function(res) {
$scope.blocks.unshift(res); $scope.txs = res.txs;
}); });
}; };
@ -31,24 +32,13 @@ angular.module('insight.system').controller('IndexController',
socket.on('tx', function(tx) { socket.on('tx', function(tx) {
console.log('Transaction received! ' + JSON.stringify(tx)); console.log('Transaction received! ' + JSON.stringify(tx));
_getTransactions();
var txStr = tx.txid.toString();
_getTransaction(txStr, function(res) {
$scope.txs.unshift(res);
if (parseInt($scope.txs.length, 10) >= parseInt(TRANSACTION_DISPLAYED, 10)) {
$scope.txs = $scope.txs.splice(0, TRANSACTION_DISPLAYED);
}
});
}); });
socket.on('block', function(block) { socket.on('block', function(block) {
var blockHash = block.hash.toString(); var blockHash = block.hash.hash.toString();
console.log('Block received! ' + JSON.stringify(block)); console.log('Block received! ' + JSON.stringify(blockHash));
if (parseInt($scope.blocks.length, 10) > parseInt(BLOCKS_DISPLAYED, 10) - 1) { _getBlocks();
$scope.blocks.pop();
}
_getBlock(blockHash);
}); });
$scope.humanSince = function(time) { $scope.humanSince = function(time) {
@ -57,18 +47,8 @@ angular.module('insight.system').controller('IndexController',
}; };
$scope.index = function() { $scope.index = function() {
Blocks.get({ _getBlocks();
limit: BLOCKS_DISPLAYED _getTransactions();
}, function(res) {
$scope.blocks = res.blocks;
$scope.blocksLength = res.lenght;
});
Transactions.get({
limit: TRANSACTION_DISPLAYED
}, function(res) {
$scope.txs = res.txs;
});
}; };
$scope.txs = []; $scope.txs = [];

View File

@ -3,11 +3,10 @@
process.env.NODE_ENV = process.env.NODE_ENV || 'development'; process.env.NODE_ENV = process.env.NODE_ENV || 'development';
var TESTING_BLOCK0 = '000000000933ea01ad0ee984209779baaec3ced90fa3f408719526f8d77f4943'; var TESTING_BLOCK0 = '00000000b873e79784647a6c82962c70d228557d24a747ea4d1b8bbe878e1206';
var TESTING_BLOCK1 = '00000000b873e79784647a6c82962c70d228557d24a747ea4d1b8bbe878e1206'; var TESTING_BLOCK1 = '000000000933ea01ad0ee984209779baaec3ced90fa3f408719526f8d77f4943';
var START_TS = 1; var START_TS = 1;
var END_TS = '1296688928~'; // 2/2/2011 23:23PM var END_TS = '1296688928~'; // 2/2/2011 23:23PM
var LIMIT = 2;
var assert = require('assert'), var assert = require('assert'),
BlockDb = require('../../lib/BlockDb').class(); BlockDb = require('../../lib/BlockDb').class();
@ -24,7 +23,7 @@ describe('BlockDb getBlocksByDate', function(){
it('Get Hash by Date', function(done) { it('Get Hash by Date', function(done) {
bDb.getBlocksByDate(START_TS, END_TS, LIMIT, function(err, list) { bDb.getBlocksByDate(START_TS, END_TS, function(err, list) {
if (err) done(err); if (err) done(err);
assert(list, 'returns list'); assert(list, 'returns list');
assert.equal(list.length,2, 'list has 2 items'); assert.equal(list.length,2, 'list has 2 items');