Skip to content

Commit

Permalink
modify statistic api (#115)
Browse files Browse the repository at this point in the history
* statistic

Signed-off-by: hanzhixiao <[email protected]>

* update password error

Signed-off-by: hanzhixiao <[email protected]>

---------

Signed-off-by: hanzhixiao <[email protected]>
  • Loading branch information
hanzhixiao authored Aug 3, 2023
1 parent a989174 commit 4591e6d
Show file tree
Hide file tree
Showing 15 changed files with 1,084 additions and 1,324 deletions.
32 changes: 28 additions & 4 deletions internal/api/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/OpenIMSDK/chat/pkg/proto/chat"
"github.com/OpenIMSDK/protocol/constant"
"github.com/OpenIMSDK/protocol/sdkws"
"github.com/OpenIMSDK/protocol/user"
"github.com/OpenIMSDK/tools/a2r"
"github.com/OpenIMSDK/tools/apiresp"
"github.com/OpenIMSDK/tools/checker"
Expand Down Expand Up @@ -305,10 +306,33 @@ func (o *AdminApi) SearchApplet(c *gin.Context) {
a2r.Call(admin.AdminClient.SearchApplet, o.adminClient, c)
}

func (o *AdminApi) NewUserCount(c *gin.Context) {
a2r.Call(chat.ChatClient.NewUserCount, o.chatClient, c)
}

func (o *AdminApi) LoginUserCount(c *gin.Context) {
a2r.Call(chat.ChatClient.UserLoginCount, o.chatClient, c)
}

func (o *AdminApi) NewUserCount(c *gin.Context) {
var req user.UserRegisterCountReq
var resp apistruct.NewUserCountResp
if err := c.BindJSON(&req); err != nil {
apiresp.GinError(c, err)
return
}
log.ZInfo(c, "NewUserCount api", "req", &req)
if err := checker.Validate(&req); err != nil {
apiresp.GinError(c, err) // 参数校验失败
return
}
imToken, err := o.imApiCaller.UserToken(c, config.GetIMAdmin(mctx.GetOpUserID(c)), constant.AdminPlatformID)
if err != nil {
apiresp.GinError(c, err)
return
}
dateCount, total, err := o.imApiCaller.UserRegisterCount(mctx.WithApiToken(c, imToken), req.Start, req.End)
if err != nil {
apiresp.GinError(c, err)
return
}
resp.DateCount = dateCount
resp.Total = total
apiresp.GinSuccess(c, resp)
}
6 changes: 3 additions & 3 deletions internal/rpc/admin/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ func ToDBAdminUpdate(req *admin.AdminUpdateInfoReq) (map[string]any, error) {
}
update["nickname"] = req.Nickname.Value
}
if req.UserID != nil {
update["user_id"] = req.UserID.Value
}
//if req.UserID != nil {
// update["user_id"] = req.UserID.Value
//}
if req.Level != nil {
update["level"] = req.Level.Value
}
Expand Down
36 changes: 4 additions & 32 deletions internal/rpc/chat/statistic.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,51 +7,23 @@ import (
"time"
)

