-
Notifications
You must be signed in to change notification settings - Fork 2
/
stats.go
62 lines (52 loc) · 1.5 KB
/
stats.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
package main
import (
"sync"
"time"
timeseries "github.com/codesuki/go-time-series"
)
type DatabendIngesterStatsRecorder struct {
ingestedBytes *timeseries.TimeSeries
ingestedRows *timeseries.TimeSeries
mu sync.Mutex
}
type DatabendIngesterStatsData struct {
BytesPerSecond float64
RowsPerSecondd float64
}
func NewDatabendIntesterStatsRecorder() *DatabendIngesterStatsRecorder {
ingestedBytes, err := timeseries.NewTimeSeries()
if err != nil {
panic(err)
}
ingestedRows, err := timeseries.NewTimeSeries()
if err != nil {
panic(err)
}
return &DatabendIngesterStatsRecorder{
ingestedBytes: ingestedBytes,
ingestedRows: ingestedRows,
}
}
func (stats *DatabendIngesterStatsRecorder) RecordMetric(bytes int, rows int) {
stats.mu.Lock()
defer stats.mu.Unlock()
stats.ingestedBytes.Increase(bytes)
stats.ingestedRows.Increase(rows)
}
func (stats *DatabendIngesterStatsRecorder) Stats(statsWindow time.Duration) DatabendIngesterStatsData {
stats.mu.Lock()
defer stats.mu.Unlock()
bytesPerSecond := stats.calcPerSecond(stats.ingestedBytes, statsWindow)
rowsPerSecond := stats.calcPerSecond(stats.ingestedRows, statsWindow)
return DatabendIngesterStatsData{
BytesPerSecond: bytesPerSecond,
RowsPerSecondd: rowsPerSecond,
}
}
func (stats *DatabendIngesterStatsRecorder) calcPerSecond(ts *timeseries.TimeSeries, duration time.Duration) float64 {
amount, err := ts.Range(time.Now().Add(-duration), time.Now())
if err != nil {
return -1
}
return float64(amount) / duration.Seconds()
}