Skip to content

Commit 7cf3c9c

Browse files
committed
Drop superfluous NotificationType mappings
1 parent 6ba49cc commit 7cf3c9c

File tree

3 files changed

+81
-104
lines changed

3 files changed

+81
-104
lines changed

cmd/icingadb-migrate/convert.go

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"github.com/icinga/icinga-go-library/types"
99
"github.com/icinga/icinga-go-library/utils"
1010
"github.com/icinga/icingadb/pkg/common"
11-
icingadbTypes "github.com/icinga/icingadb/pkg/icingadb/types"
1211
v1 "github.com/icinga/icingadb/pkg/icingadb/v1"
1312
"github.com/icinga/icingadb/pkg/icingadb/v1/history"
1413
"github.com/jmoiron/sqlx"
@@ -621,17 +620,16 @@ func convertNotificationRows(
621620
// migrated data itself via the history ID as object name, i.e. one "virtual object" per sent notification.
622621
name := strconv.FormatUint(row.NotificationId, 10)
623622

624-
nt := convertNotificationType(row.NotificationReason, row.State)
625-
626-
ntEnum, err := nt.Value()
623+
notificationType, err := convertNotificationType(row.NotificationReason, row.State)
627624
if err != nil {
625+
log.With("notification_reason", row.NotificationReason, "state", row.State).Errorf("%+v", err)
628626
continue
629627
}
630628

631629
ts := convertTime(row.EndTime.Int64, row.EndTimeUsec)
632630
tsMilli := float64(ts.Time().UnixMilli())
633-
notificationHistoryId := hashAny([]interface{}{env, name, ntEnum, tsMilli})
634-
id := hashAny([]interface{}{env, "notification", name, ntEnum, tsMilli})
631+
notificationHistoryId := hashAny([]interface{}{env, name, notificationType, tsMilli})
632+
id := hashAny([]interface{}{env, "notification", name, notificationType, tsMilli})
635633
typ := objectTypes[row.ObjecttypeId]
636634
hostId := calcObjectId(env, row.Name1)
637635
serviceId := calcServiceId(env, row.Name1, row.Name2)
@@ -654,7 +652,7 @@ func convertNotificationRows(
654652
ServiceId: serviceId,
655653
},
656654
NotificationId: calcObjectId(env, name),
657-
Type: nt,
655+
Type: history.NotificationType(notificationType),
658656
SendTime: ts,
659657
State: row.State,
660658
PreviousHardState: previousHardState,
@@ -699,34 +697,34 @@ func convertNotificationRows(
699697
return
700698
}
701699

702-
// convertNotificationType maps IDO values[1] to Icinga DB ones[2].
700+
// convertNotificationType maps IDO values [^1] to their Icinga DB counterparts [^2].
703701
//
704-
// [1]: https://github.com/Icinga/icinga2/blob/32c7f7730db154ba0dff5856a8985d125791c/lib/db_ido/dbevents.cpp#L1507-L1524
705-
// [2]: https://github.com/Icinga/icingadb/blob/8f31ac143875498797725adb9bfacf3d4/pkg/types/notification_type.go#L53-L61
706-
func convertNotificationType(notificationReason, state uint8) icingadbTypes.NotificationType {
702+
// [^1]: https://github.com/Icinga/icinga2/blob/32c7f7730db154ba0dff5856a8985d125791c/lib/db_ido/dbevents.cpp#L1507-L1524
703+
// [^2]: https://github.com/Icinga/icinga2/blob/32c7f7730db154ba0dff5856a8985d125791c73e/lib/icingadb/icingadb-utility.cpp#L157-L176
704+
func convertNotificationType(notificationReason, state uint8) (string, error) {
707705
switch notificationReason {
708706
case 0: // state
709707
if state == 0 {
710-
return 64 // recovery
708+
return "recovery", nil
711709
} else {
712-
return 32 // problem
710+
return "problem", nil
713711
}
714-
case 1: // acknowledgement
715-
return 16
716-
case 2: // flapping start
717-
return 128
718-
case 3: // flapping end
719-
return 256
720-
case 5: // downtime start
721-
return 1
722-
case 6: // downtime end
723-
return 2
724-
case 7: // downtime removed
725-
return 4
726-
case 8: // custom
727-
return 8
728-
default: // bad notification type
729-
return 0
712+
case 1:
713+
return "acknowledgement", nil
714+
case 2:
715+
return "flapping_start", nil
716+
case 3:
717+
return "flapping_end", nil
718+
case 5:
719+
return "downtime_start", nil
720+
case 6:
721+
return "downtime_end", nil
722+
case 7:
723+
return "downtime_removed", nil
724+
case 8:
725+
return "custom", nil
726+
default:
727+
return "", fmt.Errorf("bad notification type: %d", notificationReason)
730728
}
731729
}
732730

pkg/icingadb/types/notification_type.go

Lines changed: 0 additions & 62 deletions
This file was deleted.

pkg/icingadb/v1/history/notification.go

Lines changed: 54 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,25 @@
11
package history
22

33
import (
4+
"database/sql/driver"
5+
"encoding"
46
"github.com/icinga/icinga-go-library/database"
57
"github.com/icinga/icinga-go-library/types"
6-
icingadbTypes "github.com/icinga/icingadb/pkg/icingadb/types"
78
v1 "github.com/icinga/icingadb/pkg/icingadb/v1"
9+
"github.com/pkg/errors"
810
)
911

1012
type NotificationHistory struct {
1113
HistoryTableEntity `json:",inline"`
1214
HistoryTableMeta `json:",inline"`
13-
NotificationId types.Binary `json:"notification_id"`
14-
Type icingadbTypes.NotificationType `json:"type"`
15-
SendTime types.UnixMilli `json:"send_time"`
16-
State uint8 `json:"state"`
17-
PreviousHardState uint8 `json:"previous_hard_state"`
18-
Author string `json:"author"`
19-
Text types.String `json:"text"`
20-
UsersNotified uint16 `json:"users_notified"`
15+
NotificationId types.Binary `json:"notification_id"`
16+
Type NotificationType `json:"type"`
17+
SendTime types.UnixMilli `json:"send_time"`
18+
State uint8 `json:"state"`
19+
PreviousHardState uint8 `json:"previous_hard_state"`
20+
Author string `json:"author"`
21+
Text types.String `json:"text"`
22+
UsersNotified uint16 `json:"users_notified"`
2123
}
2224

2325
type UserNotificationHistory struct {
@@ -42,10 +44,49 @@ func (*HistoryNotification) TableName() string {
4244
return "history"
4345
}
4446

47+
// NotificationType represents the type of notification for a notification history entry.
48+
//
49+
// Starting with Icinga 2 v2.15, the type is will always be written to Redis as a string.
50+
// This merely exists to provide a compatibility with older history entries lying around in Redis,
51+
// which may have been written as an integer.
52+
type NotificationType string
53+
54+
// UnmarshalText implements the encoding.TextUnmarshaler interface.
55+
func (nt *NotificationType) UnmarshalText(text []byte) error {
56+
t := string(text)
57+
for bitset, name := range notificationTypes {
58+
if t == name || t == bitset {
59+
*nt = NotificationType(name)
60+
return nil
61+
}
62+
}
63+
64+
return errors.Errorf("bad notification type: %#v", t)
65+
}
66+
67+
// Value implements the driver.Valuer interface.
68+
func (nt NotificationType) Value() (driver.Value, error) { return string(nt), nil }
69+
70+
// notificationTypes maps all valid NotificationType values to their SQL representation.
71+
// The keys are the bitset values as strings, and the values are the corresponding names.
72+
var notificationTypes = map[string]string{
73+
"1": "downtime_start",
74+
"2": "downtime_end",
75+
"4": "downtime_removed",
76+
"8": "custom",
77+
"16": "acknowledgement",
78+
"32": "problem",
79+
"64": "recovery",
80+
"128": "flapping_start",
81+
"256": "flapping_end",
82+
}
83+
4584
// Assert interface compliance.
4685
var (
47-
_ UpserterEntity = (*NotificationHistory)(nil)
48-
_ UpserterEntity = (*UserNotificationHistory)(nil)
49-
_ database.TableNamer = (*HistoryNotification)(nil)
50-
_ UpserterEntity = (*HistoryNotification)(nil)
86+
_ UpserterEntity = (*NotificationHistory)(nil)
87+
_ UpserterEntity = (*UserNotificationHistory)(nil)
88+
_ database.TableNamer = (*HistoryNotification)(nil)
89+
_ UpserterEntity = (*HistoryNotification)(nil)
90+
_ encoding.TextUnmarshaler = (*NotificationType)(nil)
91+
_ driver.Valuer = NotificationType("")
5192
)

0 commit comments

Comments
 (0)