mirror of
https://github.com/hibiken/asynqmon.git
synced 2025-01-19 03:05:53 +08:00
Add API endpoint to fetch historical stats
This commit is contained in:
parent
3587acaa33
commit
93e42bd9f0
@ -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"`
|
||||
|
3
main.go
3
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")
|
||||
|
@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user