forked from contribsys/faktory
-
Notifications
You must be signed in to change notification settings - Fork 0
/
history.go
58 lines (49 loc) · 1.48 KB
/
history.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package storage
import (
"fmt"
"time"
"github.com/go-redis/redis"
)
func (store *redisStore) Success() error {
daystr := time.Now().Format("2006-01-02")
store.rclient.Incr(fmt.Sprintf("processed:%s", daystr))
store.rclient.Incr("processed")
return nil
}
func (store *redisStore) TotalProcessed() uint64 {
return uint64(store.rclient.IncrBy("processed", 0).Val())
}
func (store *redisStore) TotalFailures() uint64 {
return uint64(store.rclient.IncrBy("failures", 0).Val())
}
func (store *redisStore) Failure() error {
store.rclient.Incr("processed")
store.rclient.Incr("failures")
daystr := time.Now().Format("2006-01-02")
store.rclient.Incr(fmt.Sprintf("processed:%s", daystr))
store.rclient.Incr(fmt.Sprintf("failures:%s", daystr))
return nil
}
func (store *redisStore) History(days int, fn func(day string, procCnt uint64, failCnt uint64)) error {
ts := time.Now()
daystrs := make([]string, days)
fails := make([]*redis.IntCmd, days)
procds := make([]*redis.IntCmd, days)
_, err := store.rclient.Pipelined(func(pipe redis.Pipeliner) error {
for idx := 0; idx < days; idx++ {
daystr := ts.Format("2006-01-02")
daystrs[idx] = daystr
procds[idx] = pipe.IncrBy(fmt.Sprintf("processed:%s", daystr), 0)
fails[idx] = pipe.IncrBy(fmt.Sprintf("failures:%s", daystr), 0)
ts = ts.Add(-24 * time.Hour)
}
return nil
})
if err != nil {
return err
}
for idx := 0; idx < days; idx++ {
fn(daystrs[idx], uint64(procds[idx].Val()), uint64(fails[idx].Val()))
}
return nil
}