-
Notifications
You must be signed in to change notification settings - Fork 198
/
notify.go
174 lines (138 loc) · 5.47 KB
/
notify.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
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
package notify
import (
"fmt"
"os"
"reflect"
"regexp"
"strings"
)
//Diffrent types of clients to deliver notifications
type NotificationTypes struct {
MailNotify MailNotify `json:"mail"`
Mailgun MailgunNotify `json:"mailGun"`
Slack SlackNotify `json:"slack"`
Http HttpNotify `json:"httpEndPoint"`
Dingding DingdingNotify `json:"dingding"`
Pagerduty PagerdutyNotify `json:"pagerduty"`
}
type ResponseTimeNotification struct {
Url string
RequestType string
ExpectedResponsetime int64
MeanResponseTime int64
}
type ErrorNotification struct {
Url string
RequestType string
ResponseBody string
Error string
OtherInfo string
}
var (
errorCount = 0
notificationsList []Notify
)
type Notify interface {
GetClientName() string
Initialize() error
SendResponseTimeNotification(notification ResponseTimeNotification) error
SendErrorNotification(notification ErrorNotification) error
}
//Add notification clients given by user in config file to notificationsList
func AddNew(notificationTypes NotificationTypes) {
v := reflect.ValueOf(notificationTypes)
for i := 0; i < v.NumField(); i++ {
notifyString := fmt.Sprint(v.Field(i).Interface().(Notify))
//Check whether notify object is empty . if its not empty add to the list
if !isEmptyObject(notifyString) {
notificationsList = append(notificationsList, v.Field(i).Interface().(Notify))
}
}
if len(notificationsList) == 0 {
println("No clients Registered for Notifications")
} else {
println("Initializing Notification Clients....")
}
for _, value := range notificationsList {
initErr := value.Initialize()
if initErr != nil {
println("Notifications : Failed to Initialize ", value.GetClientName(), ".Please check the details in config file ")
println("Error Details :", initErr.Error())
} else {
println("Notifications :", value.GetClientName(), " Intialized")
}
}
}
//Send response time notification to all clients registered
func SendResponseTimeNotification(responseTimeNotification ResponseTimeNotification) {
for _, value := range notificationsList {
err := value.SendResponseTimeNotification(responseTimeNotification)
//TODO: exponential retry if fails ? what to do when error occurs ?
if err != nil {
}
}
}
//Send Error notification to all clients registered
func SendErrorNotification(errorNotification ErrorNotification) {
for _, value := range notificationsList {
err := value.SendErrorNotification(errorNotification)
//TODO: exponential retry if fails ? what to do when error occurs ?
if err != nil {
}
}
}
//Send Test notification to all registered clients .To make sure everything is working
func SendTestNotification() {
println("Sending Test notifications to the registered clients")
for _, value := range notificationsList {
err := value.SendResponseTimeNotification(ResponseTimeNotification{"http://test.com", "GET", 700, 800})
if err != nil {
println("Failed to Send Response Time notification to ", value.GetClientName(), " Please check the details entered in the config file")
println("Error Details :", err.Error())
os.Exit(3)
} else {
println("Sent Test Response Time notification to ", value.GetClientName(), ". Make sure you received it")
}
err1 := value.SendErrorNotification(ErrorNotification{"http://test.com", "GET", "This is test notification", "Test notification", "test"})
if err1 != nil {
println("Failed to Send Error notification to ", value.GetClientName(), " Please check the details entered in the config file")
println("Error Details :", err1.Error())
os.Exit(3)
} else {
println("Sent Test Error notification to ", value.GetClientName(), ". Make sure you received it")
}
}
}
func validateEmail(email string) bool {
Re := regexp.MustCompile(`^[a-z0-9._%+\-]+@[a-z0-9.\-]+\.[a-z]{2,4}$`)
return Re.MatchString(email)
}
func isEmptyObject(objectString string) bool {
objectString = strings.Replace(objectString, "0", "", -1)
objectString = strings.Replace(objectString, "map", "", -1)
objectString = strings.Replace(objectString, "[]", "", -1)
objectString = strings.Replace(objectString, " ", "", -1)
objectString = strings.Replace(objectString, "{", "", -1)
objectString = strings.Replace(objectString, "}", "", -1)
if len(objectString) > 0 {
return false
} else {
return true
}
}
//A readable message string from responseTimeNotification
func getMessageFromResponseTimeNotification(responseTimeNotification ResponseTimeNotification) string {
message := fmt.Sprintf("Notification From StatusOk\n\nOne of your apis response time is below than expected."+
"\n\nPlease find the Details below"+
"\n\nUrl: %v \nRequestType: %v \nCurrent Average Response Time: %v ms\nExpected Response Time: %v ms\n"+
"\n\nThanks", responseTimeNotification.Url, responseTimeNotification.RequestType, responseTimeNotification.MeanResponseTime, responseTimeNotification.ExpectedResponsetime)
return message
}
//A readable message string from errorNotification
func getMessageFromErrorNotification(errorNotification ErrorNotification) string {
message := fmt.Sprintf("Notification From StatusOk\n\nWe are getting error when we try to send request to one of your apis"+
"\n\nPlease find the Details below"+
"\n\nUrl: %v \nRequestType: %v \nError Message: %v \nResponse Body: %v\nOther Info:%v\n"+
"\n\nThanks", errorNotification.Url, errorNotification.RequestType, errorNotification.Error, errorNotification.ResponseBody, errorNotification.OtherInfo)
return message
}