Beispiel #1
0
// Replay the batch data from a single source
func replayBatchFromChan(clck clock.Clock, batches <-chan models.Batch, collector BatchCollector, recTime bool) error {
	defer collector.Close()

	// Find relative times
	var start, tmax time.Time
	var diff time.Duration
	zero := clck.Zero()

	for b := range batches {
		if len(b.Points) == 0 {
			// Emit empty batch
			if b.TMax.IsZero() {
				// Set tmax to last batch if not set.
				b.TMax = tmax
			} else {
				b.TMax = b.TMax.UTC()
				tmax = b.TMax
			}
			collector.CollectBatch(b)
			continue
		}
		if start.IsZero() {
			start = b.Points[0].Time
			diff = zero.Sub(start)
		}
		var lastTime time.Time
		if !recTime {
			for i := range b.Points {
				b.Points[i].Time = b.Points[i].Time.Add(diff).UTC()
			}
			lastTime = b.Points[len(b.Points)-1].Time
		} else {
			lastTime = b.Points[len(b.Points)-1].Time.Add(diff).UTC()
		}
		clck.Until(lastTime)
		if b.TMax.IsZero() {
			b.TMax = b.Points[len(b.Points)-1].Time
		} else {
			b.TMax = b.TMax.UTC()
		}
		tmax = b.TMax
		collector.CollectBatch(b)
	}
	return nil
}
Beispiel #2
0
func replayStreamFromChan(clck clock.Clock, points <-chan models.Point, collector StreamCollector, recTime bool) error {
	defer collector.Close()
	start := time.Time{}
	var diff time.Duration
	zero := clck.Zero()
	for p := range points {
		if start.IsZero() {
			start = p.Time
			diff = zero.Sub(start)
		}
		waitTime := p.Time.Add(diff).UTC()
		if !recTime {
			p.Time = waitTime
		}
		clck.Until(waitTime)
		err := collector.CollectPoint(p)
		if err != nil {
			return err
		}
	}
	return nil
}