File size: 2,507 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package message

import (
	"sort"
	"testing"
	"time"

	"github.com/google/uuid"
	"github.com/stretchr/testify/assert"
	"github.com/target/goalert/notification"
)

func destIDFrom(s string) notification.DestID {
	return notification.DestID{CMID: uuid.NullUUID{Valid: true, UUID: uuid.Must(uuid.FromBytes([]byte(s)))}}
}

func TestDedupAlerts(t *testing.T) {
	foo := destIDFrom("foofoofoofoofoof")
	bar := destIDFrom("barbarbarbarbarb")
	messages := []Message{
		{ID: "1", Type: notification.MessageTypeTest},
		{ID: "2", Type: notification.MessageTypeAlertBundle},
		{ID: "3", Type: notification.MessageTypeAlert, CreatedAt: time.Date(2021, 7, 15, 11, 0, 0, 0, time.UTC), AlertID: 1, DestID: foo}, // duplicates 4 (same dest and alert, but newer)
		{ID: "4", Type: notification.MessageTypeAlert, CreatedAt: time.Date(2021, 7, 15, 10, 0, 0, 0, time.UTC), AlertID: 1, DestID: foo},
		{ID: "5", Type: notification.MessageTypeAlert, CreatedAt: time.Date(2021, 7, 15, 12, 0, 0, 0, time.UTC), AlertID: 1, DestID: foo}, // duplicates 4 (same dest and alert, but newer)

		{ID: "6", Type: notification.MessageTypeAlert, CreatedAt: time.Date(2021, 7, 15, 8, 0, 0, 0, time.UTC), AlertID: 1, DestID: bar, SentAt: time.Unix(1, 0)}, // duplicates 7 but sent already

		{ID: "7", Type: notification.MessageTypeAlert, CreatedAt: time.Date(2021, 7, 15, 9, 0, 0, 0, time.UTC), AlertID: 1, DestID: bar},
		{ID: "8", Type: notification.MessageTypeAlert, CreatedAt: time.Date(2021, 7, 15, 13, 0, 0, 0, time.UTC), AlertID: 2, DestID: foo},
	}

	res, err := dedupAlerts(messages, func(parentID string, duplicates []string) error {
		assert.Equal(t, "4", parentID)
		sort.Strings(duplicates)
		assert.EqualValues(t, []string{"3", "5"}, duplicates)
		return nil
	})
	assert.NoError(t, err)
	assert.Len(t, res, 6)

	assert.ElementsMatch(t,
		[]Message{
			{ID: "1", Type: notification.MessageTypeTest},
			{ID: "2", Type: notification.MessageTypeAlertBundle},
			{ID: "4", Type: notification.MessageTypeAlert, CreatedAt: time.Date(2021, 7, 15, 10, 0, 0, 0, time.UTC), AlertID: 1, DestID: foo},
			{ID: "7", Type: notification.MessageTypeAlert, CreatedAt: time.Date(2021, 7, 15, 9, 0, 0, 0, time.UTC), AlertID: 1, DestID: bar},
			{ID: "8", Type: notification.MessageTypeAlert, CreatedAt: time.Date(2021, 7, 15, 13, 0, 0, 0, time.UTC), AlertID: 2, DestID: foo},
			{ID: "6", Type: notification.MessageTypeAlert, CreatedAt: time.Date(2021, 7, 15, 8, 0, 0, 0, time.UTC), AlertID: 1, DestID: bar, SentAt: time.Unix(1, 0)},
		},
		res)
}