func (o *chatSvr) NewUserCount(ctx context.Context, req *chat.NewUserCountReq) (*chat.NewUserCountResp, error) {
resp := &chat.NewUserCountResp{}
if req.Start > req.End {
return nil, errs.ErrArgs.Wrap("start > end")
}
total, err := o.Database.NewUserCountTotal(ctx, nil)
if err != nil {
return nil, err
}
start := time.UnixMilli(req.Start)
before, err := o.Database.NewUserCountTotal(ctx, &start)
if err != nil {
return nil, err
}
end := time.UnixMilli(req.End)
count, err := o.Database.NewUserCountRangeEverydayTotal(ctx, &start, &end)
if err != nil {
return nil, err
}
resp.Total = total
resp.Before = before
resp.Count = count
return resp, nil
}
func (o *chatSvr) UserLoginCount(ctx context.Context, req *chat.UserLoginCountReq) (*chat.UserLoginCountResp, error) {
resp := &chat.UserLoginCountResp{}
if req.Start > req.End {
return nil, errs.ErrArgs.Wrap("start > end")
}
total, err := o.Database.UserLoginCountTotal(ctx, nil)
total, err := o.Database.NewUserCountTotal(ctx, nil)
if err != nil {
return nil, err
}
start := time.UnixMilli(req.Start)
before, err := o.Database.UserLoginCountTotal(ctx, &start)
if err != nil {
return nil, err
}
end := time.UnixMilli(req.End)
count, err := o.Database.UserLoginCountRangeEverydayTotal(ctx, &start, &end)
count, loginCount, err := o.Database.UserLoginCountRangeEverydayTotal(ctx, &start, &end)
if err != nil {
return nil, err
}
resp.Total = total
resp.Before = before
resp.LoginCount = loginCount
resp.UnloginCount = total - loginCount
resp.Count = count
return resp, nil
}
15 changes: 8 additions & 7 deletions pkg/common/apicall/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@ func imApi() string {

// im caller.
var (
importFriend = NewApiCaller[friend.ImportFriendReq, friend.ImportFriendResp]("/friend/import_friend", imApi)
userToken = NewApiCaller[auth.UserTokenReq, auth.UserTokenResp]("/auth/user_token", imApi)
inviteToGroup = NewApiCaller[group.InviteUserToGroupReq, group.InviteUserToGroupResp]("/group/invite_user_to_group", imApi)
updateUserInfo = NewApiCaller[user.UpdateUserInfoReq, user.UpdateUserInfoResp]("/user/update_user_info", imApi)
registerUser = NewApiCaller[user.UserRegisterReq, user.UserRegisterResp]("/user/user_register", imApi)
forceOffLine = NewApiCaller[auth.ForceLogoutReq, auth.ForceLogoutResp]("/auth/force_logout", imApi)
getGroupsInfo = NewApiCaller[group.GetGroupsInfoReq, group.GetGroupsInfoResp]("/group/get_groups_info", imApi)
importFriend = NewApiCaller[friend.ImportFriendReq, friend.ImportFriendResp]("/friend/import_friend", imApi)
userToken = NewApiCaller[auth.UserTokenReq, auth.UserTokenResp]("/auth/user_token", imApi)
inviteToGroup = NewApiCaller[group.InviteUserToGroupReq, group.InviteUserToGroupResp]("/group/invite_user_to_group", imApi)
updateUserInfo = NewApiCaller[user.UpdateUserInfoReq, user.UpdateUserInfoResp]("/user/update_user_info", imApi)
registerUser = NewApiCaller[user.UserRegisterReq, user.UserRegisterResp]("/user/user_register", imApi)
forceOffLine = NewApiCaller[auth.ForceLogoutReq, auth.ForceLogoutResp]("/auth/force_logout", imApi)
getGroupsInfo = NewApiCaller[group.GetGroupsInfoReq, group.GetGroupsInfoResp]("/group/get_groups_info", imApi)
registerUserCount = NewApiCaller[user.UserRegisterCountReq, user.UserRegisterCountResp]("/statistics/user/register", imApi)
)
12 changes: 12 additions & 0 deletions pkg/common/apicall/caller.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type CallerInterface interface {
ForceOffLine(ctx context.Context, userID string) error
RegisterUser(ctx context.Context, users []*sdkws.UserInfo) error
FindGroupInfo(ctx context.Context, groupIDs []string) ([]*sdkws.GroupInfo, error)
UserRegisterCount(ctx context.Context, start int64, end int64) (map[string]int64, int64, error)
}

type Caller struct{}
Expand Down Expand Up @@ -119,3 +120,14 @@ func (c *Caller) FindGroupInfo(ctx context.Context, groupIDs []string) ([]*sdkws
}
return resp.GroupInfos, nil
}

func (c *Caller) UserRegisterCount(ctx context.Context, start int64, end int64) (map[string]int64, int64, error) {
resp, err := registerUserCount.Call(ctx, &user.UserRegisterCountReq{
Start: start,
End: end,
})
if err != nil {
return nil, 0, err
}
return resp.Count, resp.Total, nil
}
5 changes: 5 additions & 0 deletions pkg/common/apistruct/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,8 @@ type SearchDefaultGroupResp struct {
Total uint32 `json:"total"`
Groups []*sdkws.GroupInfo `json:"groups"`
}

type NewUserCountResp struct {
Total int64 `json:"total"`
DateCount map[string]int64 `json:"date_count"`
}
9 changes: 2 additions & 7 deletions pkg/common/db/database/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,8 @@ type ChatDatabaseInterface interface {
UpdatePassword(ctx context.Context, userID string, password string) error
UpdatePasswordAndDeleteVerifyCode(ctx context.Context, userID string, password string, code uint) error
NewUserCountTotal(ctx context.Context, before *time.Time) (int64, error)
NewUserCountRangeEverydayTotal(ctx context.Context, start *time.Time, end *time.Time) (map[string]int64, error)
UserLoginCountTotal(ctx context.Context, before *time.Time) (int64, error)
UserLoginCountRangeEverydayTotal(ctx context.Context, start *time.Time, end *time.Time) (map[string]int64, error)
UserLoginCountRangeEverydayTotal(ctx context.Context, start *time.Time, end *time.Time) (map[string]int64, int64, error)
}

