101 lines
2.7 KiB
Go
101 lines
2.7 KiB
Go
package srv_aicomment
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"videoweb/pub/common/log"
|
|
"videoweb/pub/common/srvdef"
|
|
"videoweb/pub/models/aicommentrecord"
|
|
)
|
|
|
|
const (
|
|
staleSubmittedTimeout = 60 * time.Minute
|
|
staleSubmittedDesc = "自动评论任务提交超过60分钟未返回,已释放并等待补交"
|
|
)
|
|
|
|
type aiCommentContentKey struct {
|
|
objType int64
|
|
vid string
|
|
}
|
|
|
|
func preparePendingAICommentVideos(ctx context.Context, videos []VideoItem) ([]VideoItem, *srvdef.SrvErr) {
|
|
if serr := expireStaleSubmittedTasks(ctx); serr != nil {
|
|
return nil, serr
|
|
}
|
|
vidsByType := make(map[int64][]string)
|
|
for _, video := range videos {
|
|
if video.VideoId != "" {
|
|
vidsByType[video.ObjType] = append(vidsByType[video.ObjType], video.VideoId)
|
|
}
|
|
}
|
|
|
|
recordsByContent := make(map[aiCommentContentKey]aicommentrecord.AiCommentRecordList)
|
|
for objType, vids := range vidsByType {
|
|
records, serr := aicommentrecord.FindByVIDsAndObjType(ctx, objType, vids)
|
|
if serr != nil {
|
|
return nil, serr
|
|
}
|
|
for _, record := range records {
|
|
if record == nil {
|
|
continue
|
|
}
|
|
key := aiCommentContentKey{objType: objType, vid: record.Vid}
|
|
recordsByContent[key] = append(recordsByContent[key], record)
|
|
}
|
|
}
|
|
|
|
pending := make([]VideoItem, 0, len(videos))
|
|
for _, video := range videos {
|
|
key := aiCommentContentKey{objType: video.ObjType, vid: video.VideoId}
|
|
if resumed, ok := resumeAICommentVideo(video, recordsByContent[key]); ok {
|
|
pending = append(pending, resumed)
|
|
}
|
|
}
|
|
return pending, nil
|
|
}
|
|
|
|
func expireStaleSubmittedTasks(ctx context.Context) *srvdef.SrvErr {
|
|
deadline := time.Now().Add(-staleSubmittedTimeout)
|
|
expired, serr := aicommentrecord.ExpireSubmittedBefore(ctx, deadline, staleSubmittedDesc)
|
|
if serr != nil {
|
|
return serr
|
|
}
|
|
if expired > 0 {
|
|
log.Warn("自动评论释放超时提交任务", log.Any("count", expired), log.Any("deadline", deadline))
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func resumeAICommentVideo(video VideoItem, records aicommentrecord.AiCommentRecordList) (VideoItem, bool) {
|
|
targetCount := aiCommentPlayletTotal
|
|
if video.CurrentCount >= int64(targetCount) {
|
|
return VideoItem{}, false
|
|
}
|
|
|
|
accountedCount := 0
|
|
for _, record := range records {
|
|
if record == nil {
|
|
continue
|
|
}
|
|
if record.Status != aicommentrecord.StatusSubmitted && record.Status != aicommentrecord.StatusSuccess {
|
|
continue
|
|
}
|
|
retainCount := record.RetainCount
|
|
if retainCount <= 0 {
|
|
retainCount = commentsPerGenerationTask
|
|
}
|
|
accountedCount += retainCount
|
|
}
|
|
if video.CurrentCount > 0 && accountedCount < int(video.CurrentCount) {
|
|
accountedCount = int(video.CurrentCount)
|
|
}
|
|
if accountedCount >= targetCount {
|
|
return VideoItem{}, false
|
|
}
|
|
|
|
video.TargetCount = targetCount
|
|
video.CommentTotal = targetCount - accountedCount
|
|
return video, true
|
|
}
|