Skip to content

Commit e51dcc2

Browse files
committed
Drop internal/utils package
With Icinga/icinga-go-library#127 being merged, the internal/utils package will be moved to the IGL and can be removed here. This is another step to unify our Go codebase.
1 parent c479cfd commit e51dcc2

File tree

13 files changed

+46
-268
lines changed

13 files changed

+46
-268
lines changed

internal/event/event.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"fmt"
88
"github.com/icinga/icinga-go-library/database"
99
"github.com/icinga/icinga-go-library/types"
10-
"github.com/icinga/icinga-notifications/internal/utils"
1110
"github.com/jmoiron/sqlx"
1211
"time"
1312
)
@@ -176,7 +175,7 @@ func (e *Event) Sync(ctx context.Context, tx *sqlx.Tx, db *database.DB, objectId
176175
}
177176

178177
eventRow := NewEventRow(e, objectId)
179-
eventID, err := utils.InsertAndFetchId(ctx, tx, utils.BuildInsertStmtWithout(db, eventRow, "id"), eventRow)
178+
eventID, err := database.InsertAndFetchId(ctx, tx, database.BuildInsertStmtWithout(db, eventRow, "id"), eventRow)
180179
if err == nil {
181180
e.ID = eventID
182181
}
@@ -206,11 +205,11 @@ func NewEventRow(e *Event, objectId types.Binary) *EventRow {
206205
return &EventRow{
207206
Time: types.UnixMilli(e.Time),
208207
ObjectID: objectId,
209-
Type: utils.ToDBString(e.Type),
208+
Type: database.ToDBString(e.Type),
210209
Severity: e.Severity,
211-
Username: utils.ToDBString(e.Username),
212-
Message: utils.ToDBString(e.Message),
210+
Username: database.ToDBString(e.Username),
211+
Message: database.ToDBString(e.Message),
213212
Mute: e.Mute,
214-
MuteReason: utils.ToDBString(e.MuteReason),
213+
MuteReason: database.ToDBString(e.MuteReason),
215214
}
216215
}

internal/incident/db_types.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"github.com/icinga/icinga-go-library/types"
77
"github.com/icinga/icinga-notifications/internal/event"
88
"github.com/icinga/icinga-notifications/internal/recipient"
9-
"github.com/icinga/icinga-notifications/internal/utils"
109
"github.com/jmoiron/sqlx"
1110
)
1211

