-
Notifications
You must be signed in to change notification settings - Fork 1
/
data_point_partitioner.go
48 lines (41 loc) · 1.56 KB
/
data_point_partitioner.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
package main
type dataPointPartitioner struct {
dataPointBuffer *circularBuffer
timeWindow int
samplesPerSecond int
}
func newDataPointPartitioner(buffer *circularBuffer, timeWindow int, samplesPerSecond int) *dataPointPartitioner {
return &dataPointPartitioner{
dataPointBuffer: buffer,
timeWindow: timeWindow,
samplesPerSecond: samplesPerSecond,
}
}
func (s *dataPointPartitioner) start(dataPoints <-chan latencyDataPoint) {
dataPointsBinnedBySecond := make(chan []latencyDataPoint)
go s.partitionRepliesBySecond(dataPoints, dataPointsBinnedBySecond)
go s.addToCircularBuffer(dataPointsBinnedBySecond)
}
func (s *dataPointPartitioner) partitionRepliesBySecond(in <-chan latencyDataPoint, out chan<- []latencyDataPoint) {
// Assumption: inbound data points are ordered by time
currentAccumulatorSecondOffset := 0
timeQuantum := 1.0 / float64(s.samplesPerSecond)
currentSlice := make([]latencyDataPoint, s.samplesPerSecond, s.samplesPerSecond)
for r := range in {
currentSecond := int(r.timeOffset)
if currentAccumulatorSecondOffset != currentSecond {
out <- currentSlice
currentSlice = make([]latencyDataPoint, s.samplesPerSecond, s.samplesPerSecond)
currentAccumulatorSecondOffset = currentSecond
}
currentSubsecondOffset := r.timeOffset - float64(int(r.timeOffset))
currentSlice[int(currentSubsecondOffset/timeQuantum)] = r
}
}
func (s *dataPointPartitioner) addToCircularBuffer(replies chan []latencyDataPoint) {
for oneSecondOfData := range replies {
for _, r := range oneSecondOfData {
s.dataPointBuffer.insert(r)
}
}
}