-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecorder.go
105 lines (86 loc) · 1.78 KB
/
recorder.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
package qube
import (
"sync"
"time"
)
type Recorder struct {
mu sync.Mutex
*Options
ID string
dataPoints []DataPoint
errorQueryCount int
StartedAt time.Time
FinishedAt time.Time
ch chan []DataPointWithErr
closed chan struct{}
}
type DataPoint struct {
Time int64
Duration time.Duration
}
type DataPointWithErr struct {
DataPoint
IsError bool
}
func NewRecorder(id string, options *Options) *Recorder {
rec := &Recorder{
Options: options,
ID: id,
dataPoints: []DataPoint{},
ch: make(chan []DataPointWithErr, options.Nagents*3),
}
return rec
}
func (rec *Recorder) Start() {
rec.closed = make(chan struct{})
push := func(dpes []DataPointWithErr) {
rec.mu.Lock()
defer rec.mu.Unlock()
for _, v := range dpes {
if !v.IsError {
rec.dataPoints = append(rec.dataPoints, v.DataPoint)
} else {
rec.errorQueryCount++
}
}
}
go func() {
for dpes := range rec.ch {
push(dpes)
}
close(rec.closed)
}()
rec.StartedAt = time.Now()
}
func (rec *Recorder) Close() {
rec.FinishedAt = time.Now()
close(rec.ch)
<-rec.closed
}
func (rec *Recorder) Add(dpes []DataPointWithErr) {
rec.ch <- dpes
}
func (rec *Recorder) Report() *Report {
return NewReport(rec)
}
func (rec *Recorder) CountAll() int {
// Lock to avoid race conditions
rec.mu.Lock()
defer rec.mu.Unlock()
return len(rec.dataPoints) + rec.errorQueryCount
}
func (rec *Recorder) CountSuccess() int {
// Lock to avoid race conditions
rec.mu.Lock()
defer rec.mu.Unlock()
return len(rec.dataPoints)
}
func (rec *Recorder) DataPoints() []DataPoint {
return rec.dataPoints
}
func (rec *Recorder) ErrorQueryCount() int {
// Lock to avoid race conditions
rec.mu.Lock()
defer rec.mu.Unlock()
return rec.errorQueryCount
}