Skip to content

Commit

Permalink
🎸 Add get func for silence, optimize current event list data
Browse files Browse the repository at this point in the history
  • Loading branch information
Cairry committed Dec 29, 2024
1 parent 9ac8c2a commit 7377399
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
12 changes: 12 additions & 0 deletions internal/repo/silence.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
14 changes: 14 additions & 0 deletions internal/services/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package services

import (
"encoding/json"
"gorm.io/gorm"
"log"
"strings"
"time"
Expand Down Expand Up @@ -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{
Expand Down
11 changes: 11 additions & 0 deletions internal/services/silence.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{})
}

Expand Down Expand Up @@ -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
}

0 comments on commit 7377399

Please sign in to comment.