This commit is contained in:
wangchuxiao
2023-01-29 14:35:52 +08:00
parent 15166f53b0
commit 069b978b23
7 changed files with 56 additions and 1143 deletions
+55 -44
View File
@@ -2,61 +2,73 @@ package relation
import (
"Open_IM/pkg/common/constant"
"gorm.io/gorm"
"time"
)
func GetActiveUserNum(from, to time.Time) (int32, error) {
var num int64
err := ChatLogDB.Table("chat_logs").Select("count(distinct(send_id))").Where("send_time >= ? and send_time <= ?", from, to).Count(&num).Error
return int32(num), err
type Statistics struct {
DB *gorm.DB
}
func GetIncreaseUserNum(from, to time.Time) (int32, error) {
var num int64
err := UserDB.Where("create_time >= ? and create_time <= ?", from, to).Count(&num).Error
return int32(num), err
func NewStatistics(db *gorm.DB) *Statistics {
return &Statistics{DB: db}
}
func GetTotalUserNum() (int32, error) {
var num int64
err := UserDB.Count(&num).Error
return int32(num), err
func (s *Statistics) getUserModel() *gorm.DB {
return s.DB.Model(&User{})
}
func GetTotalUserNumByDate(to time.Time) (int32, error) {
var num int64
err := UserDB.Where("create_time <= ?", to).Count(&num).Error
return int32(num), err
func (s *Statistics) getChatLogModel() *gorm.DB {
return s.DB.Model(&ChatLog{})
}
func GetPrivateMessageNum(from, to time.Time) (int32, error) {
var num int64
err := ChatLogDB.Where("send_time >= ? and send_time <= ? and session_type = ?", from, to, 1).Count(&num).Error
return int32(num), err
func (s *Statistics) getGroupModel() *gorm.DB {
return s.DB.Model(&Group{})
}
func GetGroupMessageNum(from, to time.Time) (int32, error) {
var num int64
err := ChatLogDB.Where("send_time >= ? and send_time <= ? and session_type = ?", from, to, 2).Count(&num).Error
return int32(num), err
func (s *Statistics) GetActiveUserNum(from, to time.Time) (num int64, err error) {
err = s.getChatLogModel().Select("count(distinct(send_id))").Where("send_time >= ? and send_time <= ?", from, to).Count(&num).Error
return num, err
}
func GetIncreaseGroupNum(from, to time.Time) (int32, error) {
var num int64
err := GroupDB.Where("create_time >= ? and create_time <= ?", from, to).Count(&num).Error
return int32(num), err
func (s *Statistics) GetIncreaseUserNum(from, to time.Time) (num int64, err error) {
err = s.getUserModel().Where("create_time >= ? and create_time <= ?", from, to).Count(&num).Error
return num, err
}
func GetTotalGroupNum() (int32, error) {
var num int64
err := GroupDB.Count(&num).Error
return int32(num), err
func (s *Statistics) GetTotalUserNum() (num int64, err error) {
err = s.getUserModel().Count(&num).Error
return num, err
}
func GetGroupNum(to time.Time) (int32, error) {
var num int64
err := GroupDB.Where("create_time <= ?", to).Count(&num).Error
return int32(num), err
func (s *Statistics) GetTotalUserNumByDate(to time.Time) (num int64, err error) {
err = s.getUserModel().Where("create_time <= ?", to).Count(&num).Error
return num, err
}
func (s *Statistics) GetPrivateMessageNum(from, to time.Time) (num int64, err error) {
err = s.getChatLogModel().Where("send_time >= ? and send_time <= ? and session_type = ?", from, to, constant.SingleChatType).Count(&num).Error
return num, err
}
func (s *Statistics) GetGroupMessageNum(from, to time.Time) (num int64, err error) {
err = s.getChatLogModel().Where("send_time >= ? and send_time <= ? and session_type in (?)", from, to, []int{constant.GroupChatType, constant.SuperGroupChatType}).Count(&num).Error
return num, err
}
func (s *Statistics) GetIncreaseGroupNum(from, to time.Time) (num int64, err error) {
err = s.getGroupModel().Where("create_time >= ? and create_time <= ?", from, to).Count(&num).Error
return num, err
}
func (s *Statistics) GetTotalGroupNum() (num int64, err error) {
err = s.getGroupModel().Count(&num).Error
return num, err
}
func (s *Statistics) GetGroupNum(to time.Time) (num int64, err error) {
err = s.getGroupModel().Where("create_time <= ?", to).Count(&num).Error
return num, err
}
type activeGroup struct {
@@ -65,14 +77,14 @@ type activeGroup struct {
MessageNum int `gorm:"column:message_num"`
}
func GetActiveGroups(from, to time.Time, limit int) ([]*activeGroup, error) {
func (s *Statistics) GetActiveGroups(from, to time.Time, limit int) ([]*activeGroup, error) {
var activeGroups []*activeGroup
err := ChatLogDB.Select("recv_id, count(*) as message_num").Where("send_time >= ? and send_time <= ? and session_type in (?)", from, to, []int{constant.GroupChatType, constant.SuperGroupChatType}).Group("recv_id").Limit(limit).Order("message_num DESC").Find(&activeGroups).Error
err := s.getChatLogModel().Select("recv_id, count(*) as message_num").Where("send_time >= ? and send_time <= ? and session_type in (?)", from, to, []int{constant.GroupChatType, constant.SuperGroupChatType}).Group("recv_id").Limit(limit).Order("message_num DESC").Find(&activeGroups).Error
for _, activeGroup := range activeGroups {
group := Group{
GroupID: activeGroup.Id,
}
GroupDB.Where("group_id= ? ", group.GroupID).Find(&group)
s.getGroupModel().Where("group_id= ? ", group.GroupID).Find(&group)
activeGroup.Name = group.GroupName
}
return activeGroups, err
@@ -84,16 +96,15 @@ type activeUser struct {
MessageNum int `gorm:"column:message_num"`
}
func GetActiveUsers(from, to time.Time, limit int) ([]*activeUser, error) {
var activeUsers []*activeUser
err := ChatLogDB.Select("send_id, count(*) as message_num").Where("send_time >= ? and send_time <= ? and session_type = ?", from, to, constant.SingleChatType).Group("send_id").Limit(limit).Order("message_num DESC").Find(&activeUsers).Error
func (s *Statistics) GetActiveUsers(from, to time.Time, limit int) (activeUsers []*activeUser, err error) {
err = s.getChatLogModel().Select("send_id, count(*) as message_num").Where("send_time >= ? and send_time <= ? and session_type in (?)", from, to, []int{constant.SingleChatType, constant.GroupChatType, constant.SuperGroupChatType}).Group("send_id").Limit(limit).Order("message_num DESC").Find(&activeUsers).Error
for _, activeUser := range activeUsers {
user := User{
UserID: activeUser.ID,
}
err = UserDB.Select("user_id, name").Find(&user).Error
err = s.getUserModel().Select("user_id, name").Find(&user).Error
if err != nil {
continue
return nil, err
}
activeUser.Name = user.Nickname
activeUser.ID = user.UserID