mirror of
https://github.com/hibiken/asynq.git
synced 2024-12-27 16:13:40 +08:00
Update HistoricalStats method in RDB
This commit is contained in:
parent
44a3d177f0
commit
5f82b4b365
@ -25,8 +25,7 @@ func (r *RDB) AllQueues() ([]string, error) {
|
|||||||
// Stats represents a state of queues at a certain time.
|
// Stats represents a state of queues at a certain time.
|
||||||
type Stats struct {
|
type Stats struct {
|
||||||
// Name of the queue (e.g. "default", "critical").
|
// Name of the queue (e.g. "default", "critical").
|
||||||
// Note: It doesn't include the prefix "asynq:queues:".
|
Queue string
|
||||||
Name string
|
|
||||||
// Paused indicates whether the queue is paused.
|
// Paused indicates whether the queue is paused.
|
||||||
// If true, tasks in the queue should not be processed.
|
// If true, tasks in the queue should not be processed.
|
||||||
Paused bool
|
Paused bool
|
||||||
@ -47,8 +46,14 @@ type Stats struct {
|
|||||||
|
|
||||||
// DailyStats holds aggregate data for a given day.
|
// DailyStats holds aggregate data for a given day.
|
||||||
type DailyStats struct {
|
type DailyStats struct {
|
||||||
|
// Name of the queue (e.g. "default", "critical").
|
||||||
|
Queue string
|
||||||
|
// Total number of tasks processed during the given day.
|
||||||
|
// The number includes both succeeded and failed tasks.
|
||||||
Processed int
|
Processed int
|
||||||
|
// Total number of tasks failed during the given day.
|
||||||
Failed int
|
Failed int
|
||||||
|
// Date this stats was taken.
|
||||||
Time time.Time
|
Time time.Time
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -120,7 +125,7 @@ func (r *RDB) CurrentStats(qname string) (*Stats, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
stats := &Stats{
|
stats := &Stats{
|
||||||
Name: qname,
|
Queue: qname,
|
||||||
Timestamp: now,
|
Timestamp: now,
|
||||||
}
|
}
|
||||||
for i := 0; i < len(data); i += 2 {
|
for i := 0; i < len(data); i += 2 {
|
||||||
@ -163,8 +168,8 @@ for _, key in ipairs(KEYS) do
|
|||||||
end
|
end
|
||||||
return res`)
|
return res`)
|
||||||
|
|
||||||
// HistoricalStats returns a list of stats from the last n days.
|
// HistoricalStats returns a list of stats from the last n days for the given queue.
|
||||||
func (r *RDB) HistoricalStats(n int) ([]*DailyStats, error) {
|
func (r *RDB) HistoricalStats(qname string, n int) ([]*DailyStats, error) {
|
||||||
if n < 1 {
|
if n < 1 {
|
||||||
return []*DailyStats{}, nil
|
return []*DailyStats{}, nil
|
||||||
}
|
}
|
||||||
@ -175,10 +180,10 @@ func (r *RDB) HistoricalStats(n int) ([]*DailyStats, error) {
|
|||||||
for i := 0; i < n; i++ {
|
for i := 0; i < n; i++ {
|
||||||
ts := now.Add(-time.Duration(i) * day)
|
ts := now.Add(-time.Duration(i) * day)
|
||||||
days = append(days, ts)
|
days = append(days, ts)
|
||||||
keys = append(keys, base.ProcessedKey(ts))
|
keys = append(keys, base.ProcessedKey(qname, ts))
|
||||||
keys = append(keys, base.FailureKey(ts))
|
keys = append(keys, base.FailureKey(qname, ts))
|
||||||
}
|
}
|
||||||
res, err := historicalStatsCmd.Run(r.client, keys, len(keys)).Result()
|
res, err := historicalStatsCmd.Run(r.client, keys).Result()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -189,6 +194,7 @@ func (r *RDB) HistoricalStats(n int) ([]*DailyStats, error) {
|
|||||||
var stats []*DailyStats
|
var stats []*DailyStats
|
||||||
for i := 0; i < len(data); i += 2 {
|
for i := 0; i < len(data); i += 2 {
|
||||||
stats = append(stats, &DailyStats{
|
stats = append(stats, &DailyStats{
|
||||||
|
Queue: qname,
|
||||||
Processed: data[i],
|
Processed: data[i],
|
||||||
Failed: data[i+1],
|
Failed: data[i+1],
|
||||||
Time: days[i/2],
|
Time: days[i/2],
|
||||||
|
@ -108,7 +108,7 @@ func TestCurrentStats(t *testing.T) {
|
|||||||
paused: []string{},
|
paused: []string{},
|
||||||
qname: "default",
|
qname: "default",
|
||||||
want: &Stats{
|
want: &Stats{
|
||||||
Name: "default",
|
Queue: "default",
|
||||||
Paused: false,
|
Paused: false,
|
||||||
Enqueued: 1,
|
Enqueued: 1,
|
||||||
InProgress: 1,
|
InProgress: 1,
|
||||||
@ -162,7 +162,7 @@ func TestCurrentStats(t *testing.T) {
|
|||||||
paused: []string{"critical", "low"},
|
paused: []string{"critical", "low"},
|
||||||
qname: "critical",
|
qname: "critical",
|
||||||
want: &Stats{
|
want: &Stats{
|
||||||
Name: "critical",
|
Queue: "critical",
|
||||||
Paused: true,
|
Paused: true,
|
||||||
Enqueued: 1,
|
Enqueued: 1,
|
||||||
InProgress: 0,
|
InProgress: 0,
|
||||||
@ -225,11 +225,12 @@ func TestHistoricalStats(t *testing.T) {
|
|||||||
now := time.Now().UTC()
|
now := time.Now().UTC()
|
||||||
|
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
|
qname // queue of interest
|
||||||
n int // number of days
|
n int // number of days
|
||||||
}{
|
}{
|
||||||
{90},
|
{"default", 90},
|
||||||
{7},
|
{"custom", 7},
|
||||||
{0},
|
{"default", 0},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tc := range tests {
|
for _, tc := range tests {
|
||||||
@ -238,31 +239,32 @@ func TestHistoricalStats(t *testing.T) {
|
|||||||
// populate last n days data
|
// populate last n days data
|
||||||
for i := 0; i < tc.n; i++ {
|
for i := 0; i < tc.n; i++ {
|
||||||
ts := now.Add(-time.Duration(i) * 24 * time.Hour)
|
ts := now.Add(-time.Duration(i) * 24 * time.Hour)
|
||||||
processedKey := base.ProcessedKey(ts)
|
processedKey := base.ProcessedKey(tc.qname, ts)
|
||||||
failedKey := base.FailureKey(ts)
|
failedKey := base.FailureKey(tc.qname, ts)
|
||||||
r.client.Set(processedKey, (i+1)*1000, 0)
|
r.client.Set(processedKey, (i+1)*1000, 0)
|
||||||
r.client.Set(failedKey, (i+1)*10, 0)
|
r.client.Set(failedKey, (i+1)*10, 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
got, err := r.HistoricalStats(tc.n)
|
got, err := r.HistoricalStats(tc.qname, tc.n)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("RDB.HistoricalStats(%v) returned error: %v", tc.n, err)
|
t.Errorf("RDB.HistoricalStats(%q, %d) returned error: %v", tc.qname, tc.n, err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(got) != tc.n {
|
if len(got) != tc.n {
|
||||||
t.Errorf("RDB.HistorycalStats(%v) returned %d daily stats, want %d", tc.n, len(got), tc.n)
|
t.Errorf("RDB.HistorycalStats(%q, %d) returned %d daily stats, want %d", tc.qname, tc.n, len(got), tc.n)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
for i := 0; i < tc.n; i++ {
|
for i := 0; i < tc.n; i++ {
|
||||||
want := &DailyStats{
|
want := &DailyStats{
|
||||||
|
Queue: tc.qname,
|
||||||
Processed: (i + 1) * 1000,
|
Processed: (i + 1) * 1000,
|
||||||
Failed: (i + 1) * 10,
|
Failed: (i + 1) * 10,
|
||||||
Time: now.Add(-time.Duration(i) * 24 * time.Hour),
|
Time: now.Add(-time.Duration(i) * 24 * time.Hour),
|
||||||
}
|
}
|
||||||
if diff := cmp.Diff(want, got[i], timeCmpOpt); diff != "" {
|
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)
|
t.Errorf("RDB.HistoricalStats for the last %d days; got %+v, want %+v; (-want,+got):\n%s", i, got[i], want, diff)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user