mirror of
https://github.com/strukturag/nextcloud-spreed-signaling
synced 2026-03-14 14:35:44 +01:00
Add formatting to bandwidth values.
This commit is contained in:
parent
75ea5e710c
commit
4e87170f0e
2 changed files with 85 additions and 1 deletions
|
|
@ -22,14 +22,47 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
"sync/atomic"
|
||||
|
||||
"github.com/strukturag/nextcloud-spreed-signaling/internal"
|
||||
)
|
||||
|
||||
var (
|
||||
Kilobit = BandwidthFromBits(1024)
|
||||
Megabit = BandwidthFromBits(1024) * Kilobit
|
||||
Gigabit = BandwidthFromBits(1024) * Megabit
|
||||
)
|
||||
|
||||
// Bandwidth stores a bandwidth in bits per second.
|
||||
type Bandwidth uint64
|
||||
|
||||
func formatWithRemainder(value uint64, divisor uint64, format string) string {
|
||||
if value%divisor == 0 {
|
||||
return fmt.Sprintf("%d %s", value/divisor, format)
|
||||
} else {
|
||||
v := float64(value) / float64(divisor)
|
||||
v = math.Trunc(v*100) / 100
|
||||
return fmt.Sprintf("%.2f %s", v, format)
|
||||
}
|
||||
}
|
||||
|
||||
// String returns the formatted bandwidth.
|
||||
func (b Bandwidth) String() string {
|
||||
if b >= Gigabit {
|
||||
return formatWithRemainder(b.Bits(), Gigabit.Bits(), "Gbps")
|
||||
} else if b >= Megabit {
|
||||
return formatWithRemainder(b.Bits(), Megabit.Bits(), "Mbps")
|
||||
} else if b >= Kilobit {
|
||||
return formatWithRemainder(b.Bits(), Kilobit.Bits(), "Kbps")
|
||||
} else if b > 0 {
|
||||
return fmt.Sprintf("%d bps", b)
|
||||
} else {
|
||||
return "unlimited"
|
||||
}
|
||||
}
|
||||
|
||||
// Bits returns the bandwidth in bits per second.
|
||||
func (b Bandwidth) Bits() uint64 {
|
||||
return uint64(b)
|
||||
|
|
@ -47,7 +80,7 @@ func BandwidthFromBits(b uint64) Bandwidth {
|
|||
|
||||
// BandwithFromBits creates a bandwidth from megabits per second.
|
||||
func BandwidthFromMegabits(b uint64) Bandwidth {
|
||||
return Bandwidth(b * 1024 * 1024)
|
||||
return Bandwidth(b) * Megabit
|
||||
}
|
||||
|
||||
// BandwidthFromBytes creates a bandwidth from bytes per second.
|
||||
|
|
|
|||
|
|
@ -56,3 +56,54 @@ func TestBandwidth(t *testing.T) {
|
|||
assert.EqualValues(1000, old)
|
||||
assert.EqualValues(2000, a.Load())
|
||||
}
|
||||
|
||||
func TestBandwidthString(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
assert := assert.New(t)
|
||||
testcases := []struct {
|
||||
value Bandwidth
|
||||
expected string
|
||||
}{
|
||||
{
|
||||
0,
|
||||
"unlimited",
|
||||
},
|
||||
{
|
||||
BandwidthFromBits(123),
|
||||
"123 bps",
|
||||
},
|
||||
{
|
||||
BandwidthFromBits(1023),
|
||||
"1023 bps",
|
||||
},
|
||||
{
|
||||
BandwidthFromBits(1024),
|
||||
"1 Kbps",
|
||||
},
|
||||
{
|
||||
BandwidthFromBits(1024 + 512),
|
||||
"1.50 Kbps",
|
||||
},
|
||||
{
|
||||
BandwidthFromBits(1024*1024 - 1),
|
||||
"1023.99 Kbps",
|
||||
},
|
||||
{
|
||||
BandwidthFromBits(1024 * 1024),
|
||||
"1 Mbps",
|
||||
},
|
||||
{
|
||||
BandwidthFromBits(1024*1024*1024 - 1),
|
||||
"1023.99 Mbps",
|
||||
},
|
||||
{
|
||||
BandwidthFromBits(1024 * 1024 * 1024),
|
||||
"1 Gbps",
|
||||
},
|
||||
}
|
||||
|
||||
for idx, tc := range testcases {
|
||||
assert.Equal(tc.expected, tc.value.String(), "failed for testcase %d (%d)", idx, tc.value.Bits())
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue