From b6af0e521957b208548bdf5d0a0e25dadf34afdb Mon Sep 17 00:00:00 2001 From: Alan Bernstein Date: Thu, 1 Oct 2020 10:25:28 -0500 Subject: [PATCH] Move usage data to new ui-specific endpoint --- http/handler.go | 53 +++++++++++++++++++++++++++++------------- server/handler_test.go | 9 +++++++ 2 files changed, 46 insertions(+), 16 deletions(-) diff --git a/http/handler.go b/http/handler.go index 606aaffa4..f000a365c 100644 --- a/http/handler.go +++ b/http/handler.go @@ -392,6 +392,8 @@ func newRouter(handler *Handler) http.Handler { router.HandleFunc("/queries", handler.handleGetActiveQueries).Methods("GET").Name("GetActiveQueries") router.HandleFunc("/version", handler.handleGetVersion).Methods("GET").Name("GetVersion") + router.HandleFunc("/ui/usage", handler.handleGetUsage).Methods("GET").Name("GetUsage") + // /internal endpoints are for internal use only; they may change at any time. // DO NOT rely on these for external applications! router.HandleFunc("/internal/cluster/message", handler.handlePostClusterMessage).Methods("POST").Name("PostClusterMessage") @@ -657,8 +659,8 @@ func (h *Handler) handlePostSchema(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusNoContent) } -// handleGetStatus handles GET /status requests. -func (h *Handler) handleGetStatus(w http.ResponseWriter, r *http.Request) { +// handleGetUsage handles GET /ui/usage requests. +func (h *Handler) handleGetUsage(w http.ResponseWriter, r *http.Request) { if !validHeaderAcceptJSON(r.Header) { http.Error(w, "JSON only acceptable response", http.StatusNotAcceptable) return @@ -667,15 +669,40 @@ func (h *Handler) handleGetStatus(w http.ResponseWriter, r *http.Request) { if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) } - usage := diskUsage{ + + disk := diskUsage{ Total: usageTotal, Indexes: usageIndexes, } + + usage := getUsageResponse{ + Disk: disk, + } + + w.Header().Set("Content-Type", "application/json") + if err := json.NewEncoder(w).Encode(usage); err != nil { + h.logger.Printf("write status response error: %s", err) + } +} + +type getUsageResponse struct { + Disk diskUsage `json:"bytesOnDisk"` +} +type diskUsage struct { + Total int64 `json:"total"` + Indexes map[string]int64 `json:"indexes"` +} + +// handleGetStatus handles GET /status requests. +func (h *Handler) handleGetStatus(w http.ResponseWriter, r *http.Request) { + if !validHeaderAcceptJSON(r.Header) { + http.Error(w, "JSON only acceptable response", http.StatusNotAcceptable) + return + } status := getStatusResponse{ - State: h.api.State(), - Nodes: h.api.Hosts(r.Context()), - LocalID: h.api.Node().ID, - BytesOnDisk: usage, + State: h.api.State(), + Nodes: h.api.Hosts(r.Context()), + LocalID: h.api.Node().ID, } w.Header().Set("Content-Type", "application/json") if err := json.NewEncoder(w).Encode(status); err != nil { @@ -731,15 +758,9 @@ type getSchemaResponse struct { } type getStatusResponse struct { - State string `json:"state"` - Nodes []*pilosa.Node `json:"nodes"` - LocalID string `json:"localID"` - BytesOnDisk diskUsage `json:"bytesOnDisk"` -} - -type diskUsage struct { - Total int64 `json:"total"` - Indexes map[string]int64 `json:"indexes"` + State string `json:"state"` + Nodes []*pilosa.Node `json:"nodes"` + LocalID string `json:"localID"` } func hash(s string) string { diff --git a/server/handler_test.go b/server/handler_test.go index c093d873d..47e4698b0 100644 --- a/server/handler_test.go +++ b/server/handler_test.go @@ -379,6 +379,15 @@ func TestHandler_Endpoints(t *testing.T) { if len(ret["nodes"].([]interface{})) != 1 { t.Fatalf("wrong length nodes list: %#v", ret) } + }) + + t.Run("UI/usage", func(t *testing.T) { + w := httptest.NewRecorder() + h.ServeHTTP(w, test.MustNewHTTPRequest("GET", "/ui/usage", nil)) + if w.Code != gohttp.StatusOK { + t.Fatalf("unexpected status code: %d", w.Code) + } + ret := mustJSONDecode(t, w.Body) usage := ret["bytesOnDisk"].(map[string]interface{}) indexes := usage["indexes"].(map[string]interface{}) if len(indexes) != 2 {