From 09f37b80769894aa02b25571c526824caa4a4e0f Mon Sep 17 00:00:00 2001 From: Yening Qin <710leo@gmail.com> Date: Thu, 1 Sep 2022 11:18:04 +0800 Subject: [PATCH] refactor: change alert mute clean (#1140) refactor: change alert mute clean --- src/models/alert_mute.go | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/models/alert_mute.go b/src/models/alert_mute.go index 16834a50..b7439fbd 100644 --- a/src/models/alert_mute.go +++ b/src/models/alert_mute.go @@ -126,13 +126,20 @@ func AlertMuteDel(ids []int64) error { } func AlertMuteStatistics(cluster string) (*Statistics, error) { + // clean expired first + buf := int64(30) + err := DB().Where("etime < ?", time.Now().Unix()-buf).Delete(new(AlertMute)).Error + if err != nil { + return nil, err + } + session := DB().Model(&AlertMute{}).Select("count(*) as total", "max(create_at) as last_updated") if cluster != "" { session = session.Where("(cluster like ? or cluster = ?)", "%"+cluster+"%", ClusterAll) } var stats []*Statistics - err := session.Find(&stats).Error + err = session.Find(&stats).Error if err != nil { return nil, err } @@ -141,13 +148,6 @@ func AlertMuteStatistics(cluster string) (*Statistics, error) { } func AlertMuteGetsByCluster(cluster string) ([]*AlertMute, error) { - // clean expired first - buf := int64(30) - err := DB().Where("etime < ?", time.Now().Unix()+buf).Delete(new(AlertMute)).Error - if err != nil { - return nil, err - } - // get my cluster's mutes session := DB().Model(&AlertMute{}) if cluster != "" { @@ -156,7 +156,7 @@ func AlertMuteGetsByCluster(cluster string) ([]*AlertMute, error) { var lst []*AlertMute var mlst []*AlertMute - err = session.Find(&lst).Error + err := session.Find(&lst).Error if err != nil { return nil, err }