44 "context"
55 "errors"
66 "fmt"
7+ "sync"
8+ "time"
9+
710 "github.com/icinga/icinga-go-library/database"
811 "github.com/icinga/icinga-go-library/types"
912 "github.com/icinga/icinga-notifications/internal/config"
@@ -13,11 +16,8 @@ import (
1316 "github.com/icinga/icinga-notifications/internal/object"
1417 "github.com/icinga/icinga-notifications/internal/recipient"
1518 "github.com/icinga/icinga-notifications/internal/rule"
16- "github.com/icinga/icinga-notifications/internal/utils"
1719 "github.com/jmoiron/sqlx"
1820 "go.uber.org/zap"
19- "sync"
20- "time"
2121)
2222
2323type ruleID = int64
@@ -252,7 +252,7 @@ func (i *Incident) RetriggerEscalations(ev *event.Event) {
252252
253253 var notifications []* NotificationEntry
254254 ctx := context .Background ()
255- err = utils . RunInTx (ctx , i . db , func ( tx * sqlx.Tx ) error {
255+ err = i . db . ExecTx (ctx , func ( ctx context. Context , tx * sqlx.Tx ) error {
256256 err := ev .Sync (ctx , tx , i .db , i .Object .ID )
257257 if err != nil {
258258 return err
@@ -298,12 +298,12 @@ func (i *Incident) processSeverityChangedEvent(ctx context.Context, tx *sqlx.Tx,
298298
299299 hr := & HistoryRow {
300300 IncidentID : i .Id ,
301- EventID : utils . ToDBInt (ev .ID ),
301+ EventID : types . MakeInt (ev .ID , types . TransformZeroIntToNull ),
302302 Time : types .UnixMilli (time .Now ()),
303303 Type : IncidentSeverityChanged ,
304304 NewSeverity : newSeverity ,
305305 OldSeverity : oldSeverity ,
306- Message : utils . ToDBString (ev .Message ),
306+ Message : types . MakeString (ev .Message , types . TransformEmptyStringToNull ),
307307 }
308308
309309 if err := hr .Sync (ctx , i .db , tx ); err != nil {
@@ -319,7 +319,7 @@ func (i *Incident) processSeverityChangedEvent(ctx context.Context, tx *sqlx.Tx,
319319
320320 hr = & HistoryRow {
321321 IncidentID : i .Id ,
322- EventID : utils . ToDBInt (ev .ID ),
322+ EventID : types . MakeInt (ev .ID , types . TransformZeroIntToNull ),
323323 Time : i .RecoveredAt ,
324324 Type : Closed ,
325325 }
@@ -357,9 +357,9 @@ func (i *Incident) processIncidentOpenedEvent(ctx context.Context, tx *sqlx.Tx,
357357 IncidentID : i .Id ,
358358 Type : Opened ,
359359 Time : types .UnixMilli (ev .Time ),
360- EventID : utils . ToDBInt (ev .ID ),
360+ EventID : types . MakeInt (ev .ID , types . TransformZeroIntToNull ),
361361 NewSeverity : i .Severity ,
362- Message : utils . ToDBString (ev .Message ),
362+ Message : types . MakeString (ev .Message , types . TransformEmptyStringToNull ),
363363 }
364364
365365 if err := hr .Sync (ctx , i .db , tx ); err != nil {
@@ -377,7 +377,7 @@ func (i *Incident) handleMuteUnmute(ctx context.Context, tx *sqlx.Tx, ev *event.
377377 return nil
378378 }
379379
380- hr := & HistoryRow {IncidentID : i .Id , EventID : utils . ToDBInt (ev .ID ), Time : types .UnixMilli (time .Now ())}
380+ hr := & HistoryRow {IncidentID : i .Id , EventID : types . MakeInt (ev .ID , types . TransformZeroIntToNull ), Time : types .UnixMilli (time .Now ())}
381381 logger := i .logger .With (zap .String ("event" , ev .String ()))
382382 if i .Object .IsMuted () {
383383 hr .Type = Muted
@@ -388,7 +388,7 @@ func (i *Incident) handleMuteUnmute(ctx context.Context, tx *sqlx.Tx, ev *event.
388388 } else {
389389 hr .Type = Unmuted
390390 // 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 )
391+ hr .Message = types . MakeString (ev .MuteReason , types . TransformEmptyStringToNull )
392392 logger .Infow ("Unmuting incident" , zap .String ("reason" , ev .MuteReason ))
393393 }
394394
@@ -426,8 +426,8 @@ func (i *Incident) evaluateRules(ctx context.Context, tx *sqlx.Tx, eventID int64
426426 hr := & HistoryRow {
427427 IncidentID : i .Id ,
428428 Time : types .UnixMilli (time .Now ()),
429- EventID : utils . ToDBInt (eventID ),
430- RuleID : utils . ToDBInt (r .ID ),
429+ EventID : types . MakeInt (eventID , types . TransformZeroIntToNull ),
430+ RuleID : types . MakeInt (r .ID , types . TransformZeroIntToNull ),
431431 Type : RuleMatched ,
432432 }
433433 if err := hr .Sync (ctx , i .db , tx ); err != nil {
@@ -534,9 +534,9 @@ func (i *Incident) triggerEscalations(ctx context.Context, tx *sqlx.Tx, ev *even
534534 hr := & HistoryRow {
535535 IncidentID : i .Id ,
536536 Time : state .TriggeredAt ,
537- EventID : utils . ToDBInt (ev .ID ),
538- RuleEscalationID : utils . ToDBInt (state .RuleEscalationID ),
539- RuleID : utils . ToDBInt (r .ID ),
537+ EventID : types . MakeInt (ev .ID , types . TransformZeroIntToNull ),
538+ RuleEscalationID : types . MakeInt (state .RuleEscalationID , types . TransformZeroIntToNull ),
539+ RuleID : types . MakeInt (r .ID , types . TransformZeroIntToNull ),
540540 Type : EscalationTriggered ,
541541 }
542542
@@ -649,12 +649,12 @@ func (i *Incident) processAcknowledgementEvent(ctx context.Context, tx *sqlx.Tx,
649649 hr := & HistoryRow {
650650 IncidentID : i .Id ,
651651 Key : recipientKey ,
652- EventID : utils . ToDBInt (ev .ID ),
652+ EventID : types . MakeInt (ev .ID , types . TransformZeroIntToNull ),
653653 Type : RecipientRoleChanged ,
654654 Time : types .UnixMilli (time .Now ()),
655655 NewRecipientRole : newRole ,
656656 OldRecipientRole : oldRole ,
657- Message : utils . ToDBString (ev .Message ),
657+ Message : types . MakeString (ev .Message , types . TransformEmptyStringToNull ),
658658 }
659659
660660 if err := hr .Sync (ctx , i .db , tx ); err != nil {
0 commit comments