@@ -92,7 +91,7 @@ func (h *HistoryRow) TableName() string {
9291
// Sync persists the current state of this history to the database and retrieves the just inserted history ID.
9392
// Returns error when failed to execute the query.
9493
func (h *HistoryRow) Sync(ctx context.Context, db *database.DB, tx *sqlx.Tx) error {
95-
historyId, err := utils.InsertAndFetchId(ctx, tx, utils.BuildInsertStmtWithout(db, h, "id"), h)
94+
historyId, err := database.InsertAndFetchId(ctx, tx, database.BuildInsertStmtWithout(db, h, "id"), h)
9695
if err != nil {
9796
return err
9897
}

internal/incident/incident.go

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313
"github.com/icinga/icinga-notifications/internal/object"
1414
"github.com/icinga/icinga-notifications/internal/recipient"
1515
"github.com/icinga/icinga-notifications/internal/rule"
16-
"github.com/icinga/icinga-notifications/internal/utils"
1716
"github.com/jmoiron/sqlx"
1817
"go.uber.org/zap"
1918
"sync"
@@ -252,7 +251,7 @@ func (i *Incident) RetriggerEscalations(ev *event.Event) {
252251

253252
var notifications []*NotificationEntry
254253
ctx := context.Background()
255-
err = utils.RunInTx(ctx, i.db, func(tx *sqlx.Tx) error {
254+
err = database.RunInTx(ctx, i.db, func(tx *sqlx.Tx) error {
256255
err := ev.Sync(ctx, tx, i.db, i.Object.ID)
257256
if err != nil {
258257
return err
@@ -298,12 +297,12 @@ func (i *Incident) processSeverityChangedEvent(ctx context.Context, tx *sqlx.Tx,
298297

299298
hr := &HistoryRow{
300299
IncidentID: i.Id,
301-
EventID: utils.ToDBInt(ev.ID),
300+
EventID: database.ToDBInt(ev.ID),
302301
Time: types.UnixMilli(time.Now()),
303302
Type: IncidentSeverityChanged,
304303
NewSeverity: newSeverity,
305304
OldSeverity: oldSeverity,
306-
Message: utils.ToDBString(ev.Message),
305+
Message: database.ToDBString(ev.Message),
307306
}
308307

309308
if err := hr.Sync(ctx, i.db, tx); err != nil {
@@ -319,7 +318,7 @@ func (i *Incident) processSeverityChangedEvent(ctx context.Context, tx *sqlx.Tx,
319318

320319
hr = &HistoryRow{
321320
IncidentID: i.Id,
322-
EventID: utils.ToDBInt(ev.ID),
321+
EventID: database.ToDBInt(ev.ID),
323322
Time: i.RecoveredAt,
324323
Type: Closed,
325324
}
@@ -357,9 +356,9 @@ func (i *Incident) processIncidentOpenedEvent(ctx context.Context, tx *sqlx.Tx,
357356
IncidentID: i.Id,
358357
Type: Opened,
359358
Time: types.UnixMilli(ev.Time),
360-
EventID: utils.ToDBInt(ev.ID),
359+
EventID: database.ToDBInt(ev.ID),
361360
NewSeverity: i.Severity,
362-
Message: utils.ToDBString(ev.Message),
361+
Message: database.ToDBString(ev.Message),
363362
}
364363

365364
if err := hr.Sync(ctx, i.db, tx); err != nil {
@@ -377,7 +376,7 @@ func (i *Incident) handleMuteUnmute(ctx context.Context, tx *sqlx.Tx, ev *event.
377376
return nil
378377
}
379378

380-
hr := &HistoryRow{IncidentID: i.Id, EventID: utils.ToDBInt(ev.ID), Time: types.UnixMilli(time.Now())}
379+
hr := &HistoryRow{IncidentID: i.Id, EventID: database.ToDBInt(ev.ID), Time: types.UnixMilli(time.Now())}
381380
logger := i.logger.With(zap.String("event", ev.String()))
382381
if i.Object.IsMuted() {
383382
hr.Type = Muted
@@ -388,7 +387,7 @@ func (i *Incident) handleMuteUnmute(ctx context.Context, tx *sqlx.Tx, ev *event.
388387
} else {
389388
hr.Type = Unmuted
390389
// On the other hand, if an object is unmuted, its mute reason is already reset, and we can't access it anymore.
391-
hr.Message = utils.ToDBString(ev.MuteReason)
390+
hr.Message = database.ToDBString(ev.MuteReason)
392391
logger.Infow("Unmuting incident", zap.String("reason", ev.MuteReason))
393392
}
394393

@@ -426,8 +425,8 @@ func (i *Incident) evaluateRules(ctx context.Context, tx *sqlx.Tx, eventID int64
426425
hr := &HistoryRow{
427426
IncidentID: i.Id,
428427
Time: types.UnixMilli(time.Now()),
429-
EventID: utils.ToDBInt(eventID),
430-
RuleID: utils.ToDBInt(r.ID),
428+
EventID: database.ToDBInt(eventID),
429+
RuleID: database.ToDBInt(r.ID),
431430
Type: RuleMatched,
432431
}
433432
if err := hr.Sync(ctx, i.db, tx); err != nil {
@@ -534,9 +533,9 @@ func (i *Incident) triggerEscalations(ctx context.Context, tx *sqlx.Tx, ev *even
534533
hr := &HistoryRow{
535534
IncidentID: i.Id,
536535
Time: state.TriggeredAt,
537-
EventID: utils.ToDBInt(ev.ID),
538-
RuleEscalationID: utils.ToDBInt(state.RuleEscalationID),
539-
RuleID: utils.ToDBInt(r.ID),
536+
EventID: database.ToDBInt(ev.ID),
537+
RuleEscalationID: database.ToDBInt(state.RuleEscalationID),
538+
RuleID: database.ToDBInt(r.ID),
540539
Type: EscalationTriggered,
541540
}
542541

@@ -649,12 +648,12 @@ func (i *Incident) processAcknowledgementEvent(ctx context.Context, tx *sqlx.Tx,
649648
hr := &HistoryRow{
650649
IncidentID: i.Id,
651650
Key: recipientKey,
652-
EventID: utils.ToDBInt(ev.ID),
651+
EventID: database.ToDBInt(ev.ID),
653652
Type: RecipientRoleChanged,
654653
Time: types.UnixMilli(time.Now()),
655654
NewRecipientRole: newRole,
656655
OldRecipientRole: oldRole,
657-
Message: utils.ToDBString(ev.Message),
656+
Message: database.ToDBString(ev.Message),
658657
}
659658

660659
if err := hr.Sync(ctx, i.db, tx); err != nil {

internal/incident/incidents.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"github.com/icinga/icinga-notifications/internal/config"
1111
"github.com/icinga/icinga-notifications/internal/event"
1212
"github.com/icinga/icinga-notifications/internal/object"
13-
"github.com/icinga/icinga-notifications/internal/utils"
1413
"github.com/jmoiron/sqlx"
1514
"github.com/pkg/errors"
1615
"go.uber.org/zap"
@@ -93,7 +92,7 @@ func LoadOpenIncidents(ctx context.Context, db *database.DB, logger *logging.Log
9392
}
9493

9594
// Restore all escalation states and incident rules matching the given incident ids.
96-
err := utils.ForEachRow[EscalationState](ctx, db, "incident_id", incidentIds, func(state *EscalationState) {
95+
err := database.ForEachRow[EscalationState](ctx, db, "incident_id", incidentIds, func(state *EscalationState) {
9796
i := incidentsById[state.IncidentID]
9897
i.EscalationState[state.RuleEscalationID] = state
9998

@@ -111,7 +110,7 @@ func LoadOpenIncidents(ctx context.Context, db *database.DB, logger *logging.Log
111110
}
112111

113112
// Restore incident recipients matching the given incident ids.
114-
err = utils.ForEachRow[ContactRow](ctx, db, "incident_id", incidentIds, func(c *ContactRow) {
113+
err = database.ForEachRow[ContactRow](ctx, db, "incident_id", incidentIds, func(c *ContactRow) {
115114
incidentsById[c.IncidentID].Recipients[c.Key] = &RecipientState{Role: c.Role}
116115
})
117116
if err != nil {
@@ -245,7 +244,7 @@ func ProcessEvent(
245244
}
246245

247246
// There is no active incident, but the event appears to be relevant, so try to persist it in the DB.
248-
err = utils.RunInTx(ctx, db, func(tx *sqlx.Tx) error { return ev.Sync(ctx, tx, db, obj.ID) })
247+
err = database.RunInTx(ctx, db, func(tx *sqlx.Tx) error { return ev.Sync(ctx, tx, db, obj.ID) })
249248
if err != nil {
250249
return errors.New("cannot sync non-state event to the database")
251250
}

internal/incident/incidents_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"github.com/icinga/icinga-notifications/internal/event"
1010
"github.com/icinga/icinga-notifications/internal/object"
1111
"github.com/icinga/icinga-notifications/internal/testutils"
12-
"github.com/icinga/icinga-notifications/internal/utils"
1312
"github.com/jmoiron/sqlx"
1413
"github.com/stretchr/testify/assert"
1514
"github.com/stretchr/testify/require"
@@ -31,8 +30,8 @@ func TestLoadOpenIncidents(t *testing.T) {
3130
source.ChangedAt = types.UnixMilli(time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC))
3231
source.Deleted = types.Bool{Bool: false, Valid: true}
3332

34-
err := utils.RunInTx(ctx, db, func(tx *sqlx.Tx) error {
35-
id, err := utils.InsertAndFetchId(ctx, tx, utils.BuildInsertStmtWithout(db, source, "id"), source)
33+
err := database.RunInTx(ctx, db, func(tx *sqlx.Tx) error {
34+
id, err := database.InsertAndFetchId(ctx, tx, database.BuildInsertStmtWithout(db, source, "id"), source)
3635
require.NoError(t, err, "populating source table should not fail")
3736

3837
source.ID = id

internal/incident/sync.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ package incident
33
import (
44
"context"
55
"fmt"
6+
"github.com/icinga/icinga-go-library/database"
67
"github.com/icinga/icinga-go-library/types"
78
"github.com/icinga/icinga-notifications/internal/event"
89
"github.com/icinga/icinga-notifications/internal/recipient"
910
"github.com/icinga/icinga-notifications/internal/rule"
10-
"github.com/icinga/icinga-notifications/internal/utils"
1111
"github.com/jmoiron/sqlx"
1212
"go.uber.org/zap"
1313
"time"
@@ -32,8 +32,8 @@ func (i *Incident) Sync(ctx context.Context, tx *sqlx.Tx) error {
3232
return fmt.Errorf("failed to upsert incident: %w", err)
3333
}
3434
} else {
35-
stmt := utils.BuildInsertStmtWithout(i.db, i, "id")
36-
incidentId, err := utils.InsertAndFetchId(ctx, tx, stmt, i)
35+
stmt := database.BuildInsertStmtWithout(i.db, i, "id")
36+
incidentId, err := database.InsertAndFetchId(ctx, tx, stmt, i)
3737
if err != nil {
3838
return err
3939
}
@@ -89,7 +89,7 @@ func (i *Incident) AddRecipient(ctx context.Context, tx *sqlx.Tx, escalation *ru
8989

9090
hr := &HistoryRow{
9191
IncidentID: i.Id,
92-
EventID: utils.ToDBInt(eventId),
92+
EventID: database.ToDBInt(eventId),
9393
Key: cr.Key,
9494
Time: types.UnixMilli(time.Now()),
9595
Type: RecipientRoleChanged,
@@ -147,12 +147,12 @@ func (i *Incident) generateNotifications(
147147
hr := &HistoryRow{
148148
IncidentID: i.Id,
149149
Key: recipient.ToKey(contact),
150-
EventID: utils.ToDBInt(ev.ID),
150+
EventID: database.ToDBInt(ev.ID),
151151
Time: types.UnixMilli(time.Now()),
152152
Type: Notified,
153-
ChannelID: utils.ToDBInt(chID),
153+
ChannelID: database.ToDBInt(chID),
154154
NotificationState: NotificationStatePending,
155-
Message: utils.ToDBString(ev.Message),
155+
Message: database.ToDBString(ev.Message),
156156
}
157157
if suppress {
158158
hr.NotificationState = NotificationStateSuppressed

internal/object/object.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
"github.com/icinga/icinga-go-library/database"
1313
"github.com/icinga/icinga-go-library/types"
1414
"github.com/icinga/icinga-notifications/internal/event"
15-
"github.com/icinga/icinga-notifications/internal/utils"
1615
"regexp"
1716
"sort"
1817
"strings"
@@ -37,7 +36,7 @@ func New(db *database.DB, ev *event.Event) *Object {
3736
SourceID: ev.SourceId,
3837
Name: ev.Name,
3938
db: db,
40-
URL: utils.ToDBString(ev.URL),
39+
URL: database.ToDBString(ev.URL),
4140
Tags: ev.Tags,
4241
ExtraTags: ev.ExtraTags,
4342
}
@@ -85,10 +84,10 @@ func FromEvent(ctx context.Context, db *database.DB, ev *event.Event) (*Object,
8584

8685
newObject.ExtraTags = ev.ExtraTags
8786
newObject.Name = ev.Name
88-
newObject.URL = utils.ToDBString(ev.URL)
87+
newObject.URL = database.ToDBString(ev.URL)
8988
if ev.Mute.Valid {
9089
if ev.Mute.Bool {
91-
newObject.MuteReason = utils.ToDBString(ev.MuteReason)
90+
newObject.MuteReason = database.ToDBString(ev.MuteReason)
9291
} else {
9392
// The ongoing event unmutes the object, so reset the mute reason to null.
9493
newObject.MuteReason = types.String{}

internal/object/objects.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"github.com/icinga/icinga-go-library/com"
77
"github.com/icinga/icinga-go-library/database"
88
"github.com/icinga/icinga-go-library/types"
9-
"github.com/icinga/icinga-notifications/internal/utils"
109
"github.com/jmoiron/sqlx"
1110
"github.com/pkg/errors"
1211
"golang.org/x/sync/errgroup"
@@ -58,7 +57,7 @@ func restoreObjectsFromQuery(ctx context.Context, db *database.DB, query string,
5857
g.Go(func() error {
5958
defer close(objects)
6059

61-
err := utils.ExecAndApply[Object](ctx, db, query, args, func(o *Object) {
60+
err := database.ExecAndApply[Object](ctx, db, query, args, func(o *Object) {
6261
o.db = db
6362
o.Tags = map[string]string{}
6463
o.ExtraTags = map[string]string{}
@@ -93,15 +92,15 @@ func restoreObjectsFromQuery(ctx context.Context, db *database.DB, query string,
9392
}
9493

9594
// Restore object ID tags matching the given object ids
96-
err := utils.ForEachRow[IdTagRow](ctx, db, "object_id", ids, func(ir *IdTagRow) {
95+
err := database.ForEachRow[IdTagRow](ctx, db, "object_id", ids, func(ir *IdTagRow) {
9796
objectsMap[ir.ObjectId.String()].Tags[ir.Tag] = ir.Value
9897
})
9998
if err != nil {
10099
return errors.Wrap(err, "cannot restore objects ID tags")
101100
}
102101

103102
// Restore object extra tags matching the given object ids
104-
err = utils.ForEachRow[ExtraTagRow](ctx, db, "object_id", ids, func(et *ExtraTagRow) {
103+
err = database.ForEachRow[ExtraTagRow](ctx, db, "object_id", ids, func(et *ExtraTagRow) {
105104
objectsMap[et.ObjectId.String()].ExtraTags[et.Tag] = et.Value
106105
})
107106
if err != nil {

internal/object/objects_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"github.com/icinga/icinga-go-library/types"
77
"github.com/icinga/icinga-notifications/internal/event"
88
"github.com/icinga/icinga-notifications/internal/testutils"
9-
"github.com/icinga/icinga-notifications/internal/utils"
109
"github.com/jmoiron/sqlx"
1110
"github.com/stretchr/testify/assert"
1211
"github.com/stretchr/testify/require"
@@ -19,14 +18,14 @@ func TestRestoreMutedObjects(t *testing.T) {
1918
db := testutils.GetTestDB(ctx, t)
2019

2120
var sourceID int64
22-
err := utils.RunInTx(ctx, db, func(tx *sqlx.Tx) error {
21+
err := database.RunInTx(ctx, db, func(tx *sqlx.Tx) error {
2322
args := map[string]any{
2423
"type": "notifications",
2524
"name": "Icinga Notifications",
2625
"changed_at": int64(1720702049000),
2726
}
2827
// We can't use config.Source here unfortunately due to cyclic import error!
29-
id, err := utils.InsertAndFetchId(ctx, tx, `INSERT INTO source (type, name, changed_at) VALUES (:type, :name, :changed_at)`, args)
28+
id, err := database.InsertAndFetchId(ctx, tx, `INSERT INTO source (type, name, changed_at) VALUES (:type, :name, :changed_at)`, args)
3029
require.NoError(t, err, "populating source table should not fail")
3130

3231
sourceID = id

internal/recipient/recipient.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ package recipient
22

33
import (
44
"fmt"
5+
"github.com/icinga/icinga-go-library/database"
56
"github.com/icinga/icinga-go-library/types"
6-
"github.com/icinga/icinga-notifications/internal/utils"
77
"go.uber.org/zap/zapcore"
88
"time"
99
)
@@ -55,11 +55,11 @@ func (r Key) MarshalText() (text []byte, err error) {
5555
func ToKey(r Recipient) Key {
5656
switch v := r.(type) {
5757
case *Contact:
58-
return Key{ContactID: utils.ToDBInt(v.ID)}
58+
return Key{ContactID: database.ToDBInt(v.ID)}
5959
case *Group:
60-
return Key{GroupID: utils.ToDBInt(v.ID)}
60+
return Key{GroupID: database.ToDBInt(v.ID)}
6161
case *Schedule:
62-
return Key{ScheduleID: utils.ToDBInt(v.ID)}
62+
return Key{ScheduleID: database.ToDBInt(v.ID)}
6363
default:
6464
panic(fmt.Sprintf("unexpected recipient type: %T", r))
6565
}

0 commit comments

Comments
 (0)