2019-01-31 00:02:38 +00:00
|
|
|
package data
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2019-02-03 04:11:26 +00:00
|
|
|
type Sampler struct {
|
2019-01-31 00:02:38 +00:00
|
|
|
consumer Consumer
|
2019-01-31 23:40:05 +00:00
|
|
|
item Item
|
2019-01-31 00:02:38 +00:00
|
|
|
}
|
|
|
|
|
2019-02-03 04:11:26 +00:00
|
|
|
func NewSampler(consumer Consumer, item Item, rateMs int) Sampler {
|
2019-01-31 00:02:38 +00:00
|
|
|
|
2019-02-17 01:54:48 +00:00
|
|
|
ticker := time.NewTicker(time.Duration(rateMs * int(time.Millisecond)))
|
2019-02-03 04:11:26 +00:00
|
|
|
sampler := Sampler{consumer, item}
|
2019-01-31 00:02:38 +00:00
|
|
|
|
|
|
|
go func() {
|
2019-02-17 01:54:48 +00:00
|
|
|
sampler.sample()
|
|
|
|
for ; true; <-ticker.C {
|
2019-02-05 03:26:00 +00:00
|
|
|
sampler.sample()
|
2019-01-31 00:02:38 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2019-02-03 04:11:26 +00:00
|
|
|
return sampler
|
2019-01-31 00:02:38 +00:00
|
|
|
}
|
|
|
|
|
2019-02-03 04:11:26 +00:00
|
|
|
func (self *Sampler) sample() {
|
2019-01-31 00:02:38 +00:00
|
|
|
|
2019-01-31 23:40:05 +00:00
|
|
|
value, err := self.item.nextValue()
|
2019-01-31 00:02:38 +00:00
|
|
|
|
2019-02-03 03:30:45 +00:00
|
|
|
sample := Sample{
|
|
|
|
Value: value,
|
|
|
|
Error: err,
|
|
|
|
Label: self.item.Label,
|
2019-01-31 00:02:38 +00:00
|
|
|
}
|
|
|
|
|
2019-02-03 03:30:45 +00:00
|
|
|
self.consumer.ConsumeSample(sample)
|
2019-01-31 00:02:38 +00:00
|
|
|
}
|