service/library/contentaudit/imageaudit/imageaudit.go

392 lines
14 KiB
Go
Raw Normal View History

2023-12-21 22:17:40 +08:00
package imageaudit
import (
"encoding/json"
"net/http"
2023-12-21 22:17:40 +08:00
"service/api/consts"
imageauditproto "service/api/proto/imageaudit/proto"
imageaudittaskproto "service/api/proto/imageaudittask/proto"
"service/bizcommon/util"
"service/dbstruct"
"service/library/logger"
imageaudit "github.com/alibabacloud-go/imageaudit-20191230/v3/client"
teautils "github.com/alibabacloud-go/tea-utils/v2/service"
"github.com/alibabacloud-go/tea/tea"
goproto "google.golang.org/protobuf/proto"
"github.com/gin-gonic/gin"
)
2024-01-20 06:33:10 +08:00
// 刷新批次号
func RefreshBatchId() string {
2024-01-18 18:19:31 +08:00
batchId := defaultImageAuditTaskScheduler.batchId
defaultImageAuditTaskScheduler.batchId = genereteBatchId()
2024-01-20 06:33:10 +08:00
return batchId
}
// 图像审核主逻辑
func Run(batchId string) (successNum int, failNum int, err error) {
2024-01-18 18:19:31 +08:00
// 查询该批次所有审核任务
imageaudittasks, err := _DefaultImageAuditTask.OpList(&gin.Context{}, &imageaudittaskproto.OpListReq{
BatchId: goproto.String(batchId),
Status: goproto.Int64(consts.ImageAudit_Created),
2024-01-19 17:31:38 +08:00
Sort: "ct",
2024-01-18 18:19:31 +08:00
})
2024-01-20 06:33:10 +08:00
if err != nil {
logger.Info("_DefaultImageAuditTask OpList fail: %v", err)
}
if len(imageaudittasks) == 0 {
return 0, 0, nil
}
2024-01-19 17:31:38 +08:00
2024-01-20 06:33:10 +08:00
logger.Info("Image audit batch started, batchId : %v, task number : %v", batchId, len(imageaudittasks))
// 1.构建批量任务控制块初始化必要的actionMap、填充送审image等信息
ctrlBlock := NewImageAuditTaskBatchControlBlock(imageaudittasks, batchId)
2023-12-21 22:17:40 +08:00
2024-01-20 06:33:10 +08:00
// 2.创建请求
// oss不在上海的服务器需要调用Advance接口
2024-01-20 06:33:10 +08:00
req, err := createScanImageAdvanceRequest(ctrlBlock)
2023-12-21 22:17:40 +08:00
if err != nil {
logger.Info("Create Scan ImageRequest fail: %v", err)
2024-01-20 06:33:10 +08:00
handleBatchError(ctrlBlock, err)
failNum = len(imageaudittasks)
2023-12-21 22:17:40 +08:00
return
}
2024-01-20 06:33:10 +08:00
// 3.调用阿里的服务,收到应答
2023-12-21 22:17:40 +08:00
runtime := &teautils.RuntimeOptions{
ConnectTimeout: tea.Int(30000),
}
_result, err := defaultImageAuditClient.ScanImageAdvance(req, runtime)
2023-12-21 22:17:40 +08:00
if err != nil {
2024-01-05 00:23:59 +08:00
if _t, ok := err.(*tea.SDKError); ok {
logger.Error("ScanImageAdvance fail, errinfo : %v", util.DerefString(_t.Data))
}
2024-01-20 06:33:10 +08:00
handleBatchError(ctrlBlock, err)
failNum = len(imageaudittasks)
return
2023-12-21 22:17:40 +08:00
}
2024-02-27 02:21:24 +08:00
logger.Info("Receive the response from ScanImageAdvance: %v", _result.String())
2023-12-21 22:17:40 +08:00
2024-01-20 06:33:10 +08:00
// 4.处理应答
err = handleScanImageResponse(ctrlBlock, _result)
2023-12-21 22:17:40 +08:00
2024-01-20 06:33:10 +08:00
successNum = len(imageaudittasks)
2023-12-21 22:17:40 +08:00
return
}
2024-01-20 06:33:10 +08:00
func createScanImageAdvanceRequest(ctrlBlock *ImageAuditTaskBatchControlBlock) (request *imageaudit.ScanImageAdvanceRequest, err error) {
httpClient := http.Client{}
2024-01-20 06:33:10 +08:00
imageauditIds := ctrlBlock.ImageAuditIds
reqTasks := make([]*imageaudit.ScanImageAdvanceRequestTask, 0)
2024-01-01 19:24:32 +08:00
reqs := make([]*imageaudit.ScanImageRequestTask, 0)
2024-01-20 06:33:10 +08:00
for i, image := range ctrlBlock.Images {
var file *http.Response
file, err = httpClient.Get(image.Images[0].Urls[0])
if err != nil {
logger.Error("httpClient Get fail : %v", err)
return
2023-12-21 22:17:40 +08:00
}
2024-01-20 06:33:10 +08:00
reqTasks = append(reqTasks, &imageaudit.ScanImageAdvanceRequestTask{
DataId: goproto.String(imageauditIds[i]),
ImageURLObject: file.Body,
})
reqs = append(reqs, &imageaudit.ScanImageRequestTask{
DataId: goproto.String(imageauditIds[i]),
ImageURL: goproto.String(image.Images[0].Urls[0]),
})
2023-12-21 22:17:40 +08:00
}
request = &imageaudit.ScanImageAdvanceRequest{
2023-12-21 22:17:40 +08:00
Scene: scenes,
Task: reqTasks,
}
2024-01-01 19:24:32 +08:00
logger.Info("本次打包:%v", reqs)
2023-12-21 22:17:40 +08:00
return
}
2024-01-20 06:33:10 +08:00
func handleScanImageResponse(ctrlBlock *ImageAuditTaskBatchControlBlock, resp *imageaudit.ScanImageResponse) (err error) {
2023-12-21 22:17:40 +08:00
results := resp.Body.Data.Results
2024-01-20 06:33:10 +08:00
taskCtrlBlocks := ctrlBlock.TaskCtrlBlocks
img2taskIndexMap := ctrlBlock.Img2taskIndexMap
actionMap := ctrlBlock.ActionMap
2023-12-21 22:17:40 +08:00
for i, result := range results {
2024-02-27 01:05:14 +08:00
logger.Info("Handling %vs result...", i)
2023-12-21 22:17:40 +08:00
isTaskCompleted := false
isActionCompleted := false
action := &ImageAuditAction{}
// 1.dataId是imageaudit表主键Id, 唯一标识一次对单张图片的图像审核
2024-02-27 01:05:14 +08:00
logger.Info("Getting dataId...")
2023-12-21 22:17:40 +08:00
dataId := util.DerefString(result.DataId)
// 2.立即在imageaudit-imageaudit中更新该次审核结果
2024-02-27 01:05:14 +08:00
logger.Info("Handling its audit record...")
2023-12-21 22:17:40 +08:00
pass, err := handleImageAudit(dataId, result)
if err != nil {
logger.Error("handleImageAudit fail: %v", err)
}
// 3.取出task
2024-02-27 01:05:14 +08:00
logger.Info("Retriving its audit task...")
2024-01-20 06:33:10 +08:00
taskIndex := img2taskIndexMap[i]
task := taskCtrlBlocks[taskIndex]
2023-12-21 22:17:40 +08:00
// 4.在task中记录本分片结果并累积已到达的分片数直到所有分片到达决定该任务是否成功(非分片任务分片数为1)
2024-02-27 01:05:14 +08:00
logger.Info("Recording it to task...")
2023-12-21 22:17:40 +08:00
isTaskCompleted = handleTask(task, pass)
// 5.通过task的actionId去actionId-[]*task的map查出本批次对该字段的动作链更新其中关于自己的状态
2024-02-27 01:05:14 +08:00
logger.Info("Recording the task result...")
2023-12-21 22:17:40 +08:00
if isTaskCompleted {
isActionCompleted, action = handleTaskAction(task, actionMap)
}
// 6.等待所有动作链元素均已抵达,判定本批次对该字段的操作是否成功
2024-02-27 01:05:14 +08:00
logger.Info("Recording the action result...")
2023-12-21 22:17:40 +08:00
if isActionCompleted {
if err = finalizeTask(action); err != nil {
logger.Error("finalizeTask fail: %v", err)
}
}
2024-02-27 01:05:14 +08:00
logger.Info("%vs result handled...", i)
2023-12-21 22:17:40 +08:00
}
return
}
func handleImageAudit(dataId string, result *imageaudit.ScanImageResponseBodyDataResults) (pass bool, err error) {
ctx := &gin.Context{}
imageaudit := &dbstruct.ImageAudit{
Id: goproto.String(dataId),
}
pass, err = copyScanResultInfo(imageaudit, result)
if err != nil {
logger.Error("Copy Scan Result Info fail: %v\n", err)
return
}
if err = _DefaultImageAudit.OpUpdate(ctx, &imageauditproto.OpUpdateReq{
ImageAudit: imageaudit,
}); err != nil {
logger.Error("_DefaultImageAudit OpUpdate fail: %v\n", err)
return
}
return
}
// 处理task若task已分片在task中记录本分片结果并累积已到达的分片数直到所有分片到达决定该任务是否成功
func handleTask(task *ImageAuditTaskControlBlock, pass bool) (isTaskCompleted bool) {
task.ImageAuditTask.AuditedMediaResults = append(task.ImageAuditTask.AuditedMediaResults, pass)
2023-12-21 22:17:40 +08:00
task.IsTaskPassed = task.IsTaskPassed && pass
task.AuditedFragmentsNum++
return task.AuditedFragmentsNum == int(util.DerefInt64(task.ImageAuditTask.FragmentsNum))
}
// 通过task的actionId查出action, 通知该task已完成
func handleTaskAction(task *ImageAuditTaskControlBlock, actionMap map[string]*ImageAuditAction) (isActionCompleted bool, action *ImageAuditAction) {
action = actionMap[task.ActionId]
action.AuditedTaskNum++
isActionCompleted = action.TaskNum == action.AuditedTaskNum
return
}
// 等待所有动作链元素均已抵达,判定本批次对该字段的操作是否成功
func finalizeTask(action *ImageAuditAction) (err error) {
2024-01-20 06:33:10 +08:00
lastValidTask, lastValidTaskIndex := action.TaskChain[0], 0 // 仅在动作链终态不成功时有意义,表示最后一个可以用以回退的失败任务和它的索引
passTaskIds := make([]string, 0) // 所有已审核成功的任务的taskId
expiredTaskIds := make([]string, 0) // 所有已失效任务的taskId失效任务是指那些前驱任务依然失败的失败任务或之后已有任务成功的失败任务这些任务已失去实际意义不可以用来回退
statuses := make([]int64, len(action.TaskChain)) // 所有任务的终态状态,成功,回退,或失效
isActionInPassedStatus := true // 当前动作链是否处于成功中
2023-12-21 22:17:40 +08:00
for i, task := range action.TaskChain {
if !task.IsTaskPassed { // 动作链检测到失败任务
if isActionInPassedStatus { // 若动作链处于成功状态,则这是目前为止,最后一个可以用于回退的任务
2024-01-20 06:33:10 +08:00
lastValidTask, lastValidTaskIndex = task, i
2023-12-21 22:17:40 +08:00
statuses[i] = consts.ImageAudit_Rollbacked
isActionInPassedStatus = false
} else { // 否则,若动作链处于失败状态,则该任务已失效
statuses[i] = consts.ImageAudit_Expired
expiredTaskIds = append(expiredTaskIds, util.DerefString(task.ImageAuditTask.Id))
}
} else { // 若动作链检测到成功任务,则重置动作链任务态,并将最后有效任务标志为失效(它已不再用于回退)
passTaskIds = append(passTaskIds, util.DerefString(task.ImageAuditTask.Id))
statuses[i] = consts.ImageAudit_Passed
isActionInPassedStatus = true
if statuses[lastValidTaskIndex] == consts.ImageAudit_Rollbacked {
statuses[lastValidTaskIndex] = consts.ImageAudit_Expired
expiredTaskIds = append(expiredTaskIds, util.DerefString(action.TaskChain[lastValidTaskIndex].ImageAuditTask.Id))
}
}
}
// 判定任务链终态,终态非成功,执行回退操作,记录到数据库中
if !action.IsPassed() {
2024-01-20 06:33:10 +08:00
if err = executeRollBack(lastValidTask.ImageAuditTask); err != nil {
2023-12-21 22:17:40 +08:00
return
}
}
// 更新所有任务状态
if err = updatePassedTasks(passTaskIds); err != nil {
return
}
if err = updateExpiredTasks(expiredTaskIds); err != nil {
return
}
logger.Info("action statuses : %v", statuses)
return
}
// 将图片审核结果copy到图片审核实体中
func copyScanResultInfo(imageaudit *dbstruct.ImageAudit, result *imageaudit.ScanImageResponseBodyDataResults) (pass bool, err error) {
pass = true
for _, subresult := range result.SubResults {
scene := ImageAuditScene(util.DerefString(subresult.Scene))
switch scene {
case Porn:
imageaudit.PornSceneSuggestion = subresult.Suggestion
imageaudit.PornSceneLabel = subresult.Label
imageaudit.PornSceneRate = goproto.Float64(float64(util.DerefFloat32(subresult.Rate)))
pass = pass && (util.StringsContains(PornPassLabels, util.DerefString(subresult.Label)))
case Terrorism:
imageaudit.TerrorismSceneSuggestion = subresult.Suggestion
imageaudit.TerrorismSceneLabel = subresult.Label
imageaudit.TerrorismSceneRate = goproto.Float64(float64(util.DerefFloat32(subresult.Rate)))
pass = pass && (util.StringsContains(TerrorismPassLabels, util.DerefString(subresult.Label)))
case Ad:
imageaudit.AdSceneSuggestion = subresult.Suggestion
imageaudit.AdSceneLabel = subresult.Label
imageaudit.AdSceneRate = goproto.Float64(float64(util.DerefFloat32(subresult.Rate)))
pass = pass && (util.StringsContains(AdPassLabels, util.DerefString(subresult.Label)))
case Live:
imageaudit.LiveSceneSuggestion = subresult.Suggestion
imageaudit.LiveSceneLabel = subresult.Label
imageaudit.LiveSceneRate = goproto.Float64(float64(util.DerefFloat32(subresult.Rate)))
pass = pass && (util.StringsContains(LivePassLabels, util.DerefString(subresult.Label)))
case Logo:
imageaudit.LogoSceneSuggestion = subresult.Suggestion
imageaudit.LogoSceneLabel = subresult.Label
imageaudit.LogoSceneRate = goproto.Float64(float64(util.DerefFloat32(subresult.Rate)))
pass = pass && (util.StringsContains(LogoPassLabels, util.DerefString(subresult.Label)))
}
}
if pass {
imageaudit.Status = goproto.Int64(consts.ImageAudit_Passed)
} else {
imageaudit.Status = goproto.Int64(consts.ImageAudit_Rejected)
var bytes []byte
bytes, err = json.Marshal(result)
if err != nil {
return
}
imageaudit.Details = &bytes
}
return
}
2024-01-20 06:33:10 +08:00
func executeRollBack(lastValidTask *dbstruct.ImageAuditTask) (err error) {
2023-12-21 22:17:40 +08:00
ctx := &gin.Context{}
2024-01-20 06:33:10 +08:00
if err = _DefaultResultHandler.Handle(ctx, lastValidTask, consts.ImageAuditTaskUpdate_Rollback); err != nil {
logger.Error("Roll back taskId:%v fail:%v", util.DerefString(lastValidTask.Id), err)
2023-12-21 22:17:40 +08:00
if err = _DefaultImageAuditTask.OpUpdate(ctx, &imageaudittaskproto.OpUpdateReq{
ImageAuditTask: &dbstruct.ImageAuditTask{
2024-01-20 06:33:10 +08:00
Id: lastValidTask.Id,
2023-12-21 22:17:40 +08:00
Status: goproto.Int64(consts.ImageAudit_Failed),
Remarks: goproto.String("任务审核失败,回退失败,请联系管理员排查"),
},
}); err != nil {
logger.Error("_DefaultImageAudit OpUpdate fail: %v\n", err)
}
return
}
if err = _DefaultImageAuditTask.OpUpdate(ctx, &imageaudittaskproto.OpUpdateReq{
ImageAuditTask: &dbstruct.ImageAuditTask{
2024-01-20 06:33:10 +08:00
Id: lastValidTask.Id,
2023-12-21 22:17:40 +08:00
Status: goproto.Int64(consts.ImageAudit_Rollbacked),
Remarks: goproto.String("任务审核失败,已将字段回退至之前的版本"),
},
}); err != nil {
logger.Error("_DefaultImageAudit OpUpdate fail: %v\n", err)
}
return
}
func updatePassedTasks(passTaskIds []string) (err error) {
ctx := &gin.Context{}
if err = _DefaultImageAuditTask.OpUpdateByIds(ctx, &imageaudittaskproto.OpUpdateByIdsReq{
ImageAuditTask: &dbstruct.ImageAuditTask{
Status: goproto.Int64(consts.ImageAudit_Passed),
Remarks: goproto.String("任务审核通过"),
},
Ids: passTaskIds,
}); err != nil {
logger.Error("_DefaultImageAudit OpUpdateByIds fail: %v\n", err)
return
}
return
}
func updateExpiredTasks(expiredTaskIds []string) (err error) {
ctx := &gin.Context{}
if err = _DefaultImageAuditTask.OpUpdateByIds(ctx, &imageaudittaskproto.OpUpdateByIdsReq{
ImageAuditTask: &dbstruct.ImageAuditTask{
Status: goproto.Int64(consts.ImageAudit_Expired),
Remarks: goproto.String("该任务审核未通过,但之前已有对同字段的审核任务失败,或之后有对同字段的审核任务成功,该任务已失效"),
},
Ids: expiredTaskIds,
}); err != nil {
logger.Error("_DefaultImageAudit OpUpdateByIds fail: %v\n", err)
return
}
return
}
2024-01-20 06:33:10 +08:00
func handleBatchError(ctrlBlock *ImageAuditTaskBatchControlBlock, _err error) (err error) {
logger.Info("All tasks of this batchId: %v has failed, rolling back...", ctrlBlock.BatchId)
ctx := &gin.Context{}
2024-01-20 06:33:10 +08:00
if err = _DefaultImageAudit.OpUpdateByBatchId(ctx, ctrlBlock.BatchId, &dbstruct.ImageAudit{
Status: goproto.Int64(consts.ImageAudit_ServiceFailed),
Remarks: goproto.String("批次任务失败原因详见task表"),
}); err != nil {
logger.Error("_DefaultImageAudit OpUpdateByBatchId fail: %v\n", err)
return
}
2024-01-20 06:33:10 +08:00
if err = _DefaultImageAuditTask.OpUpdateByBatchId(ctx, ctrlBlock.BatchId, &dbstruct.ImageAuditTask{
Status: goproto.Int64(consts.ImageAudit_ServiceFailed),
Remarks: goproto.String(_err.Error()),
}); err != nil {
logger.Error("_DefaultImageAuditTask OpUpdateByBatchId fail: %v\n", err)
return
}
2023-12-28 22:46:14 +08:00
// 回退
2024-01-20 06:33:10 +08:00
for _, taskCtrlBlock := range ctrlBlock.TaskCtrlBlocks {
task := taskCtrlBlock.ImageAuditTask
if err = _DefaultResultHandler.Handle(ctx, task, consts.ImageAuditTaskUpdate_Rollback); err != nil {
2023-12-28 22:46:14 +08:00
if err = _DefaultImageAuditTask.OpUpdate(ctx, &imageaudittaskproto.OpUpdateReq{
ImageAuditTask: &dbstruct.ImageAuditTask{
2024-01-20 06:33:10 +08:00
Id: task.Id,
2023-12-28 22:46:14 +08:00
Status: goproto.Int64(consts.ImageAudit_Failed),
Remarks: goproto.String("任务审核失败,回退失败,请联系管理员排查"),
},
}); err != nil {
logger.Error("_DefaultImageAudit OpUpdate fail: %v\n", err)
}
}
}
return
}