Skip to content

Commit

Permalink
alerts wip/4
Browse files Browse the repository at this point in the history
  • Loading branch information
mmetc committed Sep 23, 2024
1 parent f152929 commit 4cc9406
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 13 deletions.
4 changes: 3 additions & 1 deletion pkg/apiserver/controllers/v1/alerts.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,9 @@ func (c *Controller) CreateAlert(gctx *gin.Context) {

// FindAlerts: returns alerts from the database based on the specified filter
func (c *Controller) FindAlerts(gctx *gin.Context) {
result, err := c.DBClient.QueryAlertWithFilter(gctx.Request.URL.Query())
ctx := gctx.Request.Context()

result, err := c.DBClient.QueryAlertWithFilter(ctx, gctx.Request.URL.Query())
if err != nil {
c.HandleDBErrors(gctx, err)
return
Expand Down
20 changes: 10 additions & 10 deletions pkg/database/alerts.go
Original file line number Diff line number Diff line change
Expand Up @@ -996,11 +996,11 @@ func (c *Client) AlertsCountPerScenario(ctx context.Context, filters map[string]
return counts, nil
}

func (c *Client) TotalAlerts() (int, error) {
return c.Ent.Alert.Query().Count(c.CTX)
func (c *Client) TotalAlerts(ctx context.Context) (int, error) {
return c.Ent.Alert.Query().Count(ctx)
}

func (c *Client) QueryAlertWithFilter(filter map[string][]string) ([]*ent.Alert, error) {
func (c *Client) QueryAlertWithFilter(ctx context.Context, filter map[string][]string) ([]*ent.Alert, error) {
sort := "DESC" // we sort by desc by default

if val, ok := filter["sort"]; ok {
Expand Down Expand Up @@ -1047,7 +1047,7 @@ func (c *Client) QueryAlertWithFilter(filter map[string][]string) ([]*ent.Alert,
WithOwner()

if limit == 0 {
limit, err = alerts.Count(c.CTX)
limit, err = alerts.Count(ctx)
if err != nil {
return nil, fmt.Errorf("unable to count nb alerts: %w", err)
}
Expand All @@ -1059,7 +1059,7 @@ func (c *Client) QueryAlertWithFilter(filter map[string][]string) ([]*ent.Alert,
alerts = alerts.Order(ent.Desc(alert.FieldCreatedAt), ent.Desc(alert.FieldID))
}

result, err := alerts.Limit(paginationSize).Offset(offset).All(c.CTX)
result, err := alerts.Limit(paginationSize).Offset(offset).All(ctx)
if err != nil {
return nil, errors.Wrapf(QueryFail, "pagination size: %d, offset: %d: %s", paginationSize, offset, err)
}
Expand Down Expand Up @@ -1088,35 +1088,35 @@ func (c *Client) QueryAlertWithFilter(filter map[string][]string) ([]*ent.Alert,
return ret, nil
}

func (c *Client) DeleteAlertGraphBatch(alertItems []*ent.Alert) (int, error) {
func (c *Client) DeleteAlertGraphBatch(ctx context.Context, alertItems []*ent.Alert) (int, error) {
idList := make([]int, 0)
for _, alert := range alertItems {
idList = append(idList, alert.ID)
}

_, err := c.Ent.Event.Delete().
Where(event.HasOwnerWith(alert.IDIn(idList...))).Exec(c.CTX)
Where(event.HasOwnerWith(alert.IDIn(idList...))).Exec(ctx)
if err != nil {
c.Log.Warningf("DeleteAlertGraphBatch : %s", err)
return 0, errors.Wrapf(DeleteFail, "alert graph delete batch events")
}

_, err = c.Ent.Meta.Delete().
Where(meta.HasOwnerWith(alert.IDIn(idList...))).Exec(c.CTX)
Where(meta.HasOwnerWith(alert.IDIn(idList...))).Exec(ctx)
if err != nil {
c.Log.Warningf("DeleteAlertGraphBatch : %s", err)
return 0, errors.Wrapf(DeleteFail, "alert graph delete batch meta")
}

_, err = c.Ent.Decision.Delete().
Where(decision.HasOwnerWith(alert.IDIn(idList...))).Exec(c.CTX)
Where(decision.HasOwnerWith(alert.IDIn(idList...))).Exec(ctx)
if err != nil {
c.Log.Warningf("DeleteAlertGraphBatch : %s", err)
return 0, errors.Wrapf(DeleteFail, "alert graph delete batch decisions")
}

deleted, err := c.Ent.Alert.Delete().
Where(alert.IDIn(idList...)).Exec(c.CTX)
Where(alert.IDIn(idList...)).Exec(ctx)
if err != nil {
c.Log.Warningf("DeleteAlertGraphBatch : %s", err)
return deleted, errors.Wrapf(DeleteFail, "alert graph delete batch")
Expand Down
4 changes: 2 additions & 2 deletions pkg/database/flush.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ func (c *Client) FlushAlerts(ctx context.Context, MaxAge string, MaxItems int) e
c.FlushOrphans(ctx)
c.Log.Debug("Done flushing orphan alerts")

totalAlerts, err = c.TotalAlerts()
totalAlerts, err = c.TotalAlerts(ctx)
if err != nil {
c.Log.Warningf("FlushAlerts (max items count): %s", err)
return fmt.Errorf("unable to get alerts count: %w", err)
Expand Down Expand Up @@ -268,7 +268,7 @@ func (c *Client) FlushAlerts(ctx context.Context, MaxAge string, MaxItems int) e
// This gives us the oldest alert that we want to keep
// We then delete all the alerts with an id lower than this one
// We can do this because the id is auto-increment, and the database won't reuse the same id twice
lastAlert, err := c.QueryAlertWithFilter(map[string][]string{
lastAlert, err := c.QueryAlertWithFilter(ctx, map[string][]string{
"sort": {"DESC"},
"limit": {"1"},
// we do not care about fetching the edges, we just want the id
Expand Down

0 comments on commit 4cc9406

Please sign in to comment.