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

268 lines
10 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/log"
"Open_IM/pkg/grpc-etcdv3/getcdv3"
2 years ago
admin "Open_IM/pkg/proto/admin_cms"
3 years ago
"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
2 years ago
reqPb admin.GetMessageStatisticsReq
3 years ago
)
2 years ago
reqPb.StatisticsReq = &admin.StatisticsReq{}
2 years ago
if err := c.BindJSON(&req); err != nil {
2 years ago
log.NewError(req.OperationID, utils.GetSelfFuncName(), "BindJSON failed ", err.Error())
c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": err.Error()})
3 years ago
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
}
2 years ago
client := admin.NewAdminCMSClient(etcdConn)
3 years ago
respPb, err := client.GetMessageStatistics(context.Background(), &reqPb)
if err != nil {
3 years ago
log.NewError(reqPb.OperationID, utils.GetSelfFuncName(), "GetMessageStatistics failed", err.Error())
2 years ago
c.JSON(http.StatusInternalServerError, gin.H{"errCode": 400, "errMsg": err.Error()})
3 years ago
return
}
2 years ago
3 years ago
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\""
2 years ago
MessageNum int "json:\"messageNum\""
3 years ago
}{
Date: v.Date,
MessageNum: int(v.Num),
})
}
for _, v := range respPb.GroupMessageNumList {
resp.GroupMessageNumList = append(resp.GroupMessageNumList, struct {
Date string "json:\"date\""
2 years ago
MessageNum int "json:\"messageNum\""
3 years ago
}{
Date: v.Date,
MessageNum: int(v.Num),
})
}
log.NewInfo(reqPb.OperationID, utils.GetSelfFuncName(), "resp: ", resp)
2 years ago
c.JSON(http.StatusOK, gin.H{"errCode": respPb.CommonResp.ErrCode, "errMsg": respPb.CommonResp.ErrMsg, "data": resp})
3 years ago
}
3 years ago
func GetUserStatistics(c *gin.Context) {
var (
req cms_api_struct.GetUserStatisticsRequest
resp cms_api_struct.GetUserStatisticsResponse
2 years ago
reqPb admin.GetUserStatisticsReq
3 years ago
)
2 years ago
reqPb.StatisticsReq = &admin.StatisticsReq{}
2 years ago
if err := c.BindJSON(&req); err != nil {
2 years ago
log.NewError(req.OperationID, utils.GetSelfFuncName(), "BindJSON failed ", err.Error())
c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": err.Error()})
3 years ago
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
}
2 years ago
client := admin.NewAdminCMSClient(etcdConn)
3 years ago
respPb, err := client.GetUserStatistics(context.Background(), &reqPb)
if err != nil {
2 years ago
log.NewError(reqPb.OperationID, utils.GetSelfFuncName(), "GetUserStatistics failed", err.Error(), reqPb.String())
c.JSON(http.StatusBadRequest, gin.H{"errCode": 500, "errMsg": err.Error()})
3 years ago
return
}
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\""
2 years ago
ActiveUserNum int "json:\"activeUserNum\""
3 years ago
}{
Date: v.Date,
ActiveUserNum: int(v.Num),
})
}
for _, v := range respPb.IncreaseUserNumList {
resp.IncreaseUserNumList = append(resp.IncreaseUserNumList, struct {
Date string "json:\"date\""
2 years ago
IncreaseUserNum int "json:\"increaseUserNum\""
3 years ago
}{
Date: v.Date,
IncreaseUserNum: int(v.Num),
})
}
for _, v := range respPb.TotalUserNumList {
resp.TotalUserNumList = append(resp.TotalUserNumList, struct {
Date string "json:\"date\""
2 years ago
TotalUserNum int "json:\"totalUserNum\""
3 years ago
}{
Date: v.Date,
TotalUserNum: int(v.Num),
})
}
log.NewInfo(reqPb.OperationID, utils.GetSelfFuncName(), "resp: ", resp)
2 years ago
c.JSON(http.StatusOK, gin.H{"errCode": respPb.CommonResp.ErrCode, "errMsg": respPb.CommonResp.ErrMsg, "data": resp})
3 years ago
}
3 years ago
func GetGroupStatistics(c *gin.Context) {
var (
req cms_api_struct.GetGroupStatisticsRequest
resp cms_api_struct.GetGroupStatisticsResponse
2 years ago
reqPb admin.GetGroupStatisticsReq
3 years ago
)
2 years ago
reqPb.StatisticsReq = &admin.StatisticsReq{}
2 years ago
if err := c.BindJSON(&req); err != nil {
2 years ago
log.NewError(req.OperationID, "BindJSON failed ", err.Error())
c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": err.Error()})
3 years ago
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
}
2 years ago
client := admin.NewAdminCMSClient(etcdConn)
3 years ago
respPb, err := client.GetGroupStatistics(context.Background(), &reqPb)
if err != nil {
3 years ago
log.NewError(reqPb.OperationID, utils.GetSelfFuncName(), "GetGroupStatistics failed", err.Error())
2 years ago
c.JSON(http.StatusBadRequest, gin.H{"errCode": 500, "errMsg": err.Error()})
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\""
2 years ago
IncreaseGroupNum int "json:\"increaseGroupNum\""
3 years ago
}{
Date: v.Date,
IncreaseGroupNum: int(v.Num),
})
}
for _, v := range respPb.TotalGroupNumList {
resp.TotalGroupNumList = append(resp.TotalGroupNumList,
struct {
Date string "json:\"date\""
2 years ago
TotalGroupNum int "json:\"totalGroupNum\""
3 years ago
}{
Date: v.Date,
TotalGroupNum: int(v.Num),
})
3 years ago
3 years ago
}
log.NewInfo(reqPb.OperationID, utils.GetSelfFuncName(), "resp: ", resp)
2 years ago
c.JSON(http.StatusOK, gin.H{"errCode": respPb.CommonResp.ErrCode, "errMsg": respPb.CommonResp.ErrMsg, "data": resp})
3 years ago
}
func GetActiveUser(c *gin.Context) {
3 years ago
var (
req cms_api_struct.GetActiveUserRequest
resp cms_api_struct.GetActiveUserResponse
2 years ago
reqPb admin.GetActiveUserReq
3 years ago
)
2 years ago
reqPb.StatisticsReq = &admin.StatisticsReq{}
2 years ago
if err := c.BindJSON(&req); err != nil {
2 years ago
log.NewError(req.OperationID, "BindJSON failed ", err.Error())
c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": err.Error()})
3 years ago
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
}
2 years ago
client := admin.NewAdminCMSClient(etcdConn)
3 years ago
respPb, err := client.GetActiveUser(context.Background(), &reqPb)
if err != nil {
3 years ago
log.NewError(reqPb.OperationID, utils.GetSelfFuncName(), "GetActiveUser failed ", err.Error())
2 years ago
c.JSON(http.StatusBadRequest, gin.H{"errCode": 500, "errMsg": err.Error()})
3 years ago
return
}
utils.CopyStructFields(&resp.ActiveUserList, respPb.Users)
log.NewInfo(reqPb.OperationID, utils.GetSelfFuncName(), "resp: ", resp)
2 years ago
c.JSON(http.StatusOK, gin.H{"errCode": respPb.CommonResp.ErrCode, "errMsg": respPb.CommonResp.ErrMsg, "data": resp})
3 years ago
}
func GetActiveGroup(c *gin.Context) {
3 years ago
var (
req cms_api_struct.GetActiveGroupRequest
resp cms_api_struct.GetActiveGroupResponse
2 years ago
reqPb admin.GetActiveGroupReq
3 years ago
)
2 years ago
reqPb.StatisticsReq = &admin.StatisticsReq{}
2 years ago
if err := c.BindJSON(&req); err != nil {
log.NewError(req.OperationID, utils.GetSelfFuncName(), "BindJSON failed ", err.Error())
2 years ago
c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": err.Error()})
3 years ago
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
}
2 years ago
client := admin.NewAdminCMSClient(etcdConn)
3 years ago
respPb, err := client.GetActiveGroup(context.Background(), &reqPb)
if err != nil {
log.NewError(reqPb.OperationID, utils.GetSelfFuncName(), "GetActiveGroup failed ", err.Error())
2 years ago
c.JSON(http.StatusBadRequest, gin.H{"errCode": 500, "errMsg": err.Error()})
3 years ago
return
}
for _, group := range respPb.Groups {
resp.ActiveGroupList = append(resp.ActiveGroupList, struct {
2 years ago
GroupName string "json:\"groupName\""
GroupId string "json:\"groupID\""
MessageNum int "json:\"messageNum\""
3 years ago
}{
GroupName: group.GroupName,
GroupId: group.GroupId,
MessageNum: int(group.MessageNum),
})
}
log.NewInfo(reqPb.OperationID, utils.GetSelfFuncName(), "resp: ", resp)
2 years ago
c.JSON(http.StatusOK, gin.H{"errCode": respPb.CommonResp.ErrCode, "errMsg": respPb.CommonResp.ErrMsg, "data": resp})
3 years ago
}