-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.go
192 lines (152 loc) · 3.89 KB
/
types.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package tools
import (
"fmt"
"os"
"strconv"
"strings"
"time"
"go.uber.org/zap/zapcore"
"github.com/paulbellamy/ratecounter"
"github.com/spf13/viper"
)
func getBlockRangeFromFlag() (out BlockRange, err error) {
stringRange := viper.GetString("range")
if stringRange == "" {
return
}
rawRanges := strings.Split(stringRange, ",")
if len(rawRanges) == 0 {
return
}
if len(rawRanges) > 1 {
return out, fmt.Errorf("accepting a single range for now, got %d", len(rawRanges))
}
out, err = decodeRange(rawRanges[0])
if err != nil {
return out, fmt.Errorf("decode range: %w", err)
}
return
}
type BlockRange struct {
Start uint64
Stop uint64
}
func (b BlockRange) Unbounded() bool {
return b.Start == 0 && b.Stop == 0
}
func (b BlockRange) ReprocRange() string {
return fmt.Sprintf("%d:%d", b.Start, b.Stop+1)
}
func (b BlockRange) String() string {
return fmt.Sprintf("%s - %s", blockNum(b.Start), blockNum(b.Stop))
}
func decodeRanges(rawRanges string) (out []BlockRange, err error) {
for _, rawRange := range strings.Split(rawRanges, ",") {
blockRange, err := decodeRange(rawRange)
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
out = append(out, blockRange)
}
return
}
func decodeRange(rawRange string) (out BlockRange, err error) {
parts := strings.SplitN(rawRange, ":", 2)
if len(parts) != 2 {
return out, fmt.Errorf("invalid range %q, not matching format `<start>:<end>`", rawRange)
}
out.Start, err = decodeBlockNum("start", parts[0])
if err != nil {
return
}
out.Stop, err = decodeBlockNum("end", parts[1])
if err != nil {
return
}
return
}
func decodeBlockNum(tag string, part string) (out uint64, err error) {
trimmedValue := strings.Trim(part, " ")
if trimmedValue != "" {
out, err = strconv.ParseUint(trimmedValue, 10, 64)
if err != nil {
return out, fmt.Errorf("`<%s>` value %q is not a valid integer", tag, part)
}
if out < 0 {
return out, fmt.Errorf("`<%s>` value %q should be positive (or 0)", tag, part)
}
}
return
}
type FilteringFilters struct {
Include string
Exclude string
System string
}
func (f *FilteringFilters) Key() string {
return f.System + f.Exclude + f.System
}
type stateFile struct {
StartBlock uint64
Source string
}
type stats struct {
startTime time.Time
timeToFirstBlock time.Duration
blockReceived *counter
bytesReceived *counter
restartCount *counter
}
func newStats() *stats {
return &stats{
startTime: time.Now(),
blockReceived: &counter{0, ratecounter.NewRateCounter(1 * time.Second), "block", "s"},
bytesReceived: &counter{0, ratecounter.NewRateCounter(1 * time.Second), "byte", "s"},
restartCount: &counter{0, ratecounter.NewRateCounter(1 * time.Minute), "restart", "m"},
}
}
func (s *stats) MarshalLogObject(encoder zapcore.ObjectEncoder) error {
encoder.AddString("block", s.blockReceived.String())
encoder.AddString("bytes", s.bytesReceived.String())
return nil
}
func (s *stats) duration() time.Duration {
return time.Now().Sub(s.startTime)
}
func (s *stats) recordBlock(payloadSize int64) {
if s.timeToFirstBlock == 0 {
s.timeToFirstBlock = time.Now().Sub(s.startTime)
}
s.blockReceived.IncBy(1)
s.bytesReceived.IncBy(payloadSize)
}
type counter struct {
total uint64
counter *ratecounter.RateCounter
unit string
timeUnit string
}
func (c *counter) IncBy(value int64) {
if value <= 0 {
return
}
c.counter.Incr(value)
c.total += uint64(value)
}
func (c *counter) Total() uint64 {
return c.total
}
func (c *counter) Rate() int64 {
return c.counter.Rate()
}
func (c *counter) String() string {
return fmt.Sprintf("%d %s/%s (%d total)", c.counter.Rate(), c.unit, c.timeUnit, c.total)
}
func (c *counter) Overall(elapsed time.Duration) string {
rate := float64(c.total)
if elapsed.Minutes() > 1 {
rate = rate / elapsed.Minutes()
}
return fmt.Sprintf("%d %s/%s (%d %s total)", uint64(rate), c.unit, "min", c.total, c.unit)
}