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/pkg/common/db/relation/statistics_model.go

114 lines
3.8 KiB

2 years ago
package relation
3 years ago
import (
2 years ago
"Open_IM/pkg/common/constant"
2 years ago
"gorm.io/gorm"
3 years ago
"time"
)
2 years ago
type Statistics struct {
DB *gorm.DB
3 years ago
}
2 years ago
func NewStatistics(db *gorm.DB) *Statistics {
return &Statistics{DB: db}
3 years ago
}
2 years ago
func (s *Statistics) getUserModel() *gorm.DB {
return s.DB.Model(&User{})
3 years ago
}
2 years ago
func (s *Statistics) getChatLogModel() *gorm.DB {
return s.DB.Model(&ChatLog{})
3 years ago
}
2 years ago
func (s *Statistics) getGroupModel() *gorm.DB {
return s.DB.Model(&Group{})
3 years ago
}
2 years ago
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
3 years ago
}
2 years ago
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
3 years ago
}
2 years ago
func (s *Statistics) GetTotalUserNum() (num int64, err error) {
err = s.getUserModel().Count(&num).Error
return num, err
3 years ago
}
2 years ago
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
3 years ago
}
type activeGroup struct {
Name string
Id string `gorm:"column:recv_id"`
MessageNum int `gorm:"column:message_num"`
}
2 years ago
func (s *Statistics) GetActiveGroups(from, to time.Time, limit int) ([]*activeGroup, error) {
3 years ago
var activeGroups []*activeGroup
2 years ago
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
3 years ago
for _, activeGroup := range activeGroups {
group := Group{
3 years ago
GroupID: activeGroup.Id,
}
2 years ago
s.getGroupModel().Where("group_id= ? ", group.GroupID).Find(&group)
3 years ago
activeGroup.Name = group.GroupName
}
return activeGroups, err
}
type activeUser struct {
Name string
2 years ago
ID string `gorm:"column:send_id"`
3 years ago
MessageNum int `gorm:"column:message_num"`
}
2 years ago
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
3 years ago
for _, activeUser := range activeUsers {
user := User{
2 years ago
UserID: activeUser.ID,
}
2 years ago
err = s.getUserModel().Select("user_id, name").Find(&user).Error
2 years ago
if err != nil {
2 years ago
return nil, err
3 years ago
}
activeUser.Name = user.Nickname
2 years ago
activeUser.ID = user.UserID
3 years ago
}
return activeUsers, err
}