Skip to content

Commit

Permalink
Merge pull request mattbaird#213 from weberr13/master
Browse files Browse the repository at this point in the history
Bug fixes in cat interface stuff
  • Loading branch information
weberr13 committed Aug 24, 2015
2 parents 10db87f + 099aafc commit 189ee19
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 38 deletions.
32 changes: 18 additions & 14 deletions lib/catindexinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,50 +8,53 @@ import (

var ErrInvalidIndexLine = errors.New("Cannot parse indexline")

// Create an IndexInfo from the string _cat/indices would produce
//Create an IndexInfo from the string _cat/indices would produce
//EX: health status index pri rep docs.count docs.deleted store.size pri.store.size
//green open logs-2015-06-19 2 0 135389346 0 53048922233 53048922233
func NewCatIndexInfo(indexLine string) (catIndex *CatIndexInfo, err error) {
split := strings.Fields(indexLine)
if len(split) < 4 {
if len(split) < 5 {
return nil, ErrInvalidIndexLine
}
catIndex = &CatIndexInfo{}
catIndex.Store = CatIndexStore{}
catIndex.Docs = CatIndexDocs{}
catIndex.Health = split[0]
catIndex.Name = split[1]
catIndex.Shards, err = strconv.Atoi(split[2])
catIndex.Status = split[1]
catIndex.Name = split[2]
catIndex.Shards, err = strconv.Atoi(split[3])
if err != nil {
catIndex.Shards = 0
}
catIndex.Replicas, err = strconv.Atoi(split[3])
catIndex.Replicas, err = strconv.Atoi(split[4])
if err != nil {
catIndex.Replicas = 0
}
if len(split) == 4 {
if len(split) == 5 {
return catIndex, nil
}
catIndex.Docs.Count, err = strconv.ParseInt(split[4], 10, 64)
catIndex.Docs.Count, err = strconv.ParseInt(split[5], 10, 64)
if err != nil {
catIndex.Docs.Count = 0
}
if len(split) == 5 {
if len(split) == 6 {
return catIndex, nil
}
catIndex.Docs.Deleted, err = strconv.ParseInt(split[5], 10, 64)
catIndex.Docs.Deleted, err = strconv.ParseInt(split[6], 10, 64)
if err != nil {
catIndex.Docs.Deleted = 0
}
if len(split) == 6 {
if len(split) == 7 {
return catIndex, nil
}
catIndex.Store.Size, err = strconv.ParseInt(split[6], 10, 64)
catIndex.Store.Size, err = strconv.ParseInt(split[7], 10, 64)
if err != nil {
catIndex.Store.Size = 0
}
if len(split) == 7 {
if len(split) == 8 {
return catIndex, nil
}
catIndex.Store.PriSize, err = strconv.ParseInt(split[7], 10, 64)
catIndex.Store.PriSize, err = strconv.ParseInt(split[8], 10, 64)
if err != nil {
catIndex.Store.PriSize = 0
}
Expand All @@ -61,7 +64,8 @@ func NewCatIndexInfo(indexLine string) (catIndex *CatIndexInfo, err error) {
// Pull all the index info from the connection
func (c *Conn) GetCatIndexInfo(pattern string) (catIndices []CatIndexInfo) {
catIndices = make([]CatIndexInfo, 0)
args := map[string]interface{}{"bytes": "b"}
//force it to only show the fileds we know about
args := map[string]interface{}{"bytes": "b", "h": "health,status,index,pri,rep,docs.count,docs.deleted,store.size,pri.store.size"}
indices, err := c.DoCommand("GET", "/_cat/indices/"+pattern, args, nil)
if err == nil {
indexLines := strings.Split(string(indices[:]), "\n")
Expand Down
54 changes: 31 additions & 23 deletions lib/catindexinfo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,24 @@ func TestCatIndexInfo(t *testing.T) {
_, err := NewCatIndexInfo("red ")
So(err, ShouldNotBeNil)
})
Convey("Create index line from a bad shards index listing", t, func() {
i, err := NewCatIndexInfo("red foo-2000-01-01-bar a 1 1234 3 11000 13000")
Convey("catIndex Create index line from a bad shards index listing", t, func() {
i, err := NewCatIndexInfo("green open logs-2015-06-19 2 1 135389346 20 53048922233 53048922233")
So(err, ShouldBeNil)
So(i.Health, ShouldEqual, "red")
So(i.Name, ShouldEqual, "foo-2000-01-01-bar")
So(i.Shards, ShouldEqual, 0)
So(i.Health, ShouldEqual, "green")
So(i.Status, ShouldEqual, "open")
So(i.Name, ShouldEqual, "logs-2015-06-19")
So(i.Shards, ShouldEqual, 2)
So(i.Replicas, ShouldEqual, 1)
So(i.Docs.Count, ShouldEqual, 1234)
So(i.Docs.Deleted, ShouldEqual, 3)
So(i.Store.Size, ShouldEqual, 11000)
So(i.Store.PriSize, ShouldEqual, 13000)
So(i.Docs.Count, ShouldEqual, 135389346)
So(i.Docs.Deleted, ShouldEqual, 20)
So(i.Store.Size, ShouldEqual, 53048922233)
So(i.Store.PriSize, ShouldEqual, 53048922233)
})
Convey("Create index line from a bad replicas index listing", t, func() {
i, err := NewCatIndexInfo("red foo-2000-01-01-bar 2 a 1234 3 11000 13000")
Convey("catIndex Create index line from a bad replicas index listing", t, func() {
i, err := NewCatIndexInfo("red open foo-2000-01-01-bar 2 0 1234 3 11000 13000")
So(err, ShouldBeNil)
So(i.Health, ShouldEqual, "red")
So(i.Status, ShouldEqual, "open")
So(i.Name, ShouldEqual, "foo-2000-01-01-bar")
So(i.Shards, ShouldEqual, 2)
So(i.Replicas, ShouldEqual, 0)
Expand All @@ -34,9 +36,10 @@ func TestCatIndexInfo(t *testing.T) {
So(i.Store.Size, ShouldEqual, 11000)
So(i.Store.PriSize, ShouldEqual, 13000)
})
Convey("Create index line from a complete index listing", t, func() {
i, err := NewCatIndexInfo("red foo-2000-01-01-bar 2 1 1234 3 11000 13000")
Convey("catIndex Create index line from a complete index listing", t, func() {
i, err := NewCatIndexInfo("red closed foo-2000-01-01-bar 2 1 1234 3 11000 13000")
So(err, ShouldBeNil)
So(i.Status, ShouldEqual, "closed")
So(i.Health, ShouldEqual, "red")
So(i.Name, ShouldEqual, "foo-2000-01-01-bar")
So(i.Shards, ShouldEqual, 2)
Expand All @@ -46,10 +49,11 @@ func TestCatIndexInfo(t *testing.T) {
So(i.Store.Size, ShouldEqual, 11000)
So(i.Store.PriSize, ShouldEqual, 13000)
})
Convey("Create index line from a bad docs index listing", t, func() {
i, err := NewCatIndexInfo("red foo-2000-01-01-bar 2 1 a 3 11000 13000")
Convey("catIndex Create index line from a bad docs index listing", t, func() {
i, err := NewCatIndexInfo("red open foo-2000-01-01-bar 2 1 a 3 11000 13000")
So(err, ShouldBeNil)
So(i.Health, ShouldEqual, "red")
So(i.Status, ShouldEqual, "open")
So(i.Name, ShouldEqual, "foo-2000-01-01-bar")
So(i.Shards, ShouldEqual, 2)
So(i.Replicas, ShouldEqual, 1)
Expand All @@ -58,10 +62,11 @@ func TestCatIndexInfo(t *testing.T) {
So(i.Store.Size, ShouldEqual, 11000)
So(i.Store.PriSize, ShouldEqual, 13000)
})
Convey("Create index line from a bad deletes index listing", t, func() {
i, err := NewCatIndexInfo("red foo-2000-01-01-bar 2 1 1234 a 11000 13000")
Convey("catIndex Create index line from a bad deletes index listing", t, func() {
i, err := NewCatIndexInfo("red open foo-2000-01-01-bar 2 1 1234 a 11000 13000")
So(err, ShouldBeNil)
So(i.Health, ShouldEqual, "red")
So(i.Status, ShouldEqual, "open")
So(i.Name, ShouldEqual, "foo-2000-01-01-bar")
So(i.Shards, ShouldEqual, 2)
So(i.Replicas, ShouldEqual, 1)
Expand All @@ -70,10 +75,11 @@ func TestCatIndexInfo(t *testing.T) {
So(i.Store.Size, ShouldEqual, 11000)
So(i.Store.PriSize, ShouldEqual, 13000)
})
Convey("Create index line from a kinda short index listing", t, func() {
i, err := NewCatIndexInfo("red foo-2000-01-01-bar 2 1 1234")
Convey("catIndex Create index line from a kinda short index listing", t, func() {
i, err := NewCatIndexInfo("red open foo-2000-01-01-bar 2 1 1234")
So(err, ShouldBeNil)
So(i.Health, ShouldEqual, "red")
So(i.Status, ShouldEqual, "open")
So(i.Name, ShouldEqual, "foo-2000-01-01-bar")
So(i.Shards, ShouldEqual, 2)
So(i.Replicas, ShouldEqual, 1)
Expand All @@ -82,10 +88,11 @@ func TestCatIndexInfo(t *testing.T) {
So(i.Store.Size, ShouldEqual, 0)
So(i.Store.PriSize, ShouldEqual, 0)
})
Convey("Create index line from a kinda sorta short index listing", t, func() {
i, err := NewCatIndexInfo("red foo-2000-01-01-bar 2 1 1234 3")
Convey("catIndex Create index line from a kinda sorta short index listing", t, func() {
i, err := NewCatIndexInfo("red open foo-2000-01-01-bar 2 1 1234 3")
So(err, ShouldBeNil)
So(i.Health, ShouldEqual, "red")
So(i.Status, ShouldEqual, "open")
So(i.Name, ShouldEqual, "foo-2000-01-01-bar")
So(i.Shards, ShouldEqual, 2)
So(i.Replicas, ShouldEqual, 1)
Expand All @@ -94,10 +101,11 @@ func TestCatIndexInfo(t *testing.T) {
So(i.Store.Size, ShouldEqual, 0)
So(i.Store.PriSize, ShouldEqual, 0)
})
Convey("Create index line from a short index listing", t, func() {
i, err := NewCatIndexInfo("red foo-2000-01-01-bar 2 1")
Convey("catIndex Create index line from a short index listing", t, func() {
i, err := NewCatIndexInfo("red open foo-2000-01-01-bar 2 1")
So(err, ShouldBeNil)
So(i.Health, ShouldEqual, "red")
So(i.Status, ShouldEqual, "open")
So(i.Name, ShouldEqual, "foo-2000-01-01-bar")
So(i.Shards, ShouldEqual, 2)
So(i.Replicas, ShouldEqual, 1)
Expand Down
1 change: 1 addition & 0 deletions lib/catresponses.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package elastigo

type CatIndexInfo struct {
Health string
Status string
Name string
Shards int
Replicas int
Expand Down
3 changes: 2 additions & 1 deletion lib/catshardinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ func (s *CatShardInfo) String() string {
// Get all the shards, even the bad ones
func (c *Conn) GetCatShards() (shards CatShards) {
shards = make(CatShards, 0)
args := map[string]interface{}{"bytes": "b"}
//force it to only respond with the columns we know about and in a forced order
args := map[string]interface{}{"bytes": "b", "h": "index,shard,prirep,state,docs,store,ip,node"}
s, err := c.DoCommand("GET", "/_cat/shards", args, nil)
if err == nil {
catShardLines := strings.Split(string(s[:]), "\n")
Expand Down

0 comments on commit 189ee19

Please sign in to comment.