This repository has been archived by the owner on Sep 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
report.go
131 lines (112 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
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
package main
import (
"encoding/csv"
"os"
"reflect"
"strconv"
"github.com/olekukonko/tablewriter"
)
type Reporter interface {
Append(ps *PageStats)
Render() error
}
func structToMap(ps *PageStats) map[string]string {
values := make(map[string]string)
s := reflect.ValueOf(ps).Elem()
typeOfT := s.Type()
for i := 0; i < s.NumField(); i++ {
f := s.Field(i)
var v string
switch f.Interface().(type) {
case int, int8, int16, int32, int64:
v = strconv.FormatInt(f.Int(), 10)
case uint, uint8, uint16, uint32, uint64:
v = strconv.FormatUint(f.Uint(), 10)
case float32:
v = strconv.FormatFloat(f.Float(), 'f', 4, 32)
case float64:
v = strconv.FormatFloat(f.Float(), 'f', 4, 64)
case []byte:
v = string(f.Bytes())
case string:
v = f.String()
}
values[typeOfT.Field(i).Name] = v
}
return values
}
func mapValues(m map[string]string, fields []string) []string {
row := make([]string, 0, len(fields))
for _, k := range fields {
if v, ok := m[k]; ok {
row = append(row, v)
}
}
return row
}
type TableReporter struct {
table *tablewriter.Table
fields []string
}
func StatHeaders() []string {
statType := reflect.TypeOf(PageStats{})
header := make([]string, 0, statType.NumField())
for i := 0; i < statType.NumField(); i++ {
field := statType.Field(i)
header = append(header, field.Name)
}
return header
}
func NewTableReporter() *TableReporter {
tr := &TableReporter{
table: tablewriter.NewWriter(os.Stdout),
fields: StatHeaders(),
}
tr.table.SetHeader(tr.fields)
return tr
}
func NewTSVReporter() *TableReporter {
tr := NewTableReporter()
tr.table.SetAutoWrapText(false)
tr.table.SetAutoFormatHeaders(true)
tr.table.SetHeaderAlignment(tablewriter.ALIGN_LEFT)
tr.table.SetAlignment(tablewriter.ALIGN_LEFT)
tr.table.SetCenterSeparator("")
tr.table.SetColumnSeparator("")
tr.table.SetRowSeparator("")
tr.table.SetHeaderLine(false)
tr.table.SetBorder(false)
tr.table.SetTablePadding("\t") // pad with tabs
tr.table.SetNoWhiteSpace(true)
return tr
}
func (tr *TableReporter) Append(ps *PageStats) {
m := structToMap(ps)
row := mapValues(m, tr.fields)
tr.table.Append(row)
}
func (tr *TableReporter) Render() error {
tr.table.Render()
return nil
}
type CSVReporter struct {
w *csv.Writer
fields []string
}
func NewCSVReporter() *CSVReporter {
cr := &CSVReporter{
w: csv.NewWriter(os.Stdout),
fields: StatHeaders(),
}
cr.w.Write(cr.fields)
return cr
}
func (cr *CSVReporter) Append(ps *PageStats) {
m := structToMap(ps)
row := mapValues(m, cr.fields)
cr.w.Write(row)
}
func (cr *CSVReporter) Render() error {
cr.w.Flush()
return nil
}