Skip to content

Commit

Permalink
Humanize byte value to two decimal places
Browse files Browse the repository at this point in the history
Signed-off-by: Bala.FA <[email protected]>
  • Loading branch information
balamurugana committed Nov 9, 2024
1 parent 943dae9 commit fd30003
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 10 deletions.
10 changes: 5 additions & 5 deletions cmd/kubectl-directpv/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ import (
"fmt"
"os"

"github.com/dustin/go-humanize"
"github.com/fatih/color"
"github.com/jedib0t/go-pretty/v6/table"
"github.com/minio/directpv/pkg/consts"
"github.com/minio/directpv/pkg/utils"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -91,8 +91,8 @@ func infoMain(ctx context.Context) {
} else {
writer.AppendRow([]interface{}{
fmt.Sprintf("%s %s", color.GreenString(dot), n),
humanize.IBytes(info.DriveSize),
humanize.IBytes(info.VolumeSize),
utils.IBytes(info.DriveSize),
utils.IBytes(info.VolumeSize),
fmt.Sprintf("%d", info.VolumeCount),
fmt.Sprintf("%d", info.DriveCount),
})
Expand All @@ -104,8 +104,8 @@ func infoMain(ctx context.Context) {
if len(nodeInfoMap) > 0 {
fmt.Printf(
"\n%s/%s used, %s volumes, %s drives\n",
humanize.IBytes(totalVolumeSize),
humanize.IBytes(totalDriveSize),
utils.IBytes(totalVolumeSize),
utils.IBytes(totalDriveSize),
color.HiWhiteString("%d", totalVolumeCount),
color.HiWhiteString("%d", totalDriveCount),
)
Expand Down
3 changes: 1 addition & 2 deletions cmd/kubectl-directpv/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"os"
"path"

"github.com/dustin/go-humanize"
"github.com/jedib0t/go-pretty/v6/table"
"github.com/minio/directpv/pkg/admin"
"github.com/minio/directpv/pkg/consts"
Expand Down Expand Up @@ -63,7 +62,7 @@ func printableBytes(value int64) string {
return "-"
}

return humanize.IBytes(uint64(value))
return utils.IBytes(uint64(value))
}

func newTableWriter(header table.Row, sortBy []table.SortBy, noHeader bool) table.Writer {
Expand Down
4 changes: 2 additions & 2 deletions pkg/admin/move.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ func (client *Client) Move(ctx context.Context, args MoveArgs, log LogFunc) erro

if destDrive.Status.FreeCapacity < requiredCapacity {
return fmt.Errorf("insufficient free capacity on destination drive; required=%v free=%v",
humanize.IBytes(uint64(requiredCapacity)),
humanize.IBytes(uint64(destDrive.Status.FreeCapacity)))
humanize.Comma(requiredCapacity),
humanize.Comma(destDrive.Status.FreeCapacity))
}

for _, volume := range volumes {
Expand Down
26 changes: 25 additions & 1 deletion pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ import (
"sigs.k8s.io/yaml"
)

var uuidRegex = regexp.MustCompile("^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$")
var (
uuidRegex = regexp.MustCompile("^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$")
byteUnits = []string{"B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"}
)

// IsUUID checks whether value is UUID string.
func IsUUID(value string) bool {
Expand Down Expand Up @@ -162,3 +165,24 @@ func ToLabelValues(slice []string) (values []directpvtypes.LabelValue) {
}
return
}

// IBytes produces a human readable representation of an IEC size rounding to two decimal places.
func IBytes(ui64 uint64) string {
value := ui64
base := uint64(1)
var unit string
for _, unit = range byteUnits {
if value < 1024 {
break
}
value /= 1024
base *= 1024
}
reminder := float64(ui64-(value*base)) / float64(base)

rounded := uint64(100 * reminder)
if rounded != 0 {
return fmt.Sprintf("%v.%v %v", value, rounded, unit)
}
return fmt.Sprintf("%v %v", value, unit)
}

0 comments on commit fd30003

Please sign in to comment.