This repository has been archived by the owner on Apr 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathprint.go
59 lines (44 loc) · 1.41 KB
/
print.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
package metrin
import (
"bytes"
"html/template"
"reflect"
"time"
"github.com/aws/aws-sdk-go/service/cloudwatch"
)
// BuildPrintStringInput - includes params and datapoints
type BuildPrintStringInput struct {
Params *cloudwatch.GetMetricStatisticsInput
Datapoints []*cloudwatch.Datapoint
TemplateString string
}
// TemplateInput - input type for each template execution
type TemplateInput struct {
Params *cloudwatch.GetMetricStatisticsInput
Datapoint *cloudwatch.Datapoint
}
// BuildPrintStrings - returns slice of built string
func BuildPrintStrings(input BuildPrintStringInput) []string {
var strings []string
buildTemplate := template.New("")
buildTemplate.Funcs(template.FuncMap{
"unixtime": func(t time.Time) int64 { return t.Unix() },
"deref": func(v *float64) float64 { return *v },
"getvalue": func(datapoint *cloudwatch.Datapoint, params *cloudwatch.GetMetricStatisticsInput, statIndex int) *float64 {
r := reflect.Indirect(reflect.ValueOf(datapoint))
f := r.FieldByName(*params.Statistics[statIndex]).Interface().(*float64)
return f
},
})
template.Must(buildTemplate.Parse(input.TemplateString))
for i := range input.Datapoints {
datapoint := input.Datapoints[i]
buffer := new(bytes.Buffer)
buildTemplate.Execute(buffer, TemplateInput{
Params: input.Params,
Datapoint: datapoint,
})
strings = append(strings, buffer.String())
}
return strings
}