From 1ffcafe061dfa982f721bcbbeec0616c91f7b5b8 Mon Sep 17 00:00:00 2001 From: R4SAS Date: Sun, 30 Jun 2019 10:49:01 +0000 Subject: [PATCH] prettify diff and hashrate output --- routes/block.js | 2 ++ routes/index.js | 13 +++++++++++++ 2 files changed, 15 insertions(+) diff --git a/routes/block.js b/routes/block.js index ca34c24..79bc364 100644 --- a/routes/block.js +++ b/routes/block.js @@ -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, }); diff --git a/routes/index.js b/routes/index.js index db46248..d231683 100644 --- a/routes/index.js +++ b/routes/index.js @@ -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) { @@ -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,