func NewChatDatabase(db *gorm.DB) ChatDatabaseInterface {
Expand Down Expand Up @@ -226,14 +225,10 @@ func (o *ChatDatabase) NewUserCountTotal(ctx context.Context, before *time.Time)
return o.register.CountTotal(ctx, before)
}

func (o *ChatDatabase) NewUserCountRangeEverydayTotal(ctx context.Context, start *time.Time, end *time.Time) (map[string]int64, error) {
return o.register.CountRangeEverydayTotal(ctx, start, end)
}

func (o *ChatDatabase) UserLoginCountTotal(ctx context.Context, before *time.Time) (int64, error) {
return o.userLoginRecord.CountTotal(ctx, before)
}

func (o *ChatDatabase) UserLoginCountRangeEverydayTotal(ctx context.Context, start *time.Time, end *time.Time) (map[string]int64, error) {
func (o *ChatDatabase) UserLoginCountRangeEverydayTotal(ctx context.Context, start *time.Time, end *time.Time) (map[string]int64, int64, error) {
return o.userLoginRecord.CountRangeEverydayTotal(ctx, start, end)
}
22 changes: 0 additions & 22 deletions pkg/common/db/model/chat/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,25 +50,3 @@ func (o *Register) CountTotal(ctx context.Context, before *time.Time) (count int
}
return count, nil
}

func (o *Register) CountRangeEverydayTotal(ctx context.Context, start *time.Time, end *time.Time) (map[string]int64, error) {
var res []struct {
Date time.Time `gorm:"column:date"`
Count int64 `gorm:"column:count"`
}
err := o.db.WithContext(ctx).
Model(&chat.Register{}).
Select("DATE(create_time) AS date, count(1) AS count").
Where("create_time >= ? and create_time < ?", start, end).
Group("date").
Find(&res).
Error
if err != nil {
return nil, errs.Wrap(err)
}
v := make(map[string]int64)
for _, r := range res {
v[r.Date.Format("2006-01-02")] = r.Count
}
return v, nil
}
12 changes: 7 additions & 5 deletions pkg/common/db/model/chat/user_login_record.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,24 +54,26 @@ func (o *UserLoginRecord) CountTotal(ctx context.Context, before *time.Time) (co
return count, nil
}

func (o *UserLoginRecord) CountRangeEverydayTotal(ctx context.Context, start *time.Time, end *time.Time) (map[string]int64, error) {
func (o *UserLoginRecord) CountRangeEverydayTotal(ctx context.Context, start *time.Time, end *time.Time) (map[string]int64, int64, error) {
var res []struct {
Date time.Time `gorm:"column:date"`
Count int64 `gorm:"column:count"`
}
var loginCount int64
err := o.db.WithContext(ctx).
Model(&chat.UserLoginRecord{}).
Select("DATE(create_time) AS date, count(1) AS count").
Where("create_time >= ? and create_time < ?", start, end).
Select("DATE(login_time) AS date, count(distinct(user_id)) AS count").
Where("login_time >= ? and login_time < ?", start, end).
Group("date").
Find(&res).
Error
if err != nil {
return nil, errs.Wrap(err)
return nil, 0, errs.Wrap(err)
}
v := make(map[string]int64)
for _, r := range res {
loginCount += r.Count
v[r.Date.Format("2006-01-02")] = r.Count
}
return v, nil
return v, loginCount, nil
}
1 change: 0 additions & 1 deletion pkg/common/db/table/chat/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,4 @@ type RegisterInterface interface {
NewTx(tx any) RegisterInterface
Create(ctx context.Context, registers ...*Register) error
CountTotal(ctx context.Context, before *time.Time) (int64, error)
CountRangeEverydayTotal(ctx context.Context, start *time.Time, end *time.Time) (map[string]int64, error)
}
2 changes: 1 addition & 1 deletion pkg/common/db/table/chat/user_login_record.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,5 @@ type UserLoginRecordInterface interface {
NewTx(tx any) UserLoginRecordInterface
Create(ctx context.Context, records ...*UserLoginRecord) error
CountTotal(ctx context.Context, before *time.Time) (int64, error)
CountRangeEverydayTotal(ctx context.Context, start *time.Time, end *time.Time) (map[string]int64, error)
CountRangeEverydayTotal(ctx context.Context, start *time.Time, end *time.Time) (map[string]int64, int64, error)
}
Loading

0 comments on commit 4591e6d

Please sign in to comment.