fusenapi/server/websocket/internal/logic/getstatlogic.go
laodaming 12795e2115 fix
2023-11-01 14:56:36 +08:00

92 lines
2.7 KiB
Go

package logic
import (
"context"
"fusenapi/utils/auth"
"fusenapi/utils/basic"
"strings"
"sync"
"fusenapi/server/websocket/internal/svc"
"fusenapi/server/websocket/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type GetStatLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewGetStatLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetStatLogic {
return &GetStatLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
// 处理进入前逻辑w,r
// func (l *GetStatLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) {
// }
var GetStatMutex sync.Mutex
func (l *GetStatLogic) GetStat(req *types.GetStatReq, userinfo *auth.UserInfo) (resp *basic.Response) {
if req.Password != "fusen1314" {
return resp.SetStatusWithMessage(basic.CodeOK, "你干嘛,哎哟")
}
GetStatMutex.Lock()
defer GetStatMutex.Unlock()
userStat := make(map[string]interface{})
currentWebsocketConnectCount := 0
currentCombineApiCount := 0
currentUnityHandleCount := 0
combineErrorCount := 0
req.UserKeys = strings.Trim(req.UserKeys, " ")
if req.UserKeys != "" { //指定用户群体
reqUserList := strings.Split(req.UserKeys, ",")
for _, key := range reqUserList {
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] = value
} else {
userStat[key] = mapUserWsStatItem{}
}
}
} else { //不指定用户
mapUserWsStat.Range(func(key, value any) bool {
if stat, ok := value.(mapUserWsStatItem); ok {
currentWebsocketConnectCount += stat.CurWsConnectCount
currentCombineApiCount += stat.CurCombineCount
currentUnityHandleCount += stat.CurUnityHandleCount
combineErrorCount += stat.CombineErrCount
} else {
logx.Error("断言mapUserWsStatItem错误")
}
userStat[key.(string)] = value
return true
})
}
return resp.SetStatusWithMessage(basic.CodeOK, "success", types.GetStatRsp{
WsTotalCount: currentWebsocketConnectCount,
CurCombineCount: currentCombineApiCount,
CurUnityHandleCount: currentUnityHandleCount,
CombineErrorCount: combineErrorCount,
UserWsStat: userStat,
})
}
// 处理逻辑后 w,r 如:重定向, resp 必须重新处理
// func (l *GetStatLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) {
// // httpx.OkJsonCtx(r.Context(), w, resp)
// }