Extract traffic formatter

This commit is contained in:
Shelikhoo 2021-12-20 14:46:18 +00:00
parent f12cfe6a9f
commit 9208364475
No known key found for this signature in database
GPG key ID: C4D5E79D22B25316

View file

@ -72,28 +72,28 @@ func (b *bytesSyncLogger) AddInbound(amount int) {
// ThroughputSummary view a formatted summary of the throughput totals // ThroughputSummary view a formatted summary of the throughput totals
func (b *bytesSyncLogger) ThroughputSummary() string { func (b *bytesSyncLogger) ThroughputSummary() string {
var inUnit, outUnit string
units := []string{"B", "KB", "MB", "GB"}
inbound := b.inbound inbound := b.inbound
outbound := b.outbound outbound := b.outbound
for i, u := range units { inbound, inUnit := formatTraffic(inbound)
inUnit = u outbound, outUnit := formatTraffic(outbound)
if (inbound < 1000) || (i == len(units)-1) {
break
}
inbound = inbound / 1000
}
for i, u := range units {
outUnit = u
if (outbound < 1000) || (i == len(units)-1) {
break
}
outbound = outbound / 1000
}
t := time.Now() t := time.Now()
return fmt.Sprintf("Traffic throughput (up|down): %d %s|%d %s -- (%d OnMessages, %d Sends, over %d seconds)", inbound, inUnit, outbound, outUnit, b.outEvents, b.inEvents, int(t.Sub(b.start).Seconds())) return fmt.Sprintf("Traffic throughput (up|down): %d %s|%d %s -- (%d OnMessages, %d Sends, over %d seconds)", inbound, inUnit, outbound, outUnit, b.outEvents, b.inEvents, int(t.Sub(b.start).Seconds()))
} }
func (b *bytesSyncLogger) GetStat() (in int, out int) { return b.inbound, b.outbound } func (b *bytesSyncLogger) GetStat() (in int, out int) { return b.inbound, b.outbound }
func formatTraffic(amount int) (value int, unit string) {
value = amount
units := []string{"B", "KB", "MB", "GB"}
for i, u := range units {
unit = u
if (value < 1000) || (i == len(units)-1) {
break
}
value = value / 1000
}
return
}