@@ -0,0 +1,108 @@
|
||||
package esvideo
|
||||
|
||||
import (
|
||||
"math"
|
||||
"time"
|
||||
|
||||
"91porn-server/common/elastic"
|
||||
"91porn-server/common/localcache"
|
||||
"91porn-server/common/log"
|
||||
"91porn-server/models"
|
||||
"91porn-server/models/v/tagmod"
|
||||
"91porn-server/models/v/vidmod"
|
||||
"91porn-server/models/v/vidpopmod"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
)
|
||||
|
||||
const vidPopCfgKey = "vidPopCfgKey"
|
||||
|
||||
// SyncDataToES 同步数据到ES
|
||||
func SyncDataToES(data []*vidmod.VideoModel) error {
|
||||
if len(data) == 0 {
|
||||
return nil
|
||||
}
|
||||
// 获取标签名字
|
||||
var tagsID []primitive.ObjectID
|
||||
for _, v := range data {
|
||||
tagsID = append(tagsID, v.Tags...)
|
||||
}
|
||||
tags, err := tagmod.FindTagsByIDS(tagsID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var tagsNameMap = make(map[primitive.ObjectID]string)
|
||||
for _, v := range tags {
|
||||
tagsNameMap[v.ID] = v.TagName
|
||||
}
|
||||
var source = elastic.M{}
|
||||
for _, v := range data {
|
||||
var tagsName []string
|
||||
for _, v1 := range v.Tags {
|
||||
tagsName = append(tagsName, tagsNameMap[v1])
|
||||
}
|
||||
source[v.ID.Hex()] = vidmod.ESVideo{
|
||||
ID: v.ID,
|
||||
PublisherID: v.PublisherID,
|
||||
Title: v.Title,
|
||||
NewsType: v.NewsType,
|
||||
Tags: v.Tags,
|
||||
TagsName: tagsName,
|
||||
Filename: v.Filename,
|
||||
PlayCount: v.PlayCount,
|
||||
EffectivePlayCount: v.EffectivePlayCount,
|
||||
PurchaseCount: v.PurchaseCount,
|
||||
LikeCount: v.LikeCount,
|
||||
CommentCount: v.CommentCount,
|
||||
CollectCount: v.CollectCount,
|
||||
ShareCount: v.ShareCount,
|
||||
FakeLikeCount: v.FakeLikeCount,
|
||||
FakeShareCount: v.FakeShareCount,
|
||||
FakePlayCount: v.FakePlayCount,
|
||||
Status: v.Status,
|
||||
Location: v.Location,
|
||||
CreatedAt: v.CreatedAt,
|
||||
UpdatedAt: v.UpdatedAt,
|
||||
ReviewedAt: v.ReviewAt,
|
||||
Hot: calcHot(v),
|
||||
PlayTime: v.PlayTime,
|
||||
Coins: v.Coins,
|
||||
}
|
||||
}
|
||||
log.Info("Elastic sync video data committing...")
|
||||
if err = es.Bulk(models.ESInfoVideoTable, source); err != nil {
|
||||
log.Info("Elastic sync video data commit failed.")
|
||||
return err
|
||||
}
|
||||
log.Info("Elastic sync video data committed.")
|
||||
return nil
|
||||
}
|
||||
|
||||
func calcHot(video *vidmod.VideoModel) float64 {
|
||||
if video.Status == vidmod.CheckPass || video.Status == vidmod.Free {
|
||||
cfg, err := getVideoPopularityConfig()
|
||||
if err != nil {
|
||||
log.Error("Elastic sync video getVideoPopularityConfig", log.E(err))
|
||||
return 0
|
||||
}
|
||||
qualityScore := float64(video.PlayCount + cfg.EffectivePlayCountMultiplier*video.EffectivePlayCount +
|
||||
cfg.LikeCountMultiplier*video.LikeCount)
|
||||
hot := (qualityScore + float64(cfg.InitialPopularity)) / math.Pow(1+time.Since(video.ReviewAt).Hours(),
|
||||
cfg.ReviewTimePower)
|
||||
return hot
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func getVideoPopularityConfig() (*vidpopmod.VideoPopularityConfig, error) {
|
||||
cfg, ok := localcache.C.Get(vidPopCfgKey)
|
||||
if ok {
|
||||
return cfg.(*vidpopmod.VideoPopularityConfig), nil
|
||||
}
|
||||
res, err := vidpopmod.FindOne()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
localcache.C.Set(vidPopCfgKey, &res, 5*time.Minute)
|
||||
return &res, nil
|
||||
}
|
||||
Reference in New Issue
Block a user