From 7377399c950ea6eefd823d7c8823d09f2cdfb100 Mon Sep 17 00:00:00 2001 From: Cairry <115769353+Cairry@users.noreply.github.com> Date: Sun, 29 Dec 2024 14:53:42 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=B8=20Add=20get=20func=20for=20silence?= =?UTF-8?q?,=20optimize=20current=20event=20list=20data?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/repo/silence.go | 12 ++++++++++++ internal/services/event.go | 14 ++++++++++++++ internal/services/silence.go | 11 +++++++++++ 3 files changed, 37 insertions(+) diff --git a/internal/repo/silence.go b/internal/repo/silence.go index 3f66cd9..4dc2319 100644 --- a/internal/repo/silence.go +++ b/internal/repo/silence.go @@ -12,6 +12,7 @@ type ( InterSilenceRepo interface { List(r models.AlertSilenceQuery) (models.SilenceResponse, error) + Get(r models.AlertSilenceQuery) (models.AlertSilences, error) Create(r models.AlertSilences) error Update(r models.AlertSilences) error Delete(r models.AlertSilenceQuery) error @@ -59,6 +60,17 @@ func (sr SilenceRepo) List(r models.AlertSilenceQuery) (models.SilenceResponse, }, nil } +func (sr SilenceRepo) Get(r models.AlertSilenceQuery) (models.AlertSilences, error) { + var data models.AlertSilences + db := sr.db.Model(models.AlertSilences{}) + db.Where("tenant_id = ?", r.TenantId) + db.Where("fingerprint = ?", r.Fingerprint) + if err := db.First(&data).Error; err != nil { + return data, err + } + return data, nil +} + func (sr SilenceRepo) Create(r models.AlertSilences) error { err := sr.g.Create(models.AlertSilences{}, r) if err != nil { diff --git a/internal/services/event.go b/internal/services/event.go index d62dd31..4884668 100644 --- a/internal/services/event.go +++ b/internal/services/event.go @@ -2,6 +2,7 @@ package services import ( "encoding/json" + "gorm.io/gorm" "log" "strings" "time" @@ -115,6 +116,19 @@ func (e eventService) ListCurrentEvent(req interface{}) (interface{}, interface{ dataList = dsTypeDataList } + var newDontIsMuteDataList []models.AlertCurEvent + for _, event := range dataList { + _, err := newInterSilenceService(e.ctx).Get(&models.AlertSilenceQuery{ + TenantId: event.TenantId, + Fingerprint: event.Fingerprint, + Status: 0, + }) + if err != nil && err == gorm.ErrRecordNotFound { + newDontIsMuteDataList = append(newDontIsMuteDataList, event) + } + } + dataList = newDontIsMuteDataList + return models.CurEventResponse{ List: pageSlice(dataList, int(r.Page.Index), int(r.Page.Size)), Page: models.Page{ diff --git a/internal/services/silence.go b/internal/services/silence.go index 6fdc648..93314b1 100644 --- a/internal/services/silence.go +++ b/internal/services/silence.go @@ -17,6 +17,7 @@ type InterSilenceService interface { Create(req interface{}) (interface{}, interface{}) Update(req interface{}) (interface{}, interface{}) Delete(req interface{}) (interface{}, interface{}) + Get(req interface{}) (interface{}, interface{}) List(req interface{}) (interface{}, interface{}) } @@ -103,3 +104,13 @@ func (ass alertSilenceService) List(req interface{}) (interface{}, interface{}) return data, nil } + +func (ass alertSilenceService) Get(req interface{}) (interface{}, interface{}) { + r := req.(*models.AlertSilenceQuery) + data, err := ass.ctx.DB.Silence().Get(*r) + if err != nil { + return nil, err + } + + return data, nil +}