Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions internal/event/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"fmt"
"github.com/icinga/icinga-go-library/database"
"github.com/icinga/icinga-go-library/types"
"github.com/icinga/icinga-notifications/internal/utils"
"github.com/jmoiron/sqlx"
"time"
)
Expand Down Expand Up @@ -176,7 +175,7 @@ func (e *Event) Sync(ctx context.Context, tx *sqlx.Tx, db *database.DB, objectId
}

eventRow := NewEventRow(e, objectId)
eventID, err := utils.InsertAndFetchId(ctx, tx, utils.BuildInsertStmtWithout(db, eventRow, "id"), eventRow)
eventID, err := database.InsertObtainID(ctx, tx, database.BuildInsertStmtWithout(db, eventRow, "id"), eventRow)
if err == nil {
e.ID = eventID
}
Expand Down Expand Up @@ -206,11 +205,11 @@ func NewEventRow(e *Event, objectId types.Binary) *EventRow {
return &EventRow{
Time: types.UnixMilli(e.Time),
ObjectID: objectId,
Type: utils.ToDBString(e.Type),
Type: types.MakeString(e.Type, types.TransformEmptyStringToNull),
Severity: e.Severity,
Username: utils.ToDBString(e.Username),
Message: utils.ToDBString(e.Message),
Username: types.MakeString(e.Username, types.TransformEmptyStringToNull),
Message: types.MakeString(e.Message, types.TransformEmptyStringToNull),
Mute: e.Mute,
MuteReason: utils.ToDBString(e.MuteReason),
MuteReason: types.MakeString(e.MuteReason, types.TransformEmptyStringToNull),
}
}
3 changes: 1 addition & 2 deletions internal/incident/db_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"github.com/icinga/icinga-go-library/types"
"github.com/icinga/icinga-notifications/internal/event"
"github.com/icinga/icinga-notifications/internal/recipient"
"github.com/icinga/icinga-notifications/internal/utils"
"github.com/jmoiron/sqlx"
)

