This commit is contained in:
laodaming 2023-11-01 14:35:37 +08:00
parent 91b45e3595
commit 347cfdeaef
5 changed files with 69 additions and 16 deletions

View File

@ -41,24 +41,39 @@ func (l *GetStatLogic) GetStat(req *types.GetStatReq, userinfo *auth.UserInfo) (
currentWebsocketConnectCount := 0 currentWebsocketConnectCount := 0
currentCombineApiCount := 0 currentCombineApiCount := 0
currentUnityHandleCount := 0 currentUnityHandleCount := 0
combineErrorCount := 0
lenUserList := len(req.UserKey)
switch lenUserList {
case 0: //遍历所有
mapUserWsStat.Range(func(key, value any) bool { mapUserWsStat.Range(func(key, value any) bool {
statData, ok := mapUserWsStat.Load(key) if stat, ok := value.(mapUserWsStatItem); ok {
if ok { //存在就累加
if stat, ok := statData.(mapUserWsStatItem); ok {
//连接数等于0的用户就不返回
if stat.CurWsConnectCount == 0 {
return true
}
currentWebsocketConnectCount += stat.CurWsConnectCount currentWebsocketConnectCount += stat.CurWsConnectCount
currentCombineApiCount += stat.CurCombineCount currentCombineApiCount += stat.CurCombineCount
currentUnityHandleCount += stat.CurUnityHandleCount currentUnityHandleCount += stat.CurUnityHandleCount
combineErrorCount += stat.CombineErrCount
} else { } else {
logx.Error("断言mapUserWsStatItem错误") logx.Error("断言mapUserWsStatItem错误")
} }
}
userStat[key.(string)] = value userStat[key.(string)] = value
return true return true
}) })
default: //有指定用户
for _, key := range req.UserKey {
value, ok := mapUserWsStat.Load(key)
if ok { //存在就累加
if stat, ok := value.(mapUserWsStatItem); ok {
currentWebsocketConnectCount += stat.CurWsConnectCount
currentCombineApiCount += stat.CurCombineCount
currentUnityHandleCount += stat.CurUnityHandleCount
combineErrorCount += stat.CombineErrCount
} else {
logx.Error("断言mapUserWsStatItem错误")
}
}
userStat[key] = mapUserWsStatItem{}
}
}
return resp.SetStatusWithMessage(basic.CodeOK, "success", types.GetStatRsp{ return resp.SetStatusWithMessage(basic.CodeOK, "success", types.GetStatRsp{
WsTotalCount: currentWebsocketConnectCount, WsTotalCount: currentWebsocketConnectCount,
CurCombineCount: currentCombineApiCount, CurCombineCount: currentCombineApiCount,

View File

@ -235,6 +235,8 @@ func (w *wsConnectItem) renderImage(renderImageData websocket_data.RenderImageRe
res, err := w.logic.svcCtx.Repositories.ImageHandle.LogoCombine(w.logic.ctx, &combineReq) res, err := w.logic.svcCtx.Repositories.ImageHandle.LogoCombine(w.logic.ctx, &combineReq)
if err != nil { if err != nil {
w.renderErrResponse(renderImageData.RequestId, renderImageData.RenderData.TemplateTag, "", "合成刀版图失败:"+err.Error(), renderImageData.RenderData.ProductId, w.userId, w.guestId, productTemplate.Id, model3dInfo.Id, productSize.Id, *productTemplate.ElementModelId) w.renderErrResponse(renderImageData.RequestId, renderImageData.RenderData.TemplateTag, "", "合成刀版图失败:"+err.Error(), renderImageData.RenderData.ProductId, w.userId, w.guestId, productTemplate.Id, model3dInfo.Id, productSize.Id, *productTemplate.ElementModelId)
//统计合图失败数
increaseCombineRequestErrorCount(w.userId, w.guestId)
logx.Error("合成刀版图失败,合成请求数据:", combineReq, "错误信息:", err) logx.Error("合成刀版图失败,合成请求数据:", combineReq, "错误信息:", err)
return return
} }

View File

@ -13,6 +13,7 @@ type websocketStatType string
const ( const (
TYPE_CUR_CONNECT_COUNT websocketStatType = "TYPE_CUR_CONNECT_COUNT" //ws连接数 TYPE_CUR_CONNECT_COUNT websocketStatType = "TYPE_CUR_CONNECT_COUNT" //ws连接数
TYPE_CUR_COMBINE_IMAGE_COUNT websocketStatType = "TYPE_CUR_COMBINE_IMAGE_COUNT" //合图数 TYPE_CUR_COMBINE_IMAGE_COUNT websocketStatType = "TYPE_CUR_COMBINE_IMAGE_COUNT" //合图数
TYPE_CUR_COMBINE_IMAGE_ERR_COUNT websocketStatType = "TYPE_CUR_COMBINE_IMAGE_ERR_COUNT" //合图失败数
TYPE_CUR_UNITY_HANDLE_COUNT websocketStatType = "TYPE_CUR_UNITY_HANDLE_COUNT" //unity处理数 TYPE_CUR_UNITY_HANDLE_COUNT websocketStatType = "TYPE_CUR_UNITY_HANDLE_COUNT" //unity处理数
) )
@ -24,6 +25,7 @@ type websocketStatData struct {
} }
type mapUserWsStatItem struct { type mapUserWsStatItem struct {
CurCombineCount int `json:"cur_combine_count"` //当前合图数 CurCombineCount int `json:"cur_combine_count"` //当前合图数
CombineErrCount int `json:"combine_err_count"` //合图失败数
CurWsConnectCount int `json:"cur_ws_connect_count"` //当前连接数 CurWsConnectCount int `json:"cur_ws_connect_count"` //当前连接数
CurUnityHandleCount int `json:"cur_unity_handle_count"` //当前unity处理数 CurUnityHandleCount int `json:"cur_unity_handle_count"` //当前unity处理数
} }
@ -104,6 +106,23 @@ func decreaseCombineRequestCount(userId, guestId int64) {
} }
} }
// 累增合图失败数计数
func increaseCombineRequestErrorCount(userId, guestId int64) {
data := websocketStatData{
UserId: userId,
GuestId: guestId,
Type: TYPE_CUR_COMBINE_IMAGE_ERR_COUNT,
Num: 1,
}
select {
case websocketStat <- data:
return
case <-time.After(time.Millisecond * 200):
logx.Error("increaseCombineRequestErrorCount 输入管道超时,丢弃消息")
return
}
}
// 累增unity请求数计数 // 累增unity请求数计数
func increaseUnityRequestCount(userId, guestId int64) { func increaseUnityRequestCount(userId, guestId int64) {
data := websocketStatData{ data := websocketStatData{
@ -210,6 +229,19 @@ func ConsumeWebsocketStatData(ctx context.Context) {
} }
//保存统计 //保存统计
mapUserWsStat.Store(key, stat) mapUserWsStat.Store(key, stat)
case TYPE_CUR_COMBINE_IMAGE_ERR_COUNT: //合图失败计数
if !ok {
continue
}
//存在
stat, ok := statData.(mapUserWsStatItem)
if !ok {
logx.Error("断言mapUserWsStatItem错误")
continue
}
stat.CombineErrCount += data.Num
//保存统计
mapUserWsStat.Store(key, stat)
} }
} }
} }

