From 93e42bd9f0ac6491ba79931e59677d3269ab40fd Mon Sep 17 00:00:00 2001 From: Ken Hibino Date: Sun, 27 Dec 2020 15:45:54 -0800 Subject: [PATCH] Add API endpoint to fetch historical stats --- conversion_helpers.go | 8 ++++++++ main.go | 3 +++ queue_handlers.go | 28 ++++++++++++++++++++++++++++ 3 files changed, 39 insertions(+) diff --git a/conversion_helpers.go b/conversion_helpers.go index 54025c8..5cc999b 100644 --- a/conversion_helpers.go +++ b/conversion_helpers.go @@ -72,6 +72,14 @@ func toDailyStats(s *asynq.DailyStats) *DailyStats { } } +func toDailyStatsList(in []*asynq.DailyStats) []*DailyStats { + out := make([]*DailyStats, len(in)) + for i, s := range in { + out[i] = toDailyStats(s) + } + return out +} + type BaseTask struct { ID string `json:"id"` Type string `json:"type"` diff --git a/main.go b/main.go index d03f9e1..09aaaeb 100644 --- a/main.go +++ b/main.go @@ -75,6 +75,9 @@ func main() { api.HandleFunc("/queues/{qname}:pause", newPauseQueueHandlerFunc(inspector)).Methods("POST") api.HandleFunc("/queues/{qname}:resume", newResumeQueueHandlerFunc(inspector)).Methods("POST") + // Queue Historical Stats endpoint. + api.HandleFunc("/queue_stats", newListQueueStatsHandlerFunc(inspector)).Methods("GET") + // Task endpoints. api.HandleFunc("/queues/{qname}/active_tasks", newListActiveTasksHandlerFunc(inspector)).Methods("GET") api.HandleFunc("/queues/{qname}/active_tasks/{task_id}:cancel", newCancelActiveTaskHandlerFunc(inspector)).Methods("POST") diff --git a/queue_handlers.go b/queue_handlers.go index e774a00..c3af31f 100644 --- a/queue_handlers.go +++ b/queue_handlers.go @@ -106,3 +106,31 @@ func newResumeQueueHandlerFunc(inspector *asynq.Inspector) http.HandlerFunc { w.WriteHeader(http.StatusNoContent) } } + +type ListQueueStatsResponse struct { + Stats map[string][]*DailyStats `json:"stats"` +} + +func newListQueueStatsHandlerFunc(inspector *asynq.Inspector) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + qnames, err := inspector.Queues() + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + resp := ListQueueStatsResponse{Stats: make(map[string][]*DailyStats)} + const numdays = 90 // Get stats for the last 90 days. + for _, qname := range qnames { + stats, err := inspector.History(qname, numdays) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + resp.Stats[qname] = toDailyStatsList(stats) + } + if err := json.NewEncoder(w).Encode(resp); err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + } +}