add SI bytes TXRX

This commit is contained in:
Callan Bryant 2020-03-06 22:32:04 +00:00
parent 151de953b5
commit 9068c861f5
No known key found for this signature in database
GPG Key ID: C31FA9DF3ACBFFAA
2 changed files with 18 additions and 0 deletions

View File

@ -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
}

14
util.go
View File

@ -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])
}