forked from aergoio/aergo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stat.go
161 lines (132 loc) · 3.23 KB
/
stat.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package chain
import (
"encoding/json"
"sync"
"time"
"github.com/aergoio/aergo/types"
)
//go:generate stringer -type=statIndex
type statIndex int
const (
// Warning: Each statIndex contant has a String method, which is
// automically generated by 'stringer' with 'go generate' command. For the
// detail, check https://blog.golang.org/generate
// ReorgStat is a constant representing a stat about reorganization.
ReorgStat statIndex = iota
// MaxStat is a constant representing a value less than which all the
// constants corresponding chain stats must be.
MaxStat
)
var (
// To add a new one to chain stats, implements statItem interface and add
// its constructor here. Additionally you need to add a constant
// corresponding to its index like statReorg above.
statItemCtors = map[statIndex]func() statItem{
ReorgStat: newStReorg,
}
)
type stats []*stat
func newStats() stats {
s := make(stats, MaxStat)
for i := statIndex(0); i < MaxStat; i++ {
s[i] = newStat(statItemCtors[i]())
}
return s
}
func (s stats) JSON() string {
r := make(map[string]json.RawMessage)
for i := statIndex(0); i < MaxStat; i++ {
if b, err := json.Marshal(s.clone(i)); err == nil {
r[i.String()] = json.RawMessage(b)
}
}
if m, err := json.Marshal(r); err == nil {
return string(m)
}
return ""
}
func (s stats) get(idx statIndex) *stat {
return []*stat(s)[idx]
}
func (s stats) clone(idx statIndex) interface{} {
i := s.get(idx)
i.RLock()
defer i.RUnlock()
return i.clone()
}
func (s stats) updateEvent(idx statIndex, args ...interface{}) {
i := s.get(idx)
i.Lock()
defer i.Unlock()
i.updateEvent(args...)
}
type stat struct {
sync.RWMutex
statItem
}
func newStat(i statItem) *stat {
return &stat{statItem: i}
}
type statItem interface {
updateEvent(args ...interface{})
clone() interface{}
}
type stReorg struct {
totalElapsed time.Duration
Count int64
AverageElapsed float64 `json:"Average Elapsed Time,omitempty"`
Latest *evReorg `json:",omitempty"`
}
func newStReorg() statItem {
return &stReorg{}
}
type evReorg struct {
OldBest *blockInfo `json:"Old Best,omitempty"`
Fork *blockInfo `json:"Fork At,omitempty"`
NewBest *blockInfo `json:"New Best,omitempty"`
Time time.Time
}
type blockInfo struct {
Hash string
Height types.BlockNo
}
func (sr *stReorg) getCount() int64 {
return sr.Count
}
func (sr *stReorg) getLatestEvent() interface{} {
return sr.Latest
}
func (sr *stReorg) updateEvent(args ...interface{}) {
if len(args) != 4 {
logger.Info().Int("len", len(args)).Msg("invalid # of arguments for the reorg stat update")
return
}
et := args[0].(time.Duration)
bi := make([]*blockInfo, len(args))
for i, a := range args[1:] {
var block *types.Block
ok := false
if block, ok = a.(*types.Block); !ok {
logger.Info().Int("arg idx", i).Msg("invalid type of argument")
return
}
bi[i] = &blockInfo{Hash: block.ID(), Height: block.BlockNo()}
}
sr.Latest = &evReorg{
OldBest: bi[0],
NewBest: bi[1],
Fork: bi[2],
Time: time.Now(),
}
sr.totalElapsed += et
sr.Count++
sr.AverageElapsed = (sr.totalElapsed / time.Duration(sr.Count)).Seconds()
}
func (sr *stReorg) clone() interface{} {
c := *sr
if sr.Latest != nil {
l := *sr.Latest
c.Latest = &l
}
return &c
}