From f00d5ca9e85abe1ab9d373c75b45f886de0f2775 Mon Sep 17 00:00:00 2001 From: Josh Baker Date: Mon, 28 Mar 2016 16:11:29 -0700 Subject: [PATCH] resp stats --- controller/controller.go | 4 +-- controller/stats.go | 78 +++++++++++++++++++++++++++------------- 2 files changed, 56 insertions(+), 26 deletions(-) diff --git a/controller/controller.go b/controller/controller.go index 06d22210..7b8e2c50 100644 --- a/controller/controller.go +++ b/controller/controller.go @@ -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": diff --git a/controller/stats.go b/controller/stats.go index 6b134ae8..5b40afb7 100644 --- a/controller/stats.go +++ b/controller/stats.go @@ -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() - 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 { - 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) - if err != nil { - return "", err + switch msg.OutputType { + case server.JSON: + 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) { 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