View File

@ -31,11 +31,13 @@ type CloseWebsocketReq struct {
type GetStatReq struct { type GetStatReq struct {
Password string `form:"password"` Password string `form:"password"`
UserKey []string `form:"user_key,optional"`
} }
type GetStatRsp struct { type GetStatRsp struct {
WsTotalCount int `json:"ws_total_count"` //ws连接总数 WsTotalCount int `json:"ws_total_count"` //ws连接总数
CurCombineCount int `json:"cur_combine_count"` //合图任务数 CurCombineCount int `json:"cur_combine_count"` //合图任务数
CombineErrorCount int `json:"combine_error_count"` //合图失败数
CurUnityHandleCount int `json:"cur_unity_handle_count"` //当前unity请求总数 CurUnityHandleCount int `json:"cur_unity_handle_count"` //当前unity请求总数
UserWsStat interface{} `json:"user_ws_stat"` //用户连接统计 UserWsStat interface{} `json:"user_ws_stat"` //用户连接统计
} }

View File

@ -53,10 +53,12 @@ type CloseWebsocketReq {
//获取ws统计信息 //获取ws统计信息
type GetStatReq { type GetStatReq {
Password string `form:"password"` Password string `form:"password"`
UserKey []string `form:"user_key,optional"`
} }
type GetStatRsp { type GetStatRsp {
WsTotalCount int `json:"ws_total_count"` //ws连接总数 WsTotalCount int `json:"ws_total_count"` //ws连接总数
CurCombineCount int `json:"cur_combine_count"` //合图任务数 CurCombineCount int `json:"cur_combine_count"` //合图任务数
CombineErrorCount int `json:"combine_error_count"` //合图失败数
CurUnityHandleCount int `json:"cur_unity_handle_count"` //当前unity请求总数 CurUnityHandleCount int `json:"cur_unity_handle_count"` //当前unity请求总数
UserWsStat interface{} `json:"user_ws_stat"` //用户连接统计 UserWsStat interface{} `json:"user_ws_stat"` //用户连接统计
} }