Skip to content
This repository has been archived by the owner on Dec 2, 2020. It is now read-only.

block stats / last block #174

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions libs/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ module.exports = function(logger, portalConfig, poolConfigs){
case 'pool_stats':
res.end(JSON.stringify(portalStats.statPoolHistory));
return;
case 'getblocksstats':
portalStats.getBlocksStats(function (data) {
res.end(JSON.stringify(data));
});
return;
case 'payments':
var poolBlocks = [];
for(var pool in portalStats.stats.pools) {
Expand Down
1 change: 1 addition & 0 deletions libs/shareProcessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ module.exports = function(logger, poolConfig){
redisCommands.push(['rename', coin + ':shares:roundCurrent', coin + ':shares:round' + shareData.height]);
redisCommands.push(['rename', coin + ':shares:timesCurrent', coin + ':shares:times' + shareData.height]);
redisCommands.push(['sadd', coin + ':blocksPending', [shareData.blockHash, shareData.txHash, shareData.height, shareData.worker, dateNow].join(':')]);
redisCommands.push(['hset', 'Allblocks', coin + "-" + shareData.height, [shareData.blockHash, shareData.txHash, shareData.height, shareData.worker, dateNow].join(':')]); //used for block stat
Copy link
Collaborator

@hellcatz hellcatz Aug 3, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like duplicate data in redis that is not required.

To get the last mined block without this duplicate data being stored in redis.

Since pending/confirmed blocks are sorted in newest first order, the last mined block is always first in the array. https://github.com/z-classic/z-nomp/blob/master/libs/stats.js#L375
it.stats.pools[pool].pending.blocks[0] or it.stats.pools[pool].confirmed.blocks[0]

Example

var lastblock;
if (it.stats.pools[pool].pending.blocks.length > 0) {
  lastblock = it.stats.pools[pool].pending.blocks[0];
} else {
  lastblock = it.stats.pools[pool].confirmed.blocks[0];
}

Some additional checks may be required in case it.stats.pools[pool].confirmed.blocks or it.stats.pools[pool].pending.blocks is undefined. (No pending blocks, etc.)

Copy link
Contributor Author

@lludlow lludlow Aug 3, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't needed for last block mined, it was something I added to get all blocks from a single api call

The last block is just sort/slice of it.stats.pools[pool.]pending.blocks and confirmed.blocks Didn't think about the if no pending
https://github.com/madbuda/z-nomp-mb/blob/25c761865b1a30cd1a7f286a71fc32cd5f2c364a/libs/stats.js#L391

Copy link
Collaborator

@hellcatz hellcatz Aug 3, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there is a better way to implement this.getBlocksStats api call without double saving the block data in redis. I may have a little time to look at it tonight...

redisCommands.push(['hincrby', coin + ':stats', 'validBlocks', 1]);
}
else if (shareData.blockHash){
Expand Down
17 changes: 17 additions & 0 deletions libs/stats.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,21 @@ module.exports = function(logger, portalConfig, poolConfigs){
});
}

// For block stats
this.getBlocksStats = function (cback) {
var client = redisClients[0].client;
client.hgetall("Allblocks", function (error, data) {
if (error) {
logger.log("error:-" + error);
cback("");
return;
}

cback(data);

});
};

Copy link
Collaborator

@hellcatz hellcatz Aug 5, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Results in the same JSON output without adding the Allblocks duplicate data in redis and without querying redis on every request.

   this.getBlocks = function (cback) {
        var allBlocks = {};
        async.each(_this.stats.pools, function(pool, pcb) {

            if (_this.stats.pools[pool.name].pending && _this.stats.pools[pool.name].pending.blocks)
                for (var i=0; i<_this.stats.pools[pool.name].pending.blocks.length; i++)
                    allBlocks[pool.name+"-"+_this.stats.pools[pool.name].pending.blocks[i].split(':')[2]] = _this.stats.pools[pool.name].pending.blocks[i];
        
            if (_this.stats.pools[pool.name].confirmed && _this.stats.pools[pool.name].confirmed.blocks)
                for (var i=0; i<_this.stats.pools[pool.name].confirmed.blocks.length; i++)
                    allBlocks[pool.name+"-"+_this.stats.pools[pool.name].confirmed.blocks[i].split(':')[2]] = _this.stats.pools[pool.name].confirmed.blocks[i];
            
            pcb();
        }, function(err) {
            cback(allBlocks);            
        });
    };

Please give it a try. Note, I changed the function name.

function gatherStatHistory(){
var retentionTime = (((Date.now() / 1000) - portalConfig.website.stats.historicalRetention) | 0).toString();
redisStats.zrangebyscore(['statHistory', retentionTime, '+inf'], function(err, replies){
Expand Down Expand Up @@ -373,11 +388,13 @@ module.exports = function(logger, portalConfig, poolConfigs){
/* show all pending blocks */
pending: {
blocks: replies[i + 6].sort(sortBlocks),
lastblock: replies[i + 6].sort(sortBlocks).slice(0,1),
confirms: (replies[i + 9] || {})
},
/* show last 5 found blocks */
confirmed: {
blocks: replies[i + 7].sort(sortBlocks).slice(0,5)
lastblock: replies[i + 7].sort(sortBlocks).slice(0,1)
},
payments: [],
currentRoundShares: (replies[i + 8] || {}),
Expand Down
1 change: 1 addition & 0 deletions website/pages/api.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<li><a href="/api/payments">/payments</a> - payment history</li>
<li><a href="/api/worker_stats?taddr">/worker_stats?taddr</a> - historical time per pool json </li>
<li><a href="/api/live_stats">/live_stats</a> - live stats </li>
<li><a href="/api/getblocksstats">/getblocksstats</a> - Block stats</li>

</ul>
</div>