| 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) { |
| |
| return nil |
| } |
| if err != nil { |
| return fmt.Errorf("query out-of-date alert status: %w", err) |
| } |
|
|
| if sub.LastAlertStatus == sub.Status { |
| |
| 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) { |
| |
| 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: |
| |
| 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 { |
| |
| 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 { |
| |
| return q.StatusMgrDeleteSub(ctx, sub.ID) |
| } |
| forceUpdate := typeInfo.SupportsStatusUpdates && typeInfo.StatusUpdatesRequired |
| if !forceUpdate && entry.UserID.UUID == info.UserID { |
| |
| |
| 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, |
| }) |
| } |
|
|