-
Notifications
You must be signed in to change notification settings - Fork 25
/
ctrs.go
64 lines (53 loc) · 1.73 KB
/
ctrs.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
// Copyright (c) 2019, The Emergent Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package env
import (
"fmt"
"github.com/emer/emergent/v2/estats"
"github.com/emer/emergent/v2/etime"
)
// Counters contains an ordered slice of timescales,
// and a lookup map of counters by timescale
// used to manage counters in the Env.
type Counters struct {
// ordered list of the counter timescales, from outer-most (highest) to inner-most (lowest)
Order []etime.Times
// map of the counters by timescale
Counters map[etime.Times]*Counter
}
// SetTimes initializes Counters for given mode
// and list of times ordered from highest to lowest
func (cs *Counters) SetTimes(mode string, times ...etime.Times) {
cs.Order = times
cs.Counters = make(map[etime.Times]*Counter, len(times))
for _, tm := range times {
cs.Counters[tm] = &Counter{Scale: tm}
}
}
// ByTime returns counter by timescale key -- nil if not found
func (cs *Counters) ByScope(tm etime.Times) *Counter {
return cs.Counters[tm]
}
// ByTime returns counter by timescale key. returns nil, error if not found.
func (cs *Counters) ByTime(tm etime.Times) (*Counter, error) {
ct, ok := cs.Counters[tm]
if ok {
return ct, nil
}
return nil, fmt.Errorf("env.Counters: scope not found: %s", tm.String())
}
// Init does Init on all the counters
func (cs *Counters) Init() {
for _, ct := range cs.Counters {
ct.Init()
}
}
// CountersToStats sets the current counter values to estats Int values
// by their time names only (no eval Mode).
func (cs *Counters) CountersToStats(mode string, stats *estats.Stats) {
for _, ct := range cs.Counters {
tm := ct.Scale.String()
stats.SetInt(mode+":"+tm, ct.Cur)
}
}