diff --git a/server/websocket/internal/logic/datatransferlogic.go b/server/websocket/internal/logic/datatransferlogic.go index b5929a92..76ee817d 100644 --- a/server/websocket/internal/logic/datatransferlogic.go +++ b/server/websocket/internal/logic/datatransferlogic.go @@ -136,7 +136,7 @@ func (l *DataTransferLogic) setConnPool(conn *websocket.Conn, userInfo auth.User userId: userInfo.UserId, guestId: userInfo.GuestId, renderProperty: renderProperty{ - renderImageTask: make(map[string]string), + renderImageTask: make(map[string]*renderTask), renderImageTaskCtlChan: make(chan renderImageControlChanItem, 100), renderChan: make(chan []byte, 100), }, diff --git a/server/websocket/internal/logic/rendernotifylogic.go b/server/websocket/internal/logic/rendernotifylogic.go index 038a7515..95472578 100644 --- a/server/websocket/internal/logic/rendernotifylogic.go +++ b/server/websocket/internal/logic/rendernotifylogic.go @@ -2,13 +2,12 @@ package logic import ( "context" + "fusenapi/server/websocket/internal/svc" + "fusenapi/server/websocket/internal/types" "fusenapi/utils/auth" "fusenapi/utils/basic" "fusenapi/utils/file" - "os" - - "fusenapi/server/websocket/internal/svc" - "fusenapi/server/websocket/internal/types" + "time" "github.com/zeromicro/go-zero/core/logx" ) @@ -45,9 +44,10 @@ func (l *RenderNotifyLogic) RenderNotify(req *types.RenderNotifyReq, userinfo *a logx.Error("渲染回调参数错误:invalid param image") return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "invalid param image") } - f, _ := os.Create("b.txt") - defer f.Close() - f.WriteString(req.Image) + //存base64打印测试 + /* f, _ := os.Create("b.txt") + defer f.Close() + f.WriteString(req.Image)*/ // 上传文件 var upload = file.Upload{ Ctx: l.ctx, @@ -77,6 +77,13 @@ func (l *RenderNotifyLogic) RenderNotify(req *types.RenderNotifyReq, userinfo *a logx.Error("渲染回调断言websocket连接失败") return true } + //记录收到unity渲染结果时间 + ws.modifyRenderTaskTimeConsuming(renderImageControlChanItem{ + Option: 2, + TaskProperty: renderTask{ + UnityRenderEndTime: time.Now().UTC().Unix(), + }, + }) //发送处理并删除任务 ws.deleteRenderTask(renderImageControlChanItem{ Option: 0, //0删除 1添加 diff --git a/server/websocket/internal/logic/ws_render_image_logic.go b/server/websocket/internal/logic/ws_render_image_logic.go index f04a470e..969e403d 100644 --- a/server/websocket/internal/logic/ws_render_image_logic.go +++ b/server/websocket/internal/logic/ws_render_image_logic.go @@ -4,6 +4,7 @@ import ( "bytes" "encoding/json" "errors" + "fmt" "fusenapi/constants" "fusenapi/service/repositories" "fusenapi/utils/curl" @@ -17,17 +18,25 @@ import ( // 云渲染属性 type renderProperty struct { - renderImageTask map[string]string //需要渲染的图片任务 key是taskId val 是renderId + renderImageTask map[string]*renderTask //需要渲染的图片任务 key是taskId val 是renderId renderImageTaskCtlChan chan renderImageControlChanItem //渲染任务新增移除的控制通道 renderChan chan []byte //渲染的缓冲队列 } +type renderTask struct { + RenderId string //渲染id(前端传的) + CombineBeginTime int64 //合图开始时间 + CombineEndTime int64 //合图结束时间 + UnityRenderBeginTime int64 //发送给unity时间 + UnityRenderEndTime int64 //unity回调结果时间 +} // 渲染任务新增移除的控制通道的数据 type renderImageControlChanItem struct { - Option int // 0删除 1添加 - TaskId string //map的key - RenderId string // map的val(增加任务时候传) - RenderNotifyImageUrl string //渲染回调数据(删除任务时候传) + Option int // 0删除 1添加 + TaskId string //map的key + RenderId string // map的val(增加任务时候传) + RenderNotifyImageUrl string //渲染回调数据(删除任务时候传) + TaskProperty renderTask //渲染任务的属性 } // 发送到渲染缓冲池 @@ -129,8 +138,10 @@ func (w *wsConnectItem) consumeRenderCache(data []byte) { } else { //返回给客户端 b := w.respondDataFormat(constants.WEBSOCKET_RENDER_IMAGE, websocket_data.RenderImageRspMsg{ - RenderId: renderImageData.RenderId, - Image: *resource.ResourceUrl, + RenderId: renderImageData.RenderId, + Image: *resource.ResourceUrl, + CombineTakesTime: "耗时0秒(缓存)", + UnityRenderTakesTime: "耗时0秒(缓存)", }) //发送数据到out chan w.sendToOutChan(b) @@ -167,6 +178,13 @@ func (w *wsConnectItem) assembleRenderData(taskId string, info websocket_data.Re logx.Error("failed to get template info:", err) return err } + //记录刀版图合成开始时间 + w.modifyRenderTaskTimeConsuming(renderImageControlChanItem{ + Option: 2, + TaskProperty: renderTask{ + CombineBeginTime: time.Now().UTC().Unix(), + }, + }) //获取刀版图 combineReq := repositories.LogoCombineReq{ UserId: info.RenderData.UserId, @@ -190,6 +208,13 @@ func (w *wsConnectItem) assembleRenderData(taskId string, info websocket_data.Re logx.Error("合成刀版图失败,合成的刀版图是空指针:", err) return err } + //记录刀版图合成结束时间 + w.modifyRenderTaskTimeConsuming(renderImageControlChanItem{ + Option: 2, + TaskProperty: renderTask{ + CombineEndTime: time.Now().UTC().Unix(), + }, + }) logx.Info("合成刀版图成功,合成刀版图数据:", combineReq, ",logo图片:", info.RenderData.Logo, " 刀版图:", *res.ResourceUrl) //获取渲染设置信息 element, err := w.logic.svcCtx.AllModels.FsProductTemplateElement.FindOneByModelId(w.logic.ctx, *productTemplate.ModelId) @@ -289,6 +314,13 @@ func (w *wsConnectItem) assembleRenderData(taskId string, info websocket_data.Re logx.Error("failed to send data to unity") return err } + //记录发送到unity时间 + w.modifyRenderTaskTimeConsuming(renderImageControlChanItem{ + Option: 2, + TaskProperty: renderTask{ + UnityRenderBeginTime: time.Now().UTC().Unix(), + }, + }) logx.Info("发送到unity成功,刀版图:", combineImage, " 请求unity的数据:", string(postDataBytes)) return nil } @@ -321,6 +353,20 @@ func (w *wsConnectItem) deleteRenderTask(data renderImageControlChanItem) { } } +// 修改耗时属性(只有耗时属性可以更新) +func (w *wsConnectItem) modifyRenderTaskTimeConsuming(data renderImageControlChanItem) { + //强制设为修改耗时属性 + data.Option = 2 + select { + case <-w.closeChan: //关闭 + return + case w.renderProperty.renderImageTaskCtlChan <- data: + return + case <-time.After(time.Second * 3): + return + } +} + // 操作连接中渲染任务的增加/删除(任务map不能读写并发,所以放在chan里面串行执行) func (w *wsConnectItem) operationRenderTask() { for { @@ -331,15 +377,43 @@ func (w *wsConnectItem) operationRenderTask() { switch data.Option { case 0: //渲染结果回调,删除任务 //存在任务,则发送渲染结果给前端 - if renderId, ok := w.renderProperty.renderImageTask[data.TaskId]; ok { + if taskData, ok := w.renderProperty.renderImageTask[data.TaskId]; ok { + CombineTakesTime := "" + UnityRenderTakesTime := "" + if taskData.CombineBeginTime > 0 && taskData.CombineEndTime > 0 { + CombineTakesTime = fmt.Sprintf("耗时%d秒", taskData.CombineEndTime-taskData.CombineBeginTime) + } + if taskData.UnityRenderBeginTime > 0 && taskData.UnityRenderEndTime > 0 { + UnityRenderTakesTime = fmt.Sprintf("耗时%d秒", taskData.UnityRenderEndTime-taskData.UnityRenderBeginTime) + } + //发送到出口 w.sendToOutChan(w.respondDataFormat(constants.WEBSOCKET_RENDER_IMAGE, websocket_data.RenderImageRspMsg{ - RenderId: renderId, - Image: data.RenderNotifyImageUrl, + RenderId: taskData.RenderId, + Image: data.RenderNotifyImageUrl, + CombineTakesTime: CombineTakesTime, + UnityRenderTakesTime: UnityRenderTakesTime, })) } delete(w.renderProperty.renderImageTask, data.TaskId) case 1: //新增任务 - w.renderProperty.renderImageTask[data.TaskId] = data.RenderId + w.renderProperty.renderImageTask[data.TaskId] = &renderTask{ + RenderId: data.RenderId, + } + case 2: //修改(耗时)属性 + if taskData, ok := w.renderProperty.renderImageTask[data.TaskId]; ok { + if data.TaskProperty.CombineBeginTime != 0 { + taskData.CombineBeginTime = data.TaskProperty.CombineBeginTime + } + if data.TaskProperty.CombineEndTime != 0 { + taskData.CombineEndTime = data.TaskProperty.CombineEndTime + } + if data.TaskProperty.UnityRenderBeginTime != 0 { + taskData.UnityRenderBeginTime = data.TaskProperty.UnityRenderBeginTime + } + if data.TaskProperty.UnityRenderEndTime != 0 { + taskData.UnityRenderEndTime = data.TaskProperty.UnityRenderEndTime + } + } } } } diff --git a/utils/websocket_data/render_data.go b/utils/websocket_data/render_data.go index 0238bcfa..ca152ded 100644 --- a/utils/websocket_data/render_data.go +++ b/utils/websocket_data/render_data.go @@ -25,8 +25,10 @@ type RenderData struct { // websocket发送渲染完的数据 type RenderImageRspMsg struct { - RenderId string `json:"render_id"` //渲染id - Image string `json:"image"` //渲染结果图片 + RenderId string `json:"render_id"` //渲染id + Image string `json:"image"` //渲染结果图片 + CombineTakesTime string `json:"combine_takes_time"` //合图需要时间 + UnityRenderTakesTime string `json:"unity_render_takes_time"` //unity渲染用时 } type ThirdPartyLoginRspMsg struct { //websocket三方登录的通知数据