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

103 lines
3.1 KiB

2 years ago
package relation
3 years ago
import (
2 years ago
"Open_IM/pkg/common/constant"
3 years ago
"time"
)
func GetActiveUserNum(from, to time.Time) (int32, error) {
2 years ago
var num int64
2 years ago
err := ChatLogDB.Table("chat_logs").Select("count(distinct(send_id))").Where("send_time >= ? and send_time <= ?", from, to).Count(&num).Error
2 years ago
return int32(num), err
3 years ago
}
func GetIncreaseUserNum(from, to time.Time) (int32, error) {
2 years ago
var num int64
2 years ago
err := UserDB.Where("create_time >= ? and create_time <= ?", from, to).Count(&num).Error
2 years ago
return int32(num), err
3 years ago
}
func GetTotalUserNum() (int32, error) {
2 years ago
var num int64
2 years ago
err := UserDB.Count(&num).Error
2 years ago
return int32(num), err
3 years ago
}
func GetTotalUserNumByDate(to time.Time) (int32, error) {
2 years ago
var num int64
2 years ago
err := UserDB.Where("create_time <= ?", to).Count(&num).Error
2 years ago
return int32(num), err
3 years ago
}
func GetPrivateMessageNum(from, to time.Time) (int32, error) {
2 years ago
var num int64
2 years ago
err := ChatLogDB.Where("send_time >= ? and send_time <= ? and session_type = ?", from, to, 1).Count(&num).Error
2 years ago
return int32(num), err
3 years ago
}
func GetGroupMessageNum(from, to time.Time) (int32, error) {
2 years ago
var num int64
2 years ago
err := ChatLogDB.Where("send_time >= ? and send_time <= ? and session_type = ?", from, to, 2).Count(&num).Error
2 years ago
return int32(num), err
3 years ago
}
func GetIncreaseGroupNum(from, to time.Time) (int32, error) {
2 years ago
var num int64
2 years ago
err := GroupDB.Where("create_time >= ? and create_time <= ?", from, to).Count(&num).Error
2 years ago
return int32(num), err
3 years ago
}
func GetTotalGroupNum() (int32, error) {
2 years ago
var num int64
2 years ago
err := GroupDB.Count(&num).Error
2 years ago
return int32(num), err
3 years ago
}
func GetGroupNum(to time.Time) (int32, error) {
2 years ago
var num int64
2 years ago
err := GroupDB.Where("create_time <= ?", to).Count(&num).Error
2 years ago
return int32(num), err
3 years ago
}
type activeGroup struct {
Name string
Id string `gorm:"column:recv_id"`
MessageNum int `gorm:"column:message_num"`
}
func GetActiveGroups(from, to time.Time, limit int) ([]*activeGroup, error) {
var activeGroups []*activeGroup
2 years ago
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
3 years ago
for _, activeGroup := range activeGroups {
group := Group{
3 years ago
GroupID: activeGroup.Id,
}
2 years ago
GroupDB.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"`
}
func GetActiveUsers(from, to time.Time, limit int) ([]*activeUser, error) {
var activeUsers []*activeUser
2 years ago
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
3 years ago
for _, activeUser := range activeUsers {
user := User{
2 years ago
UserID: activeUser.ID,
}
2 years ago
err = UserDB.Select("user_id, name").Find(&user).Error
2 years ago
if err != nil {
continue
3 years ago
}
activeUser.Name = user.Nickname
2 years ago
activeUser.ID = user.UserID
3 years ago
}
return activeUsers, err
}