From e97d6b95c10350e5cb1df7c5e68aed16182f8e2c Mon Sep 17 00:00:00 2001 From: Max Pedraza Date: Tue, 30 Nov 2021 01:58:29 +0100 Subject: [PATCH] In status page the rx and tx bytes now are shown with units --- handler/routes.go | 28 ++++++++++++++++++++++++++++ templates/status.html | 4 ++-- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/handler/routes.go b/handler/routes.go index 156e701..ef14aab 100644 --- a/handler/routes.go +++ b/handler/routes.go @@ -469,6 +469,8 @@ func Status(db store.IStore) echo.HandlerFunc { PublicKey string ReceivedBytes int64 TransmitBytes int64 + Received string + Transmit string LastHandshakeTime time.Time LastHandshakeRel time.Duration Connected bool @@ -528,6 +530,32 @@ func Status(db store.IStore) echo.HandlerFunc { } pVm.Connected = pVm.LastHandshakeRel.Minutes() < 3. + units := []string{" ", " K", " M", " G", " T", " P", " E", " Z", " Y"} + + pow := 0 + temporal := float64(pVm.ReceivedBytes) + for ; temporal > 1024; temporal /= 1024 { + pow += 1 + if pow == len(units)-1 { + break + } + } + pVm.Received = fmt.Sprintf("%.3f", temporal) + pVm.Received = strings.TrimSuffix(strings.TrimRight(pVm.Received, "0"), ".") + pVm.Received = fmt.Sprintf("%s %sBytes", pVm.Received, units[pow]) + + pow = 0 + temporal = float64(pVm.TransmitBytes) + for ; temporal > 1024; temporal /= 1024 { + pow += 1 + if pow == len(units)-1 { + break + } + } + pVm.Transmit = fmt.Sprintf("%.3f", temporal) + pVm.Transmit = strings.TrimSuffix(strings.TrimRight(pVm.Transmit, "0"), ".") + pVm.Transmit = fmt.Sprintf("%s %sBytes", pVm.Transmit, units[pow]) + if _client, ok := m[pVm.PublicKey]; ok { pVm.Name = _client.Name pVm.Email = _client.Email diff --git a/templates/status.html b/templates/status.html index f3311a5..e0a41b1 100644 --- a/templates/status.html +++ b/templates/status.html @@ -41,8 +41,8 @@ Connected Peers {{ $peer.Name }} {{ $peer.Email }} {{ $peer.PublicKey }} - {{ $peer.ReceivedBytes }} - {{ $peer.TransmitBytes }} + {{ $peer.Received }} + {{ $peer.Transmit }} {{ $peer.Connected }} {{ $peer.LastHandshakeTime }}