This repository has been archived by the owner on May 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
templates.go
115 lines (101 loc) · 3.57 KB
/
templates.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
/*
* Copyright (C) 2015 zulily, Inc.
*
* 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 reckon
import (
"fmt"
"io"
"text/template"
)
func summarize(m map[int]int64) int64 {
// trim off entries that constitute < 1% of the total
return trimAndSum(m, 0.01)
}
func fmtFloat(n float64) string {
return fmt.Sprintf("%.2f", n)
}
func percentage(n, total int64) string {
return fmt.Sprintf("%.2f", 100.0*float64(n)/float64(total))
}
// chartJS returns the static js what we need on the HTML templates in order to
// render charts. The js itself has been turned into Go src using go-bindata.
// This func panics if there is any error accessing the embedded asset data.
func chartJS() string {
data, err := Asset("Chart.min.js")
if err != nil {
panic(err)
}
return string(data)
}
type chartData struct {
DOMElement string
Data map[int]int64
}
func barChart(domElement string, freq map[int]int64) chartData {
return chartData{
DOMElement: domElement,
Data: freq,
}
}
// RenderHTML renders an HTML report for a Results instance to the supplied
// io.Writer
func RenderHTML(s *Results, out io.Writer) error {
s.StringKeys = trim(s.StringKeys, MaxExampleKeys)
s.StringValues = trim(s.StringValues, MaxExampleValues)
s.SetKeys = trim(s.SetKeys, MaxExampleKeys)
s.SetElements = trim(s.SetElements, MaxExampleElements)
s.SortedSetKeys = trim(s.SortedSetKeys, MaxExampleKeys)
s.SortedSetElements = trim(s.SortedSetElements, MaxExampleElements)
s.HashKeys = trim(s.HashKeys, MaxExampleKeys)
s.HashElements = trim(s.HashElements, MaxExampleElements)
s.HashValues = trim(s.HashValues, MaxExampleValues)
s.ListKeys = trim(s.ListKeys, MaxExampleKeys)
s.ListElements = trim(s.ListElements, MaxExampleElements)
fm := template.FuncMap{
"summarize": summarize,
"percentage": percentage,
"power": ComputePowerOfTwoFreq,
"stats": ComputeStatistics,
"fmtFloat": fmtFloat,
"barChart": barChart,
"chartJS": chartJS,
}
t := template.Must(template.New("htmloutput").Funcs(fm).Parse(htmlTmpl))
return t.ExecuteTemplate(out, "base", s)
}
// RenderText renders a plaintext report for a Results instance to the supplied
// io.Writer
func RenderText(s *Results, out io.Writer) error {
s.StringKeys = trim(s.StringKeys, MaxExampleKeys)
s.StringValues = trim(s.StringValues, MaxExampleValues)
s.SetKeys = trim(s.SetKeys, MaxExampleKeys)
s.SetElements = trim(s.SetElements, MaxExampleElements)
s.SortedSetKeys = trim(s.SortedSetKeys, MaxExampleKeys)
s.SortedSetElements = trim(s.SortedSetElements, MaxExampleElements)
s.HashKeys = trim(s.HashKeys, MaxExampleKeys)
s.HashElements = trim(s.HashElements, MaxExampleElements)
s.HashValues = trim(s.HashValues, MaxExampleValues)
s.ListKeys = trim(s.ListKeys, MaxExampleKeys)
s.ListElements = trim(s.ListElements, MaxExampleElements)
fm := template.FuncMap{
"summarize": summarize,
"percentage": percentage,
"power": ComputePowerOfTwoFreq,
"stats": ComputeStatistics,
"fmtFloat": fmtFloat,
}
t := template.Must(template.New("output").Funcs(fm).Parse(statsTempl))
return t.ExecuteTemplate(out, "base", s)
}