forked from baron-chain/cosmos-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevent_stats.go
55 lines (45 loc) · 1.1 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
package simulation
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(moduleName, op, evResult string) {
_, ok := es[moduleName]
if !ok {
es[moduleName] = make(map[string]map[string]int)
}
_, ok = es[moduleName][op]
if !ok {
es[moduleName][op] = make(map[string]int)
}
es[moduleName][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)
}
}