-
Notifications
You must be signed in to change notification settings - Fork 4
/
incident.go
140 lines (123 loc) · 3.3 KB
/
incident.go
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
package models
import (
"time"
)
type Incident struct {
GUID string `json:"guid" gorm:"primary_key"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
State IncidentState `json:"state"`
ComponentState ComponentState `json:"component_state"`
Components *Components `json:"components" gorm:"type:varchar(300)"`
Messages []Message `json:"messages" gorm:"ForeignKey:IncidentGUID;"`
Metadata []Metadata `json:"metadata" gorm:"ForeignKey:IncidentGUID;"`
IsScheduled bool `json:"is_scheduled"`
ScheduledEnd time.Time `json:"scheduled_end"`
Origin string `json:"origin"`
Persistent bool `json:"persistent"`
}
type IncidentUpdateRequest struct {
GUID *string `json:"guid"`
CreatedAt time.Time `json:"created_at"`
State *IncidentState `json:"state"`
ComponentState *ComponentState `json:"component_state"`
Components *Components `json:"components"`
Messages *[]Message `json:"messages"`
Metadata *[]Metadata `json:"metadata"`
IsScheduled *bool `json:"is_scheduled"`
ScheduledEnd time.Time `json:"scheduled_end"`
Origin *string `json:"origin"`
NoNotify bool `json:"no_notify"`
Persistent *bool `json:"persistent"`
}
func (i Incident) MainMessage() Message {
if len(i.Messages) == 0 {
return Message{}
}
return i.Messages[len(i.Messages)-1]
}
func (i Incident) UpdateMessages() []Message {
return i.Messages[:len(i.Messages)-1]
}
func (i Incident) UpdateMessagesReverse() []Message {
messages := i.UpdateMessages()
newOrder := make([]Message, len(messages))
p := len(messages) - 1
for _, message := range messages {
newOrder[p] = message
p--
}
return newOrder
}
func (i Incident) LastMessage() Message {
if len(i.Messages) == 0 {
return Message{}
}
return i.Messages[0]
}
func (i Incident) IsNew() bool {
return i.CreatedAt.Equal(i.UpdatedAt)
}
type Incidents []Incident
func (p Incidents) Len() int {
return len(p)
}
func (p Incidents) Less(i, j int) bool {
return p[i].CreatedAt.Before(p[j].CreatedAt)
}
func (p Incidents) Swap(i, j int) {
p[i], p[j] = p[j], p[i]
}
func (p Incidents) Find(guid string) Incident {
for _, incident := range p {
if incident.GUID == guid {
return incident
}
}
return Incident{}
}
func (p Incidents) Filter(guid string) Incidents {
incidents := make(Incidents, 0)
for _, incident := range p {
if incident.GUID == guid {
continue
}
incidents = append(incidents, incident)
}
return incidents
}
func TextState(state ComponentState) string {
switch state {
case DegradedPerformance:
return "Degraded Performance"
case PartialOutage:
return "Partial Outage"
case UnderMaintenance:
return "Under Maintenance"
case MajorOutage:
return "Major Outage"
}
return "Operational"
}
func TextIncidentState(state IncidentState) string {
switch state {
case Resolved:
return "resolved"
case Monitoring:
return "monitoring"
case Idle:
return "idle"
}
return "unresolved"
}
func TextScheduledState(state IncidentState) string {
switch state {
case Resolved:
return "finished"
case Monitoring:
return "monitoring"
case Idle:
return "idle"
}
return "started"
}