Skip to content

Commit 212b9b2

Browse files
committed
Rename incident.Id -> ID & drop incident#ID()
1 parent 7ab48ff commit 212b9b2

File tree

4 files changed

+25
-29
lines changed

4 files changed

+25
-29
lines changed

internal/incident/incident.go

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import (
2424
type escalationID = int64
2525

2626
type Incident struct {
27-
Id int64 `db:"id"`
27+
ID int64 `db:"id"`
2828
ObjectID types.Binary `db:"object_id"`
2929
StartedAt types.UnixMilli `db:"started_at"`
3030
RecoveredAt types.UnixMilli `db:"recovered_at"`
@@ -79,11 +79,7 @@ func NewIncident(
7979
}
8080

8181
func (i *Incident) String() string {
82-
return fmt.Sprintf("#%d", i.Id)
83-
}
84-
85-
func (i *Incident) ID() int64 {
86-
return i.Id
82+
return fmt.Sprintf("#%d", i.ID)
8783
}
8884

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

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

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

360356
hr := &HistoryRow{
361-
IncidentID: i.Id,
357+
IncidentID: i.ID,
362358
Type: Opened,
363359
Time: types.UnixMilli(ev.Time),
364360
EventID: utils.ToDBInt(ev.ID),
@@ -381,7 +377,7 @@ func (i *Incident) handleMuteUnmute(ctx context.Context, tx *sqlx.Tx, ev *event.
381377
return nil
382378
}
383379

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

417413
hr := &HistoryRow{
418-
IncidentID: i.Id,
414+
IncidentID: i.ID,
419415
Time: types.UnixMilli(time.Now()),
420416
EventID: utils.ToDBInt(ev.ID),
421417
RuleID: utils.ToDBInt(r.ID),
@@ -510,7 +506,7 @@ func (i *Incident) triggerEscalations(ctx context.Context, tx *sqlx.Tx, ev *even
510506
}
511507

512508
hr := &HistoryRow{
513-
IncidentID: i.Id,
509+
IncidentID: i.ID,
514510
Time: state.TriggeredAt,
515511
EventID: utils.ToDBInt(ev.ID),
516512
RuleEscalationID: utils.ToDBInt(state.RuleEscalationID),
@@ -544,7 +540,7 @@ func (i *Incident) notifyContacts(ctx context.Context, ev *event.Event, notifica
544540
}
545541

546542
incidentUrl := baseUrl.JoinPath("/notifications/incident")
547-
incidentUrl.RawQuery = fmt.Sprintf("id=%d", i.ID())
543+
incidentUrl.RawQuery = fmt.Sprintf("id=%d", i.ID)
548544

549545
req := &plugin.NotificationRequest{
550546
Object: &plugin.Object{
@@ -554,7 +550,7 @@ func (i *Incident) notifyContacts(ctx context.Context, ev *event.Event, notifica
554550
ExtraTags: i.Object.ExtraTags,
555551
},
556552
Incident: &plugin.Incident{
557-
Id: i.Id,
553+
Id: i.ID,
558554
Url: incidentUrl.String(),
559555
Severity: i.Severity.String(),
560556
},
@@ -659,7 +655,7 @@ func (i *Incident) processAcknowledgementEvent(ctx context.Context, tx *sqlx.Tx,
659655
i.logger.Infof("Contact %q role changed from %s to %s", contact.String(), oldRole.String(), newRole.String())
660656

661657
hr := &HistoryRow{
662-
IncidentID: i.Id,
658+
IncidentID: i.ID,
663659
Key: recipientKey,
664660
EventID: utils.ToDBInt(ev.ID),
665661
Type: RecipientRoleChanged,
@@ -737,7 +733,7 @@ func (i *Incident) getRecipientsChannel(t time.Time) rule.ContactChannels {
737733
func (i *Incident) restoreRecipients(ctx context.Context) error {
738734
contact := &ContactRow{}
739735
var contacts []*ContactRow
740-
err := i.db.SelectContext(ctx, &contacts, i.db.Rebind(i.db.BuildSelectStmt(contact, contact)+` WHERE "incident_id" = ?`), i.Id)
736+
err := i.db.SelectContext(ctx, &contacts, i.db.Rebind(i.db.BuildSelectStmt(contact, contact)+` WHERE "incident_id" = ?`), i.ID)
741737
if err != nil {
742738
i.logger.Errorw("Failed to restore incident recipients from the database", zap.Error(err))
743739
return err

internal/incident/incidents.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,11 @@ func LoadOpenIncidents(ctx context.Context, db *database.DB, logger *logging.Log
8080
incidentsByObjId := make(map[string]*Incident, chunkLen)
8181

8282
for _, i := range bulk {
83-
incidentsById[i.Id] = i
83+
incidentsById[i.ID] = i
8484
incidentsByObjId[i.ObjectID.String()] = i
8585

8686
objectIds = append(objectIds, i.ObjectID)
87-
incidentIds = append(incidentIds, i.Id)
87+
incidentIds = append(incidentIds, i.ID)
8888
}
8989

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

193193
m := make(map[int64]*Incident)
194194
for _, incident := range currentIncidents {
195-
m[incident.Id] = incident
195+
m[incident.ID] = incident
196196
}
197197
return m
198198
}

internal/incident/incidents_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func TestLoadOpenIncidents(t *testing.T) {
6868
RemoveCurrent(i.Object)
6969

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

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

127127
if i != nil {
128-
assert.Equal(t, i.Id, current.Id, "incidents linked to the same object don't have the same ID")
128+
assert.Equal(t, i.ID, current.ID, "incidents linked to the same object don't have the same ID")
129129
assert.Equal(t, i.Severity, current.Severity, "failed to restore incident severity")
130130
assert.Equal(t, i.StartedAt, current.StartedAt, "failed to restore incident started at")
131131
assert.Equal(t, i.RecoveredAt, current.RecoveredAt, "failed to restore incident recovered at")

internal/incident/sync.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func (i *Incident) Upsert() interface{} {
2525
// Before syncing any incident related database entries, this method should be called at least once.
2626
// Returns an error on db failure.
2727
func (i *Incident) Sync(ctx context.Context, tx *sqlx.Tx) error {
28-
if i.Id != 0 {
28+
if i.ID != 0 {
2929
stmt, _ := i.db.BuildUpsertStmt(i)
3030
_, err := tx.NamedExecContext(ctx, stmt, i)
3131
if err != nil {
@@ -38,14 +38,14 @@ func (i *Incident) Sync(ctx context.Context, tx *sqlx.Tx) error {
3838
return err
3939
}
4040

41-
i.Id = incidentId
41+
i.ID = incidentId
4242
}
4343

4444
return nil
4545
}
4646

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

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

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

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

7373
for _, escalationRecipient := range escalation.Recipients {
7474
r := escalationRecipient.Recipient
75-
cr := &ContactRow{IncidentID: i.Id, Role: newRole}
75+
cr := &ContactRow{IncidentID: i.ID, Role: newRole}
7676

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

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

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

0 commit comments

Comments
 (0)