Skip to content

Commit

Permalink
Merge pull request #3133 from target/alert-feedback
Browse files Browse the repository at this point in the history
add option to set noise reasons for an alert
  • Loading branch information
mastercactapus authored Jul 12, 2023
2 parents e206381 + eadc84a commit 34dcd40
Show file tree
Hide file tree
Showing 19 changed files with 805 additions and 100 deletions.
7 changes: 7 additions & 0 deletions alert/feedback.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package alert

// Feedback represents user provided information about a given alert
type Feedback struct {
AlertID int
NoiseReason string
}
59 changes: 43 additions & 16 deletions alert/queries.sql
Original file line number Diff line number Diff line change
@@ -1,23 +1,50 @@
-- name: LockOneAlertService :one
SELECT maintenance_expires_at notnull::bool AS is_maint_mode,
SELECT
maintenance_expires_at NOTNULL::bool AS is_maint_mode,
alerts.status
FROM services svc
FROM
services svc
JOIN alerts ON alerts.service_id = svc.id
WHERE alerts.id = $1 FOR
UPDATE;
WHERE
alerts.id = $1
FOR UPDATE;

-- name: RequestAlertEscalationByTime :one
UPDATE escalation_policy_state
SET force_escalation = TRUE
WHERE alert_id = $1
AND (
last_escalation <= $2::timestamptz
OR last_escalation IS NULL
) RETURNING TRUE;
UPDATE
escalation_policy_state
SET
force_escalation = TRUE
WHERE
alert_id = $1
AND (last_escalation <= $2::timestamptz
OR last_escalation IS NULL)
RETURNING
TRUE;

-- name: AlertHasEPState :one
SELECT EXISTS (
SELECT 1
FROM escalation_policy_state
WHERE alert_id = $1
) AS has_ep_state;
SELECT
EXISTS (
SELECT
1
FROM
escalation_policy_state
WHERE
alert_id = $1) AS has_ep_state;

-- name: AlertFeedback :one
SELECT
alert_id,
noise_reason
FROM
alert_feedback
WHERE
alert_id = $1;

-- name: SetAlertFeedback :exec
INSERT INTO alert_feedback(alert_id, noise_reason)
VALUES ($1, $2)
ON CONFLICT (alert_id)
DO UPDATE SET
noise_reason = $2
WHERE
alert_feedback.alert_id = $1;
44 changes: 44 additions & 0 deletions alert/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -791,3 +791,47 @@ func (s *Store) State(ctx context.Context, alertIDs []int) ([]State, error) {

return list, nil
}

func (s *Store) Feedback(ctx context.Context, alertID int) (*Feedback, error) {
err := permission.LimitCheckAny(ctx, permission.System, permission.User)
if err != nil {
return nil, err
}

row, err := gadb.New(s.db).AlertFeedback(ctx, int64(alertID))
if errors.Is(err, sql.ErrNoRows) {
return &Feedback{
AlertID: alertID,
}, nil
}
if err != nil {
return nil, err
}

return &Feedback{
AlertID: int(row.AlertID),
NoiseReason: row.NoiseReason,
}, err
}

func (s Store) UpdateFeedback(ctx context.Context, feedback *Feedback) error {
err := permission.LimitCheckAny(ctx, permission.System, permission.User)
if err != nil {
return err
}

err = validate.Text("NoiseReason", feedback.NoiseReason, 1, 255)
if err != nil {
return err
}

err = gadb.New(s.db).SetAlertFeedback(ctx, gadb.SetAlertFeedbackParams{
AlertID: int64(feedback.AlertID),
NoiseReason: feedback.NoiseReason,
})
if err != nil {
return err
}

return nil
}
5 changes: 5 additions & 0 deletions gadb/models.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

78 changes: 62 additions & 16 deletions gadb/queries.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 34dcd40

Please sign in to comment.