-
Notifications
You must be signed in to change notification settings - Fork 0
/
report.go
100 lines (86 loc) · 2.6 KB
/
report.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
package main
import (
"encoding/csv"
"fmt"
"os"
"time"
)
type alertReport struct {
Alerts []alert `json:"alerts" csv:"alerts"`
AlertTotal int `json:"alert_total" csv:"alert_total"`
OffHourTotal int `json:"off_hour_total" csv:"off_hour_total"`
SleepHourTotal int `json:"sleep_hour_total" csv:"sleep_hour_total"`
}
type responder struct {
Name string `json:"name" csv:"name"`
EscalationPolicy string `json:"escalation_policy" csv:"escalation_policy"`
OffHour int `json:"off_hour" csv:"off_hour"`
SleepHour int `json:"sleep_hour" csv:"sleep_hour"`
}
type alert struct {
ID string `json:"id" csv:"id"`
Desc string `json:"desc" csv:"desc"`
Responders map[string]responder `json:"responders" csv:"responders"`
DateTime time.Time `json:"date_time" csv:"date_time"`
URL string `json:"alert_url" csv:"alert_url"`
}
func min(x, y int) int {
if x < y {
return x
}
return y
}
// func (r *alertReport) emitTableCondensed() {
// tw := tablewriter.NewWriter(os.Stdout)
// tw.SetHeader([]string{"Alert", "Description", "Team", "Responder", "Off Hour Alert", "Sleep Hour Alert"})
// tw.SetFooter([]string{"", "", "", fmt.Sprintf("Sleep Hours: %v", r.SleepHourTotal), fmt.Sprintf("Off Hours: %v", r.OffHourTotal), fmt.Sprintf("Alerts: %v", r.AlertTotal)})
// for _, a := range r.Alerts {
// for _, v := range a.Responders {
// if v.SleepHour > 0 || v.OffHour > 0 {
// tw.Append([]string{a.ID, a.Desc, fmt.Sprint(v.Teams), v.Name, fmt.Sprint(v.OffHour), fmt.Sprint(v.SleepHour)})
// }
// }
// }
// fmt.Printf("All Alerts from: %v to: %v\n", startMonth, endMonth)
// tw.Render()
// }
// func (r *alertReport) emitJson() error {
// formatter := json.Marshal
// data, err := formatter(r.Alerts)
// if err != nil {
// return err
// }
// fmt.Println(string(data))
// return nil
// }
func (r *alertReport) emitCsv() error {
file, err := os.Create("report.csv")
if err != nil {
return err
}
defer file.Close()
writer := csv.NewWriter(file)
defer writer.Flush()
header := []string{"time in zone", "Alert ID", "Description", "Responder Name", "Teams", "Off Hour", "Sleep Hour", "URL"}
if err := writer.Write(header); err != nil {
return err
}
for _, a := range r.Alerts {
for _, v := range a.Responders {
record := []string{
a.DateTime.String(),
a.ID,
a.Desc,
v.Name,
v.EscalationPolicy,
fmt.Sprint(v.OffHour),
fmt.Sprint(v.SleepHour),
a.URL,
}
if err := writer.Write(record); err != nil {
return err
}
}
}
return nil
}