mirror of
https://github.com/featurebasedb/featurebase.git
synced 2026-09-10 15:01:03 +00:00
Move usage data to new ui-specific endpoint
This commit is contained in:
parent
6f2fe3afe1
commit
b6af0e5219
2 changed files with 46 additions and 16 deletions
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue