Skip to content

Commit

Permalink
export some structs (#972)
Browse files Browse the repository at this point in the history
  • Loading branch information
Connor1996 committed Mar 3, 2018
1 parent b0a0226 commit 72d2225
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 30 deletions.
4 changes: 2 additions & 2 deletions server/api/label.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ func (h *labelsHandler) GetStores(w http.ResponseWriter, r *http.Request) {
maxDownTime := h.svr.GetScheduleConfig().MaxStoreDownTime.Duration

stores := cluster.GetStores()
storesInfo := &storesInfo{
Stores: make([]*storeInfo, 0, len(stores)),
storesInfo := &StoresInfo{
Stores: make([]*StoreInfo, 0, len(stores)),
}

stores = filter.filter(stores)
Expand Down
2 changes: 1 addition & 1 deletion server/api/label_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ func (s *testLabelsStoreSuite) TestStoresLabelFilter(c *C) {
}
for _, t := range table {
url := fmt.Sprintf("%s/labels/stores?name=%s&value=%s", s.urlPrefix, t.name, t.value)
info := new(storesInfo)
info := new(StoresInfo)
err := readJSONWithURL(url, info)
c.Assert(err, IsNil)
checkStoresInfo(c, info.Stores, t.want)
Expand Down
36 changes: 20 additions & 16 deletions server/api/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,14 @@ import (
"github.com/unrolled/render"
)

type metaStore struct {
// MetaStore contains meta information about a store.
type MetaStore struct {
*metapb.Store
StateName string `json:"state_name"`
}

type storeStatus struct {
// StoreStatus contains status about a store.
type StoreStatus struct {
Capacity typeutil.ByteSize `json:"capacity,omitempty"`
Available typeutil.ByteSize `json:"available,omitempty"`
LeaderCount int `json:"leader_count,omitempty"`
Expand All @@ -53,23 +55,24 @@ type storeStatus struct {
Uptime *typeutil.Duration `json:"uptime,omitempty"`
}

type storeInfo struct {
Store *metaStore `json:"store"`
Status *storeStatus `json:"status"`
// StoreInfo contains information about a store.
type StoreInfo struct {
Store *MetaStore `json:"store"`
Status *StoreStatus `json:"status"`
}

const (
disconnectedName = "Disconnected"
downStateName = "Down"
)

func newStoreInfo(store *core.StoreInfo, maxStoreDownTime time.Duration) *storeInfo {
s := &storeInfo{
Store: &metaStore{
func newStoreInfo(store *core.StoreInfo, maxStoreDownTime time.Duration) *StoreInfo {
s := &StoreInfo{
Store: &MetaStore{
Store: store.Store,
StateName: store.State.String(),
},
Status: &storeStatus{
Status: &StoreStatus{
Capacity: typeutil.ByteSize(store.Stats.GetCapacity()),
Available: typeutil.ByteSize(store.Stats.GetAvailable()),
LeaderCount: store.LeaderCount,
Expand Down Expand Up @@ -109,9 +112,10 @@ func newStoreInfo(store *core.StoreInfo, maxStoreDownTime time.Duration) *storeI
return s
}

type storesInfo struct {
// StoresInfo records stores' info.
type StoresInfo struct {
Count int `json:"count"`
Stores []*storeInfo `json:"stores"`
Stores []*StoreInfo `json:"stores"`
}

type storeHandler struct {
Expand Down Expand Up @@ -323,8 +327,8 @@ func (h *storesHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
maxStoreDownTime := h.svr.GetScheduleConfig().MaxStoreDownTime.Duration

stores := cluster.GetStores()
storesInfo := &storesInfo{
Stores: make([]*storeInfo, 0, len(stores)),
StoresInfo := &StoresInfo{
Stores: make([]*StoreInfo, 0, len(stores)),
}

urlFilter, err := newStoreStateFilter(r.URL)
Expand All @@ -342,11 +346,11 @@ func (h *storesHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}

storeInfo := newStoreInfo(store, maxStoreDownTime)
storesInfo.Stores = append(storesInfo.Stores, storeInfo)
StoresInfo.Stores = append(StoresInfo.Stores, storeInfo)
}
storesInfo.Count = len(storesInfo.Stores)
StoresInfo.Count = len(StoresInfo.Stores)

h.rd.JSON(w, http.StatusOK, storesInfo)
h.rd.JSON(w, http.StatusOK, StoresInfo)
}

type storeStateFilter struct {
Expand Down
22 changes: 11 additions & 11 deletions server/api/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func (s *testStoreSuite) TearDownSuite(c *C) {
s.cleanup()
}

func checkStoresInfo(c *C, ss []*storeInfo, want []*metapb.Store) {
func checkStoresInfo(c *C, ss []*StoreInfo, want []*metapb.Store) {
c.Assert(len(ss), Equals, len(want))
mapWant := make(map[uint64]*metapb.Store)
for _, s := range want {
Expand All @@ -96,19 +96,19 @@ func checkStoresInfo(c *C, ss []*storeInfo, want []*metapb.Store) {

func (s *testStoreSuite) TestStoresList(c *C) {
url := fmt.Sprintf("%s/stores", s.urlPrefix)
info := new(storesInfo)
info := new(StoresInfo)
err := readJSONWithURL(url, info)
c.Assert(err, IsNil)
checkStoresInfo(c, info.Stores, s.stores[:3])

url = fmt.Sprintf("%s/stores?state=0", s.urlPrefix)
info = new(storesInfo)
info = new(StoresInfo)
err = readJSONWithURL(url, info)
c.Assert(err, IsNil)
checkStoresInfo(c, info.Stores, s.stores[:2])

url = fmt.Sprintf("%s/stores?state=1", s.urlPrefix)
info = new(storesInfo)
info = new(StoresInfo)
err = readJSONWithURL(url, info)
c.Assert(err, IsNil)
checkStoresInfo(c, info.Stores, s.stores[2:3])
Expand All @@ -117,15 +117,15 @@ func (s *testStoreSuite) TestStoresList(c *C) {

func (s *testStoreSuite) TestStoreGet(c *C) {
url := fmt.Sprintf("%s/store/1", s.urlPrefix)
info := new(storeInfo)
info := new(StoreInfo)
err := readJSONWithURL(url, info)
c.Assert(err, IsNil)
checkStoresInfo(c, []*storeInfo{info}, s.stores[:1])
checkStoresInfo(c, []*StoreInfo{info}, s.stores[:1])
}

func (s *testStoreSuite) TestStoreLabel(c *C) {
url := fmt.Sprintf("%s/store/1", s.urlPrefix)
var info storeInfo
var info StoreInfo
err := readJSONWithURL(url, &info)
c.Assert(err, IsNil)
c.Assert(info.Store.Labels, HasLen, 0)
Expand Down Expand Up @@ -190,29 +190,29 @@ func (s *testStoreSuite) TestStoreDelete(c *C) {

func (s *testStoreSuite) TestStoreSetState(c *C) {
url := fmt.Sprintf("%s/store/1", s.urlPrefix)
info := storeInfo{}
info := StoreInfo{}
err := readJSONWithURL(url, &info)
c.Assert(err, IsNil)
c.Assert(info.Store.State, Equals, metapb.StoreState_Up)

// Set to Offline.
info = storeInfo{}
info = StoreInfo{}
err = postJSON(&http.Client{}, url+"/state?state=Offline", nil)
c.Assert(err, IsNil)
err = readJSONWithURL(url, &info)
c.Assert(err, IsNil)
c.Assert(info.Store.State, Equals, metapb.StoreState_Offline)

// Invalid state.
info = storeInfo{}
info = StoreInfo{}
err = postJSON(&http.Client{}, url+"/state?state=Foo", nil)
c.Assert(err, NotNil)
err = readJSONWithURL(url, &info)
c.Assert(err, IsNil)
c.Assert(info.Store.State, Equals, metapb.StoreState_Offline)

// Set back to Up.
info = storeInfo{}
info = StoreInfo{}
err = postJSON(&http.Client{}, url+"/state?state=Up", nil)
c.Assert(err, IsNil)
err = readJSONWithURL(url, &info)
Expand Down

0 comments on commit 72d2225

Please sign in to comment.