Commit fb2dde7d authored by luxq's avatar luxq

add api to get user free used info

parent 2e77974a
......@@ -3,6 +3,8 @@ package cachedata
import (
"context"
"github.com/odysseus/cache/model"
"github.com/stretchr/testify/assert"
"strings"
"testing"
)
......@@ -15,24 +17,36 @@ func newCache() *CacheData {
return _cache
}
_cache = NewCacheData(context.Background(), RedisConnParam{
Addr: "43.198.252.255:6379",
Addr: "18.167.203.17:6379",
Password: "iH0g2CqzjI6SfercGwsT",
DbIndex: 0,
}, model.DbConfig{
Host: "43.198.252.255",
Host: "18.167.203.17",
Port: 3306,
DbName: "ai",
Passwd: "RFnnKHRar5xk7TEF",
User: "ai",
})
//_cache = NewCacheData(context.Background(), RedisConnParam{
// Addr: "43.198.252.255:6379",
// Password: "iH0g2CqzjI6SfercGwsT",
// DbIndex: 0,
//}, model.DbConfig{
// Host: "43.198.252.255",
// Port: 3306,
// DbName: "ai",
// Passwd: "RFnnKHRar5xk7TEF",
// User: "ai",
//})
return _cache
}
func TestCacheData_Query(t *testing.T) {
cache := newCache()
testPath := "/txt2Img/demianhjw/aigic"
testUid := int64(23)
//testPath := "/txt2Img/demianhjw/aigic"
testPath := "/predictions/agicoin/thinkdiffusionxl"
testUid := int64(61)
task, err := cache.Query(testPath, testUid)
if err != nil {
......@@ -40,7 +54,6 @@ func TestCacheData_Query(t *testing.T) {
}
t.Log(task)
}
func TestCacheData_MQuery(t *testing.T) {
cache := newCache()
testPaths := []string{
......@@ -138,3 +151,37 @@ func TestCacheData_UpdateUserBalanceAndLevel(t *testing.T) {
t.Error(err)
}
}
func TestCacheData_GetWhWithAddr(t *testing.T) {
cache := newCache()
testAddr := "0x84A3175be614F5886f99Da506dF08682DF530739"
wh, err := cache.GetWhWithAddr(testAddr)
if err != nil {
t.Error(err)
}
t.Log(wh)
}
func TestCacheData_SetTaskDataToRedis(t *testing.T) {
m := " a b c "
tm := strings.TrimSpace(m)
assert.Equal(t, strings.Compare(tm, "a b c"), 0)
}
func TestCacheData_GetUserFreeUsedInfo(t *testing.T) {
cache := newCache()
uids := []int64{
61,
}
tids := []int64{
1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
}
user, err := cache.GetUserFreeUsedInfo(uids, tids)
if err != nil {
t.Error(err)
}
t.Log(user)
}
......@@ -117,6 +117,73 @@ func (c *CacheData) Query(path string, uid int64) (*model.TaskType, error) {
return nil, ErrBalanceNotEnough
}
type UserTaskFreeUsedInfo struct {
Tid int64 // task type id
TaskDayUsed int64 // 当天使用次数
TaskMonthUsed int64 // 当月使用次数
}
type UserFreeUsedInfo struct {
Uid int64
TasksUsed map[int64]UserTaskFreeUsedInfo
TotalDayUsed int64
TotalMonthUsed int64
}
func (c *CacheData) GetUserFreeUsedInfo(uids []int64, tids []int64) (map[int64]UserFreeUsedInfo, error) {
res := make(map[int64]UserFreeUsedInfo)
for _, uid := range uids {
res[uid] = c.getUserFreeCallUsed(uid, tids)
}
return res, nil
}
func (c *CacheData) getUserFreeCallUsed(uid int64, tids []int64) UserFreeUsedInfo {
var (
layoutDay = "2006-01-02"
layoutMonth = "2006-01"
userDayKey = fmt.Sprintf("k-u-%d:%s:", uid, time.Now().Format(layoutDay))
userMonthKey = fmt.Sprintf("k-u-%d:%s:", uid, time.Now().Format(layoutMonth))
)
pipeline := c.rdb.Pipeline()
taskUserDayResult := make(map[int64]*goredislib.StringCmd)
taskUserMonthResult := make(map[int64]*goredislib.StringCmd)
userTotalDayCmd := pipeline.Get(c.ctx, userDayKey)
userTotalMonthCmd := pipeline.Get(c.ctx, userMonthKey)
for _, tid := range tids {
taskUserDayCmd := pipeline.Get(c.ctx, fmt.Sprintf("k-t-%d-u-%d:%s:", tid, uid, time.Now().Format(layoutDay)))
taskUserMonthCmd := pipeline.Get(c.ctx, fmt.Sprintf("k-t-%d-u-%d:%s:", tid, uid, time.Now().Format(layoutMonth)))
taskUserDayResult[tid] = taskUserDayCmd
taskUserMonthResult[tid] = taskUserMonthCmd
}
pipeline.Exec(c.ctx)
res := UserFreeUsedInfo{
Uid: uid,
TasksUsed: make(map[int64]UserTaskFreeUsedInfo),
TotalDayUsed: 0,
TotalMonthUsed: 0,
}
if userTotalDayCmd.Err() == nil {
res.TotalDayUsed, _ = strconv.ParseInt(userTotalDayCmd.Val(), 10, 64)
}
if userTotalMonthCmd.Err() == nil {
res.TotalMonthUsed, _ = strconv.ParseInt(userTotalMonthCmd.Val(), 10, 64)
}
for _, tid := range tids {
taskDayUsed, _ := strconv.ParseInt(taskUserDayResult[tid].Val(), 10, 64)
taskMonthUsed, _ := strconv.ParseInt(taskUserMonthResult[tid].Val(), 10, 64)
res.TasksUsed[tid] = UserTaskFreeUsedInfo{
Tid: tid,
TaskDayUsed: taskDayUsed,
TaskMonthUsed: taskMonthUsed,
}
}
return res
}
func (c *CacheData) checkQueryForFreeTimes(uid int64, userLevel *model.UserLevel, task *model.TaskType, taskAndUserLevel *model.UserLevelTaskType) (bool, error) {
layoutDay := "2006-01-02"
layoutMonth := "2006-01"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment