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 <bala@minio.io>
  • Loading branch information
balamurugana committed Nov 9, 2024
1 parent 943dae9 commit aea54a2
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 9 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
25 changes: 25 additions & 0 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,3 +162,28 @@ func ToLabelValues(slice []string) (values []directpvtypes.LabelValue) {
}
return
}

var (
byteUnits = []string{"B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"}
)

// 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("%vv%v %v", value, rounded, unit)
}
return fmt.Sprintf("%v %v", value, unit)
}

0 comments on commit aea54a2

Please sign in to comment.