You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Open-IM-Server/internal/cms_api/statistics/statistics.go

271 lines
9.8 KiB

3 years ago
package statistics
import (
3 years ago
"Open_IM/pkg/cms_api_struct"
3 years ago
"Open_IM/pkg/common/config"
"Open_IM/pkg/common/constant"
openIMHttp "Open_IM/pkg/common/http"
"Open_IM/pkg/common/log"
"Open_IM/pkg/grpc-etcdv3/getcdv3"
pb "Open_IM/pkg/proto/statistics"
"Open_IM/pkg/utils"
"context"
"net/http"
3 years ago
"strings"
3 years ago
"github.com/gin-gonic/gin"
)
3 years ago
func GetMessagesStatistics(c *gin.Context) {
var (
3 years ago
req cms_api_struct.GetMessageStatisticsRequest
resp cms_api_struct.GetMessageStatisticsResponse
reqPb pb.GetMessageStatisticsReq
3 years ago
)
3 years ago
reqPb.StatisticsReq = &pb.StatisticsReq{}
if err := c.ShouldBindQuery(&req); err != nil {
log.NewError("0", utils.GetSelfFuncName(), "BindJSON failed ", err.Error())
openIMHttp.RespHttp200(c, constant.ErrArgs, nil)
return
}
reqPb.OperationID = utils.OperationIDGenerator()
log.NewInfo(reqPb.OperationID, utils.GetSelfFuncName(), "req: ", req)
3 years ago
utils.CopyStructFields(&reqPb.StatisticsReq, &req)
2 years ago
etcdConn := getcdv3.GetDefaultConn(config.Config.Etcd.EtcdSchema, strings.Join(config.Config.Etcd.EtcdAddr, ","), config.Config.RpcRegisterName.OpenImStatisticsName, reqPb.OperationID)
if etcdConn == nil {
2 years ago
errMsg := reqPb.OperationID + "getcdv3.GetDefaultConn == nil"
log.NewError(reqPb.OperationID, errMsg)
c.JSON(http.StatusInternalServerError, gin.H{"errCode": 500, "errMsg": errMsg})
return
}
3 years ago
client := pb.NewUserClient(etcdConn)
respPb, err := client.GetMessageStatistics(context.Background(), &reqPb)
if err != nil {
3 years ago
log.NewError(reqPb.OperationID, utils.GetSelfFuncName(), "GetMessageStatistics failed", err.Error())
3 years ago
openIMHttp.RespHttp200(c, err, resp)
3 years ago
return
}
// utils.CopyStructFields(&resp, respPb)
resp.GroupMessageNum = int(respPb.GroupMessageNum)
resp.PrivateMessageNum = int(respPb.PrivateMessageNum)
for _, v := range respPb.PrivateMessageNumList {
resp.PrivateMessageNumList = append(resp.PrivateMessageNumList, struct {
Date string "json:\"date\""
MessageNum int "json:\"message_num\""
}{
Date: v.Date,
MessageNum: int(v.Num),
})
}
for _, v := range respPb.GroupMessageNumList {
resp.GroupMessageNumList = append(resp.GroupMessageNumList, struct {
Date string "json:\"date\""
MessageNum int "json:\"message_num\""
}{
Date: v.Date,
MessageNum: int(v.Num),
})
}
log.NewInfo(reqPb.OperationID, utils.GetSelfFuncName(), "resp: ", resp)
3 years ago
openIMHttp.RespHttp200(c, constant.OK, resp)
3 years ago
}
3 years ago
func GetUserStatistics(c *gin.Context) {
var (
req cms_api_struct.GetUserStatisticsRequest
resp cms_api_struct.GetUserStatisticsResponse
reqPb pb.GetUserStatisticsReq
)
reqPb.StatisticsReq = &pb.StatisticsReq{}
if err := c.ShouldBindQuery(&req); err != nil {
log.NewError("0", utils.GetSelfFuncName(), "BindJSON failed ", err.Error())
openIMHttp.RespHttp200(c, constant.ErrArgs, nil)
return
}
reqPb.OperationID = utils.OperationIDGenerator()
log.NewInfo(reqPb.OperationID, utils.GetSelfFuncName(), "req: ", req)
3 years ago
utils.CopyStructFields(&reqPb.StatisticsReq, &req)
2 years ago
etcdConn := getcdv3.GetDefaultConn(config.Config.Etcd.EtcdSchema, strings.Join(config.Config.Etcd.EtcdAddr, ","), config.Config.RpcRegisterName.OpenImStatisticsName, reqPb.OperationID)
if etcdConn == nil {
2 years ago
errMsg := reqPb.OperationID + "getcdv3.GetDefaultConn == nil"
log.NewError(reqPb.OperationID, errMsg)
c.JSON(http.StatusInternalServerError, gin.H{"errCode": 500, "errMsg": errMsg})
return
}
3 years ago
client := pb.NewUserClient(etcdConn)
respPb, err := client.GetUserStatistics(context.Background(), &reqPb)
if err != nil {
log.NewError(reqPb.OperationID, utils.GetSelfFuncName(), "GetUserStatistics failed", err.Error())
3 years ago
openIMHttp.RespHttp200(c, err, nil)
3 years ago
return
}
// utils.CopyStructFields(&resp, respPb)
resp.ActiveUserNum = int(respPb.ActiveUserNum)
resp.IncreaseUserNum = int(respPb.IncreaseUserNum)
resp.TotalUserNum = int(respPb.TotalUserNum)
for _, v := range respPb.ActiveUserNumList {
resp.ActiveUserNumList = append(resp.ActiveUserNumList, struct {
Date string "json:\"date\""
ActiveUserNum int "json:\"active_user_num\""
}{
Date: v.Date,
ActiveUserNum: int(v.Num),
})
}
for _, v := range respPb.IncreaseUserNumList {
resp.IncreaseUserNumList = append(resp.IncreaseUserNumList, struct {
Date string "json:\"date\""
IncreaseUserNum int "json:\"increase_user_num\""
}{
Date: v.Date,
IncreaseUserNum: int(v.Num),
})
}
for _, v := range respPb.TotalUserNumList {
resp.TotalUserNumList = append(resp.TotalUserNumList, struct {
Date string "json:\"date\""
TotalUserNum int "json:\"total_user_num\""
}{
Date: v.Date,
TotalUserNum: int(v.Num),
})
}
log.NewInfo(reqPb.OperationID, utils.GetSelfFuncName(), "resp: ", resp)
3 years ago
openIMHttp.RespHttp200(c, constant.OK, resp)
3 years ago
}
3 years ago
func GetGroupStatistics(c *gin.Context) {
var (
req cms_api_struct.GetGroupStatisticsRequest
resp cms_api_struct.GetGroupStatisticsResponse
reqPb pb.GetGroupStatisticsReq
)
reqPb.StatisticsReq = &pb.StatisticsReq{}
if err := c.ShouldBindQuery(&req); err != nil {
log.NewError("0", "BindJSON failed ", err.Error())
openIMHttp.RespHttp200(c, constant.ErrArgs, nil)
return
}
reqPb.OperationID = utils.OperationIDGenerator()
log.NewInfo(reqPb.OperationID, utils.GetSelfFuncName(), "req: ", req)
3 years ago
utils.CopyStructFields(&reqPb.StatisticsReq, &req)
2 years ago
etcdConn := getcdv3.GetDefaultConn(config.Config.Etcd.EtcdSchema, strings.Join(config.Config.Etcd.EtcdAddr, ","), config.Config.RpcRegisterName.OpenImStatisticsName, reqPb.OperationID)
if etcdConn == nil {
2 years ago
errMsg := reqPb.OperationID + "getcdv3.GetDefaultConn == nil"
log.NewError(reqPb.OperationID, errMsg)
c.JSON(http.StatusInternalServerError, gin.H{"errCode": 500, "errMsg": errMsg})
return
}
3 years ago
client := pb.NewUserClient(etcdConn)
respPb, err := client.GetGroupStatistics(context.Background(), &reqPb)
if err != nil {
3 years ago
log.NewError(reqPb.OperationID, utils.GetSelfFuncName(), "GetGroupStatistics failed", err.Error())
3 years ago
openIMHttp.RespHttp200(c, err, nil)
3 years ago
return
}
// utils.CopyStructFields(&resp, respPb)
resp.IncreaseGroupNum = int(respPb.GetIncreaseGroupNum())
resp.TotalGroupNum = int(respPb.GetTotalGroupNum())
for _, v := range respPb.IncreaseGroupNumList {
resp.IncreaseGroupNumList = append(resp.IncreaseGroupNumList,
struct {
Date string "json:\"date\""
IncreaseGroupNum int "json:\"increase_group_num\""
}{
Date: v.Date,
IncreaseGroupNum: int(v.Num),
})
}
for _, v := range respPb.TotalGroupNumList {
resp.TotalGroupNumList = append(resp.TotalGroupNumList,
struct {
Date string "json:\"date\""
TotalGroupNum int "json:\"total_group_num\""
}{
Date: v.Date,
TotalGroupNum: int(v.Num),
})
3 years ago
3 years ago
}
log.NewInfo(reqPb.OperationID, utils.GetSelfFuncName(), "resp: ", resp)
3 years ago
openIMHttp.RespHttp200(c, constant.OK, resp)
3 years ago
}
func GetActiveUser(c *gin.Context) {
3 years ago
var (
req cms_api_struct.GetActiveUserRequest
resp cms_api_struct.GetActiveUserResponse
reqPb pb.GetActiveUserReq
)
reqPb.StatisticsReq = &pb.StatisticsReq{}
if err := c.ShouldBindQuery(&req); err != nil {
log.NewError("0", "BindJSON failed ", err.Error())
openIMHttp.RespHttp200(c, constant.ErrArgs, nil)
return
}
reqPb.OperationID = utils.OperationIDGenerator()
log.NewInfo(reqPb.OperationID, utils.GetSelfFuncName(), "req: ", req)
3 years ago
utils.CopyStructFields(&reqPb.StatisticsReq, req)
2 years ago
etcdConn := getcdv3.GetDefaultConn(config.Config.Etcd.EtcdSchema, strings.Join(config.Config.Etcd.EtcdAddr, ","), config.Config.RpcRegisterName.OpenImStatisticsName, reqPb.OperationID)
if etcdConn == nil {
2 years ago
errMsg := reqPb.OperationID + "getcdv3.GetDefaultConn == nil"
log.NewError(reqPb.OperationID, errMsg)
c.JSON(http.StatusInternalServerError, gin.H{"errCode": 500, "errMsg": errMsg})
return
}
3 years ago
client := pb.NewUserClient(etcdConn)
respPb, err := client.GetActiveUser(context.Background(), &reqPb)
if err != nil {
3 years ago
log.NewError(reqPb.OperationID, utils.GetSelfFuncName(), "GetActiveUser failed ", err.Error())
3 years ago
openIMHttp.RespHttp200(c, err, nil)
3 years ago
return
}
utils.CopyStructFields(&resp.ActiveUserList, respPb.Users)
log.NewInfo(reqPb.OperationID, utils.GetSelfFuncName(), "resp: ", resp)
3 years ago
openIMHttp.RespHttp200(c, constant.OK, resp)
3 years ago
}
func GetActiveGroup(c *gin.Context) {
3 years ago
var (
req cms_api_struct.GetActiveGroupRequest
resp cms_api_struct.GetActiveGroupResponse
reqPb pb.GetActiveGroupReq
)
reqPb.StatisticsReq = &pb.StatisticsReq{}
if err := c.ShouldBindQuery(&req); err != nil {
3 years ago
log.NewError("0", utils.GetSelfFuncName(), "BindJSON failed ", err.Error())
3 years ago
openIMHttp.RespHttp200(c, constant.ErrArgs, nil)
return
}
reqPb.OperationID = utils.OperationIDGenerator()
log.NewInfo(reqPb.OperationID, utils.GetSelfFuncName(), "req: ", req)
3 years ago
utils.CopyStructFields(&reqPb.StatisticsReq, req)
2 years ago
etcdConn := getcdv3.GetDefaultConn(config.Config.Etcd.EtcdSchema, strings.Join(config.Config.Etcd.EtcdAddr, ","), config.Config.RpcRegisterName.OpenImStatisticsName, reqPb.OperationID)
if etcdConn == nil {
2 years ago
errMsg := reqPb.OperationID + "getcdv3.GetDefaultConn == nil"
log.NewError(reqPb.OperationID, errMsg)
c.JSON(http.StatusInternalServerError, gin.H{"errCode": 500, "errMsg": errMsg})
return
}
3 years ago
client := pb.NewUserClient(etcdConn)
respPb, err := client.GetActiveGroup(context.Background(), &reqPb)
if err != nil {
log.NewError(reqPb.OperationID, utils.GetSelfFuncName(), "GetActiveGroup failed ", err.Error())
3 years ago
openIMHttp.RespHttp200(c, err, nil)
3 years ago
return
}
for _, group := range respPb.Groups {
resp.ActiveGroupList = append(resp.ActiveGroupList, struct {
GroupName string "json:\"group_name\""
GroupId string "json:\"group_id\""
MessageNum int "json:\"message_num\""
}{
GroupName: group.GroupName,
GroupId: group.GroupId,
MessageNum: int(group.MessageNum),
})
}
log.NewInfo(reqPb.OperationID, utils.GetSelfFuncName(), "resp: ", resp)
3 years ago
openIMHttp.RespHttp200(c, constant.OK, resp)
3 years ago
}