File size: 3,321 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package statusmgr

import (
	"context"
	"database/sql"
	"fmt"

	"github.com/google/uuid"
	"github.com/pkg/errors"
	"github.com/target/goalert/gadb"
	"github.com/target/goalert/util/log"
)

func (db *DB) update(ctx context.Context, tx *sql.Tx, id int64) error {
	q := gadb.New(tx)

	sub, err := q.StatusMgrFindOne(ctx, id)
	if errors.Is(err, sql.ErrNoRows) {
		// subscription was deleted or locked by another job
		return nil
	}
	if err != nil {
		return fmt.Errorf("query out-of-date alert status: %w", err)
	}

	if sub.LastAlertStatus == sub.Status {
		// fast path, no update needed (possible duplicate job)
		return nil
	}

	var eventType gadb.EnumAlertLogEvent
	switch sub.Status {
	case gadb.EnumAlertStatusTriggered:
		eventType = gadb.EnumAlertLogEventEscalated
	case gadb.EnumAlertStatusActive:
		eventType = gadb.EnumAlertLogEventAcknowledged
	case gadb.EnumAlertStatusClosed:
		eventType = gadb.EnumAlertLogEventClosed
	}

	entry, err := q.StatusMgrLogEntry(ctx, gadb.StatusMgrLogEntryParams{
		AlertID:   sub.AlertID,
		EventType: eventType,
	})
	if errors.Is(err, sql.ErrNoRows) {
		// no log entry, ignore
		err = nil
	}
	if err != nil {
		return fmt.Errorf("lookup latest log entry of '%s' for alert #%d: %w", eventType, sub.AlertID, err)
	}

	switch {
	case entry.ID == 0:
		// no log entry, log error but continue
		log.Log(ctx, fmt.Errorf("no log entry found for alert #%d status update (%s), skipping", sub.AlertID, eventType))
	case sub.ContactMethodID.Valid:
		info, err := q.ContactMethodFineOne(ctx, sub.ContactMethodID.UUID)
		if errors.Is(err, sql.ErrNoRows) || info.Disabled {
			// contact method was deleted or disabled
			return q.StatusMgrDeleteSub(ctx, sub.ID)
		}
		if err != nil {
			return fmt.Errorf("lookup contact method info: %w", err)
		}
		typeInfo, err := db.reg.TypeInfo(ctx, info.Dest.DestV1.Type)
		if err != nil {
			return fmt.Errorf("lookup contact method type info: %w", err)
		}
		if !typeInfo.SupportsStatusUpdates {
			// contact method doesn't support status updates
			return q.StatusMgrDeleteSub(ctx, sub.ID)
		}
		forceUpdate := typeInfo.SupportsStatusUpdates && typeInfo.StatusUpdatesRequired
		if !forceUpdate && entry.UserID.UUID == info.UserID {
			// We don't want to update a user for their own actions, unless
			// the contact method is forced to always send updates.
			break
		}
		err = q.StatusMgrSendUserMsg(ctx, gadb.StatusMgrSendUserMsgParams{
			ID:      uuid.New(),
			CmID:    sub.ContactMethodID.UUID,
			AlertID: sub.AlertID,
			UserID:  info.UserID,
			LogID:   sql.NullInt64{Int64: int64(entry.ID), Valid: true},
		})
		if err != nil {
			return fmt.Errorf("send user status update message: %w", err)
		}
	case sub.ChannelID.Valid:
		err = q.StatusMgrSendChannelMsg(ctx, gadb.StatusMgrSendChannelMsgParams{
			ID:        uuid.New(),
			ChannelID: sub.ChannelID.UUID,
			AlertID:   sub.AlertID,
			LogID:     sql.NullInt64{Int64: int64(entry.ID), Valid: true},
		})
		if err != nil {
			return fmt.Errorf("send channel status update message: %w", err)
		}
	default:
		return fmt.Errorf("invalid subscription: %v", sub)
	}

	if sub.Status == gadb.EnumAlertStatusClosed {
		return q.StatusMgrDeleteSub(ctx, sub.ID)
	}

	return q.StatusMgrUpdateSub(ctx, gadb.StatusMgrUpdateSubParams{
		ID:              sub.ID,
		LastAlertStatus: sub.Status,
	})
}