-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathrecord_stats.go
77 lines (65 loc) · 1.38 KB
/
record_stats.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
package callback
import (
"encoding/csv"
"github.com/codingbeard/cbutil"
"os"
"path/filepath"
"strconv"
"strings"
"time"
)
type RecordStats struct {
RecordDir string
RecordFileName string
OnEvent Event
OnMode Mode
recordFile *os.File
recordWriter *csv.Writer
}
func (r *RecordStats) Init() error {
file, e := os.Create(filepath.Join(r.RecordDir, r.RecordFileName))
if e != nil {
return e
}
writer := csv.NewWriter(file)
r.recordFile = file
r.recordWriter = writer
return nil
}
func (r *RecordStats) Call(event Event, mode Mode, epoch int, batch int, logs []Log) ([]Action, error) {
if event == r.OnEvent && mode == r.OnMode {
if epoch == 1 {
columns := []string{
"datetime",
"event",
"mode",
"epoch",
"batch",
}
for _, log := range logs {
columns = append(columns, strings.ToLower(log.Name))
}
e := r.recordWriter.Write(columns)
if e != nil {
return []Action{ActionNop}, e
}
r.recordWriter.Flush()
}
values := []string{
time.Now().Format(cbutil.DateTimeFormat),
string(event),
string(mode),
strconv.Itoa(epoch),
strconv.Itoa(batch),
}
for _, log := range logs {
values = append(values, strconv.FormatFloat(float64(log.Value), 'f', -1, 32))
}
e := r.recordWriter.Write(values)
if e != nil {
return nil, e
}
r.recordWriter.Flush()
}
return []Action{ActionNop}, nil
}