Skip to content

Commit

Permalink
add heartbeat type and rename somrename some database functions
Browse files Browse the repository at this point in the history
  • Loading branch information
almostinf committed Sep 23, 2024
1 parent 56b489d commit cca0fbc
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 64 deletions.
2 changes: 1 addition & 1 deletion database/redis/contact.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ func (connector *DbConnector) RemoveContact(contactID string) error {
pipe.SRem(ctx, teamContactsKey(existing.Team), contactID)

if !errors.Is(getEmergencyContactErr, database.ErrNil) {
removeEmergencyContactPipe(ctx, pipe, emergencyContact)
addRemoveEmergencyContactToPipe(ctx, pipe, emergencyContact)
}

_, err = pipe.Exec(ctx)
Expand Down
4 changes: 2 additions & 2 deletions database/redis/contact_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,7 @@ var user2Contacts = []*moira.ContactData{
var user2EmergencyContacts = []moira.EmergencyContact{
{
ContactID: "ContactID-000000000000003",
EmergencyTypes: []moira.EmergencyContactType{moira.EmergencyTypeNotifierOff},
HeartbeatTypes: []moira.HeartbeatType{moira.HeartbeatNotifierOff},
},
}

Expand Down Expand Up @@ -605,6 +605,6 @@ var team2Contacts = []*moira.ContactData{
var team2EmergencyContacts = []moira.EmergencyContact{
{
ContactID: "TeamContactID-000000000000003",
EmergencyTypes: []moira.EmergencyContactType{moira.EmergencyTypeNotifierOff},
HeartbeatTypes: []moira.HeartbeatType{moira.HeartbeatNotifierOff},
},
}
32 changes: 16 additions & 16 deletions database/redis/emergency_contact.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,14 @@ func (connector *DbConnector) getEmergencyContactIDs() ([]string, error) {
return emergencyContactIDs, nil
}

// GetEmergencyTypeContactIDs a method for obtaining contact IDs by specific emergency type.
func (connector *DbConnector) GetEmergencyTypeContactIDs(emergencyType moira.EmergencyContactType) ([]string, error) {
// GetHeartbeatTypeContactIDs a method for obtaining contact IDs by specific emergency type.
func (connector *DbConnector) GetHeartbeatTypeContactIDs(heartbeatType moira.HeartbeatType) ([]string, error) {
c := *connector.client
ctx := connector.context

contactIDs, err := c.SMembers(ctx, emergencyTypeContactsKey(emergencyType)).Result()
contactIDs, err := c.SMembers(ctx, heartbeatTypeContactsKey(heartbeatType)).Result()
if err != nil {
return nil, fmt.Errorf("failed to get emergency type contact IDs '%s': %w", emergencyType, err)
return nil, fmt.Errorf("failed to get heartbeat type contact IDs '%s': %w", heartbeatType, err)
}

return contactIDs, nil
Expand All @@ -104,8 +104,8 @@ func (connector *DbConnector) saveEmergencyContacts(emergencyContacts []moira.Em

pipe := c.TxPipeline()
for _, emergencyContact := range emergencyContacts {
if err := saveEmergencyContactPipe(ctx, pipe, emergencyContact); err != nil {
return err
if err := addSaveEmergencyContactToPipe(ctx, pipe, emergencyContact); err != nil {
return fmt.Errorf("failed to add save emergency contact '%s' to pipe: %w", emergencyContact.ContactID, err)
}
}

Expand All @@ -123,7 +123,7 @@ func (connector *DbConnector) SaveEmergencyContact(emergencyContact moira.Emerge

pipe := c.TxPipeline()

if err := saveEmergencyContactPipe(ctx, pipe, emergencyContact); err != nil {
if err := addSaveEmergencyContactToPipe(ctx, pipe, emergencyContact); err != nil {
return err
}

Expand All @@ -146,7 +146,7 @@ func (connector *DbConnector) RemoveEmergencyContact(contactID string) error {

pipe := c.TxPipeline()

removeEmergencyContactPipe(ctx, pipe, emergencyContact)
addRemoveEmergencyContactToPipe(ctx, pipe, emergencyContact)

if _, err := pipe.Exec(ctx); err != nil {
return fmt.Errorf("failed to remove emergency contact '%s': %w", contactID, err)
Expand All @@ -155,33 +155,33 @@ func (connector *DbConnector) RemoveEmergencyContact(contactID string) error {
return nil
}

func saveEmergencyContactPipe(ctx context.Context, pipe redis.Pipeliner, emergencyContact moira.EmergencyContact) error {
func addSaveEmergencyContactToPipe(ctx context.Context, pipe redis.Pipeliner, emergencyContact moira.EmergencyContact) error {
emergencyContactBytes, err := reply.GetEmergencyContactBytes(emergencyContact)
if err != nil {
return fmt.Errorf("failed to get emergency contact '%s' bytes: %w", emergencyContact.ContactID, err)
}

pipe.Set(ctx, emergencyContactsKey(emergencyContact.ContactID), emergencyContactBytes, redis.KeepTTL)

for _, emergencyType := range emergencyContact.EmergencyTypes {
pipe.SAdd(ctx, emergencyTypeContactsKey(emergencyType), emergencyContact.ContactID)
for _, heartbeatType := range emergencyContact.HeartbeatTypes {
pipe.SAdd(ctx, heartbeatTypeContactsKey(heartbeatType), emergencyContact.ContactID)
}

return nil
}

func removeEmergencyContactPipe(ctx context.Context, pipe redis.Pipeliner, emergencyContact moira.EmergencyContact) {
func addRemoveEmergencyContactToPipe(ctx context.Context, pipe redis.Pipeliner, emergencyContact moira.EmergencyContact) {
pipe.Del(ctx, emergencyContactsKey(emergencyContact.ContactID))

for _, emergencyType := range emergencyContact.EmergencyTypes {
pipe.SRem(ctx, emergencyTypeContactsKey(emergencyType), emergencyContact.ContactID)
for _, heartbeatType := range emergencyContact.HeartbeatTypes {
pipe.SRem(ctx, heartbeatTypeContactsKey(heartbeatType), emergencyContact.ContactID)
}
}

func emergencyContactsKey(contactID string) string {
return "moira-emergency-contacts:" + contactID
}

func emergencyTypeContactsKey(emergencyType moira.EmergencyContactType) string {
return "moira-emergency-type-contacts:" + string(emergencyType)
func heartbeatTypeContactsKey(heartbeatType moira.HeartbeatType) string {
return "moira-heartbeat-type-contacts:" + string(heartbeatType)
}
20 changes: 10 additions & 10 deletions database/redis/emergency_contact_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,17 @@ var (

testEmergencyContact = moira.EmergencyContact{
ContactID: testContactID,
EmergencyTypes: []moira.EmergencyContactType{moira.EmergencyTypeNotifierOff},
HeartbeatTypes: []moira.HeartbeatType{moira.HeartbeatNotifierOff},
}

testEmergencyContact2 = moira.EmergencyContact{
ContactID: testContactID2,
EmergencyTypes: []moira.EmergencyContactType{moira.EmergencyTypeNotifierOff},
HeartbeatTypes: []moira.HeartbeatType{moira.HeartbeatNotifierOff},
}

testEmergencyContact3 = moira.EmergencyContact{
ContactID: testContactID3,
EmergencyTypes: []moira.EmergencyContactType{moira.EmergencyTypeRedisDisconnected},
HeartbeatTypes: []moira.HeartbeatType{moira.HearbeatTypeNotSet},
}
)

Expand Down Expand Up @@ -145,9 +145,9 @@ func TestGetEmergencyTypeContactIDs(t *testing.T) {
database.Flush()
defer database.Flush()

Convey("Test GetEmergencyTypeContactIDs", t, func() {
Convey("Without any emergency contacts by type", func() {
emergencyContactIDs, err := database.GetEmergencyTypeContactIDs(moira.EmergencyTypeNotifierOff)
Convey("Test GetHeartbeatTypeContactIDs", t, func() {
Convey("Without any emergency contacts by heartbeat type", func() {
emergencyContactIDs, err := database.GetHeartbeatTypeContactIDs(moira.HeartbeatNotifierOff)
So(err, ShouldBeNil)
So(emergencyContactIDs, ShouldBeEmpty)
})
Expand All @@ -159,14 +159,14 @@ func TestGetEmergencyTypeContactIDs(t *testing.T) {
testEmergencyContact3,
})

emergencyContactIDs, err := database.GetEmergencyTypeContactIDs(moira.EmergencyTypeNotifierOff)
emergencyContactIDs, err := database.GetHeartbeatTypeContactIDs(moira.HeartbeatNotifierOff)
So(err, ShouldBeNil)
assert.ElementsMatch(t, emergencyContactIDs, []string{
testContactID,
testContactID2,
})

emergencyContactIDs, err = database.GetEmergencyTypeContactIDs(moira.EmergencyTypeRedisDisconnected)
emergencyContactIDs, err = database.GetHeartbeatTypeContactIDs(moira.HeartbeatNotifierOff)
So(err, ShouldBeNil)
assert.ElementsMatch(t, emergencyContactIDs, []string{
testContactID3,
Expand Down Expand Up @@ -197,7 +197,7 @@ func TestSaveEmergencyContact(t *testing.T) {
So(err, ShouldBeNil)
So(emergencyContacts, ShouldResemble, expectedEmergencyContacts)

emergencyContactIDs, err := database.GetEmergencyTypeContactIDs(moira.EmergencyTypeNotifierOff)
emergencyContactIDs, err := database.GetHeartbeatTypeContactIDs(moira.HeartbeatNotifierOff)
So(err, ShouldBeNil)
So(emergencyContactIDs, ShouldResemble, expectedEmergencyContactIDs)
})
Expand Down Expand Up @@ -230,7 +230,7 @@ func TestSaveEmergencyContacts(t *testing.T) {
So(err, ShouldBeNil)
assert.ElementsMatch(t, emergencyContacts, expectedEmergencyContacts)

emergencyContactIDs, err := database.GetEmergencyTypeContactIDs(moira.EmergencyTypeNotifierOff)
emergencyContactIDs, err := database.GetHeartbeatTypeContactIDs(moira.HeartbeatNotifierOff)
So(err, ShouldBeNil)
assert.ElementsMatch(t, emergencyContactIDs, expectedEmergencyContactIDs)
})
Expand Down
8 changes: 4 additions & 4 deletions database/redis/reply/emergency_contact.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,21 @@ import (
)

type emergencyContactStorageElement struct {
ContactID string `json:"contact_id"`
EmergencyTypes []moira.EmergencyContactType `json:"emergency_types"`
ContactID string `json:"contact_id"`
HeartbeatTypes []moira.HeartbeatType `json:"heartbeat_types"`
}

func (se emergencyContactStorageElement) toEmergencyContact() moira.EmergencyContact {
return moira.EmergencyContact{
ContactID: se.ContactID,
EmergencyTypes: se.EmergencyTypes,
HeartbeatTypes: se.HeartbeatTypes,
}
}

func toEmergencyContactStorageElement(emergencyContact moira.EmergencyContact) emergencyContactStorageElement {
return emergencyContactStorageElement{
ContactID: emergencyContact.ContactID,
EmergencyTypes: emergencyContact.EmergencyTypes,
HeartbeatTypes: emergencyContact.HeartbeatTypes,
}
}

Expand Down
6 changes: 3 additions & 3 deletions database/redis/reply/emergency_contacts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ import (
)

const (
testEmergencyContactVal = `{"contact_id":"test-contact-id","emergency_types":["notifier_off"]}`
testEmptyEmergencyContactVal = `{"contact_id":"","emergency_types":null}`
testEmergencyContactVal = `{"contact_id":"test-contact-id","heartbeat_types":["notifier_off"]}`
testEmptyEmergencyContactVal = `{"contact_id":"","heartbeat_types":null}`
)

var (
testEmergencyContact = moira.EmergencyContact{
ContactID: "test-contact-id",
EmergencyTypes: []moira.EmergencyContactType{moira.EmergencyTypeNotifierOff},
HeartbeatTypes: []moira.HeartbeatType{moira.HeartbeatNotifierOff},
}
testEmptyEmergencyContact = moira.EmergencyContact{}
)
Expand Down
21 changes: 9 additions & 12 deletions datatypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,21 +222,18 @@ func (contact *ContactData) ToTemplateContact() *templating.Contact {
}
}

// EmergencyContactType are Moira's special internal types of problems.
type EmergencyContactType string
// HeartbeatType are Moira's special internal types of problems.
type HeartbeatType string

const (
EmergencyTypeNotifierOff EmergencyContactType = "notifier_off"
EmergencyTypeRedisDisconnected EmergencyContactType = "redis_disconnected"
EmergencyTypeCheckerNoTriggerCheck EmergencyContactType = "checker_no_trigger_check"
EmergencyTypeFilterNoMetricsReceived EmergencyContactType = "filter_no_metrics_received"
EmergencyTypeRemoteCheckerNoTriggerCheck EmergencyContactType = "remote_checker_no_trigger_check"
HearbeatTypeNotSet HeartbeatType = "type_not_set"
HeartbeatNotifierOff HeartbeatType = "notifier_off"
)

// IsValid checks if such an emergency type exists.
func (emergencyType EmergencyContactType) IsValid() bool {
switch emergencyType {
case EmergencyTypeNotifierOff, EmergencyTypeRedisDisconnected, EmergencyTypeCheckerNoTriggerCheck, EmergencyTypeFilterNoMetricsReceived, EmergencyTypeRemoteCheckerNoTriggerCheck:
// IsValid checks if such an heartbeat type exists.
func (heartbeatType HeartbeatType) IsValid() bool {
switch heartbeatType {
case HeartbeatNotifierOff:
return true
default:
return false
Expand All @@ -246,7 +243,7 @@ func (emergencyType EmergencyContactType) IsValid() bool {
// EmergencyContact is the structure for contacts to which notifications will go in the event of special internal Moira problems.
type EmergencyContact struct {
ContactID string
EmergencyTypes []EmergencyContactType
HeartbeatTypes []HeartbeatType
}

// SubscriptionData represents user subscription.
Expand Down
14 changes: 5 additions & 9 deletions datatypes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1072,15 +1072,11 @@ func TestScheduledNotificationIsDelayed(t *testing.T) {
})
}

func TestIsValidEmergencyType(t *testing.T) {
Convey("Test IsValid emergency type", t, func() {
func TestIsValidHeartbeatType(t *testing.T) {
Convey("Test IsValid heartbeat type", t, func() {
Convey("Test valid cases", func() {
testcases := []EmergencyContactType{
EmergencyTypeNotifierOff,
EmergencyTypeRedisDisconnected,
EmergencyTypeCheckerNoTriggerCheck,
EmergencyTypeFilterNoMetricsReceived,
EmergencyTypeRemoteCheckerNoTriggerCheck,
testcases := []HeartbeatType{
HeartbeatNotifierOff,
}

for _, testcase := range testcases {
Expand All @@ -1089,7 +1085,7 @@ func TestIsValidEmergencyType(t *testing.T) {
})

Convey("Test invalid cases", func() {
testcases := []EmergencyContactType{
testcases := []HeartbeatType{
"notifier_on",
"checker_off",
}
Expand Down
2 changes: 1 addition & 1 deletion interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ type Database interface {
GetEmergencyContacts() ([]*EmergencyContact, error)
GetEmergencyContactsByIDs(contactIDs []string) ([]*EmergencyContact, error)
GetEmergencyContact(contactID string) (EmergencyContact, error)
GetEmergencyTypeContactIDs(emergencyType EmergencyContactType) ([]string, error)
GetHeartbeatTypeContactIDs(heartbeatType HeartbeatType) ([]string, error)
SaveEmergencyContact(emergencyContact EmergencyContact) error
RemoveEmergencyContact(contactID string) error

Expand Down
12 changes: 6 additions & 6 deletions mock/moira-alert/database.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit cca0fbc

Please sign in to comment.