-
Notifications
You must be signed in to change notification settings - Fork 575
/
event_stats.go
65 lines (54 loc) · 1.37 KB
/
event_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
package stats
import (
"encoding/json"
"fmt"
"io"
"os"
)
// EventStats defines an object that keeps a tally of each event that has occurred
// during a simulation.
type EventStats map[string]map[string]map[string]int
// NewEventStats creates a new empty EventStats object
func NewEventStats() EventStats {
return make(EventStats)
}
// Tally increases the count of a simulation event.
func (es EventStats) Tally(route, op, evResult string) {
_, ok := es[route]
if !ok {
es[route] = make(map[string]map[string]int)
}
_, ok = es[route][op]
if !ok {
es[route][op] = make(map[string]int)
}
es[route][op][evResult]++
}
// Print the event stats in JSON format.
func (es EventStats) Print(w io.Writer) {
obj, err := json.MarshalIndent(es, "", " ")
if err != nil {
panic(err)
}
fmt.Fprintln(w, string(obj))
}
// ExportJSON saves the event stats as a JSON file on a given path
func (es EventStats) ExportJSON(path string) {
bz, err := json.MarshalIndent(es, "", " ")
if err != nil {
panic(err)
}
err = os.WriteFile(path, bz, 0o600)
if err != nil {
panic(err)
}
}
func (es EventStats) ExportEvents(ExportStatsPath string, w io.Writer) {
if ExportStatsPath != "" {
fmt.Printf("Exporting simulation statistics to %s...", ExportStatsPath)
es.ExportJSON(ExportStatsPath)
} else {
// TODO: Don't print, also export to a file of timestamp'd path
es.Print(w)
}
}