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":
// err = c.cmdReadOnly(nline)
// resp = okResp()
// case "stats":
// resp, err = c.cmdStats(nline)
case "stats":
res, err = c.cmdStats(msg)
case "server":
res, err = c.cmdServer(msg)
case "scan":

View File

@ -12,15 +12,21 @@ import (
"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()
var key string
vs := msg.Values[1:]
var ms = []map[string]interface{}{}
if line == "" {
if len(vs) == 0 {
return "", errInvalidNumberOfArguments
}
for len(line) > 0 {
line, key = token(line)
var vals []resp.Value
var key string
var ok bool
for {
vs, key, ok = tokenval(vs)
if !ok {
break
}
col := c.getCol(key)
if col != nil {
m := make(map[string]interface{})
@ -28,16 +34,36 @@ func (c *Controller) cmdStats(line string) (string, error) {
m["num_points"] = points
m["in_memory_size"] = col.TotalWeight()
m["num_objects"] = col.Count()
switch msg.OutputType {
case server.JSON:
ms = append(ms, m)
case server.RESP:
vals = append(vals, resp.ArrayValue(respValuesSimpleMap(m)))
}
} else {
switch msg.OutputType {
case server.JSON:
ms = append(ms, nil)
case server.RESP:
vals = append(vals, resp.NullValue())
}
}
}
switch msg.OutputType {
case server.JSON:
data, err := json.Marshal(ms)
if err != nil {
return "", err
}
return `{"ok":true,"stats":` + string(data) + `,"elapsed":"` + time.Now().Sub(start).String() + "\"}", nil
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 res, nil
}
func (c *Controller) cmdServer(msg *server.Message) (res string, err error) {
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() + "\"}"
case server.RESP:
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)))
}
vals := respValuesSimpleMap(m)
data, err := resp.ArrayValue(vals).MarshalRESP()
if err != nil {
return "", err
@ -110,6 +124,22 @@ func (c *Controller) cmdServer(msg *server.Message) (res string, err error) {
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) {
start := time.Now()
var key string