2018-11-07 21:54:12 -08:00
|
|
|
package simulation
|
|
|
|
|
|
|
|
import (
|
2019-07-26 13:09:15 -07:00
|
|
|
"encoding/json"
|
2018-11-07 21:54:12 -08:00
|
|
|
"fmt"
|
2019-04-12 15:52:16 -07:00
|
|
|
"io"
|
2019-07-26 13:09:15 -07:00
|
|
|
"io/ioutil"
|
2018-11-07 21:54:12 -08:00
|
|
|
)
|
|
|
|
|
2019-07-26 13:09:15 -07:00
|
|
|
// 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
|
2018-11-07 21:54:12 -08:00
|
|
|
|
2019-07-26 13:09:15 -07:00
|
|
|
// NewEventStats creates a new empty EventStats object
|
|
|
|
func NewEventStats() EventStats {
|
|
|
|
return make(EventStats)
|
2018-11-07 21:54:12 -08:00
|
|
|
}
|
|
|
|
|
2019-07-26 13:09:15 -07:00
|
|
|
// 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]++
|
2018-11-07 21:54:12 -08:00
|
|
|
}
|
|
|
|
|
2019-07-26 13:09:15 -07:00
|
|
|
// Print the event stats in JSON format.
|
|
|
|
func (es EventStats) Print(w io.Writer) {
|
|
|
|
obj, err := json.MarshalIndent(es, "", " ")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
2018-11-07 21:54:12 -08:00
|
|
|
}
|
2019-04-12 15:52:16 -07:00
|
|
|
|
2019-08-19 09:06:27 -07:00
|
|
|
fmt.Fprintln(w, string(obj))
|
2019-07-26 13:09:15 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|
2019-04-12 15:52:16 -07:00
|
|
|
|
2019-07-26 13:09:15 -07:00
|
|
|
err = ioutil.WriteFile(path, bz, 0644)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
2018-11-07 21:54:12 -08:00
|
|
|
}
|
|
|
|
}
|