59 lines
2.4 KiB
Go
59 lines
2.4 KiB
Go
package logic
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"time"
|
|
|
|
"bj_power_mes/ent"
|
|
"bj_power_mes/ent/alert"
|
|
"bj_power_mes/ent/backuprecord"
|
|
)
|
|
|
|
// 备份超期预警(backup_overdue):
|
|
// 顶栏「数据归档清理提醒」角标已移除,提醒统一由预警中心承载。
|
|
// 事件驱动(无定时器):跟随看板快照等高频入口做惰性检查,每日最多落一条未读预警;
|
|
// 预警去重=同类型存在 UNREAD 即不再落(内存 lastCreatedDay 双保险,避免用户标记已读后被反复轰炸)。
|
|
const AlertTypeBackupOverdue = "backup_overdue"
|
|
|
|
// backupOverdueLastDay 最近一次落 backup_overdue 预警的日期(进程内,重启后允许当天再落一次)
|
|
var backupOverdueLastDay string
|
|
|
|
// CheckBackupOverdueAlert 惰性检查备份超期:超期则落预警,已恢复则自动关闭未读预警。
|
|
// 由 DashboardSnapshot 等高频路径调用;内部自带去重与天级节流,重复调用无副作用。
|
|
func (s *Service) CheckBackupOverdueAlert(ctx context.Context) {
|
|
days := s.CfgInt(ctx, CfgArchiveRemindDays, 7)
|
|
if days <= 0 {
|
|
return // 提醒周期=0:用户已关闭提醒
|
|
}
|
|
last, err := s.ctx.EntClient.BackupRecord.Query().
|
|
Where(backuprecord.StatusEQ("success")).
|
|
Order(ent.Desc(backuprecord.FieldExecutedAt)).
|
|
First(ctx)
|
|
if err != nil {
|
|
return // 从未成功备份过(初始化阶段)或查询失败:不提醒
|
|
}
|
|
due := last.ExecutedAt + int64(days)*86400
|
|
if time.Now().Unix() < due {
|
|
// 已在周期内:自动关闭历史遗留的未读超期预警(备份完成后自愈)
|
|
_, _ = s.ctx.EntClient.Alert.Update().
|
|
Where(alert.TypeEQ(AlertTypeBackupOverdue), alert.StatusEQ("UNREAD")).
|
|
SetStatus("READ").Save(ctx)
|
|
return
|
|
}
|
|
// 超期:存在未读预警则不重复落;当日已落过(被处置后)也不再落
|
|
n, _ := s.ctx.EntClient.Alert.Query().
|
|
Where(alert.TypeEQ(AlertTypeBackupOverdue), alert.StatusEQ("UNREAD")).Count(ctx)
|
|
if n > 0 {
|
|
return
|
|
}
|
|
today := time.Now().Format("2006-01-02")
|
|
if backupOverdueLastDay == today {
|
|
return
|
|
}
|
|
backupOverdueLastDay = today
|
|
msg := fmt.Sprintf("距上次成功备份已超过 %d 天(最近成功备份:%s)。请先在「数据归档」页确认 U 盘备份完整,再手动清理旧数据(仅提醒,不自动删除)。",
|
|
days, time.Unix(last.ExecutedAt, 0).Format("2006-01-02 15:04"))
|
|
_ = s.CreateAlert(ctx, AlertTypeBackupOverdue, "警告", "数据归档", msg)
|
|
}
|