mirror of
https://github.com/hibiken/asynq.git
synced 2025-10-03 05:12:01 +08:00
Add history command to asynqmon tool
This commit is contained in:
@@ -28,6 +28,13 @@ type Stats struct {
|
||||
Timestamp time.Time
|
||||
}
|
||||
|
||||
// DailyStats holds aggregate data for a given day.
|
||||
type DailyStats struct {
|
||||
Processed int
|
||||
Failed int
|
||||
Time time.Time
|
||||
}
|
||||
|
||||
// EnqueuedTask is a task in a queue and is ready to be processed.
|
||||
type EnqueuedTask struct {
|
||||
ID xid.ID
|
||||
@@ -131,6 +138,51 @@ func (r *RDB) CurrentStats() (*Stats, error) {
|
||||
}, nil
|
||||
}
|
||||
|
||||
// HistoricalStats returns a list of stats from the last n days.
|
||||
func (r *RDB) HistoricalStats(n int) ([]*DailyStats, error) {
|
||||
if n < 1 {
|
||||
return []*DailyStats{}, nil
|
||||
}
|
||||
const day = 24 * time.Hour
|
||||
now := time.Now().UTC()
|
||||
var days []time.Time
|
||||
var keys []string
|
||||
for i := 0; i < n; i++ {
|
||||
ts := now.Add(-time.Duration(i) * day)
|
||||
days = append(days, ts)
|
||||
keys = append(keys, base.ProcessedKey(ts))
|
||||
keys = append(keys, base.FailureKey(ts))
|
||||
}
|
||||
script := redis.NewScript(`
|
||||
local res = {}
|
||||
for _, key in ipairs(KEYS) do
|
||||
local n = redis.call("GET", key)
|
||||
if not n then
|
||||
n = 0
|
||||
end
|
||||
table.insert(res, tonumber(n))
|
||||
end
|
||||
return res
|
||||
`)
|
||||
res, err := script.Run(r.client, keys, len(keys)).Result()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
data, err := cast.ToIntSliceE(res)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var stats []*DailyStats
|
||||
for i := 0; i < len(data); i += 2 {
|
||||
stats = append(stats, &DailyStats{
|
||||
Processed: data[i],
|
||||
Failed: data[i+1],
|
||||
Time: days[i/2],
|
||||
})
|
||||
}
|
||||
return stats, nil
|
||||
}
|
||||
|
||||
// RedisInfo returns a map of redis info.
|
||||
func (r *RDB) RedisInfo() (map[string]string, error) {
|
||||
res, err := r.client.Info().Result()
|
||||
|
@@ -166,6 +166,55 @@ func TestCurrentStatsWithoutData(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestHistoricalStats(t *testing.T) {
|
||||
r := setup(t)
|
||||
now := time.Now().UTC()
|
||||
|
||||
tests := []struct {
|
||||
n int // number of days
|
||||
}{
|
||||
{90},
|
||||
{7},
|
||||
{0},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
h.FlushDB(t, r.client)
|
||||
|
||||
// populate last n days data
|
||||
for i := 0; i < tc.n; i++ {
|
||||
ts := now.Add(-time.Duration(i) * 24 * time.Hour)
|
||||
processedKey := base.ProcessedKey(ts)
|
||||
failedKey := base.FailureKey(ts)
|
||||
r.client.Set(processedKey, (i+1)*1000, 0)
|
||||
r.client.Set(failedKey, (i+1)*10, 0)
|
||||
}
|
||||
|
||||
got, err := r.HistoricalStats(tc.n)
|
||||
if err != nil {
|
||||
t.Errorf("RDB.HistoricalStats(%v) returned error: %v", tc.n, err)
|
||||
continue
|
||||
}
|
||||
|
||||
if len(got) != tc.n {
|
||||
t.Errorf("RDB.HistorycalStats(%v) returned %d daily stats, want %d", tc.n, len(got), tc.n)
|
||||
continue
|
||||
}
|
||||
|
||||
for i := 0; i < tc.n; i++ {
|
||||
want := &DailyStats{
|
||||
Processed: (i + 1) * 1000,
|
||||
Failed: (i + 1) * 10,
|
||||
Time: now.Add(-time.Duration(i) * 24 * time.Hour),
|
||||
}
|
||||
if diff := cmp.Diff(want, got[i], timeCmpOpt); diff != "" {
|
||||
t.Errorf("RDB.HistoricalStats %d days ago data; got %+v, want %+v; (-want,+got):\n%s", i, got[i], want, diff)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestRedisInfo(t *testing.T) {
|
||||
r := setup(t)
|
||||
|
||||
|
Reference in New Issue
Block a user