Skip to content

Commit

Permalink
prettify diff and hashrate output
Browse files Browse the repository at this point in the history
  • Loading branch information
r4sas committed Aug 24, 2019
1 parent 6108749 commit 1ffcafe
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
2 changes: 2 additions & 0 deletions routes/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ router.get('/:hash', async function(req, res, next) {
const block = blockInstance.toJSON();
block.confirmations = lastBlock.maxheight - block.height + 1;
block.time = block.time.toUTCString();
block.difficulty = block.difficulty.toFixed(8);
block.hashrate = block.hashrate.toLocaleString() + " H/s";
res.render('block', {
block,
});
Expand Down
13 changes: 13 additions & 0 deletions routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@ var models = require('../models');
var express = require('express');
var router = express.Router();

function formatRate(bytes, decimals = 2) {
if (bytes === 0) return '0 Bytes';

const k = 1000;
const dm = decimals < 0 ? 0 : decimals;
const sizes = ['H/s', 'KH/s', 'MH/s', 'GH/s', 'TH/s', 'PH/s', 'EH/s', 'ZH/s', 'YH/s'];

const i = Math.floor(Math.log(bytes) / Math.log(k));

return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i];
}

/* GET home page. */
router.get('/', async function(req, res, next) {

Expand All @@ -13,6 +25,7 @@ router.get('/', async function(req, res, next) {
blocks.forEach(function(arrayItem) {
arrayItem.ago = arrayItem.time.toUTCString().substring(5);
arrayItem.difficulty = arrayItem.difficulty.toFixed(8);
arrayItem.hashrate = formatRate(arrayItem.hashrate, 4);
});
res.render('index', {
blocks,
Expand Down

0 comments on commit 1ffcafe

Please sign in to comment.