2023-01-28 13:19:36 +08:00
package relation
2022-02-07 08:44:21 +08:00
import (
2022-08-31 00:43:06 +08:00
"Open_IM/pkg/common/constant"
2023-01-29 14:35:52 +08:00
"gorm.io/gorm"
2022-02-07 08:44:21 +08:00
"time"
)
2023-01-29 14:35:52 +08:00
type Statistics struct {
DB * gorm . DB
2022-02-07 08:44:21 +08:00
}
2023-01-29 14:35:52 +08:00
func NewStatistics ( db * gorm . DB ) * Statistics {
return & Statistics { DB : db }
2022-02-07 08:44:21 +08:00
}
2023-01-29 14:35:52 +08:00
func ( s * Statistics ) getUserModel ( ) * gorm . DB {
return s . DB . Model ( & User { } )
2022-02-07 08:44:21 +08:00
}
2023-01-29 14:35:52 +08:00
func ( s * Statistics ) getChatLogModel ( ) * gorm . DB {
return s . DB . Model ( & ChatLog { } )
2022-02-07 08:44:21 +08:00
}
2023-01-29 14:35:52 +08:00
func ( s * Statistics ) getGroupModel ( ) * gorm . DB {
return s . DB . Model ( & Group { } )
2022-02-07 08:44:21 +08:00
}
2023-01-29 14:35:52 +08:00
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
2022-02-07 08:44:21 +08:00
}
2023-01-29 14:35:52 +08:00
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
2022-02-07 08:44:21 +08:00
}
2023-01-29 14:35:52 +08:00
func ( s * Statistics ) GetTotalUserNum ( ) ( num int64 , err error ) {
err = s . getUserModel ( ) . Count ( & num ) . Error
return num , err
2022-02-07 08:44:21 +08:00
}
2023-01-29 14:35:52 +08:00
func ( s * Statistics ) GetTotalUserNumByDate ( to time . Time ) ( num int64 , err error ) {
err = s . getUserModel ( ) . Where ( "create_time <= ?" , to ) . Count ( & num ) . Error
return num , err
}
2023-01-29 17:20:31 +08:00
func ( s * Statistics ) GetSingleChatMessageNum ( from , to time . Time ) ( num int64 , err error ) {
2023-01-29 14:35:52 +08:00
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
2022-02-07 08:44:21 +08:00
}
2023-01-29 17:20:31 +08:00
func ( s * Statistics ) GetActiveGroups ( from , to time . Time , limit int ) ( [ ] * ActiveGroup , error ) {
var activeGroups [ ] * ActiveGroup
2023-01-29 14:35:52 +08:00
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
2022-02-07 08:44:21 +08:00
for _ , activeGroup := range activeGroups {
2023-01-04 17:22:55 +08:00
group := Group {
2023-01-29 17:20:31 +08:00
GroupID : activeGroup . ID ,
2022-02-07 08:44:21 +08:00
}
2023-01-29 14:35:52 +08:00
s . getGroupModel ( ) . Where ( "group_id= ? " , group . GroupID ) . Find ( & group )
2022-02-07 08:44:21 +08:00
activeGroup . Name = group . GroupName
}
return activeGroups , err
}
2023-01-29 17:20:31 +08:00
func ( s * Statistics ) GetActiveUsers ( from , to time . Time , limit int ) ( activeUsers [ ] * ActiveUser , err error ) {
2023-01-29 14:35:52 +08:00
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
2022-02-07 08:44:21 +08:00
for _ , activeUser := range activeUsers {
2023-01-04 17:22:55 +08:00
user := User {
2022-08-31 00:43:06 +08:00
UserID : activeUser . ID ,
}
2023-01-29 14:35:52 +08:00
err = s . getUserModel ( ) . Select ( "user_id, name" ) . Find ( & user ) . Error
2022-08-31 00:43:06 +08:00
if err != nil {
2023-01-29 14:35:52 +08:00
return nil , err
2022-02-07 08:44:21 +08:00
}
activeUser . Name = user . Nickname
2022-09-06 17:46:55 +08:00
activeUser . ID = user . UserID
2022-02-07 08:44:21 +08:00
}
return activeUsers , err
}