diff --git a/go.mod b/go.mod index 9cdc8fd34e1..337103d86ee 100644 --- a/go.mod +++ b/go.mod @@ -9,7 +9,8 @@ require ( github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1 // indirect github.com/coreos/go-semver v0.2.0 github.com/coreos/pkg v0.0.0-20160727233714-3ac0863d7acf - github.com/dustin/go-humanize v0.0.0-20180421182945-02af3965c54e + github.com/docker/go-units v0.4.0 + github.com/dustin/go-humanize v0.0.0-20180421182945-02af3965c54e // indirect github.com/eknkc/amber v0.0.0-20171010120322-cdade1c07385 // indirect github.com/ghodss/yaml v1.0.1-0.20190212211648-25d852aebe32 github.com/gogo/protobuf v1.0.0 diff --git a/go.sum b/go.sum index 398ffcab422..91163e22e7b 100644 --- a/go.sum +++ b/go.sum @@ -21,6 +21,8 @@ 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/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM= github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ= +github.com/docker/go-units v0.4.0 h1:3uh0PgVws3nIA0Q+MwDC8yjEPf9zjRfZZWXZYDct3Tw= +github.com/docker/go-units v0.4.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk= github.com/dustin/go-humanize v0.0.0-20171111073723-bb3d318650d4/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk= github.com/dustin/go-humanize v0.0.0-20180421182945-02af3965c54e h1:Fw7ZmgiklsLh5EQWyHh1sumKSCG1+yjEctIpGKib87s= github.com/dustin/go-humanize v0.0.0-20180421182945-02af3965c54e/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk= diff --git a/pkg/typeutil/size.go b/pkg/typeutil/size.go index 3b58c73ccfa..efbc52dba9b 100644 --- a/pkg/typeutil/size.go +++ b/pkg/typeutil/size.go @@ -16,7 +16,7 @@ package typeutil import ( "strconv" - gh "github.com/dustin/go-humanize" + "github.com/docker/go-units" "github.com/pkg/errors" ) @@ -25,7 +25,7 @@ type ByteSize uint64 // MarshalJSON returns the size as a JSON string. func (b ByteSize) MarshalJSON() ([]byte, error) { - return []byte(`"` + gh.IBytes(uint64(b)) + `"`), nil + return []byte(`"` + units.BytesSize(float64(b)) + `"`), nil } // UnmarshalJSON parses a JSON string into the bytesize. @@ -34,7 +34,7 @@ func (b *ByteSize) UnmarshalJSON(text []byte) error { if err != nil { return errors.WithStack(err) } - v, err := gh.ParseBytes(s) + v, err := units.RAMInBytes(s) if err != nil { return errors.WithStack(err) } @@ -44,7 +44,7 @@ func (b *ByteSize) UnmarshalJSON(text []byte) error { // UnmarshalText parses a Toml string into the bytesize. func (b *ByteSize) UnmarshalText(text []byte) error { - v, err := gh.ParseBytes(string(text)) + v, err := units.RAMInBytes(string(text)) if err != nil { return errors.WithStack(err) } diff --git a/pkg/typeutil/size_test.go b/pkg/typeutil/size_test.go index 5102213352b..54be8893bf1 100644 --- a/pkg/typeutil/size_test.go +++ b/pkg/typeutil/size_test.go @@ -37,4 +37,9 @@ func (s *testSizeSuite) TestJSON(c *C) { var nb ByteSize err = json.Unmarshal(o, &nb) c.Assert(err, IsNil) + + b = ByteSize(1756821276000) + o, err = json.Marshal(b) + c.Assert(err, IsNil) + c.Assert(string(o), Equals, `"1.598TiB"`) } diff --git a/server/api/store_test.go b/server/api/store_test.go index dccd56bdd17..0daffe3bd27 100644 --- a/server/api/store_test.go +++ b/server/api/store_test.go @@ -14,6 +14,7 @@ package api import ( + "context" "encoding/json" "fmt" "io/ioutil" @@ -22,6 +23,7 @@ import ( "strings" "time" + "github.com/docker/go-units" . "github.com/pingcap/check" "github.com/pingcap/kvproto/pkg/metapb" "github.com/pingcap/kvproto/pkg/pdpb" @@ -134,9 +136,26 @@ func (s *testStoreSuite) TestStoresList(c *C) { func (s *testStoreSuite) TestStoreGet(c *C) { url := fmt.Sprintf("%s/store/1", s.urlPrefix) + s.svr.StoreHeartbeat( + context.Background(), &pdpb.StoreHeartbeatRequest{ + Header: &pdpb.RequestHeader{ClusterId: s.svr.ClusterID()}, + Stats: &pdpb.StoreStats{ + StoreId: 1, + Capacity: 1798985089024, + Available: 1709868695552, + UsedSize: 85150956358, + }, + }, + ) + info := new(StoreInfo) err := readJSONWithURL(url, info) + c.Assert(err, IsNil) + capacity, _ := units.RAMInBytes("1.636TiB") + available, _ := units.RAMInBytes("1.555TiB") + c.Assert(int64(info.Status.Capacity), Equals, capacity) + c.Assert(int64(info.Status.Available), Equals, available) checkStoresInfo(c, []*StoreInfo{info}, s.stores[:1]) }