feat: 增加定时任务更新观看次数
This commit is contained in:
parent
3b2a6d059e
commit
ab8bdde9d9
133
pkg/cron/task.go
133
pkg/cron/task.go
@ -5,6 +5,7 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"fonchain-fiee/api/aryshare"
|
||||||
"fonchain-fiee/api/bundle"
|
"fonchain-fiee/api/bundle"
|
||||||
"fonchain-fiee/api/cast"
|
"fonchain-fiee/api/cast"
|
||||||
"fonchain-fiee/pkg/cache"
|
"fonchain-fiee/pkg/cache"
|
||||||
@ -15,6 +16,7 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/go-redis/redis"
|
"github.com/go-redis/redis"
|
||||||
@ -39,6 +41,9 @@ func InitTasks() error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("添加定时任务失败: %v", err)
|
log.Printf("添加定时任务失败: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 每2分钟执行一次标签观看次数更新任务
|
||||||
|
// err = cm.AddTask("updateCastTagWatchCount", "0 */2 * * * *", UpdateCastTagWatchCountTask)
|
||||||
cm.Start()
|
cm.Start()
|
||||||
|
|
||||||
// 启动队列消费者
|
// 启动队列消费者
|
||||||
@ -496,3 +501,131 @@ func AyrshareMetricsCollectorTask() {
|
|||||||
func RefreshArtistOrderTask() {
|
func RefreshArtistOrderTask() {
|
||||||
service.CastProvider.Tools(context.Background(), &cast.ToolsReq{Action: "refreshArtistOrder"})
|
service.CastProvider.Tools(context.Background(), &cast.ToolsReq{Action: "refreshArtistOrder"})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// UpdateCastTagWatchCountTask 更新标签观看次数的定时任务(每5分钟执行一次)
|
||||||
|
func UpdateCastTagWatchCountTask() {
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
// 计算两天前的00:00:00
|
||||||
|
now := time.Now()
|
||||||
|
twoDaysAgo := now.AddDate(0, 0, -2)
|
||||||
|
createdAtStart := time.Date(twoDaysAgo.Year(), twoDaysAgo.Month(), twoDaysAgo.Day(), 0, 0, 0, 0, twoDaysAgo.Location())
|
||||||
|
createdAtEnd := now
|
||||||
|
|
||||||
|
// 格式化时间字符串:2026-01-01 00:00:00
|
||||||
|
createdAtStartStr := createdAtStart.Format("2006-01-02 15:04:05")
|
||||||
|
createdAtEndStr := createdAtEnd.Format("2006-01-02 15:04:05")
|
||||||
|
|
||||||
|
// 调用 ListCastTags 接口,筛选 IsWatchCountCalled = 2 的数据
|
||||||
|
listReq := &cast.ListCastTagsReq{
|
||||||
|
CreatedAtStart: createdAtStartStr,
|
||||||
|
CreatedAtEnd: createdAtEndStr,
|
||||||
|
IsWatchCountCalled: 2, // 2表示未调用
|
||||||
|
Page: 1,
|
||||||
|
PageSize: 20,
|
||||||
|
}
|
||||||
|
|
||||||
|
listResp, err := service.CastProvider.ListCastTags(ctx, listReq)
|
||||||
|
if err != nil {
|
||||||
|
zap.L().Error("获取标签列表失败", zap.Error(err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if listResp.Data == nil || len(listResp.Data) == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
zap.L().Info("获取到需要更新的标签", zap.Int("count", len(listResp.Data)))
|
||||||
|
|
||||||
|
// 获取有效的 profileKey
|
||||||
|
profileKey, err := serverCast.GetValidProfileKey(ctx, []uint32{1})
|
||||||
|
if err != nil {
|
||||||
|
zap.L().Error("获取有效profileKey失败", zap.Error(err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// 准备批量更新的数据
|
||||||
|
updateData := make([]*cast.CastTagInfo, 0, len(listResp.Data))
|
||||||
|
|
||||||
|
// 遍历每个标签,调用 RecommendHashtags 接口
|
||||||
|
for _, tag := range listResp.Data {
|
||||||
|
if tag.HashTag == "" {
|
||||||
|
zap.L().Warn("标签HashTag为空,跳过", zap.String("uuid", tag.Uuid))
|
||||||
|
// 即使HashTag为空,也要更新IsWatchCountCalled为1
|
||||||
|
updateData = append(updateData, &cast.CastTagInfo{
|
||||||
|
Uuid: tag.Uuid,
|
||||||
|
WatchCount: 1,
|
||||||
|
IsWatchCountCalled: 1,
|
||||||
|
})
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// 调用 RecommendHashtags 接口
|
||||||
|
recommendReq := &aryshare.RecommendHashtagsRequest{
|
||||||
|
Keyword: tag.HashTag,
|
||||||
|
ProfileKey: profileKey,
|
||||||
|
}
|
||||||
|
|
||||||
|
recommendResp, err := service.AyrshareProvider.RecommendHashtags(ctx, recommendReq)
|
||||||
|
if err != nil {
|
||||||
|
zap.L().Error("调用RecommendHashtags接口失败",
|
||||||
|
zap.String("hashTag", tag.HashTag),
|
||||||
|
zap.String("uuid", tag.Uuid),
|
||||||
|
zap.Error(err))
|
||||||
|
// 调用失败时,将WatchCount更新为1,IsWatchCountCalled更新为1
|
||||||
|
updateData = append(updateData, &cast.CastTagInfo{
|
||||||
|
Uuid: tag.Uuid,
|
||||||
|
WatchCount: 1,
|
||||||
|
IsWatchCountCalled: 1,
|
||||||
|
})
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// 对比返回结果,查找完全一致的标签
|
||||||
|
var matchedViewCount int64 = 0
|
||||||
|
if recommendResp.Recommendations != nil {
|
||||||
|
for _, recommendation := range recommendResp.Recommendations {
|
||||||
|
// 完全一致匹配(不区分大小写)
|
||||||
|
if strings.EqualFold(recommendation.Name, tag.HashTag) {
|
||||||
|
matchedViewCount = recommendation.ViewCount
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 根据匹配结果更新WatchCount
|
||||||
|
var watchCount int32 = 1
|
||||||
|
if matchedViewCount > 0 {
|
||||||
|
watchCount = int32(matchedViewCount)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 添加到更新列表
|
||||||
|
updateData = append(updateData, &cast.CastTagInfo{
|
||||||
|
Uuid: tag.Uuid,
|
||||||
|
WatchCount: watchCount,
|
||||||
|
IsWatchCountCalled: 1,
|
||||||
|
})
|
||||||
|
|
||||||
|
zap.L().Debug("处理标签完成",
|
||||||
|
zap.String("hashTag", tag.HashTag),
|
||||||
|
zap.String("uuid", tag.Uuid),
|
||||||
|
zap.Int64("matchedViewCount", matchedViewCount),
|
||||||
|
zap.Int32("watchCount", watchCount))
|
||||||
|
}
|
||||||
|
|
||||||
|
// 如果没有需要更新的数据,直接返回
|
||||||
|
if len(updateData) == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// 批量更新标签
|
||||||
|
batchUpdateReq := &cast.BatchUpdateCastTagsReq{
|
||||||
|
Data: updateData,
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = service.CastProvider.BatchUpdateCastTags(ctx, batchUpdateReq)
|
||||||
|
if err != nil {
|
||||||
|
zap.L().Error("批量更新标签失败", zap.Error(err), zap.Int("count", len(updateData)))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user