-
Notifications
You must be signed in to change notification settings - Fork 231
/
sla.go
494 lines (436 loc) · 12.5 KB
/
sla.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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
/*
* 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://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 is the package for SLA report
package report
import (
"bytes"
"encoding/csv"
"encoding/json"
"fmt"
"strings"
"time"
"github.com/megaease/easeprobe/global"
"github.com/megaease/easeprobe/probe"
log "github.com/sirupsen/logrus"
)
// Availability is the Availability JSON structure
type Availability struct {
UpTime time.Duration `json:"up"`
DownTime time.Duration `json:"down"`
SLA float64 `json:"sla"`
}
// Summary is the Summary JSON structure
type Summary struct {
Total int64 `json:"total"`
Up int64 `json:"up"`
Down int64 `json:"down"`
}
// LatestProbe is the LatestProbe JSON structure
type LatestProbe struct {
Time time.Time `json:"time"`
Status probe.Status `json:"status"`
Message string `json:"message"`
}
// SLA is the SLA JSON structure
type SLA struct {
Name string `json:"name"`
Endpoint string `json:"endpoint"`
Availability Availability `json:"sla"`
ProbeTimes Summary `json:"probe_summary"`
LatestProbe LatestProbe `json:"latest_probe"`
}
// SLAObject covert the result to SLA struct
func SLAObject(r *probe.Result) SLA {
return SLA{
Name: r.Name,
Endpoint: r.Endpoint,
Availability: Availability{
UpTime: r.Stat.UpTime,
DownTime: r.Stat.DownTime,
SLA: r.SLAPercent(),
},
ProbeTimes: Summary{
Total: r.Stat.Total,
Up: r.Stat.Status[probe.StatusUp],
Down: r.Stat.Status[probe.StatusDown] + r.Stat.Status[probe.StatusUnknown],
},
LatestProbe: LatestProbe{
Time: r.StartTime,
Status: r.Status,
Message: r.Message,
},
}
}
// SLAJSONSection return the JSON format string to stat
func SLAJSONSection(r *probe.Result) string {
sla := SLAObject(r)
j, err := json.Marshal(&sla)
if err != nil {
log.Errorf("error: %v", err)
return ""
}
return string(j)
}
// SLAJSON return a full stat report
func SLAJSON(probers []probe.Prober) string {
var sla []SLA
for _, p := range probers {
r := probe.GetResultData(p.Name())
sla = append(sla, SLAObject(r))
}
j, err := json.Marshal(&sla)
if err != nil {
log.Errorf("error: %v", err)
return ""
}
return string(j)
}
// SLATextSection return the Text format string to stat
func SLATextSection(r *probe.Result) string {
text := "Name: %s - %s, \n\tAvailability: Up - %s, Down - %s, SLA: %.2f%%\n\tProbe-Times: Total: %d ( %s ), \n\tLatest-Probe:%s - %s, Message:%s"
return fmt.Sprintf(text, r.Name, r.Endpoint,
DurationStr(r.Stat.UpTime), DurationStr(r.Stat.DownTime), r.SLAPercent(),
r.Stat.Total, SLAStatusText(r.Stat, Text),
FormatTime(r.StartTime),
r.Status.Emoji()+" "+r.Status.String(), JSONEscape(r.Message))
}
// SLAText return a full stat report
func SLAText(probers []probe.Prober) string {
text := "[Overall SLA Report]\n\n"
for _, p := range probers {
r := probe.GetResultData(p.Name())
text += SLATextSection(r) + "\n"
}
return text
}
// SLALogSection return the Log format string to stat
func SLALogSection(r *probe.Result) string {
text := `name="%s"; endpoint="%s"; up="%s"; down="%s"; sla="%.2f%%"; total="%d(%s)"; latest_time="%s"; latest_status="%s"; message="%s"`
return fmt.Sprintf(text, r.Name, r.Endpoint,
DurationStr(r.Stat.UpTime), DurationStr(r.Stat.DownTime), r.SLAPercent(),
r.Stat.Total, SLAStatusText(r.Stat, Log),
FormatTime(r.StartTime),
r.Status.String(), r.Message)
}
// SLALog return a full stat report with Log format
func SLALog(probers []probe.Prober) string {
var text string
n := len(probers)
for i, p := range probers {
r := probe.GetResultData(p.Name())
text += fmt.Sprintf("SLA-Report-%d-%d %s\n", i+1, n, SLALogSection(r))
}
return text
}
// SLAMarkdownSection return the Markdown format string to stat
func SLAMarkdownSection(r *probe.Result, f Format) string {
text := "\n**%s** - %s\n"
if f == MarkdownSocial {
text = "\n*%s* - %s\n"
}
text += "- Availability: Up - `%s`, Down - `%s`, SLA: `%.2f%%` \n" +
"- Probe-Times: Total: `%d` ( %s ) \n" +
"- Latest-Probe: %s - %s \n" +
" ```%s```\n"
return fmt.Sprintf(text, r.Name, r.Endpoint,
DurationStr(r.Stat.UpTime), DurationStr(r.Stat.DownTime), r.SLAPercent(),
r.Stat.Total, SLAStatusText(r.Stat, MarkdownSocial),
FormatTime(r.StartTime),
r.Status.Emoji()+" "+r.Status.String(), r.Message)
}
// SLAMarkdown return a full stat report with Markdown format
func SLAMarkdown(probers []probe.Prober) string {
return slaMarkdown(probers, Markdown)
}
// SLAMarkdownSocial return a full stat report with social markdown
func SLAMarkdownSocial(probers []probe.Prober) string {
return slaMarkdown(probers, MarkdownSocial)
}
func slaMarkdown(probers []probe.Prober, f Format) string {
md := "**Overall SLA Report**\n"
if f == MarkdownSocial {
md = "*Overall SLA Report*\n"
}
for _, p := range probers {
r := probe.GetResultData(p.Name())
md += SLAMarkdownSection(r, f)
}
md += "\n> " + global.FooterString() + " at " + FormatTime(time.Now())
return md
}
// SLAHTMLSection return the HTML format string to stat
func SLAHTMLSection(r *probe.Result) string {
html := `
<tr>
<td class="head" colspan="3"><b>%s</b> - %s<td>
</tr>
<tr>
<td class="data"><b>Availability</b><br><b>Uptime: </b>%s, <b>Downtime: </b>%s </td>
<td class="data"><b>SLA<b><br>%.2f%%</td>
<td class="data"><b>Probe-Times</b><br><b>Total</b>: %d ( %s )</td>
</tr>
<tr>
<td class="data" colspan="3"><b>Latest Probe</b>: %s - %s<br>%s<td>
</tr>
`
return fmt.Sprintf(html, r.Name, r.Endpoint,
DurationStr(r.Stat.UpTime), DurationStr(r.Stat.DownTime),
r.SLAPercent(),
r.Stat.Total, SLAStatusText(r.Stat, HTML),
FormatTime(r.StartTime),
r.Status.Emoji()+" "+r.Status.String(), JSONEscape(r.Message))
}
// SLAHTML return a full stat report
func SLAHTML(probers []probe.Prober) string {
return SLAHTMLFilter(probers, nil)
}
// SLAHTMLFilter return a stat report with filter
func SLAHTMLFilter(probers []probe.Prober, filter *SLAFilter) string {
html := HTMLHeader("Overall SLA Report")
if filter == nil {
filter = NewEmptyFilter()
}
probers = filter.Filter(probers)
table := `<table style="font-size: 16px; line-height: 20px;">`
for _, p := range probers {
r := probe.GetResultData(p.Name())
table += SLAHTMLSection(r)
}
table += `</table>`
html = html + filter.HTML() + table
html += HTMLFooter(FormatTime(time.Now()))
return html
}
// SLASlackSection return the slack json format string to stat
func SLASlackSection(r *probe.Result) string {
json := `
{
"type": "mrkdwn",
"text": "*%s* - %s` +
`\n>*Availability*\n>\t` + " *Up*: `%s` *Down* `%s` - *SLA*: `%.2f %%`" +
`\n>*Probe Times*\n>\t*Total* : %d ( %s )` +
`\n>*Latest Probe*\n>\t%s | %s` +
`\n>\t%s"` + `
}`
t := SlackTimeFormation(r.StartTime, "", global.GetTimeFormat())
message := JSONEscape(r.Message)
if r.Status != probe.StatusUp {
message = "`" + message + "`"
}
return fmt.Sprintf(json, r.Name, JSONEscape(r.Endpoint),
DurationStr(r.Stat.UpTime), DurationStr(r.Stat.DownTime), r.SLAPercent(),
r.Stat.Total, SLAStatusText(r.Stat, MarkdownSocial),
t, r.Status.Emoji()+" "+r.Status.String(), message)
}
// SLASlack generate all probes stat message to slack block string
func SLASlack(probers []probe.Prober) string {
summary := SLASummary(probers)
json := `{
"text": "Overall SLA Report - ` + summary + ` ",
"blocks": [
{
"type": "header",
"text": {
"type": "plain_text",
"text": "Overall SLA Report",
"emoji": true
}
}`
sectionHead := `
{
"type": "section",
"fields": [`
sectionFoot := `
]
}`
total := len(probers)
pageCnt := 10
pages := total / pageCnt
if total%pageCnt > 0 {
pages++
}
for p := 0; p < pages; p++ {
start := p * pageCnt
end := (p + 1) * pageCnt
if len(probers) < end {
end = len(probers)
}
json += "," + sectionHead
for i := start; i < end-1; i++ {
r := probe.GetResultData(probers[i].Name())
json += SLASlackSection(r) + ","
}
r := probe.GetResultData(probers[end-1].Name())
json += SLASlackSection(r)
json += sectionFoot
}
context := `,
{
"type": "context",
"elements": [
{
"type": "image",
"image_url": "` + global.GetEaseProbe().IconURL + `",
"alt_text": "` + global.OrgProg + `"
},
{
"type": "mrkdwn",
"text": "` + global.FooterString() + ` %s"
}
]
}`
time := SlackTimeFormation(time.Now(), " reported at ", global.GetTimeFormat())
json += fmt.Sprintf(context, time)
json += `]}`
return json
}
// SLAStatusText return the string of status statices
func SLAStatusText(s probe.Stat, t Format) string {
status := ""
format := "%s : %d \t"
switch t {
case MarkdownSocial:
format = "%s : `%d` \t"
case Markdown:
format = "**%s** : `%d` \t"
case HTML:
format = "<b>%s</b> : %d \t"
case Log:
format = "%s:%d "
}
for k, v := range s.Status {
status += fmt.Sprintf(format, k.String(), v)
}
return strings.TrimSpace(status)
}
// SLALarkSection return the Text format string to stat
func SLALarkSection(r *probe.Result) string {
text := `
{
"tag": "hr"
}, {
"tag": "div",
"text": {
"content": "**Name:** %s - %s\n**Availability:** Up - %s, Down - %s\n**SLA:** %.2f%%\n**Probe-Times:** Total: %d ( %s )\n**Latest-Probe:** %s - %s\n**Message:**%s",
"tag": "lark_md"
}
},`
return fmt.Sprintf(text, r.Name, r.Endpoint,
DurationStr(r.Stat.UpTime), DurationStr(r.Stat.DownTime), r.SLAPercent(),
r.Stat.Total, SLAStatusText(r.Stat, Lark),
FormatTime(r.StartTime),
r.Status.Emoji()+" "+r.Status.String(), JSONEscape(r.Message))
}
// SLALark return a full stat report
func SLALark(probers []probe.Prober) string {
json := `
{
"msg_type": "interactive",
"card": {
"header": {
"template": "blue",
"title": {
"content": "%s",
"tag": "plain_text"
}
},
"config": {
"wide_screen_mode": true
},
"elements": [%s
{
"tag": "hr"
}, {
"tag": "note",
"elements": [
{
"tag": "plain_text",
"content": "` + global.FooterString() + `"
}
]
}
]
}
}`
title := "Overall SLA Report"
sections := []string{}
for _, p := range probers {
r := probe.GetResultData(p.Name())
sections = append(sections, SLALarkSection(r))
}
elements := strings.Join(sections, "")
s := fmt.Sprintf(json, title, elements)
fmt.Printf("SLA: %s\n", s)
return s
}
// SLASummary return a summary stat report
func SLASummary(probers []probe.Prober) string {
sla := 0.0
for _, p := range probers {
r := probe.GetResultData(p.Name())
sla += r.SLAPercent()
}
sla /= float64(len(probers))
summary := fmt.Sprintf("Total %d Services, Average %.2f%% SLA", len(probers), sla)
summary += "\n" + global.FooterString()
return summary
}
// SLACSVSection set the CSV format for SLA
func SLACSVSection(r *probe.Result) []string {
return []string{
// Name, Endpoint,
r.Name, r.Endpoint,
// UpTime, DownTime, SLA
DurationStr(r.Stat.UpTime), DurationStr(r.Stat.DownTime), fmt.Sprintf("%.2f%%", r.SLAPercent()),
// ProbeSummary - Total( Up, Down)
fmt.Sprintf("%d(%s)", r.Stat.Total, SLAStatusText(r.Stat, Text)),
// LatestProbe, LatestStatus
FormatTime(r.StartTime), r.Status.String(),
// Message
r.Message,
}
}
// SLACSV return a full stat report with CSV format
func SLACSV(probers []probe.Prober) string {
data := [][]string{
{"Name", "Endpoint", "UpTime", "DownTime", "SLA", "ProbeSummary", "LatestProbe", "LatestStatus", "Message"},
}
for _, p := range probers {
r := probe.GetResultData(p.Name())
data = append(data, SLACSVSection(r))
}
buf := new(bytes.Buffer)
w := csv.NewWriter(buf)
if err := w.WriteAll(data); err != nil {
log.Errorf("SLACSV(): Failed to write to csv buffer: %v", err)
return ""
}
return buf.String()
}
// SLAShell set the environment for SLA
func SLAShell(probers []probe.Prober) string {
env := make(map[string]string)
env["EASEPROBE_TYPE"] = "SLA"
env["EASEPROBE_JSON"] = SLAJSON(probers)
env["EASEPROBE_CSV"] = SLACSV(probers)
buf, err := json.Marshal(env)
if err != nil {
log.Errorf("SLAShell(): Failed to marshal env to json: %s", err)
return ""
}
return string(buf)
}