File size: 3,994 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package oncall

import (
	"fmt"
	"time"
)

// TimeIterator will iterate between start and end at a particular step interval.
type TimeIterator struct {
	t, start, end, step int64

	nextStep int64
	init     bool

	sub []SubIterator
}

// A SubIterator can be added to a TimeIterator via the Register method.
type SubIterator interface {
	// Process will be called with each timestamp that needs processing. Each call will be sequential, but
	// it may not be called for each step.
	//
	// Process should return the value of the next required timestamp if it is known otherwise 0.
	// If the iterator has no more events to process -1 can be returned to signal complete.
	//
	// The value returned by Process must be -1, 0, or greater than t.
	Process(t int64) int64

	// Done will be called when the iterator is no longer needed.
	Done()
}

// Startable is a SubIterator method that allows reporting an alternate start timestamp that may extend beyond the parent TimeIterator.
type Startable interface {
	// StartUnix should return the earliest start time for this SubIterator. If it is unknown 0 should be returned.
	StartUnix() int64
}

// NextFunc can be used as a SubIterator.
type NextFunc func(int64) int64

// Process implements the SubIterator.Process method by calling the NextFunc.
func (fn NextFunc) Process(t int64) int64 { return fn(t) }

// Done is just a stub to implement the SubIterator.Done method.
func (fn NextFunc) Done() {}

// NewTimeIterator will create a new TimeIterator with the given configuration.
func NewTimeIterator(start, end time.Time, step time.Duration) *TimeIterator {
	step = step.Truncate(time.Second)
	stepUnix := step.Nanoseconds() / int64(time.Second)
	start = start.Truncate(step)
	end = end.Truncate(step)

	return &TimeIterator{
		step:     stepUnix,
		start:    start.Unix(),
		end:      end.Unix(),
		nextStep: start.Unix(),
	}
}

// Register adds a new sub Iterator.
func (iter *TimeIterator) Register(sub SubIterator) { iter.sub = append(iter.sub, sub) }

// Next will return true until iteration completes.
func (iter *TimeIterator) Next() bool {
	if !iter.init {
		for _, s := range iter.sub {
			st, ok := s.(Startable)
			if !ok {
				continue
			}
			start := st.StartUnix()
			start = start - start%iter.step
			if start != 0 && start < iter.start {
				iter.start = start
			}

		}
		iter.nextStep = iter.start
		iter.init = true
	}
	if iter.t >= iter.end {
		return false
	}
	iter.t = iter.nextStep
	iter.nextStep = 0

	var nextStep int64
	for _, sub := range iter.sub {
		nextStep = sub.Process(iter.t)
		if nextStep > 0 && nextStep <= iter.t {
			panic(fmt.Sprintf("nextStep was not in the future; got %d; want > %d (start=%d, end=%d)\n%#v", nextStep, iter.t, iter.start, iter.end, sub))
		}
		if nextStep == -1 {
			// -1 means nothing left, jump to end
			nextStep = iter.end
		}
		if iter.nextStep == 0 {
			// no hints yet, so use current one
			iter.nextStep = nextStep
		} else if nextStep > 0 && nextStep < iter.nextStep {
			// we have a next timestamp, update it only if it's sooner than the current hint
			iter.nextStep = nextStep
		}
	}

	if iter.nextStep == 0 {
		// no hints, so proceed to the next step
		iter.nextStep = iter.t + iter.step
	} else if iter.nextStep > iter.end {
		// clamp nextStep to end
		iter.nextStep = iter.end
	}
	return true
}

// Close should be called when the iterator is no longer needed.
func (iter *TimeIterator) Close() {
	for _, s := range iter.sub {
		s.Done()
	}
}

// Unix will return the current unix timestamp (seconds).
func (iter *TimeIterator) Unix() int64 { return iter.t }

// Start will return start time of the TimeIterator.
func (iter *TimeIterator) Start() time.Time { return time.Unix(iter.start, 0) }

// End will return the end time of the TimeIterator.
func (iter *TimeIterator) End() time.Time { return time.Unix(iter.end, 0) }

// Step will return the iterators step value.
func (iter *TimeIterator) Step() time.Duration { return time.Second * time.Duration(iter.step) }