resp stats

This commit is contained in:
Josh Baker 2016-03-28 16:11:29 -07:00
parent b11c619650
commit f00d5ca9e8
2 changed files with 56 additions and 26 deletions

View File

@ -355,8 +355,8 @@ func (c *Controller) command(msg *server.Message, w io.Writer) (res string, d co
// case "readonly": // case "readonly":
// err = c.cmdReadOnly(nline) // err = c.cmdReadOnly(nline)
// resp = okResp() // resp = okResp()
// case "stats": case "stats":
// resp, err = c.cmdStats(nline) res, err = c.cmdStats(msg)
case "server": case "server":
res, err = c.cmdServer(msg) res, err = c.cmdServer(msg)
case "scan": case "scan":

View File

@ -12,15 +12,21 @@ import (
"github.com/tidwall/tile38/controller/server" "github.com/tidwall/tile38/controller/server"
) )
func (c *Controller) cmdStats(line string) (string, error) { func (c *Controller) cmdStats(msg *server.Message) (res string, err error) {
start := time.Now() start := time.Now()
var key string vs := msg.Values[1:]
var ms = []map[string]interface{}{} var ms = []map[string]interface{}{}
if line == "" { if len(vs) == 0 {
return "", errInvalidNumberOfArguments return "", errInvalidNumberOfArguments
} }
for len(line) > 0 { var vals []resp.Value
line, key = token(line) var key string
var ok bool
for {
vs, key, ok = tokenval(vs)
if !ok {
break
}
col := c.getCol(key) col := c.getCol(key)
if col != nil { if col != nil {
m := make(map[string]interface{}) m := make(map[string]interface{})
@ -28,16 +34,36 @@ func (c *Controller) cmdStats(line string) (string, error) {
m["num_points"] = points m["num_points"] = points
m["in_memory_size"] = col.TotalWeight() m["in_memory_size"] = col.TotalWeight()
m["num_objects"] = col.Count() m["num_objects"] = col.Count()
ms = append(ms, m) switch msg.OutputType {
case server.JSON:
ms = append(ms, m)
case server.RESP:
vals = append(vals, resp.ArrayValue(respValuesSimpleMap(m)))
}
} else { } else {
ms = append(ms, nil) switch msg.OutputType {
case server.JSON:
ms = append(ms, nil)
case server.RESP:
vals = append(vals, resp.NullValue())
}
} }
} }
data, err := json.Marshal(ms) switch msg.OutputType {
if err != nil { case server.JSON:
return "", err data, err := json.Marshal(ms)
if err != nil {
return "", err
}
res = `{"ok":true,"stats":` + string(data) + `,"elapsed":"` + time.Now().Sub(start).String() + "\"}"
case server.RESP:
data, err := resp.ArrayValue(vals).MarshalRESP()
if err != nil {
return "", err
}
res = string(data)
} }
return `{"ok":true,"stats":` + string(data) + `,"elapsed":"` + time.Now().Sub(start).String() + "\"}", nil return res, nil
} }
func (c *Controller) cmdServer(msg *server.Message) (res string, err error) { func (c *Controller) cmdServer(msg *server.Message) (res string, err error) {
start := time.Now() start := time.Now()
@ -88,19 +114,7 @@ func (c *Controller) cmdServer(msg *server.Message) (res string, err error) {
} }
res = `{"ok":true,"stats":` + string(data) + `,"elapsed":"` + time.Now().Sub(start).String() + "\"}" res = `{"ok":true,"stats":` + string(data) + `,"elapsed":"` + time.Now().Sub(start).String() + "\"}"
case server.RESP: case server.RESP:
var keys []string vals := respValuesSimpleMap(m)
for key, _ := range m {
keys = append(keys, key)
}
sort.Strings(keys)
var vals []resp.Value
for _, key := range keys {
val := m[key]
vals = append(vals, resp.StringValue(key))
vals = append(vals, resp.StringValue(fmt.Sprintf("%v", val)))
}
data, err := resp.ArrayValue(vals).MarshalRESP() data, err := resp.ArrayValue(vals).MarshalRESP()
if err != nil { if err != nil {
return "", err return "", err
@ -110,6 +124,22 @@ func (c *Controller) cmdServer(msg *server.Message) (res string, err error) {
return res, nil return res, nil
} }
func respValuesSimpleMap(m map[string]interface{}) []resp.Value {
var keys []string
for key, _ := range m {
keys = append(keys, key)
}
sort.Strings(keys)
var vals []resp.Value
for _, key := range keys {
val := m[key]
vals = append(vals, resp.StringValue(key))
vals = append(vals, resp.StringValue(fmt.Sprintf("%v", val)))
}
return vals
}
func (c *Controller) statsCollections(line string) (string, error) { func (c *Controller) statsCollections(line string) (string, error) {
start := time.Now() start := time.Now()
var key string var key string