Merge remote-tracking branch 'origin/errcode' into errcode

This commit is contained in:
Gordon
2023-02-10 20:58:54 +08:00
109 changed files with 992 additions and 36145 deletions
-100
View File
@@ -1,100 +0,0 @@
package relation
import (
"Open_IM/pkg/common/config"
"fmt"
"time"
"gorm.io/driver/mysql"
"gorm.io/gorm"
"gorm.io/gorm/logger"
)
type Mysql struct {
gormConn *gorm.DB
}
func (m *Mysql) GormConn() *gorm.DB {
return m.gormConn
}
func (m *Mysql) SetGormConn(gormConn *gorm.DB) {
m.gormConn = gormConn
}
func (m *Mysql) InitConn() *Mysql {
dsn := fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8mb4&parseTime=true&loc=Local",
config.Config.Mysql.DBUserName, config.Config.Mysql.DBPassword, config.Config.Mysql.DBAddress[0], "mysql")
var db *gorm.DB
db, err := gorm.Open(mysql.Open(dsn), nil)
if err != nil {
time.Sleep(time.Duration(30) * time.Second)
db, err = gorm.Open(mysql.Open(dsn), nil)
if err != nil {
panic(err.Error() + " open failed " + dsn)
}
}
sql := fmt.Sprintf("CREATE DATABASE IF NOT EXISTS %s default charset utf8 COLLATE utf8_general_ci;", config.Config.Mysql.DBDatabaseName)
err = db.Exec(sql).Error
if err != nil {
panic(err.Error() + " Exec failed:" + sql)
}
dsn = fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8mb4&parseTime=true&loc=Local",
config.Config.Mysql.DBUserName, config.Config.Mysql.DBPassword, config.Config.Mysql.DBAddress[0], config.Config.Mysql.DBDatabaseName)
newLogger := logger.New(
Writer{},
logger.Config{
SlowThreshold: time.Duration(config.Config.Mysql.SlowThreshold) * time.Millisecond, // Slow SQL threshold
LogLevel: logger.LogLevel(config.Config.Mysql.LogLevel), // Log level
IgnoreRecordNotFoundError: true, // Ignore ErrRecordNotFound error for logger
Colorful: true, // Disable color
},
)
db, err = gorm.Open(mysql.Open(dsn), &gorm.Config{
Logger: newLogger,
})
if err != nil {
panic(err.Error() + " Open failed " + dsn)
}
sqlDB, err := db.DB()
if err != nil {
panic(err.Error() + " DB.DB() failed ")
}
sqlDB.SetConnMaxLifetime(time.Second * time.Duration(config.Config.Mysql.DBMaxLifeTime))
sqlDB.SetMaxOpenConns(config.Config.Mysql.DBMaxOpenConns)
sqlDB.SetMaxIdleConns(config.Config.Mysql.DBMaxIdleConns)
if db == nil {
panic("db is nil")
}
m.SetGormConn(db)
return m
}
//models := []interface{}{&Friend{}, &FriendRequest{}, &Group{}, &GroupMember{}, &GroupRequest{},
// &User{}, &Black{}, &ChatLog{}, &Conversation{}, &AppVersion{}}
func (m *Mysql) AutoMigrateModel(model interface{}) error {
err := m.gormConn.AutoMigrate(model)
if err != nil {
return err
}
m.gormConn.Set("gorm:table_options", "CHARSET=utf8")
m.gormConn.Set("gorm:table_options", "collation=utf8_unicode_ci")
_ = m.gormConn.Migrator().CreateTable(model)
return nil
}
type Writer struct{}
func (w Writer) Printf(format string, args ...interface{}) {
fmt.Printf(format, args...)
}
func getDBConn(db *gorm.DB, tx []any) *gorm.DB {
if len(tx) > 0 {
if txDB, ok := tx[0].(*gorm.DB); ok {
return txDB
}
}
return db
}
-101
View File
@@ -1,101 +0,0 @@
package relation
import (
"Open_IM/pkg/common/constant"
"gorm.io/gorm"
"time"
)
type Statistics struct {
DB *gorm.DB
}
func NewStatistics(db *gorm.DB) *Statistics {
return &Statistics{DB: db}
}
func (s *Statistics) getUserModel() *gorm.DB {
return s.DB.Model(&User{})
}
func (s *Statistics) getChatLogModel() *gorm.DB {
return s.DB.Model(&ChatLog{})
}
func (s *Statistics) getGroupModel() *gorm.DB {
return s.DB.Model(&Group{})
}
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 (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 (s *Statistics) GetTotalUserNum() (num int64, err error) {
err = s.getUserModel().Count(&num).Error
return 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) GetSingleChatMessageNum(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
}
func (s *Statistics) GetActiveGroups(from, to time.Time, limit int) ([]*ActiveGroup, error) {
var activeGroups []*ActiveGroup
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,
}
s.getGroupModel().Where("group_id= ? ", group.GroupID).Find(&group)
activeGroup.Name = group.GroupName
}
return activeGroups, err
}
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 = s.getUserModel().Select("user_id, name").Find(&user).Error
if err != nil {
return nil, err
}
activeUser.Name = user.Nickname
activeUser.ID = user.UserID
}
return activeUsers, err
}