Expand Down Expand Up @@ -92,7 +91,7 @@ func (h *HistoryRow) TableName() string {
// Sync persists the current state of this history to the database and retrieves the just inserted history ID.
// Returns error when failed to execute the query.
func (h *HistoryRow) Sync(ctx context.Context, db *database.DB, tx *sqlx.Tx) error {
historyId, err := utils.InsertAndFetchId(ctx, tx, utils.BuildInsertStmtWithout(db, h, "id"), h)
historyId, err := database.InsertObtainID(ctx, tx, database.BuildInsertStmtWithout(db, h, "id"), h)
if err != nil {
return err
}
Expand Down
36 changes: 18 additions & 18 deletions internal/incident/incident.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import (
"context"
"errors"
"fmt"
"sync"
"time"

"github.com/icinga/icinga-go-library/database"
"github.com/icinga/icinga-go-library/types"
"github.com/icinga/icinga-notifications/internal/config"
Expand All @@ -13,11 +16,8 @@ import (
"github.com/icinga/icinga-notifications/internal/object"
"github.com/icinga/icinga-notifications/internal/recipient"
"github.com/icinga/icinga-notifications/internal/rule"
"github.com/icinga/icinga-notifications/internal/utils"
"github.com/jmoiron/sqlx"
"go.uber.org/zap"
"sync"
"time"
)

type ruleID = int64
Expand Down Expand Up @@ -252,7 +252,7 @@ func (i *Incident) RetriggerEscalations(ev *event.Event) {

var notifications []*NotificationEntry
ctx := context.Background()
err = utils.RunInTx(ctx, i.db, func(tx *sqlx.Tx) error {
err = i.db.ExecTx(ctx, func(ctx context.Context, tx *sqlx.Tx) error {
err := ev.Sync(ctx, tx, i.db, i.Object.ID)
if err != nil {
return err
Expand Down Expand Up @@ -298,12 +298,12 @@ func (i *Incident) processSeverityChangedEvent(ctx context.Context, tx *sqlx.Tx,

hr := &HistoryRow{
IncidentID: i.Id,
EventID: utils.ToDBInt(ev.ID),
EventID: types.MakeInt(ev.ID, types.TransformZeroIntToNull),
Time: types.UnixMilli(time.Now()),
Type: IncidentSeverityChanged,
NewSeverity: newSeverity,
OldSeverity: oldSeverity,
Message: utils.ToDBString(ev.Message),
Message: types.MakeString(ev.Message, types.TransformEmptyStringToNull),
}

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

hr = &HistoryRow{
IncidentID: i.Id,
EventID: utils.ToDBInt(ev.ID),
EventID: types.MakeInt(ev.ID, types.TransformZeroIntToNull),
Time: i.RecoveredAt,
Type: Closed,
}
Expand Down Expand Up @@ -357,9 +357,9 @@ func (i *Incident) processIncidentOpenedEvent(ctx context.Context, tx *sqlx.Tx,
IncidentID: i.Id,
Type: Opened,
Time: types.UnixMilli(ev.Time),
EventID: utils.ToDBInt(ev.ID),
EventID: types.MakeInt(ev.ID, types.TransformZeroIntToNull),
NewSeverity: i.Severity,
Message: utils.ToDBString(ev.Message),
Message: types.MakeString(ev.Message, types.TransformEmptyStringToNull),
}

if err := hr.Sync(ctx, i.db, tx); err != nil {
Expand All @@ -377,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: types.MakeInt(ev.ID, types.TransformZeroIntToNull), Time: types.UnixMilli(time.Now())}
logger := i.logger.With(zap.String("event", ev.String()))
if i.Object.IsMuted() {
hr.Type = Muted
Expand All @@ -388,7 +388,7 @@ func (i *Incident) handleMuteUnmute(ctx context.Context, tx *sqlx.Tx, ev *event.
} else {
hr.Type = Unmuted
// On the other hand, if an object is unmuted, its mute reason is already reset, and we can't access it anymore.
hr.Message = utils.ToDBString(ev.MuteReason)
hr.Message = types.MakeString(ev.MuteReason, types.TransformEmptyStringToNull)
logger.Infow("Unmuting incident", zap.String("reason", ev.MuteReason))
}

Expand Down Expand Up @@ -426,8 +426,8 @@ func (i *Incident) evaluateRules(ctx context.Context, tx *sqlx.Tx, eventID int64
hr := &HistoryRow{
IncidentID: i.Id,
Time: types.UnixMilli(time.Now()),
EventID: utils.ToDBInt(eventID),
RuleID: utils.ToDBInt(r.ID),
EventID: types.MakeInt(eventID, types.TransformZeroIntToNull),
RuleID: types.MakeInt(r.ID, types.TransformZeroIntToNull),
Type: RuleMatched,
}
if err := hr.Sync(ctx, i.db, tx); err != nil {
Expand Down Expand Up @@ -534,9 +534,9 @@ func (i *Incident) triggerEscalations(ctx context.Context, tx *sqlx.Tx, ev *even
hr := &HistoryRow{
IncidentID: i.Id,
Time: state.TriggeredAt,
EventID: utils.ToDBInt(ev.ID),
RuleEscalationID: utils.ToDBInt(state.RuleEscalationID),
RuleID: utils.ToDBInt(r.ID),
EventID: types.MakeInt(ev.ID, types.TransformZeroIntToNull),
RuleEscalationID: types.MakeInt(state.RuleEscalationID, types.TransformZeroIntToNull),
RuleID: types.MakeInt(r.ID, types.TransformZeroIntToNull),
Type: EscalationTriggered,
}

Expand Down Expand Up @@ -649,12 +649,12 @@ func (i *Incident) processAcknowledgementEvent(ctx context.Context, tx *sqlx.Tx,
hr := &HistoryRow{
IncidentID: i.Id,
Key: recipientKey,
EventID: utils.ToDBInt(ev.ID),
EventID: types.MakeInt(ev.ID, types.TransformZeroIntToNull),
Type: RecipientRoleChanged,
Time: types.UnixMilli(time.Now()),
NewRecipientRole: newRole,
OldRecipientRole: oldRole,
Message: utils.ToDBString(ev.Message),
Message: types.MakeString(ev.Message, types.TransformEmptyStringToNull),
}

if err := hr.Sync(ctx, i.db, tx); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion internal/incident/incidents.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ func ProcessEvent(
}

// There is no active incident, but the event appears to be relevant, so try to persist it in the DB.
err = utils.RunInTx(ctx, db, func(tx *sqlx.Tx) error { return ev.Sync(ctx, tx, db, obj.ID) })
err = db.ExecTx(ctx, func(ctx context.Context, tx *sqlx.Tx) error { return ev.Sync(ctx, tx, db, obj.ID) })
if err != nil {
return errors.New("cannot sync non-state event to the database")
}
Expand Down
7 changes: 3 additions & 4 deletions internal/incident/incidents_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/icinga/icinga-notifications/internal/event"
"github.com/icinga/icinga-notifications/internal/object"
"github.com/icinga/icinga-notifications/internal/testutils"
"github.com/icinga/icinga-notifications/internal/utils"
"github.com/jmoiron/sqlx"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand All @@ -31,14 +30,14 @@ func TestLoadOpenIncidents(t *testing.T) {
source.ChangedAt = types.UnixMilli(time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC))
source.Deleted = types.Bool{Bool: false, Valid: true}

err := utils.RunInTx(ctx, db, func(tx *sqlx.Tx) error {
id, err := utils.InsertAndFetchId(ctx, tx, utils.BuildInsertStmtWithout(db, source, "id"), source)
err := db.ExecTx(ctx, func(ctx context.Context, tx *sqlx.Tx) error {
id, err := database.InsertObtainID(ctx, tx, database.BuildInsertStmtWithout(db, source, "id"), source)
require.NoError(t, err, "populating source table should not fail")

source.ID = id
return nil
})
require.NoError(t, err, "utils.RunInTx() should not fail")
require.NoError(t, err, "db.ExecTx should not fail")

// Reduce the default placeholders per statement to a meaningful number, so that we can
// test some parallelism when loading the incidents.
Expand Down
17 changes: 9 additions & 8 deletions internal/incident/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ package incident
import (
"context"
"fmt"
"time"

"github.com/icinga/icinga-go-library/database"
"github.com/icinga/icinga-go-library/types"
"github.com/icinga/icinga-notifications/internal/event"
"github.com/icinga/icinga-notifications/internal/recipient"
"github.com/icinga/icinga-notifications/internal/rule"
"github.com/icinga/icinga-notifications/internal/utils"
"github.com/jmoiron/sqlx"
"go.uber.org/zap"
"time"
)

// Upsert implements the contracts.Upserter interface.
Expand All @@ -32,8 +33,8 @@ func (i *Incident) Sync(ctx context.Context, tx *sqlx.Tx) error {
return fmt.Errorf("failed to upsert incident: %w", err)
}
} else {
stmt := utils.BuildInsertStmtWithout(i.db, i, "id")
incidentId, err := utils.InsertAndFetchId(ctx, tx, stmt, i)
stmt := database.BuildInsertStmtWithout(i.db, i, "id")
incidentId, err := database.InsertObtainID(ctx, tx, stmt, i)
if err != nil {
return err
}
Expand Down Expand Up @@ -89,7 +90,7 @@ func (i *Incident) AddRecipient(ctx context.Context, tx *sqlx.Tx, escalation *ru

hr := &HistoryRow{
IncidentID: i.Id,
EventID: utils.ToDBInt(eventId),
EventID: types.MakeInt(eventId, types.TransformZeroIntToNull),
Key: cr.Key,
Time: types.UnixMilli(time.Now()),
Type: RecipientRoleChanged,
Expand Down Expand Up @@ -147,12 +148,12 @@ func (i *Incident) generateNotifications(
hr := &HistoryRow{
IncidentID: i.Id,
Key: recipient.ToKey(contact),
EventID: utils.ToDBInt(ev.ID),
EventID: types.MakeInt(ev.ID, types.TransformZeroIntToNull),
Time: types.UnixMilli(time.Now()),
Type: Notified,
ChannelID: utils.ToDBInt(chID),
ChannelID: types.MakeInt(chID, types.TransformZeroIntToNull),
NotificationState: NotificationStatePending,
Message: utils.ToDBString(ev.Message),
Message: types.MakeString(ev.Message, types.TransformEmptyStringToNull),
}
if suppress {
hr.NotificationState = NotificationStateSuppressed
Expand Down
7 changes: 3 additions & 4 deletions internal/object/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"github.com/icinga/icinga-go-library/database"
"github.com/icinga/icinga-go-library/types"
"github.com/icinga/icinga-notifications/internal/event"
"github.com/icinga/icinga-notifications/internal/utils"
"regexp"
"sort"
"strings"
Expand All @@ -37,7 +36,7 @@ func New(db *database.DB, ev *event.Event) *Object {
SourceID: ev.SourceId,
Name: ev.Name,
db: db,
URL: utils.ToDBString(ev.URL),
URL: types.MakeString(ev.URL, types.TransformEmptyStringToNull),
Tags: ev.Tags,
ExtraTags: ev.ExtraTags,
}
Expand Down Expand Up @@ -85,10 +84,10 @@ func FromEvent(ctx context.Context, db *database.DB, ev *event.Event) (*Object,

newObject.ExtraTags = ev.ExtraTags
newObject.Name = ev.Name
newObject.URL = utils.ToDBString(ev.URL)
newObject.URL = types.MakeString(ev.URL, types.TransformEmptyStringToNull)
if ev.Mute.Valid {
if ev.Mute.Bool {
newObject.MuteReason = utils.ToDBString(ev.MuteReason)
newObject.MuteReason = types.MakeString(ev.MuteReason, types.TransformEmptyStringToNull)
} else {
// The ongoing event unmutes the object, so reset the mute reason to null.
newObject.MuteReason = types.String{}
Expand Down
7 changes: 3 additions & 4 deletions internal/object/objects_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"github.com/icinga/icinga-go-library/types"
"github.com/icinga/icinga-notifications/internal/event"
"github.com/icinga/icinga-notifications/internal/testutils"
"github.com/icinga/icinga-notifications/internal/utils"
"github.com/jmoiron/sqlx"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand All @@ -19,20 +18,20 @@ func TestRestoreMutedObjects(t *testing.T) {
db := testutils.GetTestDB(ctx, t)

var sourceID int64
err := utils.RunInTx(ctx, db, func(tx *sqlx.Tx) error {
err := db.ExecTx(ctx, func(ctx context.Context, tx *sqlx.Tx) error {
args := map[string]any{
"type": "notifications",
"name": "Icinga Notifications",
"changed_at": int64(1720702049000),
}
// We can't use config.Source here unfortunately due to cyclic import error!
id, err := utils.InsertAndFetchId(ctx, tx, `INSERT INTO source (type, name, changed_at) VALUES (:type, :name, :changed_at)`, args)
id, err := database.InsertObtainID(ctx, tx, `INSERT INTO source (type, name, changed_at) VALUES (:type, :name, :changed_at)`, args)
require.NoError(t, err, "populating source table should not fail")

sourceID = id
return nil
})
require.NoError(t, err, "utils.RunInTx() should not fail")
require.NoError(t, err, "db.ExecTx should not fail")

ClearCache()

Expand Down
7 changes: 3 additions & 4 deletions internal/recipient/recipient.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package recipient
import (
"fmt"
"github.com/icinga/icinga-go-library/types"
"github.com/icinga/icinga-notifications/internal/utils"
"go.uber.org/zap/zapcore"
"time"
)
Expand Down Expand Up @@ -55,11 +54,11 @@ func (r Key) MarshalText() (text []byte, err error) {
func ToKey(r Recipient) Key {
switch v := r.(type) {
case *Contact:
return Key{ContactID: utils.ToDBInt(v.ID)}
return Key{ContactID: types.MakeInt(v.ID, types.TransformZeroIntToNull)}
case *Group:
return Key{GroupID: utils.ToDBInt(v.ID)}
return Key{GroupID: types.MakeInt(v.ID, types.TransformZeroIntToNull)}
case *Schedule:
return Key{ScheduleID: utils.ToDBInt(v.ID)}
return Key{ScheduleID: types.MakeInt(v.ID, types.TransformZeroIntToNull)}
default:
panic(fmt.Sprintf("unexpected recipient type: %T", r))
}
Expand Down
Loading
Loading