Add API endpoint to fetch historical stats

This commit is contained in:
Ken Hibino 2020-12-27 15:45:54 -08:00
parent 3587acaa33
commit 93e42bd9f0
3 changed files with 39 additions and 0 deletions

View File

@ -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"`

View File

@ -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")

View File

@ -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
}
}
}