Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

statistic api #104

Merged
merged 4 commits into from
Aug 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions internal/api/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,3 +304,11 @@ func (o *AdminApi) UpdateApplet(c *gin.Context) {
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)
}
4 changes: 4 additions & 0 deletions internal/api/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,8 @@ func NewAdminRoute(router gin.IRouter, discov discoveryregistry.SvcDiscoveryRegi
initGroup.POST("/get", admin.GetClientConfig) // 获取客户端初始化配置
initGroup.POST("/set", admin.SetClientConfig) // 设置客户端初始化配置
initGroup.POST("/del", admin.DelClientConfig) // 删除客户端初始化配置

statistic := router.Group("/statistic", mw.CheckAdmin)
statistic.POST("/new_user_count", admin.NewUserCount)
statistic.POST("/login_user_count", admin.LoginUserCount)
}
57 changes: 57 additions & 0 deletions internal/rpc/chat/statistic.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package chat

import (
"context"
"github.com/OpenIMSDK/chat/pkg/proto/chat"
"github.com/OpenIMSDK/tools/errs"
"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)
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)
if err != nil {
return nil, err
}
resp.Total = total
resp.Before = before
resp.Count = count
return resp, nil
}
20 changes: 20 additions & 0 deletions pkg/common/db/database/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ type ChatDatabaseInterface interface {
LoginRecord(ctx context.Context, record *table.UserLoginRecord, verifyCodeID *uint) error
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)
}

func NewChatDatabase(db *gorm.DB) ChatDatabaseInterface {
Expand Down Expand Up @@ -217,3 +221,19 @@ func (o *ChatDatabase) UpdatePasswordAndDeleteVerifyCode(ctx context.Context, us
return nil
})
}

func (o *ChatDatabase) NewUserCountTotal(ctx context.Context, before *time.Time) (int64, error) {
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) {
return o.userLoginRecord.CountRangeEverydayTotal(ctx, start, end)
}
34 changes: 34 additions & 0 deletions pkg/common/db/model/chat/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package chat

import (
"context"
"time"

"github.com/OpenIMSDK/tools/errs"
"gorm.io/gorm"
Expand All @@ -38,3 +39,36 @@ func (o *Register) NewTx(tx any) chat.RegisterInterface {
func (o *Register) Create(ctx context.Context, registers ...*chat.Register) error {
return errs.Wrap(o.db.WithContext(ctx).Create(registers).Error)
}

func (o *Register) CountTotal(ctx context.Context, before *time.Time) (count int64, err error) {
db := o.db.WithContext(ctx).Model(&chat.Register{})
if before != nil {
db.Where("create_time < ?", before)
}
if err := db.Count(&count).Error; err != nil {
return 0, errs.Wrap(err)
}
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
}
37 changes: 37 additions & 0 deletions pkg/common/db/model/chat/user_login_record.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ package chat

import (
"context"

"github.com/OpenIMSDK/tools/errs"
"time"

"gorm.io/gorm"

"github.com/OpenIMSDK/chat/pkg/common/db/table/chat"
Expand All @@ -38,3 +42,36 @@ func (o *UserLoginRecord) NewTx(tx any) chat.UserLoginRecordInterface {
func (o *UserLoginRecord) Create(ctx context.Context, records ...*chat.UserLoginRecord) error {
return o.db.WithContext(ctx).Create(&records).Error
}

func (o *UserLoginRecord) CountTotal(ctx context.Context, before *time.Time) (count int64, err error) {
db := o.db.WithContext(ctx).Model(&chat.UserLoginRecord{})
if before != nil {
db.Where("create_time < ?", before)
}
if err := db.Count(&count).Error; err != nil {
return 0, errs.Wrap(err)
}
return count, nil
}

func (o *UserLoginRecord) 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.UserLoginRecord{}).
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
}
2 changes: 2 additions & 0 deletions pkg/common/db/table/chat/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,6 @@ func (Register) TableName() string {
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: 2 additions & 0 deletions pkg/common/db/table/chat/user_login_record.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,6 @@ func (UserLoginRecord) TableName() string {
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)
}
Loading