forked from megaease/easeprobe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
result.go
306 lines (279 loc) · 8.14 KB
/
result.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
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/*
* Copyright (c) 2022, MegaEase
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http:https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package report
import (
"bytes"
"encoding/csv"
"encoding/json"
"fmt"
"time"
"github.com/megaease/easeprobe/global"
"github.com/megaease/easeprobe/probe"
log "github.com/sirupsen/logrus"
)
// ToLog convert the result object to Log format
func ToLog(r probe.Result) string {
tpl := `title="%s"; name="%s"; status="%s"; endpoint="%s"; rtt="%s"; time="%s"; message="%s"`
rtt := r.RoundTripTime.Round(time.Millisecond)
return fmt.Sprintf(tpl,
r.Title(), r.Name, r.Status.String(), r.Endpoint, rtt, r.StartTime.Format(r.TimeFormat), r.Message)
}
// ToText convert the result object to ToText
func ToText(r probe.Result) string {
tpl := "[%s] %s\n%s - ⏱ %s\n%s\n%s"
rtt := r.RoundTripTime.Round(time.Millisecond)
return fmt.Sprintf(tpl,
r.Title(), r.Status.Emoji(), r.Endpoint, rtt, r.Message,
global.FooterString()+" at "+r.StartTime.Format(r.TimeFormat))
}
// resultDTO only for JSON format notification
type resultDTO struct {
Name string `json:"name"`
Endpoint string `json:"endpoint"`
StartTime time.Time `json:"time"`
StartTimestamp int64 `json:"timestamp"`
RoundTripTime time.Duration `json:"rtt"`
Status probe.Status `json:"status"`
PreStatus probe.Status `json:"prestatus"`
Message string `json:"message"`
}
// ToJSON convert the result object to ToJSON
func ToJSON(r probe.Result) string {
ro := resultDTO{
Name: r.Title(),
Endpoint: r.Endpoint,
StartTime: r.StartTime,
StartTimestamp: r.StartTimestamp,
RoundTripTime: r.RoundTripTime,
Status: r.Status,
PreStatus: r.PreStatus,
Message: r.Message,
}
j, err := json.Marshal(&ro)
if err != nil {
log.Errorf("error: %v", err)
return ""
}
return string(j)
}
// ToJSONIndent convert the object to indent JSON
func ToJSONIndent(r probe.Result) string {
ro := resultDTO{
Name: r.Title(),
Endpoint: r.Endpoint,
StartTime: r.StartTime,
StartTimestamp: r.StartTimestamp,
RoundTripTime: r.RoundTripTime,
Status: r.Status,
PreStatus: r.PreStatus,
Message: r.Message,
}
j, err := json.MarshalIndent(&ro, "", " ")
if err != nil {
log.Errorf("error: %v", err)
return ""
}
return string(j)
}
// ToHTML convert the object to ToHTML
func ToHTML(r probe.Result) string {
html := HTMLHeader(r.Title()) + `
<table style="font-size: 16px; line-height: 20px;">
<tr>
<td class="head right"><b> Service Name </b></td>
<td class="data">%s</td>
</tr>
<tr>
<td class="head right"><b> Endpoint </b></td>
<td class="data">%s</td>
</tr>
<tr>
<td class="head right"><b> Status </b></td>
<td class="data">%s %s</td>
</tr>
<tr>
<td class="head right"><b> Probe Time </b></td>
<td class="data">%s</td>
</tr>
<tr>
<td class="head right"><b> Round Trip Time </b></td>
<td class="data">%s</td>
</tr>
<tr>
<td class="head right"><b> Message </b></td>
<td class="data">%s</td>
</tr>
</table>
` + HTMLFooter(r.StartTime.Format(r.TimeFormat))
rtt := r.RoundTripTime.Round(time.Millisecond)
return fmt.Sprintf(html, r.Name, r.Endpoint, r.Status.Emoji(), r.Status.String(),
r.StartTime.Format(r.TimeFormat), rtt, r.Message)
}
// ToMarkdown convert the object to ToMarkdown
func ToMarkdown(r probe.Result) string {
return markdown(r, Markdown)
}
// ToMarkdownSocial convert the object to Markdown
func ToMarkdownSocial(r probe.Result) string {
return markdown(r, MarkdownSocial)
}
func markdown(r probe.Result, f Format) string {
tpl := "**%s** %s\n%s - ⏱ %s\n%s\n> %s"
if f == MarkdownSocial {
tpl = "*%s* %s\n%s - ⏱ %s\n%s\n> %s"
}
rtt := r.RoundTripTime.Round(time.Millisecond)
return fmt.Sprintf(tpl,
r.Title(), r.Status.Emoji(), r.Endpoint, rtt, r.Message,
global.FooterString()+" at "+r.StartTime.Format(r.TimeFormat))
}
// ToSlack convert the object to ToSlack notification
// Go to https://app.slack.com/block-kit-builder to build the notification block
func ToSlack(r probe.Result) string {
json := `
{
"channel": "Alert",
"text": "%s",
"blocks": [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "%s"
}
},
{
"type": "context",
"elements": [
{
"type": "image",
"image_url": "` + global.GetEaseProbe().IconURL + `",
"alt_text": "` + global.OrgProg + `"
},
{
"type": "mrkdwn",
"text": "` + global.FooterString() + ` %s"
}
]
}
]
}
`
rtt := r.RoundTripTime.Round(time.Millisecond)
body := fmt.Sprintf("*%s*\\n>%s %s - ⏱ %s\n>%s",
r.Title(), r.Status.Emoji(), r.Endpoint, rtt, JSONEscape(r.Message))
context := SlackTimeFormation(r.StartTime, " probed at ", r.TimeFormat)
summary := fmt.Sprintf("%s %s - %s", r.Title(), r.Status.Emoji(), JSONEscape(r.Message))
return fmt.Sprintf(json, summary, body, context)
}
// ToLark convert the object to Lark notification
// Go to https://open.feishu.cn/document/ukTMukTMukTM/ucTM5YjL3ETO24yNxkjN#4996824a to build the notification block
func ToLark(r probe.Result) string {
json := `
{
"msg_type": "interactive",
"card": {
"config": {
"wide_screen_mode": true
},
"header": {
"template": "%s",
"title": {
"content": "%s",
"tag": "plain_text"
}
},
"elements": [
{
"tag": "div",
"text": {
"content": "%s",
"tag": "lark_md"
}
},
{
"tag": "hr"
},
{
"tag": "note",
"elements": [
{
"tag": "plain_text",
"content": %s
}
]
}
]
}
}`
headerColor := "gray"
switch r.Status {
case probe.StatusUp:
headerColor = "green"
case probe.StatusDown:
headerColor = "red"
case probe.StatusUnknown:
headerColor = "gray"
case probe.StatusInit:
headerColor = "blue"
}
title := fmt.Sprintf("%s %s", r.Title(), r.Status.Emoji())
rtt := r.RoundTripTime.Round(time.Millisecond)
content := fmt.Sprintf("%s - ⏱ %s\\n%s", r.Endpoint, rtt, JSONEscape(r.Message))
footer := global.FooterString() + " probed at " + r.StartTime.Format(r.TimeFormat)
return fmt.Sprintf(json, headerColor, title, content, footer)
}
// ToCSV convert the object to CSV
func ToCSV(r probe.Result) string {
rtt := fmt.Sprintf("%d", r.RoundTripTime.Round(time.Millisecond))
time := r.StartTime.UTC().Format(r.TimeFormat)
timestamp := fmt.Sprintf("%d", r.StartTimestamp)
data := [][]string{
{"Title", "Name", "Endpoint", "Status", "PreStatus", "RoundTripTime", "Time", "Timestamp", "Message"},
{r.Title(), r.Name, r.Endpoint, r.Status.String(), r.PreStatus.String(), rtt, time, timestamp, r.Message},
}
buf := new(bytes.Buffer)
w := csv.NewWriter(buf)
if err := w.WriteAll(data); err != nil {
log.Errorf("ToCSV(): Failed to write to csv buffer: %v", err)
return ""
}
return buf.String()
}
// ToShell convert the result object to shell variables
func ToShell(r probe.Result) string {
env := make(map[string]string)
// set the notify type variable
env["EASEPROBE_TYPE"] = "Status"
// set individual variables
env["EASEPROBE_TITLE"] = r.Title()
env["EASEPROBE_NAME"] = r.Name
env["EASEPROBE_ENDPOINT"] = r.Endpoint
env["EASEPROBE_STATUS"] = r.Status.String()
env["EASEPROBE_TIMESTAMP"] = fmt.Sprintf("%d", r.StartTimestamp)
env["EASEPROBE_RTT"] = fmt.Sprintf("%d", r.RoundTripTime.Round(time.Millisecond))
env["EASEPROBE_MESSAGE"] = r.Message
// set JSON and CVS format
env["EASEPROBE_JSON"] = ToJSON(r)
env["EASEPROBE_CSV"] = ToCSV(r)
buf, err := json.Marshal(env)
if err != nil {
log.Errorf("ToShell(): Failed to marshal env to json: %s", err)
return ""
}
return string(buf)
}