-
Notifications
You must be signed in to change notification settings - Fork 147
/
report.go
64 lines (52 loc) · 1.12 KB
/
report.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
package influxunifi
import (
"sync"
"time"
"github.com/davidnewhall/unifi-poller/pkg/metrics"
influx "github.com/influxdata/influxdb1-client/v2"
)
// Report is returned to the calling procedure after everything is processed.
type Report struct {
Metrics *metrics.Metrics
Errors []error
Total int
Fields int
Start time.Time
Elapsed time.Duration
ch chan *metric
wg sync.WaitGroup
bp influx.BatchPoints
}
// report is an internal interface that can be mocked and overrridden for tests.
type report interface {
add()
done()
send(m *metric)
error(err error)
batch(m *metric, pt *influx.Point)
metrics() *metrics.Metrics
}
func (r *Report) metrics() *metrics.Metrics {
return r.Metrics
}
// satisfy gomnd
const one = 1
func (r *Report) add() {
r.wg.Add(one)
}
func (r *Report) done() {
r.wg.Add(-one)
}
func (r *Report) send(m *metric) {
r.wg.Add(one)
r.ch <- m
}
/* The following methods are not thread safe. */
func (r *Report) error(err error) {
r.Errors = append(r.Errors, err)
}
func (r *Report) batch(m *metric, p *influx.Point) {
r.Total++
r.Fields += len(m.Fields)
r.bp.AddPoint(p)
}