From 9068c861f50c819cf668b47b6862e21613e6e5e1 Mon Sep 17 00:00:00 2001 From: Callan Bryant Date: Fri, 6 Mar 2020 22:32:04 +0000 Subject: [PATCH] add SI bytes TXRX --- reporttypes.go | 4 ++++ util.go | 14 ++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/reporttypes.go b/reporttypes.go index 783f1c2..173e2eb 100644 --- a/reporttypes.go +++ b/reporttypes.go @@ -102,6 +102,8 @@ func GenerateReport(dev *wgtypes.Device, conf *DsnetConfig, oldReport *DsnetRepo LastHandshakeTime: wgPeer.LastHandshakeTime, ReceiveBytes: wgPeer.ReceiveBytes, TransmitBytes: wgPeer.TransmitBytes, + ReceiveBytesSI: BytesToSI(wgPeer.ReceiveBytes), + TransmitBytesSI: BytesToSI(wgPeer.TransmitBytes), } } @@ -161,4 +163,6 @@ type PeerReport struct { LastHandshakeTime time.Time ReceiveBytes int64 TransmitBytes int64 + ReceiveBytesSI string + TransmitBytesSI string } diff --git a/util.go b/util.go index ad22247..64fda9f 100644 --- a/util.go +++ b/util.go @@ -48,3 +48,17 @@ func ConfirmOrAbort(format string, a ...interface{}) { ExitFail("Aborted.") } } + +func BytesToSI(b int64) string { + const unit = 1000 + if b < unit { + return fmt.Sprintf("%d B", b) + } + div, exp := int64(unit), 0 + for n := b / unit; n >= unit; n /= unit { + div *= unit + exp++ + } + return fmt.Sprintf("%.1f %cB", + float64(b)/float64(div), "kMGTPE"[exp]) +}