Skip to content

Commit

Permalink
feat(系统监控): 优化代码
Browse files Browse the repository at this point in the history
  • Loading branch information
devhaozi committed Jan 12, 2024
1 parent a9c1911 commit 55fc516
Showing 1 changed file with 9 additions and 10 deletions.
19 changes: 9 additions & 10 deletions app/console/commands/monitoring.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ func (receiver *Monitoring) Handle(ctx console.Context) error {
return nil
}

var setting models.Setting
monitor := services.NewSettingImpl().Get(models.SettingKeyMonitor)
setting := services.NewSettingImpl()
monitor := setting.Get(models.SettingKeyMonitor)
if !cast.ToBool(monitor) {
return nil
}
Expand All @@ -62,6 +62,9 @@ func (receiver *Monitoring) Handle(ctx console.Context) error {
cpu.Flags = nil
}

if internal.Status != internal.StatusNormal {
return nil
}
err := facades.Orm().Query().Create(&models.Monitor{
Info: info,
})
Expand All @@ -72,20 +75,16 @@ func (receiver *Monitoring) Handle(ctx console.Context) error {
}

// 删除过期数据
err = facades.Orm().Query().Where("key", "monitor_days").First(&setting)
monitorDays := setting.Get(models.SettingKeyMonitorDays)
days, err := strconv.Atoi(monitorDays)
if err != nil {
return nil
}
if setting.Value == "0" || len(setting.Value) == 0 {
return nil
}

days, err := strconv.Atoi(setting.Value)
if err != nil {
if days <= 0 || internal.Status != internal.StatusNormal {
return nil
}
_, err = facades.Orm().Query().Where("created_at < ?", carbon.Now().SubDays(days).ToDateTimeString()).Delete(&models.Monitor{})
if err != nil {
if _, err = facades.Orm().Query().Where("created_at < ?", carbon.Now().SubDays(days).ToDateTimeString()).Delete(&models.Monitor{}); err != nil {
facades.Log().Infof("[面板] 系统监控删除过期数据失败: %s", err.Error())
return nil
}
Expand Down

0 comments on commit 55fc516

Please sign in to comment.