Browse Source

refactor: util.BytesToHumanReadable

pull/1330/head
bingoo 6 years ago
parent
commit
6f84ace385
  1. 9
      unmaintained/see_dat/see_dat.go
  2. 9
      unmaintained/see_idx/see_idx.go
  3. 10
      weed/server/volume_server_ui/templates.go
  4. 21
      weed/util/size.go

9
unmaintained/see_dat/see_dat.go

@ -2,7 +2,7 @@ package main
import (
"flag"
master_ui "github.com/chrislusf/seaweedfs/weed/server/volume_server_ui"
"github.com/chrislusf/seaweedfs/weed/util"
"time"
"github.com/chrislusf/seaweedfs/weed/glog"
@ -31,11 +31,8 @@ func (scanner *VolumeFileScanner4SeeDat) ReadNeedleBody() bool { return true }
func (scanner *VolumeFileScanner4SeeDat) VisitNeedle(n *needle.Needle, offset int64, needleHeader, needleBody []byte) error {
t := time.Unix(int64(n.AppendAtNs)/int64(time.Second), int64(n.AppendAtNs)%int64(time.Second))
if readable, ok := master_ui.BytesToHumanReadable(uint64(n.Size)); ok {
glog.V(0).Infof("%d,%s%x offset %d size %d (%s) cookie %x appendedAt %v", *volumeId, n.Id, n.Cookie, offset, n.Size, readable, n.Cookie, t)
} else {
glog.V(0).Infof("%d,%s%x offset %d size %d cookie %x appendedAt %v", *volumeId, n.Id, n.Cookie, offset, n.Size, n.Cookie, t)
}
glog.V(0).Infof("%d,%s%x offset %d size %s cookie %x appendedAt %v",
*volumeId, n.Id, n.Cookie, offset, util.BytesToHumanReadable(uint64(n.Size)), n.Cookie, t)
return nil
}

9
unmaintained/see_idx/see_idx.go

@ -3,7 +3,7 @@ package main
import (
"flag"
"fmt"
master_ui "github.com/chrislusf/seaweedfs/weed/server/volume_server_ui"
"github.com/chrislusf/seaweedfs/weed/util"
"os"
"path"
"strconv"
@ -37,11 +37,8 @@ func main() {
defer indexFile.Close()
idx.WalkIndexFile(indexFile, func(key types.NeedleId, offset types.Offset, size uint32) error {
if readable, ok := master_ui.BytesToHumanReadable(uint64(size)); ok {
fmt.Printf("key:%v offset:%v size:%v (%s)\n", key, offset, size, readable)
} else {
fmt.Printf("key:%v offset:%v size:%v\n", key, offset, size)
}
fmt.Printf("key:%v offset:%v size:%v\n", key, offset, util.BytesToHumanReadable(uint64(size)))
return nil
})
}

10
weed/server/volume_server_ui/templates.go

@ -7,16 +7,8 @@ import (
"strings"
)
const unit = 1024
// BytesToHumanReadable exposes the bytesToHumanReadble.
// It returns the converted human readable representation of the bytes
// and the conversion state that true for conversion realy occurred or false for no conversion.
func BytesToHumanReadable(b uint64) (string, bool) {
return bytesToHumanReadble(b), b >= unit
}
func bytesToHumanReadble(b uint64) string {
const unit = 1024
if b < unit {
return fmt.Sprintf("%d B", b)
}

21
weed/util/size.go

@ -0,0 +1,21 @@
package util
import "fmt"
// BytesToHumanReadable exposes the bytesToHumanReadble.
// It returns the converted human readable representation of the bytes
func BytesToHumanReadable(b uint64) string {
const unit = 1024
if b < unit {
return fmt.Sprintf("%d", b)
}
div, exp := uint64(unit), 0
for n := b / unit; n >= unit; n /= unit {
div *= unit
exp++
}
return fmt.Sprintf("%d(%.2f %ciB)", b, float64(b)/float64(div), "KMGTPE"[exp])
}
Loading…
Cancel
Save