mirror of
https://github.com/openimsdk/open-im-server.git
synced 2026-05-16 06:49:02 +08:00
statistics
This commit is contained in:
@@ -648,16 +648,15 @@ func (s *groupServer) GetGroupById(_ context.Context, req *pbGroup.GetGroupByIdR
|
||||
return resp, err
|
||||
}
|
||||
resp.GroupInfo = &open_im_sdk.GroupInfo{
|
||||
GroupID: group.GroupID,
|
||||
GroupName: group.GroupName,
|
||||
FaceURL: group.FaceUrl,
|
||||
OwnerUserID: group.CreatorUserID,
|
||||
GroupID: group.GroupID,
|
||||
GroupName: group.GroupName,
|
||||
FaceURL: group.FaceUrl,
|
||||
OwnerUserID: group.CreatorUserID,
|
||||
MemberCount: 0,
|
||||
Status: group.Status,
|
||||
CreatorUserID: group.CreatorUserID,
|
||||
GroupType: group.GroupType,
|
||||
GroupType: group.GroupType,
|
||||
}
|
||||
|
||||
resp.GroupInfo.CreatorUserID = group.CreatorUserID
|
||||
return resp, nil
|
||||
}
|
||||
@@ -721,7 +720,7 @@ func (s *groupServer) GetGroups(_ context.Context, req *pbGroup.GetGroupsReq) (*
|
||||
}
|
||||
|
||||
func (s *groupServer) OperateGroupStatus(_ context.Context, req *pbGroup.OperateGroupStatusReq) (*pbGroup.OperateGroupStatusResp, error) {
|
||||
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), req.String())
|
||||
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), req.String())
|
||||
resp := &pbGroup.OperateGroupStatusResp{}
|
||||
if err := imdb.OperateGroupStatus(req.GroupId, req.Status); err != nil {
|
||||
return resp, err
|
||||
|
||||
@@ -2,13 +2,19 @@ package statistics
|
||||
|
||||
import (
|
||||
"Open_IM/pkg/common/config"
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
//"Open_IM/pkg/common/constant"
|
||||
//"Open_IM/pkg/common/db"
|
||||
//imdb "Open_IM/pkg/common/db/mysql_model/im_mysql_model"
|
||||
imdb "Open_IM/pkg/common/db/mysql_model/im_mysql_model"
|
||||
"Open_IM/pkg/common/log"
|
||||
|
||||
//cp "Open_IM/pkg/common/utils"
|
||||
"Open_IM/pkg/grpc-etcdv3/getcdv3"
|
||||
pbStatistics "Open_IM/pkg/proto/statistics"
|
||||
|
||||
//open_im_sdk "Open_IM/pkg/proto/sdk_ws"
|
||||
"Open_IM/pkg/utils"
|
||||
//"context"
|
||||
@@ -26,18 +32,18 @@ type statisticsServer struct {
|
||||
etcdAddr []string
|
||||
}
|
||||
|
||||
func NewStatisticsGroupServer(port int) *statisticsServer {
|
||||
log.NewPrivateLog("group")
|
||||
func NewStatisticsServer(port int) *statisticsServer {
|
||||
log.NewPrivateLog("Statistics")
|
||||
return &statisticsServer{
|
||||
rpcPort: port,
|
||||
rpcRegisterName: config.Config.RpcRegisterName.OpenImGroupName,
|
||||
rpcRegisterName: config.Config.RpcRegisterName.OpenImStatisticsName,
|
||||
etcdSchema: config.Config.Etcd.EtcdSchema,
|
||||
etcdAddr: config.Config.Etcd.EtcdAddr,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *statisticsServer) Run() {
|
||||
log.NewInfo("0", "group rpc start ")
|
||||
log.NewInfo("0", "Statistics rpc start ")
|
||||
ip := utils.ServerIP
|
||||
registerAddress := ip + ":" + strconv.Itoa(s.rpcPort)
|
||||
//listener network
|
||||
@@ -63,5 +69,270 @@ func (s *statisticsServer) Run() {
|
||||
log.NewError("0", "Serve failed ", err.Error())
|
||||
return
|
||||
}
|
||||
log.NewInfo("0", "group rpc success")
|
||||
}
|
||||
log.NewInfo("0", "statistics rpc success")
|
||||
}
|
||||
|
||||
func (s *statisticsServer) GetActiveGroup(_ context.Context, req *pbStatistics.GetActiveGroupReq) (*pbStatistics.GetActiveGroupResp, error) {
|
||||
log.NewInfo("GetActiveGroup", utils.GetSelfFuncName(), req.String())
|
||||
resp := &pbStatistics.GetActiveGroupResp{}
|
||||
fromTime, toTime, err := ParseTimeFromTo(req.StatisticsReq.From, req.StatisticsReq.To)
|
||||
if err != nil {
|
||||
log.NewError("GetActiveGroup", utils.GetSelfFuncName(), err.Error())
|
||||
return resp, err
|
||||
}
|
||||
activeGroups, err := imdb.GetActiveGroups(fromTime, toTime, 12)
|
||||
if err != nil {
|
||||
log.NewError("GetActiveGroup", utils.GetSelfFuncName(), err.Error())
|
||||
return resp, err
|
||||
}
|
||||
for _, activeGroup := range activeGroups {
|
||||
resp.Groups = append(resp.Groups,
|
||||
&pbStatistics.GroupResp{
|
||||
GroupName: activeGroup.Name,
|
||||
GroupId: activeGroup.Id,
|
||||
MessageNum: int32(activeGroup.MessageNum),
|
||||
})
|
||||
}
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func (s *statisticsServer) GetActiveUser(_ context.Context, req *pbStatistics.GetActiveUserReq) (*pbStatistics.GetActiveUserResp, error) {
|
||||
log.NewInfo("GetActiveUser", utils.GetSelfFuncName(), req.String())
|
||||
resp := &pbStatistics.GetActiveUserResp{}
|
||||
fromTime, toTime, err := ParseTimeFromTo(req.StatisticsReq.From, req.StatisticsReq.To)
|
||||
if err != nil {
|
||||
log.NewError("GetActiveUser", utils.GetSelfFuncName(), err.Error())
|
||||
return resp, err
|
||||
}
|
||||
activeUsers, err := imdb.GetActiveUsers(fromTime, toTime, 12)
|
||||
if err != nil {
|
||||
log.NewError("GetActiveUser", utils.GetSelfFuncName(), err.Error())
|
||||
return resp, err
|
||||
}
|
||||
for _, activeUser := range activeUsers {
|
||||
resp.Users = append(resp.Users,
|
||||
&pbStatistics.UserResp{
|
||||
UserId: activeUser.Id,
|
||||
NickName: activeUser.Name,
|
||||
MessageNum: int32(activeUser.MessageNum),
|
||||
},
|
||||
)
|
||||
}
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func ParseTimeFromTo(from, to string) (time.Time, time.Time, error) {
|
||||
var fromTime time.Time
|
||||
var toTime time.Time
|
||||
fromTime, err := utils.TimeStringToTime(from)
|
||||
if err != nil {
|
||||
return fromTime, toTime, err
|
||||
}
|
||||
toTime, err = utils.TimeStringToTime(to)
|
||||
if err != nil {
|
||||
return fromTime, toTime, err
|
||||
}
|
||||
return fromTime, toTime, nil
|
||||
}
|
||||
|
||||
func isInOneMonth(from, to time.Time) bool {
|
||||
return from.Month() == to.Month()
|
||||
}
|
||||
|
||||
func GetRangeDate(from, to time.Time) [][2]time.Time {
|
||||
interval := to.Sub(from)
|
||||
var times [][2]time.Time
|
||||
switch {
|
||||
// today
|
||||
case interval == 0:
|
||||
times = append(times, [2]time.Time{
|
||||
from, from.Add(time.Hour * 24),
|
||||
})
|
||||
// days
|
||||
case isInOneMonth(from, to):
|
||||
for i := 0; ; i++ {
|
||||
fromTime := from.Add(time.Hour * 24 * time.Duration(i))
|
||||
toTime := from.Add(time.Hour * 24 * time.Duration(i+1))
|
||||
if toTime.After(to.Add(time.Hour * 24)) {
|
||||
break
|
||||
}
|
||||
times = append(times, [2]time.Time{
|
||||
fromTime, toTime,
|
||||
})
|
||||
}
|
||||
// month
|
||||
case !isInOneMonth(from, to):
|
||||
for i := 0; ; i++ {
|
||||
if i == 0 {
|
||||
fromTime := from
|
||||
toTime := getFirstDateOfNextNMonth(fromTime, 1)
|
||||
times = append(times, [2]time.Time{
|
||||
fromTime, toTime,
|
||||
})
|
||||
} else {
|
||||
fromTime := getFirstDateOfNextNMonth(from, i)
|
||||
toTime := getFirstDateOfNextNMonth(fromTime, 1)
|
||||
if toTime.After(to) {
|
||||
toTime = to
|
||||
times = append(times, [2]time.Time{
|
||||
fromTime, toTime,
|
||||
})
|
||||
break
|
||||
}
|
||||
times = append(times, [2]time.Time{
|
||||
fromTime, toTime,
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
return times
|
||||
}
|
||||
|
||||
func getFirstDateOfNextNMonth(currentTime time.Time, n int) time.Time {
|
||||
lastOfMonth := time.Date(currentTime.Year(), currentTime.Month(), 1, 0, 0, 0, 0, currentTime.Location()).AddDate(0, n, 0)
|
||||
return lastOfMonth
|
||||
}
|
||||
|
||||
func (s *statisticsServer) GetGroupStatistics(_ context.Context, req *pbStatistics.GetGroupStatisticsReq) (*pbStatistics.GetGroupStatisticsResp, error) {
|
||||
log.NewInfo("GetGroupStatistics", utils.GetSelfFuncName(), req.String())
|
||||
resp := &pbStatistics.GetGroupStatisticsResp{}
|
||||
fromTime, toTime, err := ParseTimeFromTo(req.StatisticsReq.From, req.StatisticsReq.To)
|
||||
if err != nil {
|
||||
log.NewError("GetMessageStatistics", utils.GetSelfFuncName(), err.Error())
|
||||
return resp, err
|
||||
}
|
||||
increaseGroupNum, err := imdb.GetIncreaseGroupNum(fromTime, toTime.Add(time.Hour*24))
|
||||
if err != nil {
|
||||
log.NewError("GetMessageStatistics", utils.GetSelfFuncName(), err.Error())
|
||||
return resp, err
|
||||
}
|
||||
totalGroupNum, err := imdb.GetTotalGroupNum()
|
||||
if err != nil {
|
||||
log.NewError("GetMessageStatistics", utils.GetSelfFuncName(), err.Error())
|
||||
return resp, err
|
||||
}
|
||||
resp.IncreaseGroupNum = increaseGroupNum
|
||||
resp.TotalGroupNum = totalGroupNum
|
||||
times := GetRangeDate(fromTime, toTime)
|
||||
log.NewInfo("", "times:", times)
|
||||
for _, v := range times {
|
||||
num, err := imdb.GetIncreaseGroupNum(v[0], v[1])
|
||||
if err != nil {
|
||||
log.NewError("", utils.GetSelfFuncName(), "GetIncreaseGroupNum", v, err.Error())
|
||||
}
|
||||
resp.IncreaseGroupNumList = append(resp.IncreaseGroupNumList, &pbStatistics.DateNumList{
|
||||
Date: v[0].String(),
|
||||
Num: num,
|
||||
})
|
||||
num, err = imdb.GetGroupNum(v[0])
|
||||
if err != nil {
|
||||
log.NewError("", utils.GetSelfFuncName(), "GetIncreaseGroupNum", v, err.Error())
|
||||
}
|
||||
resp.TotalGroupNumList = append(resp.TotalGroupNumList, &pbStatistics.DateNumList{
|
||||
Date: v[0].String(),
|
||||
Num: num,
|
||||
})
|
||||
}
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func (s *statisticsServer) GetMessageStatistics(_ context.Context, req *pbStatistics.GetMessageStatisticsReq) (*pbStatistics.GetMessageStatisticsResp, error) {
|
||||
log.NewInfo("GetMessageStatistics", utils.GetSelfFuncName(), req.String())
|
||||
resp := &pbStatistics.GetMessageStatisticsResp{}
|
||||
fromTime, toTime, err := ParseTimeFromTo(req.StatisticsReq.From, req.StatisticsReq.To)
|
||||
if err != nil {
|
||||
log.NewError("GetMessageStatistics", utils.GetSelfFuncName(), err.Error())
|
||||
return resp, err
|
||||
}
|
||||
privateMessageNum, err := imdb.GetPrivateMessageNum(fromTime, toTime.Add(time.Hour*24))
|
||||
if err != nil {
|
||||
log.NewError("GetMessageStatistics", utils.GetSelfFuncName(), err.Error())
|
||||
return resp, err
|
||||
}
|
||||
groupMessageNum, err := imdb.GetGroupMessageNum(fromTime, toTime.Add(time.Hour*24))
|
||||
if err != nil {
|
||||
log.NewError("GetMessageStatistics", utils.GetSelfFuncName(), err.Error())
|
||||
}
|
||||
resp.PrivateMessageNum = privateMessageNum
|
||||
resp.GroupMessageNum = groupMessageNum
|
||||
times := GetRangeDate(fromTime, toTime)
|
||||
fmt.Println(times)
|
||||
for _, v := range times {
|
||||
num, err := imdb.GetPrivateMessageNum(v[0], v[1])
|
||||
if err != nil {
|
||||
log.NewError("", utils.GetSelfFuncName(), "GetIncreaseGroupNum", v, err.Error())
|
||||
}
|
||||
resp.PrivateMessageNumList = append(resp.PrivateMessageNumList, &pbStatistics.DateNumList{
|
||||
Date: v[0].String(),
|
||||
Num: num,
|
||||
})
|
||||
|
||||
num, err = imdb.GetGroupMessageNum(v[0], v[1])
|
||||
if err != nil {
|
||||
log.NewError("", utils.GetSelfFuncName(), "GetIncreaseGroupNum", v, err.Error())
|
||||
}
|
||||
resp.GroupMessageNumList = append(resp.GroupMessageNumList, &pbStatistics.DateNumList{
|
||||
Date: v[0].String(),
|
||||
Num: num,
|
||||
})
|
||||
}
|
||||
fmt.Println(resp)
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func (s *statisticsServer) GetUserStatistics(_ context.Context, req *pbStatistics.GetUserStatisticsReq) (*pbStatistics.GetUserStatisticsResp, error) {
|
||||
log.NewInfo("GetUserStatistics", utils.GetSelfFuncName(), req.String())
|
||||
resp := &pbStatistics.GetUserStatisticsResp{}
|
||||
fromTime, toTime, err := ParseTimeFromTo(req.StatisticsReq.From, req.StatisticsReq.To)
|
||||
if err != nil {
|
||||
return resp, err
|
||||
}
|
||||
activeUserNum, err := imdb.GetActiveUserNum(fromTime, toTime.Add(time.Hour*24))
|
||||
if err != nil {
|
||||
log.NewError("GetUserStatistics", utils.GetSelfFuncName(), err.Error())
|
||||
return resp, err
|
||||
}
|
||||
increaseUserNum, err := imdb.GetIncreaseUserNum(fromTime, toTime.Add(time.Hour*24))
|
||||
if err != nil {
|
||||
log.NewError("GetUserStatistics", utils.GetSelfFuncName(), err.Error())
|
||||
return resp, err
|
||||
}
|
||||
totalUserNum, err := imdb.GetTotalUserNum()
|
||||
if err != nil {
|
||||
log.NewError("GetUserStatistics", utils.GetSelfFuncName(), err.Error())
|
||||
return resp, err
|
||||
}
|
||||
resp.ActiveUserNum = activeUserNum
|
||||
resp.TotalUserNum = totalUserNum
|
||||
resp.IncreaseUserNum = increaseUserNum
|
||||
|
||||
times := GetRangeDate(fromTime, toTime)
|
||||
for _, v := range times {
|
||||
num, err := imdb.GetActiveUserNum(v[0], v[1])
|
||||
if err != nil {
|
||||
log.NewError("", utils.GetSelfFuncName(), "GetIncreaseGroupNum", v, err.Error())
|
||||
}
|
||||
resp.ActiveUserNumList = append(resp.ActiveUserNumList, &pbStatistics.DateNumList{
|
||||
Date: v[0].String(),
|
||||
Num: num,
|
||||
})
|
||||
num, err = imdb.GetTotalUserNumByDate(v[0])
|
||||
if err != nil {
|
||||
log.NewError("", utils.GetSelfFuncName(), "GetTotalUserNumByDate", v, err.Error())
|
||||
}
|
||||
resp.TotalUserNumList = append(resp.TotalUserNumList, &pbStatistics.DateNumList{
|
||||
Date: v[0].String(),
|
||||
Num: num,
|
||||
})
|
||||
num, err = imdb.GetIncreaseUserNum(v[0], v[1])
|
||||
if err != nil {
|
||||
log.NewError("", utils.GetSelfFuncName(), "GetIncreaseUserNum", v, err.Error())
|
||||
}
|
||||
resp.IncreaseUserNumList = append(resp.IncreaseUserNumList, &pbStatistics.DateNumList{
|
||||
Date: v[0].String(),
|
||||
Num: num,
|
||||
})
|
||||
}
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
+26
-16
@@ -243,9 +243,9 @@ func (s *userServer) UpdateUserInfo(ctx context.Context, req *pbUser.UpdateUserI
|
||||
return &pbUser.UpdateUserInfoResp{CommonResp: &pbUser.CommonResp{}}, nil
|
||||
}
|
||||
|
||||
func (s *userServer) GetUser(ctx context.Context, req *pbUser.GetUserReq) (*pbUser.GetUserResp, error) {
|
||||
func (s *userServer) GetUserById(ctx context.Context, req *pbUser.GetUserByIdReq) (*pbUser.GetUserByIdResp, error) {
|
||||
log.NewInfo(req.OperationID, "GetUser args ", req.String())
|
||||
resp := &pbUser.GetUserResp{User: &pbUser.User{}}
|
||||
resp := &pbUser.GetUserByIdResp{User: &pbUser.User{}}
|
||||
user, err := imdb.GetUserByUserID(req.UserId)
|
||||
if err != nil {
|
||||
return resp, nil
|
||||
@@ -259,7 +259,7 @@ func (s *userServer) GetUser(ctx context.Context, req *pbUser.GetUserReq) (*pbUs
|
||||
Nickname: user.Nickname,
|
||||
UserId: user.UserID,
|
||||
CreateTime: user.CreateTime.String(),
|
||||
IsBlock: isBlock,
|
||||
IsBlock: isBlock,
|
||||
}
|
||||
return resp, nil
|
||||
}
|
||||
@@ -271,11 +271,6 @@ func (s *userServer) GetUsers(ctx context.Context, req *pbUser.GetUsersReq) (*pb
|
||||
if err != nil {
|
||||
return resp, nil
|
||||
}
|
||||
usersNum, err := imdb.GetUsersNumCount()
|
||||
if err != nil {
|
||||
return resp, nil
|
||||
}
|
||||
resp.UserNum = int32(usersNum)
|
||||
for _, v := range users {
|
||||
isBlock, err := imdb.UserIsBlock(v.UserID)
|
||||
if err == nil {
|
||||
@@ -356,11 +351,9 @@ func (s *userServer) GetBlockUsers(ctx context.Context, req *pbUser.GetBlockUser
|
||||
if err != nil {
|
||||
return resp, constant.ErrDB
|
||||
}
|
||||
usersNum, err := imdb.GetBlockUsersNumCount()
|
||||
if err != nil {
|
||||
return resp, constant.ErrDB
|
||||
}
|
||||
resp.BlockUserNum = int32(usersNum)
|
||||
for _, v := range blockUsers {
|
||||
resp.BlockUsers = append(resp.BlockUsers, &pbUser.BlockUser{
|
||||
User: &pbUser.User{
|
||||
@@ -380,16 +373,33 @@ func (s *userServer) GetBlockUsers(ctx context.Context, req *pbUser.GetBlockUser
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func (s *userServer) GetBlockUser(_ context.Context, req *pbUser.GetBlockUserReq) (*pbUser.GetBlockUserResp, error) {
|
||||
func (s *userServer) GetBlockUserById(_ context.Context, req *pbUser.GetBlockUserByIdReq) (*pbUser.GetBlockUserByIdResp, error) {
|
||||
log.NewInfo(req.OperationID, "GetBlockUser args ", req.String())
|
||||
resp := &pbUser.GetBlockUserResp{}
|
||||
resp := &pbUser.GetBlockUserByIdResp{}
|
||||
user, err := imdb.GetBlockUserById(req.UserId)
|
||||
if err != nil {
|
||||
log.NewError(req.OperationID, utils.GetSelfFuncName(), err)
|
||||
return resp, err
|
||||
}
|
||||
resp.BlockUser = &pbUser.BlockUser{}
|
||||
resp.BlockUser.BeginDisableTime = (user.BeginDisableTime).String()
|
||||
resp.BlockUser.EndDisableTime = (user.EndDisableTime).String()
|
||||
resp.BlockUser = &pbUser.BlockUser{
|
||||
User: &pbUser.User{
|
||||
ProfilePhoto: user.User.FaceURL,
|
||||
Nickname: user.User.Nickname,
|
||||
UserId: user.User.UserID,
|
||||
IsBlock: true,
|
||||
},
|
||||
BeginDisableTime: (user.BeginDisableTime).String(),
|
||||
EndDisableTime: (user.EndDisableTime).String(),
|
||||
}
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func (s *userServer) DeleteUser(_ context.Context, req *pbUser.DeleteUserReq) (*pbUser.DeleteUserResp, error) {
|
||||
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), req.String())
|
||||
resp := &pbUser.DeleteUserResp{}
|
||||
if row := imdb.DeleteUser(req.UserId); row == 0 {
|
||||
log.NewError(req.OperationID, utils.GetSelfFuncName(), "delete error", row)
|
||||
return resp, constant.ErrDB
|
||||
}
|
||||
return resp, nil
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user