-
Notifications
You must be signed in to change notification settings - Fork 1
/
report.go
75 lines (60 loc) · 1.83 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
// Package report handles the rendering of the
// HTML report that is then sent out via Slack.
package report
import (
"bytes"
"embed"
"log"
"sort"
"text/template"
"time"
"github.com/giantswarm/microerror"
"github.com/giantswarm/resource-police/pkg/cortex"
)
//go:embed template.gotmpl
var f embed.FS
type TemplateData struct {
Clusters3Days []cortex.Cluster
Clusters1Day []cortex.Cluster
Clusters3Hours []cortex.Cluster
ClustersRest []cortex.Cluster
Errors []error
}
func Render(clusters []cortex.Cluster, errors []error) (string, error) {
log.Println("Rendering report")
reportTemplate, _ := f.ReadFile("template.gotmpl")
t := template.Must(template.New("report-template").Parse(string(reportTemplate)))
myData := TemplateData{
Errors: errors,
}
// Sort by provider and installation name
sort.Slice(clusters, func(i, j int) bool {
return clusters[i].Installation < clusters[j].Installation
})
// Afterwards sort by provider
sort.Slice(clusters, func(i, j int) bool {
return clusters[i].Provider < clusters[j].Provider
})
// Group clusters by age
// (more than 3 days, more than 1 day, more than 3 hours, rest).
now := time.Now().UTC()
for i := 0; i < len(clusters); i++ {
switch age := now.Sub(clusters[i].FirstTimestamp); {
case age > 3*24*time.Hour:
myData.Clusters3Days = append(myData.Clusters3Days, clusters[i])
case age > 24*time.Hour:
myData.Clusters1Day = append(myData.Clusters1Day, clusters[i])
case age > 3*time.Hour:
myData.Clusters3Hours = append(myData.Clusters3Hours, clusters[i])
default:
myData.ClustersRest = append(myData.ClustersRest, clusters[i])
}
}
var renderedReport bytes.Buffer
err := t.Execute(&renderedReport, myData)
if err != nil {
return "", microerror.Mask(err)
}
log.Println("Report has been rendered")
return renderedReport.String(), nil
}