-
Notifications
You must be signed in to change notification settings - Fork 9
/
reporter.go
245 lines (219 loc) · 6.77 KB
/
reporter.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
package nrt
import (
"encoding/json"
"fmt"
"github.com/fatih/color"
"log"
"os"
"path/filepath"
"reflect"
"strings"
)
func handleWrite(f *os.File, s string) {
_, err := f.WriteString(s)
if err != nil {
log.Printf("reporter: Error writing string: `%s`: %s", s, err)
os.Exit(1)
}
}
func (ct *ContentTest) generateParamRepr() (paramRepr string) {
fields := reflect.TypeOf(ct.Parameters)
for field := 0; field < fields.NumField(); field++ {
var fieldValues reflect.Value
fieldName := fields.FieldByIndex([]int{field}).Name
if fieldValues.Kind() == reflect.Ptr {
fieldValues = fieldValues.Elem()
}
switch fieldName {
case "ModuleFilename":
fieldValues = reflect.ValueOf(ct.GetModuleFilename())
case "Prefix":
fieldValues = reflect.ValueOf(ct.GetPrefixName())
default:
fieldValues = reflect.ValueOf(ct.Parameters).Field(field)
if fieldValues.Kind() == reflect.Ptr {
if fieldValues.IsNil() {
continue
} else {
fieldValues = fieldValues.Elem()
}
}
}
paramRepr += fmt.Sprintf("%26v: %v\n", fieldName, fieldValues)
}
return paramRepr
}
//
// ConsoleReporter - reports on test progress to the console for the user
//
type ConsoleReporter struct {
blue func(a ...interface{}) string
green func(a ...interface{}) string
blueNewline string
greenNewline string
ct *ContentTest
}
func (ct *ContentTest) CreateConsoleReporter() (ur ConsoleReporter) {
ur.ct = ct
ur.blue = color.New(color.FgWhite, color.BgBlue).SprintFunc()
ur.blueNewline = ur.blue("\\n") + "\n"
ur.green = color.New(color.FgWhite, color.BgGreen).SprintFunc()
ur.greenNewline = ur.green("\\n") + "\n"
fmt.Printf("%v\n", ur.blue("Parameters:"))
fmt.Printf("%s", ct.generateParamRepr())
fmt.Printf("%v\n", ur.blue("Placeholders:"))
for _, v := range ct.Scenario.PlaceholderMap {
fmt.Printf("%25s: \"%s\"\n", v.Variable, v.Value)
}
return ur
}
func (cr *ConsoleReporter) ReportIteration(iteration int) {
fmt.Printf("%v %v / %v\n", cr.blue("Iteration:"), iteration+1, *cr.ct.Iterations)
fmt.Printf("%v %v\n", cr.blue("Prompt:"),
strings.Replace(cr.ct.Prompt, "\n", cr.blueNewline, -1))
if len(cr.ct.Memory) > 0 {
fmt.Printf("%v %v\n", cr.blue("Memory:"),
strings.Replace(cr.ct.Memory, "\n", cr.blueNewline, -1))
}
if len(cr.ct.AuthorsNote) > 0 {
fmt.Printf("%v %v\n", cr.blue("Author's Note:"),
strings.Replace(cr.ct.AuthorsNote, "\n", cr.blueNewline, -1))
}
}
func (cr *ConsoleReporter) ReportGeneration(resp string) {
fmt.Printf("%v%v\n", cr.green("=>"),
strings.Replace(resp, "\n", cr.greenNewline, -1))
}
func (cr *ConsoleReporter) close() {
fmt.Printf("%v\n", cr.blue("== Test Instance Complete =="))
}
//
// JSONReporter - takes output of test iterations and serializes to a JSON
// file
//
type JSONReporter struct {
fileHandle *os.File
iteration int
}
func CreateJSONReporter(path string) (reportWriter JSONReporter) {
var err error
dir := filepath.Dir(path)
err = os.MkdirAll(dir, 0755)
if err != nil {
log.Printf("reporter: Cannot create path: `%s`: %s", dir, err)
os.Exit(1)
}
reportWriter.fileHandle, err = os.OpenFile(path,
os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0644)
if err != nil {
log.Printf("reporter: Cannot open file for writing: `%s`: %s", path, err)
os.Exit(1)
}
reportWriter.iteration = 0
handleWrite(reportWriter.fileHandle, "[")
return reportWriter
}
func (reportWriter *JSONReporter) SerializeIteration(result *IterationResult) {
if reportWriter.iteration != 0 {
handleWrite(reportWriter.fileHandle, ",\n")
}
reportWriter.iteration += 1
serialized, err := json.MarshalIndent(result, "", " ")
if err != nil {
log.Printf("reporter: Cannot marshal JSON: %v, %v", result, err)
os.Exit(1)
}
handleWrite(reportWriter.fileHandle, string(serialized))
reportWriter.fileHandle.Sync()
}
func (reportWriter *JSONReporter) close() {
handleWrite(reportWriter.fileHandle, "]")
reportWriter.fileHandle.Close()
}
//
// TextReporter - takes output of test iterations and serializes to a JSON
// file
//
type TextReporter struct {
fileHandle *os.File
ct *ContentTest
}
func (ct ContentTest) CreateTextReporter(path string) (textReporter TextReporter) {
var err error
dir := filepath.Dir(path)
err = os.MkdirAll(dir, 0755)
if err != nil {
log.Printf("reporter: Cannot create path: `%s`: %s", dir, err)
os.Exit(1)
}
textReporter.fileHandle, err = os.OpenFile(path,
os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0644)
if err != nil {
log.Printf("reporter: Cannot open file for writing: `%s`: %s", path, err)
os.Exit(1)
}
paramsReport := ct.generateParamRepr()
phReport := ""
for _, v := range ct.Scenario.PlaceholderMap {
phReport += fmt.Sprintf("%25s:\"%s\"\n", v.Variable, v.Value)
}
handleWrite(textReporter.fileHandle, "=== Parameters ====================================\n")
handleWrite(textReporter.fileHandle, paramsReport)
handleWrite(textReporter.fileHandle, "=== Placeholders ==================================\n")
handleWrite(textReporter.fileHandle, phReport)
handleWrite(textReporter.fileHandle, "=== Prompt ========================================\n")
handleWrite(textReporter.fileHandle, ct.Prompt)
if len(ct.Memory) > 0 {
handleWrite(textReporter.fileHandle, "\n=== Memory ========================================\n")
handleWrite(textReporter.fileHandle, ct.Memory)
}
if len(ct.AuthorsNote) > 0 {
handleWrite(textReporter.fileHandle, "\n=== Author's Note =================================\n")
handleWrite(textReporter.fileHandle, ct.AuthorsNote)
}
return textReporter
}
func (tr *TextReporter) ReportIteration(iteration int) {
handleWrite(tr.fileHandle,
fmt.Sprintf("\n\n=== Iteration %-5v ==============================\n", iteration))
tr.fileHandle.Sync()
}
func (tr *TextReporter) ReportGeneration(resp string) {
handleWrite(tr.fileHandle, resp)
tr.fileHandle.Sync()
}
func (tr *TextReporter) close() {
tr.fileHandle.Close()
}
type Reporters struct {
JSON *JSONReporter
Text *TextReporter
Console *ConsoleReporter
}
func (reporters Reporters) close() {
reporters.JSON.close()
reporters.Text.close()
reporters.Console.close()
}
func (reporters Reporters) ReportIteration(iteration int) {
reporters.Console.ReportIteration(iteration)
reporters.Text.ReportIteration(iteration)
}
func (reporters Reporters) ReportGeneration(resp string) {
reporters.Console.ReportGeneration(resp)
reporters.Text.ReportGeneration(resp)
}
func (reporters Reporters) SerializeIteration(result *IterationResult) {
reporters.JSON.SerializeIteration(result)
}
func (ct ContentTest) MakeReporters() Reporters {
consoleReport := ct.CreateConsoleReporter()
outputPath := ct.generateOutputPath()
textReport := ct.CreateTextReporter(outputPath + ".txt")
jsonReport := CreateJSONReporter(outputPath + ".json")
return Reporters{
&jsonReport,
&textReport,
&consoleReport,
}
}