Skip to content

Commit 3ab100a

Browse files
committed
Rename incident.Id -> ID & drop incident#ID()
1 parent 054d905 commit 3ab100a

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: types.MakeInt(ev.ID, types.TransformZeroIntToNull),
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: types.MakeInt(ev.ID, types.TransformZeroIntToNull),
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: types.MakeInt(ev.ID, types.TransformZeroIntToNull),
@@ -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: types.MakeInt(ev.ID, types.TransformZeroIntToNull), Time: types.UnixMilli(time.Now())}
380+
hr := &HistoryRow{IncidentID: i.ID, EventID: types.MakeInt(ev.ID, types.TransformZeroIntToNull), 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: types.MakeInt(ev.ID, types.TransformZeroIntToNull),
421417
RuleID: types.MakeInt(r.ID, types.TransformZeroIntToNull),
@@ -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: types.MakeInt(ev.ID, types.TransformZeroIntToNull),
516512
RuleEscalationID: types.MakeInt(state.RuleEscalationID, types.TransformZeroIntToNull),
@@ -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: types.MakeInt(ev.ID, types.TransformZeroIntToNull),
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
@@ -67,7 +67,7 @@ func TestLoadOpenIncidents(t *testing.T) {
6767
RemoveCurrent(i.Object)
6868

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

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

126126
if i != nil {
127-
assert.Equal(t, i.Id, current.Id, "incidents linked to the same object don't have the same ID")
127+
assert.Equal(t, i.ID, current.ID, "incidents linked to the same object don't have the same ID")
128128
assert.Equal(t, i.Severity, current.Severity, "failed to restore incident severity")
129129
assert.Equal(t, i.StartedAt, current.StartedAt, "failed to restore incident started at")
130130
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
@@ -26,7 +26,7 @@ func (i *Incident) Upsert() interface{} {
2626
// Before syncing any incident related database entries, this method should be called at least once.
2727
// Returns an error on db failure.
2828
func (i *Incident) Sync(ctx context.Context, tx *sqlx.Tx) error {
29-
if i.Id != 0 {
29+
if i.ID != 0 {
3030
stmt, _ := i.db.BuildUpsertStmt(i)
3131
_, err := tx.NamedExecContext(ctx, stmt, i)
3232
if err != nil {
@@ -39,14 +39,14 @@ func (i *Incident) Sync(ctx context.Context, tx *sqlx.Tx) error {
3939
return err
4040
}
4141

42-
i.Id = incidentId
42+
i.ID = incidentId
4343
}
4444

4545
return nil
4646
}
4747

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

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

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

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

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

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

9191
hr := &HistoryRow{
92-
IncidentID: i.Id,
92+
IncidentID: i.ID,
9393
EventID: types.MakeInt(eventId, types.TransformZeroIntToNull),
9494
Key: cr.Key,
9595
Time: types.UnixMilli(time.Now()),
@@ -126,7 +126,7 @@ func (i *Incident) AddRecipient(ctx context.Context, tx *sqlx.Tx, escalation *ru
126126
// AddRuleMatched syncs the given *rule.Rule to the database.
127127
// Returns an error on database failure.
128128
func (i *Incident) AddRuleMatched(ctx context.Context, tx *sqlx.Tx, r *rule.Rule) error {
129-
rr := &RuleRow{IncidentID: i.Id, RuleID: r.ID}
129+
rr := &RuleRow{IncidentID: i.ID, RuleID: r.ID}
130130
stmt, _ := i.db.BuildUpsertStmt(rr)
131131
_, err := tx.NamedExecContext(ctx, stmt, rr)
132132

@@ -146,7 +146,7 @@ func (i *Incident) generateNotifications(
146146
for contact, channels := range contactChannels {
147147
for chID := range channels {
148148
hr := &HistoryRow{
149-
IncidentID: i.Id,
149+
IncidentID: i.ID,
150150
Key: recipient.ToKey(contact),
151151
EventID: types.MakeInt(ev.ID, types.TransformZeroIntToNull),
152152
Time: types.UnixMilli(time.Now()),

0 commit comments

Comments
 (0)