From c387f5d17ba26ee526be0d275a7c8778f54af8ef Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Wed, 1 Nov 2023 11:22:29 +0800 Subject: [PATCH] fix --- model/gmodel/fs_cloud_render_log_gen.go | 6 +- model/gmodel/var_gen.go | 6 + .../websocket/internal/logic/getstatlogic.go | 13 +- .../internal/logic/ws_ok_response.go | 4 + .../websocket/internal/logic/ws_statistics.go | 139 ++++++++++++++---- server/websocket/internal/types/types.go | 7 +- server_api/websocket.api | 7 +- 7 files changed, 138 insertions(+), 44 deletions(-) diff --git a/model/gmodel/fs_cloud_render_log_gen.go b/model/gmodel/fs_cloud_render_log_gen.go index 04a8a495..1f8f516d 100644 --- a/model/gmodel/fs_cloud_render_log_gen.go +++ b/model/gmodel/fs_cloud_render_log_gen.go @@ -8,12 +8,8 @@ import ( type FsCloudRenderLog struct { Id int64 `gorm:"primary_key;default:0;auto_increment;" json:"id"` // ID UserId *int64 `gorm:"default:0;" json:"user_id"` // 用户id + GuestId *int64 `gorm:"default:0;" json:"guest_id"` // 游客id PostData *string `gorm:"default:'';" json:"post_data"` // - PostUrl *string `gorm:"default:'';" json:"post_url"` // - Title *string `gorm:"index;default:'';" json:"title"` // - Time *int64 `gorm:"default:0;" json:"time"` // 所用时间 - Result *string `gorm:"default:'';" json:"result"` // - Tag *string `gorm:"index;default:'';" json:"tag"` // 标识 Ctime *int64 `gorm:"default:0;" json:"ctime"` // 添加时间 } type FsCloudRenderLogModel struct { diff --git a/model/gmodel/var_gen.go b/model/gmodel/var_gen.go index 91fb2139..c5d4df3b 100644 --- a/model/gmodel/var_gen.go +++ b/model/gmodel/var_gen.go @@ -30,6 +30,7 @@ type AllModelsGen struct { FsCloudPickUpOld *FsCloudPickUpOldModel // fs_cloud_pick_up_old 云仓提货单 FsCloudReceiveEveryOld *FsCloudReceiveEveryOldModel // fs_cloud_receive_every_old FsCloudReceiveOld *FsCloudReceiveOldModel // fs_cloud_receive_old 云仓接收工厂总单 + FsCloudRenderLog *FsCloudRenderLogModel // fs_cloud_render_log 云渲染日志表 FsCloudRenderLogOld *FsCloudRenderLogOldModel // fs_cloud_render_log_old 云渲染日志表 FsCloudStorage *FsCloudStorageModel // fs_cloud_storage 仓库的基本信息, 只做映射 FsCloudStorageStock *FsCloudStorageStockModel // fs_cloud_storage_stock @@ -59,6 +60,7 @@ type AllModelsGen struct { FsMigration *FsMigrationModel // fs_migration 版本库 FsOrder *FsOrderModel // fs_order 订单表 FsOrderAffiliateOld *FsOrderAffiliateOldModel // fs_order_affiliate_old 订单附属表-流程控制时间等 + FsOrderCombineImageRecord *FsOrderCombineImageRecordModel // fs_order_combine_image_record 刀版图记录表 FsOrderDetailOld *FsOrderDetailOldModel // fs_order_detail_old 订单详细表 FsOrderDetailTemplateOld *FsOrderDetailTemplateOldModel // fs_order_detail_template_old 订单模板详细表 FsOrderOld *FsOrderOldModel // fs_order_old @@ -114,6 +116,7 @@ type AllModelsGen struct { FsUserMaterial *FsUserMaterialModel // fs_user_material 用户素材表 FsUserStock *FsUserStockModel // fs_user_stock 用户云仓库存 FsWebSet *FsWebSetModel // fs_web_set 网站配置表 + FsZipCode *FsZipCodeModel // fs_zip_code 邮编表 } @@ -145,6 +148,7 @@ func NewAllModels(gdb *gorm.DB) *AllModelsGen { FsCloudPickUpOld: NewFsCloudPickUpOldModel(gdb), FsCloudReceiveEveryOld: NewFsCloudReceiveEveryOldModel(gdb), FsCloudReceiveOld: NewFsCloudReceiveOldModel(gdb), + FsCloudRenderLog: NewFsCloudRenderLogModel(gdb), FsCloudRenderLogOld: NewFsCloudRenderLogOldModel(gdb), FsCloudStorage: NewFsCloudStorageModel(gdb), FsCloudStorageStock: NewFsCloudStorageStockModel(gdb), @@ -174,6 +178,7 @@ func NewAllModels(gdb *gorm.DB) *AllModelsGen { FsMigration: NewFsMigrationModel(gdb), FsOrder: NewFsOrderModel(gdb), FsOrderAffiliateOld: NewFsOrderAffiliateOldModel(gdb), + FsOrderCombineImageRecord: NewFsOrderCombineImageRecordModel(gdb), FsOrderDetailOld: NewFsOrderDetailOldModel(gdb), FsOrderDetailTemplateOld: NewFsOrderDetailTemplateOldModel(gdb), FsOrderOld: NewFsOrderOldModel(gdb), @@ -229,6 +234,7 @@ func NewAllModels(gdb *gorm.DB) *AllModelsGen { FsUserMaterial: NewFsUserMaterialModel(gdb), FsUserStock: NewFsUserStockModel(gdb), FsWebSet: NewFsWebSetModel(gdb), + FsZipCode: NewFsZipCodeModel(gdb), } return models } diff --git a/server/websocket/internal/logic/getstatlogic.go b/server/websocket/internal/logic/getstatlogic.go index c0d557aa..68c2aa9e 100644 --- a/server/websocket/internal/logic/getstatlogic.go +++ b/server/websocket/internal/logic/getstatlogic.go @@ -39,7 +39,8 @@ func (l *GetStatLogic) GetStat(req *types.GetStatReq, userinfo *auth.UserInfo) ( defer GetStatMutex.Unlock() userStat := make(map[string]interface{}) currentWebsocketConnectCount := 0 - currentRequestCombineApiCount := 0 + currentCombineApiCount := 0 + currentUnityHandleCount := 0 mapUserWsStat.Range(func(key, value any) bool { statData, ok := mapUserWsStat.Load(key) if ok { //存在就累加 @@ -49,7 +50,8 @@ func (l *GetStatLogic) GetStat(req *types.GetStatReq, userinfo *auth.UserInfo) ( return true } currentWebsocketConnectCount += stat.CurWsConnectCount - currentRequestCombineApiCount += stat.CurCombineCount + currentCombineApiCount += stat.CurCombineCount + currentUnityHandleCount += stat.CurUnityHandleCount } else { logx.Error("断言mapUserWsStatItem错误") } @@ -58,9 +60,10 @@ func (l *GetStatLogic) GetStat(req *types.GetStatReq, userinfo *auth.UserInfo) ( return true }) return resp.SetStatusWithMessage(basic.CodeOK, "success", types.GetStatRsp{ - WsTotalCount: currentWebsocketConnectCount, - CurRequestCombineCount: currentRequestCombineApiCount, - UserWsStat: userStat, + WsTotalCount: currentWebsocketConnectCount, + CurCombineCount: currentCombineApiCount, + CurUnityHandleCount: currentUnityHandleCount, + UserWsStat: userStat, }) } diff --git a/server/websocket/internal/logic/ws_ok_response.go b/server/websocket/internal/logic/ws_ok_response.go index 966e29ac..78e2297b 100644 --- a/server/websocket/internal/logic/ws_ok_response.go +++ b/server/websocket/internal/logic/ws_ok_response.go @@ -49,6 +49,8 @@ func (w *wsConnectItem) sendAssembleRenderDataStepResponseMessage(requestId stri // 发送组装数据到unity完毕阶段通知消息 func (w *wsConnectItem) sendRenderDataToUnityStepResponseMessage(requestId string) { + //统计unity处理数 + increaseUnityRequestCount(w.userId, w.guestId) if w.debug == nil { return } @@ -57,6 +59,8 @@ func (w *wsConnectItem) sendRenderDataToUnityStepResponseMessage(requestId strin // 发送渲染最终结果数据到前端 func (w *wsConnectItem) sendRenderResultData(data websocket_data.RenderImageRspMsg) { + //统计unity处理数 + decreaseUnityRequestCount(w.userId, w.guestId) //没开启debug if w.debug == nil { data.RenderProcessTime = websocket_data.RenderProcessTime{} diff --git a/server/websocket/internal/logic/ws_statistics.go b/server/websocket/internal/logic/ws_statistics.go index 375c80ea..a0b0307a 100644 --- a/server/websocket/internal/logic/ws_statistics.go +++ b/server/websocket/internal/logic/ws_statistics.go @@ -5,24 +5,28 @@ import ( "fmt" "github.com/zeromicro/go-zero/core/logx" "sync" + "time" ) type websocketStatType string const ( - TYPE_CONNECT_COUNT websocketStatType = "connect_count" //ws连接数 - TYPE_COMBINE_IMAGE_REQUEST_COUNT websocketStatType = "combine_count" + TYPE_CUR_CONNECT_COUNT websocketStatType = "TYPE_CUR_CONNECT_COUNT" //ws连接数 + TYPE_CUR_COMBINE_IMAGE_COUNT websocketStatType = "TYPE_CUR_COMBINE_IMAGE_COUNT" //合图数 + TYPE_CUR_UNITY_HANDLE_COUNT websocketStatType = "TYPE_CUR_UNITY_HANDLE_COUNT" //unity处理数 ) type websocketStatItem struct { - UserId int64 `json:"user_id"` - GuestId int64 `json:"guest_id"` - Type websocketStatType `json:"type"` //类型 - Value int `json:"value"` //数值 + UserId int64 `json:"user_id"` + GuestId int64 `json:"guest_id"` + Type websocketStatType `json:"type"` //类型 + Value int `json:"value"` //数值 + UnityRenderLogId int64 `json:"unity_render_log_id"` //unity渲染的日志id } type mapUserWsStatItem struct { - CurCombineCount int `json:"cur_combine_count"` //当前合图数 - CurWsConnectCount int `json:"cur_ws_connect_count"` //当前连接数 + CurCombineCount int `json:"cur_combine_count"` //当前合图数 + CurWsConnectCount int `json:"cur_ws_connect_count"` //当前连接数 + CurUnityHandleCount int `json:"cur_unity_handle_count"` //当前unity处理数 } // 统计信息 @@ -30,47 +34,109 @@ var ( //用户连接统计 mapUserWsStat = sync.Map{} //添加or减少连接的控制chan - websocketStat = make(chan websocketStatItem, 20) + websocketStat = make(chan websocketStatItem, 1000) ) // 累增ws连接数计数 func increaseWebsocketConnectCount(userId, guestId int64) { - websocketStat <- websocketStatItem{ + data := websocketStatItem{ UserId: userId, GuestId: guestId, - Type: TYPE_CONNECT_COUNT, + Type: TYPE_CUR_CONNECT_COUNT, Value: 1, } + select { + case websocketStat <- data: + return + case <-time.After(time.Millisecond * 200): + logx.Error("increaseWebsocketConnectCount 输入管道超时,丢弃消息") + return + } } // 减少ws连接数计数 func decreaseWebsocketConnectCount(userId, guestId int64) { - websocketStat <- websocketStatItem{ + data := websocketStatItem{ UserId: userId, GuestId: guestId, - Type: TYPE_CONNECT_COUNT, + Type: TYPE_CUR_CONNECT_COUNT, Value: -1, } + select { + case websocketStat <- data: + return + case <-time.After(time.Millisecond * 200): + logx.Error("decreaseWebsocketConnectCount 输入管道超时,丢弃消息") + return + } } // 累增合图请求数计数 func increaseCombineRequestCount(userId, guestId int64) { - websocketStat <- websocketStatItem{ + data := websocketStatItem{ UserId: userId, GuestId: guestId, - Type: TYPE_COMBINE_IMAGE_REQUEST_COUNT, + Type: TYPE_CUR_COMBINE_IMAGE_COUNT, Value: 1, } + select { + case websocketStat <- data: + return + case <-time.After(time.Millisecond * 200): + logx.Error("increaseCombineRequestCount 输入管道超时,丢弃消息") + return + } } // 减少合图请求数计数 func decreaseCombineRequestCount(userId, guestId int64) { - websocketStat <- websocketStatItem{ + data := websocketStatItem{ UserId: userId, GuestId: guestId, - Type: TYPE_COMBINE_IMAGE_REQUEST_COUNT, + Type: TYPE_CUR_COMBINE_IMAGE_COUNT, Value: -1, } + select { + case websocketStat <- data: + return + case <-time.After(time.Millisecond * 200): + logx.Error("decreaseCombineRequestCount 输入管道超时,丢弃消息") + return + } +} + +// 累增unity请求数计数 +func increaseUnityRequestCount(userId, guestId int64) { + data := websocketStatItem{ + UserId: userId, + GuestId: guestId, + Type: TYPE_CUR_UNITY_HANDLE_COUNT, + Value: 1, + } + select { + case websocketStat <- data: + return + case <-time.After(time.Millisecond * 200): + logx.Error("decreaseCombineRequestCount 输入管道超时,丢弃消息") + return + } +} + +// 减少unity请求数计数 +func decreaseUnityRequestCount(userId, guestId int64) { + data := websocketStatItem{ + UserId: userId, + GuestId: guestId, + Type: TYPE_CUR_UNITY_HANDLE_COUNT, + Value: -1, + } + select { + case websocketStat <- data: + return + case <-time.After(time.Millisecond * 200): + logx.Error("decreaseUnityRequestCount 输入管道超时,丢弃消息") + return + } } // 消费数据 @@ -90,9 +156,9 @@ func ConsumeWebsocketStatData(ctx context.Context) { select { case data := <-websocketStat: key := fmt.Sprintf("%d_%d", data.UserId, data.GuestId) + statData, ok := mapUserWsStat.Load(key) switch data.Type { - case TYPE_CONNECT_COUNT: //ws连接计数 - statData, ok := mapUserWsStat.Load(key) + case TYPE_CUR_CONNECT_COUNT: //ws连接计数 if !ok { mapUserWsStat.Store(key, mapUserWsStatItem{ CurWsConnectCount: data.Value, @@ -112,22 +178,39 @@ func ConsumeWebsocketStatData(ctx context.Context) { } //保存统计 mapUserWsStat.Store(key, stat) - case TYPE_COMBINE_IMAGE_REQUEST_COUNT: //请求算法合图计数 - statData, ok := mapUserWsStat.Load(key) + case TYPE_CUR_COMBINE_IMAGE_COUNT: //请求算法合图计数 //不存在 if !ok { continue } //存在 - if stat, ok := statData.(mapUserWsStatItem); ok { - stat.CurCombineCount += data.Value - if stat.CurCombineCount < 0 { - stat.CurCombineCount = 0 - } - mapUserWsStat.Store(key, stat) - } else { + stat, ok := statData.(mapUserWsStatItem) + if !ok { logx.Error("断言mapUserWsStatItem错误") + continue } + stat.CurCombineCount += data.Value + if stat.CurCombineCount < 0 { + stat.CurCombineCount = 0 + } + //保存统计 + mapUserWsStat.Store(key, stat) + case TYPE_CUR_UNITY_HANDLE_COUNT: //unity处理计数 + if !ok { + continue + } + //存在 + stat, ok := statData.(mapUserWsStatItem) + if !ok { + logx.Error("断言mapUserWsStatItem错误") + continue + } + stat.CurUnityHandleCount += data.Value + if stat.CurUnityHandleCount <= 0 { + stat.CurUnityHandleCount = 0 + } + //保存统计 + mapUserWsStat.Store(key, stat) } } } diff --git a/server/websocket/internal/types/types.go b/server/websocket/internal/types/types.go index e01e0b13..bbfaa7a4 100644 --- a/server/websocket/internal/types/types.go +++ b/server/websocket/internal/types/types.go @@ -34,9 +34,10 @@ type GetStatReq struct { } type GetStatRsp struct { - WsTotalCount int `json:"ws_total_count"` //ws连接总数 - CurRequestCombineCount int `json:"cur_request_combine_count"` //合图任务数 - UserWsStat interface{} `json:"user_ws_stat"` //用户连接统计 + WsTotalCount int `json:"ws_total_count"` //ws连接总数 + CurCombineCount int `json:"cur_combine_count"` //合图任务数 + CurUnityHandleCount int `json:"cur_unity_handle_count"` //当前unity请求总数 + UserWsStat interface{} `json:"user_ws_stat"` //用户连接统计 } type Request struct { diff --git a/server_api/websocket.api b/server_api/websocket.api index fa4056e1..2e2ccc7d 100644 --- a/server_api/websocket.api +++ b/server_api/websocket.api @@ -55,7 +55,8 @@ type GetStatReq { Password string `form:"password"` } type GetStatRsp { - WsTotalCount int `json:"ws_total_count"` //ws连接总数 - CurRequestCombineCount int `json:"cur_request_combine_count"` //合图任务数 - UserWsStat interface{} `json:"user_ws_stat"` //用户连接统计 + WsTotalCount int `json:"ws_total_count"` //ws连接总数 + CurCombineCount int `json:"cur_combine_count"` //合图任务数 + CurUnityHandleCount int `json:"cur_unity_handle_count"` //当前unity请求总数 + UserWsStat interface{} `json:"user_ws_stat"` //用户连接统计 } \ No newline at end of file