mirror of
https://github.com/hibiken/asynqmon.git
synced 2025-09-22 06:46:34 +08:00
Add API endpoint to fetch historical stats
This commit is contained in:
@@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user