File size: 4,221 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 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
package alertlog
import (
"context"
"database/sql"
"encoding/json"
"fmt"
"strconv"
"time"
"github.com/google/uuid"
"github.com/target/goalert/util/log"
)
type Entry struct {
id int
alertID int
timestamp time.Time
_type Type
message string
subject struct {
_type SubjectType
userID uuid.NullUUID
userName sql.NullString
integrationKeyID uuid.NullUUID
integrationKeyName sql.NullString
heartbeatMonitorID uuid.NullUUID
heartbeatMonitorName sql.NullString
channelID uuid.NullUUID
channelName sql.NullString
classifier string
}
meta rawJSON
}
func (e Entry) Meta(ctx context.Context) interface{} {
var dest interface{}
switch e.Type() {
case TypeEscalated:
dest = &EscalationMetaData{}
case TypeNotificationSent:
dest = &NotificationMetaData{}
case TypeCreated:
dest = &CreatedMetaData{}
case TypeClosed:
dest = &AutoClose{}
default:
return nil
}
err := json.Unmarshal(e.meta, dest)
if err != nil {
log.Debug(ctx, err)
return nil
}
return dest
}
func (e Entry) AlertID() int {
return e.alertID
}
func (e Entry) MessageID(ctx context.Context) string {
m := e.Meta(ctx)
if m == nil {
return ""
}
if m, ok := m.(*NotificationMetaData); ok && m != nil {
return m.MessageID
}
return ""
}
func (e Entry) ID() int {
return e.id
}
func (e Entry) Timestamp() time.Time {
return e.timestamp
}
func (e Entry) Type() Type {
switch e._type {
case _TypeResponseReceived:
return respRecvType(e.message)
case _TypeStatusChanged:
return statChgType(e.message)
}
return e._type
}
func (e Entry) Subject() *Subject {
if e.subject._type == SubjectTypeNone {
if e.message != "" {
return e.subjectFromMessage()
}
return nil
}
s := &Subject{
Type: e.subject._type,
Classifier: e.subject.classifier,
}
switch s.Type {
case SubjectTypeUser:
s.ID = e.subject.userID.UUID.String()
s.Name = e.subject.userName.String
case SubjectTypeIntegrationKey:
s.ID = e.subject.integrationKeyID.UUID.String()
s.Name = e.subject.integrationKeyName.String
case SubjectTypeHeartbeatMonitor:
s.ID = e.subject.heartbeatMonitorID.UUID.String()
s.Name = e.subject.heartbeatMonitorName.String
case SubjectTypeChannel:
s.ID = e.subject.channelID.UUID.String()
s.Name = e.subject.channelName.String
}
return s
}
func escalationMsg(m *EscalationMetaData) string {
msg := fmt.Sprintf(" to step #%d", m.NewStepIndex+1)
if m.Repeat {
msg += " (policy repeat)"
}
if m.Forced {
msg += " due to manual escalation"
} else if m.Deleted {
msg += " due to current step being deleted"
} else if m.OldDelayMinutes > 0 {
msg += fmt.Sprintf(" automatically after %d minutes", m.OldDelayMinutes)
}
return msg
}
func (e Entry) String(ctx context.Context) string {
var msg string
var infinitive bool
switch e.Type() {
case TypeCreated:
msg = "Created"
case TypeAcknowledged:
msg = "Acknowledged"
case TypeClosed:
msg = "Closed"
meta, ok := e.Meta(ctx).(*AutoClose)
if ok {
msg = "Closed due to inactivity (unacknowledged for " + strconv.Itoa(meta.AlertAutoCloseDays) + " days)"
}
case TypeEscalated:
msg = "Escalated"
meta, ok := e.Meta(ctx).(*EscalationMetaData)
if ok {
msg += escalationMsg(meta)
}
case TypeNotificationSent:
msg = "Notification sent"
infinitive = true
case TypeNoNotificationSent:
msg = "No notification sent"
infinitive = true
case TypePolicyUpdated:
msg = "Policy updated"
case TypeDuplicateSupressed:
msg = "Suppressed duplicate: created"
case TypeEscalationRequest:
msg = "Escalation requested"
default:
return "Error"
}
// include subject, if available
msg += subjectString(infinitive, e.Subject())
return msg
}
func (e *Entry) scanWith(scan func(...interface{}) error) error {
return scan(
&e.id,
&e.alertID,
&e.timestamp,
&e._type,
&e.message,
&e.subject._type,
&e.subject.userID,
&e.subject.userName,
&e.subject.integrationKeyID,
&e.subject.integrationKeyName,
&e.subject.heartbeatMonitorID,
&e.subject.heartbeatMonitorName,
&e.subject.channelID,
&e.subject.channelName,
&e.subject.classifier,
&e.meta,
)
}
|