This commit is contained in:
laodaming 2023-08-21 16:19:30 +08:00
parent 7eda3cdd51
commit f8bb4dff17
4 changed files with 104 additions and 21 deletions

View File

@ -136,7 +136,7 @@ func (l *DataTransferLogic) setConnPool(conn *websocket.Conn, userInfo auth.User
userId: userInfo.UserId, userId: userInfo.UserId,
guestId: userInfo.GuestId, guestId: userInfo.GuestId,
renderProperty: renderProperty{ renderProperty: renderProperty{
renderImageTask: make(map[string]string), renderImageTask: make(map[string]*renderTask),
renderImageTaskCtlChan: make(chan renderImageControlChanItem, 100), renderImageTaskCtlChan: make(chan renderImageControlChanItem, 100),
renderChan: make(chan []byte, 100), renderChan: make(chan []byte, 100),
}, },

View File

@ -2,13 +2,12 @@ package logic
import ( import (
"context" "context"
"fusenapi/server/websocket/internal/svc"
"fusenapi/server/websocket/internal/types"
"fusenapi/utils/auth" "fusenapi/utils/auth"
"fusenapi/utils/basic" "fusenapi/utils/basic"
"fusenapi/utils/file" "fusenapi/utils/file"
"os" "time"
"fusenapi/server/websocket/internal/svc"
"fusenapi/server/websocket/internal/types"
"github.com/zeromicro/go-zero/core/logx" "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") logx.Error("渲染回调参数错误invalid param image")
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "invalid param image") return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "invalid param image")
} }
f, _ := os.Create("b.txt") //存base64打印测试
defer f.Close() /* f, _ := os.Create("b.txt")
f.WriteString(req.Image) defer f.Close()
f.WriteString(req.Image)*/
// 上传文件 // 上传文件
var upload = file.Upload{ var upload = file.Upload{
Ctx: l.ctx, Ctx: l.ctx,
@ -77,6 +77,13 @@ func (l *RenderNotifyLogic) RenderNotify(req *types.RenderNotifyReq, userinfo *a
logx.Error("渲染回调断言websocket连接失败") logx.Error("渲染回调断言websocket连接失败")
return true return true
} }
//记录收到unity渲染结果时间
ws.modifyRenderTaskTimeConsuming(renderImageControlChanItem{
Option: 2,
TaskProperty: renderTask{
UnityRenderEndTime: time.Now().UTC().Unix(),
},
})
//发送处理并删除任务 //发送处理并删除任务
ws.deleteRenderTask(renderImageControlChanItem{ ws.deleteRenderTask(renderImageControlChanItem{
Option: 0, //0删除 1添加 Option: 0, //0删除 1添加

View File

@ -4,6 +4,7 @@ import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"errors" "errors"
"fmt"
"fusenapi/constants" "fusenapi/constants"
"fusenapi/service/repositories" "fusenapi/service/repositories"
"fusenapi/utils/curl" "fusenapi/utils/curl"
@ -17,17 +18,25 @@ import (
// 云渲染属性 // 云渲染属性
type renderProperty struct { type renderProperty struct {
renderImageTask map[string]string //需要渲染的图片任务 key是taskId val 是renderId renderImageTask map[string]*renderTask //需要渲染的图片任务 key是taskId val 是renderId
renderImageTaskCtlChan chan renderImageControlChanItem //渲染任务新增移除的控制通道 renderImageTaskCtlChan chan renderImageControlChanItem //渲染任务新增移除的控制通道
renderChan chan []byte //渲染的缓冲队列 renderChan chan []byte //渲染的缓冲队列
} }
type renderTask struct {
RenderId string //渲染id(前端传的)
CombineBeginTime int64 //合图开始时间
CombineEndTime int64 //合图结束时间
UnityRenderBeginTime int64 //发送给unity时间
UnityRenderEndTime int64 //unity回调结果时间
}
// 渲染任务新增移除的控制通道的数据 // 渲染任务新增移除的控制通道的数据
type renderImageControlChanItem struct { type renderImageControlChanItem struct {
Option int // 0删除 1添加 Option int // 0删除 1添加
TaskId string //map的key TaskId string //map的key
RenderId string // map的val(增加任务时候传) RenderId string // map的val(增加任务时候传)
RenderNotifyImageUrl string //渲染回调数据(删除任务时候传) RenderNotifyImageUrl string //渲染回调数据(删除任务时候传)
TaskProperty renderTask //渲染任务的属性
} }
// 发送到渲染缓冲池 // 发送到渲染缓冲池
@ -129,8 +138,10 @@ func (w *wsConnectItem) consumeRenderCache(data []byte) {
} else { } else {
//返回给客户端 //返回给客户端
b := w.respondDataFormat(constants.WEBSOCKET_RENDER_IMAGE, websocket_data.RenderImageRspMsg{ b := w.respondDataFormat(constants.WEBSOCKET_RENDER_IMAGE, websocket_data.RenderImageRspMsg{
RenderId: renderImageData.RenderId, RenderId: renderImageData.RenderId,
Image: *resource.ResourceUrl, Image: *resource.ResourceUrl,
CombineTakesTime: "耗时0秒(缓存)",
UnityRenderTakesTime: "耗时0秒(缓存)",
}) })
//发送数据到out chan //发送数据到out chan
w.sendToOutChan(b) 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) logx.Error("failed to get template info:", err)
return err return err
} }
//记录刀版图合成开始时间
w.modifyRenderTaskTimeConsuming(renderImageControlChanItem{
Option: 2,
TaskProperty: renderTask{
CombineBeginTime: time.Now().UTC().Unix(),
},
})
//获取刀版图 //获取刀版图
combineReq := repositories.LogoCombineReq{ combineReq := repositories.LogoCombineReq{
UserId: info.RenderData.UserId, UserId: info.RenderData.UserId,
@ -190,6 +208,13 @@ func (w *wsConnectItem) assembleRenderData(taskId string, info websocket_data.Re
logx.Error("合成刀版图失败,合成的刀版图是空指针:", err) logx.Error("合成刀版图失败,合成的刀版图是空指针:", err)
return err return err
} }
//记录刀版图合成结束时间
w.modifyRenderTaskTimeConsuming(renderImageControlChanItem{
Option: 2,
TaskProperty: renderTask{
CombineEndTime: time.Now().UTC().Unix(),
},
})
logx.Info("合成刀版图成功,合成刀版图数据:", combineReq, ",logo图片:", info.RenderData.Logo, " 刀版图:", *res.ResourceUrl) logx.Info("合成刀版图成功,合成刀版图数据:", combineReq, ",logo图片:", info.RenderData.Logo, " 刀版图:", *res.ResourceUrl)
//获取渲染设置信息 //获取渲染设置信息
element, err := w.logic.svcCtx.AllModels.FsProductTemplateElement.FindOneByModelId(w.logic.ctx, *productTemplate.ModelId) 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") logx.Error("failed to send data to unity")
return err return err
} }
//记录发送到unity时间
w.modifyRenderTaskTimeConsuming(renderImageControlChanItem{
Option: 2,
TaskProperty: renderTask{
UnityRenderBeginTime: time.Now().UTC().Unix(),
},
})
logx.Info("发送到unity成功,刀版图:", combineImage, " 请求unity的数据:", string(postDataBytes)) logx.Info("发送到unity成功,刀版图:", combineImage, " 请求unity的数据:", string(postDataBytes))
return nil 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里面串行执行 // 操作连接中渲染任务的增加/删除任务map不能读写并发所以放在chan里面串行执行
func (w *wsConnectItem) operationRenderTask() { func (w *wsConnectItem) operationRenderTask() {
for { for {
@ -331,15 +377,43 @@ func (w *wsConnectItem) operationRenderTask() {
switch data.Option { switch data.Option {
case 0: //渲染结果回调,删除任务 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{ w.sendToOutChan(w.respondDataFormat(constants.WEBSOCKET_RENDER_IMAGE, websocket_data.RenderImageRspMsg{
RenderId: renderId, RenderId: taskData.RenderId,
Image: data.RenderNotifyImageUrl, Image: data.RenderNotifyImageUrl,
CombineTakesTime: CombineTakesTime,
UnityRenderTakesTime: UnityRenderTakesTime,
})) }))
} }
delete(w.renderProperty.renderImageTask, data.TaskId) delete(w.renderProperty.renderImageTask, data.TaskId)
case 1: //新增任务 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
}
}
} }
} }
} }

View File

@ -25,8 +25,10 @@ type RenderData struct {
// websocket发送渲染完的数据 // websocket发送渲染完的数据
type RenderImageRspMsg struct { type RenderImageRspMsg struct {
RenderId string `json:"render_id"` //渲染id RenderId string `json:"render_id"` //渲染id
Image string `json:"image"` //渲染结果图片 Image string `json:"image"` //渲染结果图片
CombineTakesTime string `json:"combine_takes_time"` //合图需要时间
UnityRenderTakesTime string `json:"unity_render_takes_time"` //unity渲染用时
} }
type ThirdPartyLoginRspMsg struct { type ThirdPartyLoginRspMsg struct {
//websocket三方登录的通知数据 //websocket三方登录的通知数据