Initial commit: web_hghl_server
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,84 @@
|
||||
package videoupdater
|
||||
|
||||
import (
|
||||
"context"
|
||||
"math/rand"
|
||||
"time"
|
||||
"videoweb/pub/common/log"
|
||||
"videoweb/pub/models/videocategorymod"
|
||||
"videoweb/pub/models/videoinformationmod"
|
||||
"videoweb/pub/pubdef"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
)
|
||||
|
||||
const _topN = 20
|
||||
|
||||
// RefreshTopVideosUpdatedAt 遍历所有启用的分类,取排名靠前的20个资源,
|
||||
// 将 PageUpdatedAt 更新为前一个小时内的随机时间
|
||||
func RefreshTopVideosUpdatedAt() {
|
||||
ctx := context.Background()
|
||||
log.Info("videoupdater RefreshTopVideosUpdatedAt start")
|
||||
defer log.Info("videoupdater RefreshTopVideosUpdatedAt end")
|
||||
|
||||
// 查询所有启用的分类
|
||||
catFilter := bson.M{
|
||||
videocategorymod.FN.VideoCategory.Status: true,
|
||||
}
|
||||
catOpts := &options.FindOptions{}
|
||||
catOpts.SetLimit(500)
|
||||
categories, serr := videocategorymod.FindManyVideoCategorys(ctx, catFilter, catOpts)
|
||||
if serr != nil {
|
||||
log.Error("videoupdater FindManyVideoCategorys failed", log.Any("err", serr))
|
||||
return
|
||||
}
|
||||
|
||||
now := time.Now()
|
||||
oneHourAgo := now.Add(-1 * time.Hour)
|
||||
|
||||
for _, cat := range categories {
|
||||
updateCategoryTopVideos(ctx, cat, oneHourAgo, now)
|
||||
}
|
||||
}
|
||||
|
||||
func updateCategoryTopVideos(ctx context.Context, cat *videocategorymod.VideoCategory, rangeStart, rangeEnd time.Time) {
|
||||
// mdcom 的 VideoInformation.CategoryId 是单值,直接按等值匹配
|
||||
videoFilter := bson.M{
|
||||
videoinformationmod.FN.VideoInformation.Status: pubdef.InformationStatusVerifyOK,
|
||||
videoinformationmod.FN.VideoInformation.AddedTime: bson.M{"$lt": rangeEnd},
|
||||
videoinformationmod.FN.VideoInformation.CategoryId: cat.Id,
|
||||
}
|
||||
|
||||
videoOpt := &options.FindOptions{}
|
||||
videoOpt.SetSort(bson.D{
|
||||
{Key: videoinformationmod.FN.VideoInformation.Sort, Value: -1},
|
||||
{Key: videoinformationmod.FN.VideoInformation.AddedTime, Value: -1},
|
||||
})
|
||||
videoOpt.SetLimit(_topN)
|
||||
|
||||
videos, serr := videoinformationmod.FindManyVideoInformations(ctx, videoFilter, videoOpt)
|
||||
if serr != nil {
|
||||
log.Warn("videoupdater FindManyVideoInformations failed",
|
||||
log.Any("categoryId", cat.Id), log.Any("err", serr))
|
||||
return
|
||||
}
|
||||
|
||||
rangeDuration := rangeEnd.Sub(rangeStart)
|
||||
for _, v := range videos {
|
||||
randomOffset := time.Duration(rand.Int63n(int64(rangeDuration)))
|
||||
randomTime := rangeStart.Add(randomOffset)
|
||||
|
||||
filter := bson.M{videoinformationmod.FN.VideoInformation.Id: v.Id}
|
||||
update := bson.M{"$set": bson.M{videoinformationmod.FN.VideoInformation.PageUpdatedAt: randomTime}}
|
||||
if err := videoinformationmod.UpdateOneVideoInformation(ctx, filter, update); err != nil {
|
||||
log.Warn("videoupdater UpdateOneVideoInformation failed",
|
||||
log.Any("videoId", v.Id), log.Any("err", err))
|
||||
}
|
||||
}
|
||||
|
||||
if len(videos) > 0 {
|
||||
log.Info("videoupdater updated category top videos",
|
||||
log.Any("categoryId", cat.Id), log.Any("categoryName", cat.Name), log.Any("count", len(videos)))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user