forked from all_user/stat_center
66 lines
1.4 KiB
Go
66 lines
1.4 KiB
Go
package statutils
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"stat/common/redis"
|
|
"strconv"
|
|
"time"
|
|
)
|
|
|
|
var hash *redis.Hash
|
|
|
|
func getHash(key string) (*redis.Hash, error) {
|
|
if hash != nil {
|
|
if hash.Key() == key {
|
|
return hash, nil
|
|
} else {
|
|
client := redis.StatRds.Client()
|
|
if client != nil {
|
|
hash = redis.NewHash(client, key)
|
|
hash.Expire(48 * time.Hour)
|
|
return hash, nil
|
|
}
|
|
return nil, errors.New("can not get redis client")
|
|
}
|
|
} else {
|
|
client := redis.StatRds.Client()
|
|
if client != nil {
|
|
hash = redis.NewHash(client, key)
|
|
hash.Expire(48 * time.Hour)
|
|
return hash, nil
|
|
}
|
|
return nil, errors.New("can not get redis client")
|
|
}
|
|
}
|
|
|
|
func SetTodayCount(item *SyncItem) error {
|
|
hashKey := fmt.Sprintf("sync-job-data-count-%s", item.JobBegin().Local().Format("2006-02-01"))
|
|
jobKey := fmt.Sprintf("app:%d-job:%s", item.AppID, item.JobName)
|
|
todayHash, err := getHash(hashKey)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
_, err = todayHash.Set(jobKey, item.todayCount)
|
|
return err
|
|
}
|
|
|
|
func GetTodayCount(item *SyncItem) error {
|
|
hashKey := fmt.Sprintf("sync-job-data-count-%s", item.JobBegin().Local().Format("2006-02-01"))
|
|
jobKey := fmt.Sprintf("app:%d-job:%s", item.AppID, item.JobName)
|
|
todayHash, err := getHash(hashKey)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
countS, err := todayHash.Get(jobKey)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
countI, err := strconv.ParseInt(countS, 10, 64)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
item.todayCount += countI
|
|
return nil
|
|
}
|