|
|
|
@@ -0,0 +1,224 @@
|
|
|
|
|
package aicommentrecord
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"fmt"
|
|
|
|
|
"time"
|
|
|
|
|
"videoweb/pub/common/log"
|
|
|
|
|
"videoweb/pub/common/srvdef"
|
|
|
|
|
"videoweb/pub/mongos"
|
|
|
|
|
|
|
|
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
|
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
|
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var db *mongos.Collection
|
|
|
|
|
|
|
|
|
|
func Init(ctx context.Context) {
|
|
|
|
|
db = mongos.Shop.Coll(TN.AiCommentRecord)
|
|
|
|
|
indexes := []mongo.IndexModel{
|
|
|
|
|
{Keys: bson.D{{Key: FN.AiCommentRecord.Key, Value: 1}}, Options: options.Index().SetUnique(true).SetPartialFilterExpression(bson.M{FN.AiCommentRecord.Key: bson.M{"$exists": true}})},
|
|
|
|
|
{Keys: bson.D{{Key: FN.AiCommentRecord.Vid, Value: 1}, {Key: FN.AiCommentRecord.TaskId, Value: 1}}},
|
|
|
|
|
{Keys: bson.D{{Key: FN.AiCommentRecord.ObjType, Value: 1}, {Key: FN.AiCommentRecord.Vid, Value: 1}}},
|
|
|
|
|
{Keys: bson.D{{Key: FN.AiCommentRecord.Vid, Value: 1}, {Key: FN.AiCommentRecord.Status, Value: 1}, {Key: FN.AiCommentRecord.CreatedAt, Value: 1}}},
|
|
|
|
|
{Keys: bson.D{{Key: FN.AiCommentRecord.TaskId, Value: 1}, {Key: FN.AiCommentRecord.Status, Value: 1}}},
|
|
|
|
|
{Keys: bson.D{{Key: FN.AiCommentRecord.ObjType, Value: 1}, {Key: FN.AiCommentRecord.Status, Value: 1}, {Key: FN.AiCommentRecord.CreatedAt, Value: 1}}},
|
|
|
|
|
{Keys: bson.D{{Key: FN.AiCommentRecord.Status, Value: 1}, {Key: FN.AiCommentRecord.CreatedAt, Value: 1}}},
|
|
|
|
|
{Keys: bson.D{{Key: FN.AiCommentRecord.CreatedAt, Value: 1}}},
|
|
|
|
|
}
|
|
|
|
|
if _, err := db.CreateIndex(ctx, indexes); err != nil {
|
|
|
|
|
panic(fmt.Sprintf("(aicommentrecord:Init) Create AiCommentRecord Index failed! err:%+v:", err))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Reserve claims one playlet before the external submit request. A failed claim
|
|
|
|
|
// can be retried, while a submitted or completed claim blocks duplicate work.
|
|
|
|
|
func Reserve(ctx context.Context, record *AiCommentRecord) (bool, *srvdef.SrvErr) {
|
|
|
|
|
now := time.Now()
|
|
|
|
|
record.Status = StatusSubmitting
|
|
|
|
|
record.CreatedAt = now
|
|
|
|
|
record.UpdatedAt = now
|
|
|
|
|
result, err := db.UpsertOne(ctx, bson.M{FN.AiCommentRecord.Key: record.Key}, bson.M{"$setOnInsert": record})
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Warn("aicommentrecord:Reserve failed", log.E(err))
|
|
|
|
|
return false, srvdef.New(srvdef.ErrDbInsertError, "", "")
|
|
|
|
|
}
|
|
|
|
|
if result.UpsertedCount > 0 {
|
|
|
|
|
return true, nil
|
|
|
|
|
}
|
|
|
|
|
result, err = db.UpdateOne(ctx,
|
|
|
|
|
bson.M{FN.AiCommentRecord.Key: record.Key, FN.AiCommentRecord.Status: bson.M{"$in": []int{StatusFailed, StatusAbandoned}}},
|
|
|
|
|
bson.M{"$set": bson.M{
|
|
|
|
|
FN.AiCommentRecord.Status: StatusSubmitting,
|
|
|
|
|
FN.AiCommentRecord.TaskId: "",
|
|
|
|
|
FN.AiCommentRecord.RetainCount: record.RetainCount,
|
|
|
|
|
FN.AiCommentRecord.TargetCount: record.TargetCount,
|
|
|
|
|
FN.AiCommentRecord.Title: record.Title,
|
|
|
|
|
FN.AiCommentRecord.Desc: record.Desc,
|
|
|
|
|
FN.AiCommentRecord.CreatedAt: now,
|
|
|
|
|
FN.AiCommentRecord.UpdatedAt: now,
|
|
|
|
|
}},
|
|
|
|
|
)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Warn("aicommentrecord:Reserve retry failed", log.E(err))
|
|
|
|
|
return false, srvdef.New(srvdef.ErrDbUpdateError, "", "")
|
|
|
|
|
}
|
|
|
|
|
return result.ModifiedCount > 0, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func SetSubmission(ctx context.Context, key, taskID string, status int, desc string) *srvdef.SrvErr {
|
|
|
|
|
result, err := db.UpdateOne(ctx,
|
|
|
|
|
bson.M{FN.AiCommentRecord.Key: key, FN.AiCommentRecord.Status: StatusSubmitting},
|
|
|
|
|
bson.M{"$set": bson.M{
|
|
|
|
|
FN.AiCommentRecord.TaskId: taskID,
|
|
|
|
|
FN.AiCommentRecord.Status: status,
|
|
|
|
|
FN.AiCommentRecord.Desc: desc,
|
|
|
|
|
FN.AiCommentRecord.UpdatedAt: time.Now(),
|
|
|
|
|
}},
|
|
|
|
|
)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Warn("aicommentrecord:SetSubmission failed", log.E(err), log.Any("key", key))
|
|
|
|
|
return srvdef.New(srvdef.ErrDbUpdateError, "", "")
|
|
|
|
|
}
|
|
|
|
|
if result.MatchedCount == 0 {
|
|
|
|
|
log.Warn("aicommentrecord:SetSubmission reservation not found", log.Any("key", key))
|
|
|
|
|
return srvdef.New(srvdef.ErrDbUpdateError, "", "")
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func InsertMany(ctx context.Context, records AiCommentRecordList) *srvdef.SrvErr {
|
|
|
|
|
if len(records) == 0 {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
now := time.Now()
|
|
|
|
|
for _, record := range records {
|
|
|
|
|
if record.CreatedAt.IsZero() {
|
|
|
|
|
record.CreatedAt = now
|
|
|
|
|
}
|
|
|
|
|
record.UpdatedAt = now
|
|
|
|
|
}
|
|
|
|
|
if _, err := db.InsertMany(ctx, &records); err != nil {
|
|
|
|
|
log.Warn("aicommentrecord:InsertMany db.InsertMany failed", log.E(err))
|
|
|
|
|
return srvdef.New(srvdef.ErrDbInsertError, "", "")
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func FindByStatus(ctx context.Context, status int, limit int64) (AiCommentRecordList, *srvdef.SrvErr) {
|
|
|
|
|
if limit <= 0 {
|
|
|
|
|
limit = 100
|
|
|
|
|
}
|
|
|
|
|
records := make(AiCommentRecordList, 0)
|
|
|
|
|
err := db.Find(ctx,
|
|
|
|
|
&records,
|
|
|
|
|
bson.M{FN.AiCommentRecord.Status: status},
|
|
|
|
|
options.Find().SetSort(bson.D{{Key: FN.AiCommentRecord.CreatedAt, Value: 1}}).SetLimit(limit),
|
|
|
|
|
)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Warn("aicommentrecord:FindByStatus db.Find failed", log.E(err), log.Any("status", status))
|
|
|
|
|
return nil, srvdef.New(srvdef.ErrDbQueryError, "", "")
|
|
|
|
|
}
|
|
|
|
|
return records, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func FindFailedForRetry(ctx context.Context, limit int64) (AiCommentRecordList, *srvdef.SrvErr) {
|
|
|
|
|
if limit <= 0 {
|
|
|
|
|
limit = 100
|
|
|
|
|
}
|
|
|
|
|
records := make(AiCommentRecordList, 0)
|
|
|
|
|
err := db.Find(ctx, &records,
|
|
|
|
|
bson.M{FN.AiCommentRecord.Status: StatusFailed},
|
|
|
|
|
options.Find().SetSort(bson.D{{Key: FN.AiCommentRecord.UpdatedAt, Value: 1}}).SetLimit(limit),
|
|
|
|
|
)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, srvdef.New(srvdef.ErrDbQueryError, "", "")
|
|
|
|
|
}
|
|
|
|
|
return records, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func SetStatusByKey(ctx context.Context, key string, status int, desc string) *srvdef.SrvErr {
|
|
|
|
|
_, err := db.UpdateOne(ctx,
|
|
|
|
|
bson.M{FN.AiCommentRecord.Key: key},
|
|
|
|
|
bson.M{"$set": bson.M{
|
|
|
|
|
FN.AiCommentRecord.Status: status,
|
|
|
|
|
FN.AiCommentRecord.Desc: desc,
|
|
|
|
|
FN.AiCommentRecord.UpdatedAt: time.Now(),
|
|
|
|
|
}},
|
|
|
|
|
)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return srvdef.New(srvdef.ErrDbUpdateError, "", "")
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func FindByVIDsAndObjType(ctx context.Context, objType int64, vids []string) (AiCommentRecordList, *srvdef.SrvErr) {
|
|
|
|
|
if len(vids) == 0 {
|
|
|
|
|
return AiCommentRecordList{}, nil
|
|
|
|
|
}
|
|
|
|
|
records := make(AiCommentRecordList, 0)
|
|
|
|
|
filter := bson.M{FN.AiCommentRecord.Vid: bson.M{"$in": vids}}
|
|
|
|
|
if objType > 0 {
|
|
|
|
|
filter[FN.AiCommentRecord.ObjType] = objType
|
|
|
|
|
}
|
|
|
|
|
if err := db.Find(ctx, &records, filter); err != nil {
|
|
|
|
|
log.Warn("aicommentrecord:FindByVIDsAndObjType db.Find failed", log.E(err), log.Any("objType", objType), log.Any("vids", vids))
|
|
|
|
|
return nil, srvdef.New(srvdef.ErrDbQueryError, "", "")
|
|
|
|
|
}
|
|
|
|
|
return records, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func BatchUpdateStatus(ctx context.Context, taskIDs []string, status int) *srvdef.SrvErr {
|
|
|
|
|
if len(taskIDs) == 0 {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
if _, err := db.UpdateMMany(ctx,
|
|
|
|
|
bson.M{FN.AiCommentRecord.TaskId: bson.M{"$in": taskIDs}},
|
|
|
|
|
bson.M{"$set": bson.M{
|
|
|
|
|
FN.AiCommentRecord.Status: status,
|
|
|
|
|
FN.AiCommentRecord.UpdatedAt: time.Now(),
|
|
|
|
|
}},
|
|
|
|
|
); err != nil {
|
|
|
|
|
log.Warn("aicommentrecord:BatchUpdateStatus db.UpdateMMany failed", log.E(err), log.Any("taskIDs", taskIDs))
|
|
|
|
|
return srvdef.New(srvdef.ErrDbUpdateError, "", "")
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func UpdateStatusAndDescByTaskID(ctx context.Context, taskID string, status int, desc string) *srvdef.SrvErr {
|
|
|
|
|
if taskID == "" {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
if _, err := db.UpdateOne(ctx,
|
|
|
|
|
bson.M{FN.AiCommentRecord.TaskId: taskID},
|
|
|
|
|
bson.M{"$set": bson.M{
|
|
|
|
|
FN.AiCommentRecord.Status: status,
|
|
|
|
|
FN.AiCommentRecord.Desc: desc,
|
|
|
|
|
FN.AiCommentRecord.UpdatedAt: time.Now(),
|
|
|
|
|
}},
|
|
|
|
|
); err != nil {
|
|
|
|
|
log.Warn("aicommentrecord:UpdateStatusAndDescByTaskID db.UpdateOne failed", log.E(err), log.Any("taskID", taskID))
|
|
|
|
|
return srvdef.New(srvdef.ErrDbUpdateError, "", "")
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func ExpireSubmittedBefore(ctx context.Context, deadline time.Time, desc string) (int64, *srvdef.SrvErr) {
|
|
|
|
|
result, err := db.UpdateMMany(ctx,
|
|
|
|
|
bson.M{
|
|
|
|
|
FN.AiCommentRecord.Status: bson.M{"$in": []int{StatusSubmitting, StatusSubmitted}},
|
|
|
|
|
FN.AiCommentRecord.CreatedAt: bson.M{"$lt": deadline},
|
|
|
|
|
},
|
|
|
|
|
bson.M{"$set": bson.M{
|
|
|
|
|
FN.AiCommentRecord.Status: StatusFailed,
|
|
|
|
|
FN.AiCommentRecord.Desc: desc,
|
|
|
|
|
FN.AiCommentRecord.UpdatedAt: time.Now(),
|
|
|
|
|
}},
|
|
|
|
|
)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Warn("aicommentrecord:ExpireSubmittedBefore db.UpdateMMany failed", log.E(err), log.Any("deadline", deadline))
|
|
|
|
|
return 0, srvdef.New(srvdef.ErrDbUpdateError, "", "")
|
|
|
|
|
}
|
|
|
|
|
return result.ModifiedCount, nil
|
|
|
|
|
}
|