Skip to content

Commit

Permalink
fix(usage): report volume capacities in human readable formats using …
Browse files Browse the repository at this point in the history
…IEC units

Signed-off-by: Niladri Halder <[email protected]>
niladrih committed Jun 17, 2024
1 parent 51a36c0 commit cb13796
Showing 5 changed files with 24 additions and 30 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -3,7 +3,7 @@ module github.com/openebs/google-analytics-4
go 1.19

require (
github.com/docker/go-units v0.5.0
github.com/dustin/go-humanize v1.0.1
github.com/openebs/lib-csi v0.8.2
k8s.io/apimachinery v0.27.2
k8s.io/klog/v2 v2.100.1
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -14,9 +14,9 @@ github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ3
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/docker/go-units v0.5.0 h1:69rxXcBk27SvSaaxTtLh/8llcHD8vYHT7WSdRZ/jvr4=
github.com/docker/go-units v0.5.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk=
github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815/go.mod h1:WwZ+bS3ebgob9U8Nd0kOddGdZWjyMGR8Wziv+TBNwSE=
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
github.com/emicklei/go-restful/v3 v3.10.2 h1:hIovbnmBTLjHXkqEBUz3HGpXZdM7ZrE9fJIZIqlJLqE=
github.com/emicklei/go-restful/v3 v3.10.2/go.mod h1:6n3XBCmQQb25CM2LCACGz8ukIrRry+4bhvbpWn3mrbc=
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
4 changes: 2 additions & 2 deletions usage/usage.go
Original file line number Diff line number Diff line change
@@ -75,8 +75,8 @@ func (u *Usage) SetNodeCount(n string) *Usage {

// SetVolumeCapacity sets the size of a volume.
func (u *Usage) SetVolumeCapacity(volCapG string) *Usage {
s, _ := toGigaUnits(volCapG)
u.OpenebsEventBuilder.VolumeCapacity(strconv.FormatInt(s, 10))
s, _ := toHumanSize(volCapG)
u.OpenebsEventBuilder.VolumeCapacity(s)
return u
}

14 changes: 7 additions & 7 deletions usage/utils.go
Original file line number Diff line number Diff line change
@@ -16,12 +16,12 @@ limitations under the License.

package usage

import "github.com/docker/go-units"
import (
"github.com/dustin/go-humanize"
)

// toGigaUnits converts a size from xB to bytes where x={k,m,g,t,p...}
// and return the number of Gigabytes as an integer
// 1 gigabyte=1000 megabyte
func toGigaUnits(size string) (int64, error) {
sizeInBytes, err := units.FromHumanSize(size)
return sizeInBytes / units.GB, err
// toHumanSize converts sizes to legible human sizes in IEC units.
func toHumanSize(size string) (string, error) {
sizeInBytes, err := humanize.ParseBigBytes(size)
return humanize.BigIBytes(sizeInBytes), err
}
30 changes: 12 additions & 18 deletions usage/utils_test.go
Original file line number Diff line number Diff line change
@@ -18,44 +18,38 @@ package usage

import "testing"

func TestToGigaUnits(t *testing.T) {
func TestToHumanSize(t *testing.T) {
tests := map[string]struct {
stringSize string
expectedGsize int64
positiveTest bool
stringSize string
expectedSize string
positiveTest bool
}{
"One Hundred Twenty Three thousand Four Hundred Fifty Six Teribytes": {
"One Hundred Twenty Three thousand Four Hundred Fifty Six Tebibytes": {
"123456 TiB",
123456000,
"121 PiB",
true,
},
"One Gibibyte": {
"1 GiB",
1,
"1.0 GiB",
true,
},
"One Megabyte": {
"1 MB",
0, // One cannot express <1GB in integer
"977 KiB",
true,
},
"One Megabyte negative-case": {
"1 MB",
1,
false,
// 1 MB isn't 1 GB
},
"One hundred four point five gigabyte": {
"104.5 GB",
104,
"97 GiB",
true,
},
}

for testKey, testSuite := range tests {
gotValue, err := toGigaUnits(testSuite.stringSize)
if (gotValue != testSuite.expectedGsize || err != nil) && testSuite.positiveTest {
t.Fatalf("Tests failed for %s, expected=%d, got=%d", testKey, testSuite.expectedGsize, gotValue)
gotValue, err := toHumanSize(testSuite.stringSize)
if (gotValue != testSuite.expectedSize || err != nil) && testSuite.positiveTest {
t.Fatalf("Tests failed for %s, expected=%s, got=%s", testKey, testSuite.expectedSize, gotValue)
}
}
}

0 comments on commit cb13796

Please sign in to comment.