Skip to content

Commit

Permalink
lint happy.
Browse files Browse the repository at this point in the history
  • Loading branch information
PaienNate committed Jan 16, 2025
1 parent be9975b commit 238d433
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 46 deletions.
6 changes: 5 additions & 1 deletion api/api_bind.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/samber/lo"

"sealdice-core/dice"
log "sealdice-core/utils/kratos"
)

const CodeAlreadyExists = 602
Expand Down Expand Up @@ -167,7 +168,10 @@ func forceStop(c echo.Context) error {
for _, i := range diceManager.Dice {
if i.IsAlreadyLoadConfig {
i.Config.BanList.SaveChanged(i)
i.AttrsManager.CheckForSave()
err = i.AttrsManager.CheckForSave()
if err != nil {
log.Errorf("异常: %v", err)
}
i.Save(true)
for _, j := range i.ExtList {
if j.Storage != nil {
Expand Down
2 changes: 1 addition & 1 deletion dice/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2140,7 +2140,7 @@ func (d *Dice) loads() {
d.ImSession.ServiceAtNew = new(SyncMap[string, *GroupInfo])
err = model.GroupInfoListGet(d.DBOperator, func(id string, updatedAt int64, data []byte) {
var groupInfo GroupInfo
err := json.Unmarshal(data, &groupInfo)
err = json.Unmarshal(data, &groupInfo)
if err == nil {
groupInfo.GroupID = id
groupInfo.UpdatedAtTime = 0
Expand Down
15 changes: 8 additions & 7 deletions dice/dice_attrs_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,14 @@ func (am *AttrsManager) Init(d *Dice) {
return
default:
// 正常工作
am.CheckForSave()
am.CheckAndFreeUnused()
err := am.CheckForSave()
if err != nil {
log.Errorf("数据库保存程序出错: %v", err)
}
err = am.CheckAndFreeUnused()
if err != nil {
log.Errorf("数据库保存-清理程序出错: %v", err)
}
time.Sleep(60 * time.Second)
}
}
Expand Down Expand Up @@ -197,7 +203,6 @@ func (am *AttrsManager) CheckForSave() error {
})
// 整体落盘
if len(resultList) == 0 {
log.Infof("[松子调试用]定期写入用户数据(批量保存) %v 条", len(resultList))
return nil
}

Expand All @@ -211,7 +216,6 @@ func (am *AttrsManager) CheckForSave() error {
v.IsSaved = true
}
// 输出日志本次落盘了几个数据
log.Infof("[松子调试用]定期写入用户数据(批量保存) %v 条", len(resultList))

return nil
}
Expand Down Expand Up @@ -244,7 +248,6 @@ func (am *AttrsManager) CheckAndFreeUnused() error {

// 整体落盘
if len(resultList) == 0 {
log.Infof("[松子调试用]定期清理用户数据(批量保存) %v 条", len(resultList))
return nil
}

Expand All @@ -258,8 +261,6 @@ func (am *AttrsManager) CheckAndFreeUnused() error {
v, _ := am.m.LoadAndDelete(key)
v.IsSaved = true
}
// 输出日志本次落盘了几个数据
log.Infof("[松子调试用]定期清理用户数据(批量保存) %v 条", len(resultList))
return nil
}

Expand Down
8 changes: 7 additions & 1 deletion dice/ext_exp.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (

"github.com/samber/lo"
ds "github.com/sealdice/dicescript"

log "sealdice-core/utils/kratos"
)

// 如果参数中存在指定的属性,将其选出作为展示项
Expand Down Expand Up @@ -403,7 +405,11 @@ func cmdStCharFormat(mctx *MsgContext, tmpl *GameSystemTemplate) {

if tmpl != nil {
cmdStCharFormat1(mctx, tmpl, attrs.valueMap) // 这里不标记值改动,因为SetSheetType会做
mctx.Dice.AttrsManager.CheckAndFreeUnused()
err := mctx.Dice.AttrsManager.CheckAndFreeUnused()
if err != nil {
log.Errorf("check and free unused attrs error: %v", err)
return
}
}

attrs.SetSheetType(mctx.Group.System)
Expand Down
2 changes: 1 addition & 1 deletion dice/model/database/sqlite_cgo.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func createWriteDB(path string, gormConf *gorm.Config) (*gorm.DB, error) {
func SQLiteDBRWInit(path string) (*gorm.DB, *gorm.DB, error) {
// 由于现在我们只有一个写入连接,所以不需要使用事务
gormConf := &gorm.Config{
Logger: logger.Default.LogMode(logger.Silent),
Logger: logger.Default.LogMode(logger.Info),
SkipDefaultTransaction: true,
}
readDB, err := createReadDB(path, gormConf)
Expand Down
78 changes: 43 additions & 35 deletions dice/model/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"os"
"sync"

"gorm.io/gorm"

log "sealdice-core/utils/kratos"
)

Expand Down Expand Up @@ -36,6 +38,11 @@ func initEngine() {
if errEngineInstance != nil {
log.Error("数据库引擎初始化失败:", errEngineInstance)
}
err := hackMigrator(engine.GetLogDB(WRITE))
if err != nil {
log.Errorf("数据库引擎初始化失败: %v", err)
return
}
}

// getEngine 获取数据库引擎,确保只初始化一次
Expand All @@ -49,41 +56,42 @@ func GetDatabaseOperator() (DatabaseOperator, error) {
return getEngine()
}

//func hackMigrator() {
// // TODO: 将这段逻辑挪移到Migrator上
// var ids []uint64
// var logItemSums []struct {
// LogID uint64
// Count int64
// }
// logsDB.Model(&LogInfo{}).Where("size IS NULL").Pluck("id", &ids)
// if len(ids) > 0 {
// // 根据 LogInfo 表中的 IDs 查找对应的 LogOneItem 记录
// err = logsDB.Model(&LogOneItem{}).
// Where("log_id IN ?", ids).
// Group("log_id").
// Select("log_id, COUNT(*) AS count"). // 如果需要求和其他字段,可以使用 Sum
// Scan(&logItemSums).Error
// if err != nil {
// // 错误处理
// log.Infof("Error querying LogOneItem: %v", err)
// return nil, nil, err
// }
//
// // 2. 更新 LogInfo 表的 Size 字段
// for _, sum := range logItemSums {
// // 将求和结果更新到对应的 LogInfo 的 Size 字段
// err = logsDB.Model(&LogInfo{}).
// Where("id = ?", sum.LogID).
// UpdateColumn("size", sum.Count).Error // 或者是 sum.Time 等,如果要是其他字段的求和
// if err != nil {
// // 错误处理
// log.Errorf("Error updating LogInfo: %v", err)
// return nil, nil, err
// }
// }
// }
//}
func hackMigrator(logsDB *gorm.DB) error {
// TODO: 将这段逻辑挪移到Migrator上
var ids []uint64
var logItemSums []struct {
LogID uint64
Count int64
}
logsDB.Model(&LogInfo{}).Where("size IS NULL").Pluck("id", &ids)
if len(ids) > 0 {
// 根据 LogInfo 表中的 IDs 查找对应的 LogOneItem 记录
err := logsDB.Model(&LogOneItem{}).
Where("log_id IN ?", ids).
Group("log_id").
Select("log_id, COUNT(*) AS count"). // 如果需要求和其他字段,可以使用 Sum
Scan(&logItemSums).Error
if err != nil {
// 错误处理
log.Infof("Error querying LogOneItem: %v", err)
return err
}

// 2. 更新 LogInfo 表的 Size 字段
for _, sum := range logItemSums {
// 将求和结果更新到对应的 LogInfo 的 Size 字段
err = logsDB.Model(&LogInfo{}).
Where("id = ?", sum.LogID).
UpdateColumn("size", sum.Count).Error // 或者是 sum.Time 等,如果要是其他字段的求和
if err != nil {
// 错误处理
log.Errorf("Error updating LogInfo: %v", err)
return err
}
}
}
return nil
}

// DBCheck 检查数据库状态
func DBCheck() {
Expand Down

0 comments on commit 238d433

Please sign in to comment.