Skip to content

Commit

Permalink
Rename incident.Id -> ID & drop incident#ID()
Browse files Browse the repository at this point in the history
  • Loading branch information
yhabteab committed Aug 2, 2024
1 parent f9a9bec commit 39257c4
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 29 deletions.
28 changes: 12 additions & 16 deletions internal/incident/incident.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
type escalationID = int64

type Incident struct {
Id int64 `db:"id"`
ID int64 `db:"id"`
ObjectID types.Binary `db:"object_id"`
StartedAt types.UnixMilli `db:"started_at"`
RecoveredAt types.UnixMilli `db:"recovered_at"`
Expand Down Expand Up @@ -79,11 +79,7 @@ func NewIncident(
}

func (i *Incident) String() string {
return fmt.Sprintf("#%d", i.Id)
}

func (i *Incident) ID() int64 {
return i.Id
return fmt.Sprintf("#%d", i.ID)
}

func (i *Incident) HasManager() bool {
Expand Down Expand Up @@ -301,7 +297,7 @@ func (i *Incident) processSeverityChangedEvent(ctx context.Context, tx *sqlx.Tx,
i.logger.Infof("Incident severity changed from %s to %s", oldSeverity.String(), newSeverity.String())

hr := &HistoryRow{
IncidentID: i.Id,
IncidentID: i.ID,
EventID: utils.ToDBInt(ev.ID),
Time: types.UnixMilli(time.Now()),
Type: IncidentSeverityChanged,
Expand All @@ -322,7 +318,7 @@ func (i *Incident) processSeverityChangedEvent(ctx context.Context, tx *sqlx.Tx,
RemoveCurrent(i.Object)

hr = &HistoryRow{
IncidentID: i.Id,
IncidentID: i.ID,
EventID: utils.ToDBInt(ev.ID),
Time: i.RecoveredAt,
Type: Closed,
Expand Down Expand Up @@ -358,7 +354,7 @@ func (i *Incident) processIncidentOpenedEvent(ctx context.Context, tx *sqlx.Tx,
i.logger.Infow(fmt.Sprintf("Source %d opened incident at severity %q", ev.SourceId, i.Severity.String()), zap.String("message", ev.Message))

hr := &HistoryRow{
IncidentID: i.Id,
IncidentID: i.ID,
Type: Opened,
Time: types.UnixMilli(ev.Time),
EventID: utils.ToDBInt(ev.ID),
Expand All @@ -381,7 +377,7 @@ func (i *Incident) handleMuteUnmute(ctx context.Context, tx *sqlx.Tx, ev *event.
return nil
}

hr := &HistoryRow{IncidentID: i.Id, EventID: utils.ToDBInt(ev.ID), Time: types.UnixMilli(time.Now())}
hr := &HistoryRow{IncidentID: i.ID, EventID: utils.ToDBInt(ev.ID), Time: types.UnixMilli(time.Now())}
logger := i.logger.With(zap.String("event", ev.String()))
if i.Object.IsMuted() {
hr.Type = Muted
Expand Down Expand Up @@ -415,7 +411,7 @@ func (i *Incident) onFilterRuleMatch(ctx context.Context, r *rule.Rule, tx *sqlx
}

hr := &HistoryRow{
IncidentID: i.Id,
IncidentID: i.ID,
Time: types.UnixMilli(time.Now()),
EventID: utils.ToDBInt(ev.ID),
RuleID: utils.ToDBInt(r.ID),
Expand Down Expand Up @@ -505,7 +501,7 @@ func (i *Incident) triggerEscalations(ctx context.Context, tx *sqlx.Tx, ev *even
}

hr := &HistoryRow{
IncidentID: i.Id,
IncidentID: i.ID,
Time: state.TriggeredAt,
EventID: utils.ToDBInt(ev.ID),
RuleEscalationID: utils.ToDBInt(state.RuleEscalationID),
Expand Down Expand Up @@ -539,7 +535,7 @@ func (i *Incident) notifyContacts(ctx context.Context, ev *event.Event, notifica
}

incidentUrl := baseUrl.JoinPath("/notifications/incident")
incidentUrl.RawQuery = fmt.Sprintf("id=%d", i.ID())
incidentUrl.RawQuery = fmt.Sprintf("id=%d", i.ID)

req := &plugin.NotificationRequest{
Object: &plugin.Object{
Expand All @@ -549,7 +545,7 @@ func (i *Incident) notifyContacts(ctx context.Context, ev *event.Event, notifica
ExtraTags: i.Object.ExtraTags,
},
Incident: &plugin.Incident{
Id: i.Id,
Id: i.ID,
Url: incidentUrl.String(),
Severity: i.Severity.String(),
},
Expand Down Expand Up @@ -654,7 +650,7 @@ func (i *Incident) processAcknowledgementEvent(ctx context.Context, tx *sqlx.Tx,
i.logger.Infof("Contact %q role changed from %s to %s", contact.String(), oldRole.String(), newRole.String())

hr := &HistoryRow{
IncidentID: i.Id,
IncidentID: i.ID,
Key: recipientKey,
EventID: utils.ToDBInt(ev.ID),
Type: RecipientRoleChanged,
Expand Down Expand Up @@ -732,7 +728,7 @@ func (i *Incident) getRecipientsChannel(t time.Time) rule.ContactChannels {
func (i *Incident) restoreRecipients(ctx context.Context) error {
contact := &ContactRow{}
var contacts []*ContactRow
err := i.db.SelectContext(ctx, &contacts, i.db.Rebind(i.db.BuildSelectStmt(contact, contact)+` WHERE "incident_id" = ?`), i.Id)
err := i.db.SelectContext(ctx, &contacts, i.db.Rebind(i.db.BuildSelectStmt(contact, contact)+` WHERE "incident_id" = ?`), i.ID)
if err != nil {
i.logger.Errorw("Failed to restore incident recipients from the database", zap.Error(err))
return err
Expand Down
6 changes: 3 additions & 3 deletions internal/incident/incidents.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,11 @@ func LoadOpenIncidents(ctx context.Context, db *database.DB, logger *logging.Log
incidentsByObjId := make(map[string]*Incident, chunkLen)

for _, i := range bulk {
incidentsById[i.Id] = i
incidentsById[i.ID] = i
incidentsByObjId[i.ObjectID.String()] = i

objectIds = append(objectIds, i.ObjectID)
incidentIds = append(incidentIds, i.Id)
incidentIds = append(incidentIds, i.ID)
}

// Restore all incident objects matching the given object ids
Expand Down Expand Up @@ -192,7 +192,7 @@ func GetCurrentIncidents() map[int64]*Incident {

m := make(map[int64]*Incident)
for _, incident := range currentIncidents {
m[incident.Id] = incident
m[incident.ID] = incident
}
return m
}
Expand Down
4 changes: 2 additions & 2 deletions internal/incident/incidents_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func TestLoadOpenIncidents(t *testing.T) {
RemoveCurrent(i.Object)

// Mark some of the existing incidents as recovered.
if i.Id%20 == 0 { // 1000 / 20 => 50 existing incidents will be marked as recovered!
if i.ID%20 == 0 { // 1000 / 20 => 50 existing incidents will be marked as recovered!
i.RecoveredAt = types.UnixMilli(time.Now())

require.NoError(t, i.Sync(ctx, tx), "failed to update/insert incident")
Expand Down Expand Up @@ -125,7 +125,7 @@ func assertIncidents(ctx context.Context, db *database.DB, t *testing.T, testDat
assert.NotNil(t, current.Object, "failed to restore incident object")

if i != nil {
assert.Equal(t, i.Id, current.Id, "incidents linked to the same object don't have the same ID")
assert.Equal(t, i.ID, current.ID, "incidents linked to the same object don't have the same ID")
assert.Equal(t, i.Severity, current.Severity, "failed to restore incident severity")
assert.Equal(t, i.StartedAt, current.StartedAt, "failed to restore incident started at")
assert.Equal(t, i.RecoveredAt, current.RecoveredAt, "failed to restore incident recovered at")
Expand Down
16 changes: 8 additions & 8 deletions internal/incident/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func (i *Incident) Upsert() interface{} {
// Before syncing any incident related database entries, this method should be called at least once.
// Returns an error on db failure.
func (i *Incident) Sync(ctx context.Context, tx *sqlx.Tx) error {
if i.Id != 0 {
if i.ID != 0 {
stmt, _ := i.db.BuildUpsertStmt(i)
_, err := tx.NamedExecContext(ctx, stmt, i)
if err != nil {
Expand All @@ -38,14 +38,14 @@ func (i *Incident) Sync(ctx context.Context, tx *sqlx.Tx) error {
return err
}

i.Id = incidentId
i.ID = incidentId
}

return nil
}

func (i *Incident) AddEscalationTriggered(ctx context.Context, tx *sqlx.Tx, state *EscalationState) error {
state.IncidentID = i.Id
state.IncidentID = i.ID

stmt, _ := i.db.BuildUpsertStmt(state)
_, err := tx.NamedExecContext(ctx, stmt, state)
Expand All @@ -55,7 +55,7 @@ func (i *Incident) AddEscalationTriggered(ctx context.Context, tx *sqlx.Tx, stat

// AddEvent Inserts incident history record to the database and returns an error on db failure.
func (i *Incident) AddEvent(ctx context.Context, tx *sqlx.Tx, ev *event.Event) error {
ie := &EventRow{IncidentID: i.Id, EventID: ev.ID}
ie := &EventRow{IncidentID: i.ID, EventID: ev.ID}
stmt, _ := i.db.BuildInsertStmt(ie)
_, err := tx.NamedExecContext(ctx, stmt, ie)

Expand All @@ -72,7 +72,7 @@ func (i *Incident) AddRecipient(ctx context.Context, tx *sqlx.Tx, escalation *ru

for _, escalationRecipient := range escalation.Recipients {
r := escalationRecipient.Recipient
cr := &ContactRow{IncidentID: i.Id, Role: newRole}
cr := &ContactRow{IncidentID: i.ID, Role: newRole}

recipientKey := recipient.ToKey(r)
cr.Key = recipientKey
Expand All @@ -88,7 +88,7 @@ func (i *Incident) AddRecipient(ctx context.Context, tx *sqlx.Tx, escalation *ru
i.logger.Infof("Contact %q role changed from %s to %s", r, state.Role.String(), newRole.String())

hr := &HistoryRow{
IncidentID: i.Id,
IncidentID: i.ID,
EventID: utils.ToDBInt(eventId),
Key: cr.Key,
Time: types.UnixMilli(time.Now()),
Expand Down Expand Up @@ -125,7 +125,7 @@ func (i *Incident) AddRecipient(ctx context.Context, tx *sqlx.Tx, escalation *ru
// AddRuleMatched syncs the given *rule.Rule to the database.
// Returns an error on database failure.
func (i *Incident) AddRuleMatched(ctx context.Context, tx *sqlx.Tx, r *rule.Rule) error {
rr := &RuleRow{IncidentID: i.Id, RuleID: r.ID}
rr := &RuleRow{IncidentID: i.ID, RuleID: r.ID}
stmt, _ := i.db.BuildUpsertStmt(rr)
_, err := tx.NamedExecContext(ctx, stmt, rr)

Expand All @@ -145,7 +145,7 @@ func (i *Incident) generateNotifications(
for contact, channels := range contactChannels {
for chID := range channels {
hr := &HistoryRow{
IncidentID: i.Id,
IncidentID: i.ID,
Key: recipient.ToKey(contact),
EventID: utils.ToDBInt(ev.ID),
Time: types.UnixMilli(time.Now()),
Expand Down

0 comments on commit 39257c4

Please sign in to comment.