Created
May 25, 2015 09:14
-
-
Save przmv/be48438dc738ed4a9957 to your computer and use it in GitHub Desktop.
gonx interval reducer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package elblog | |
import ( | |
"time" | |
"github.com/pshevtsov/gonx" | |
) | |
const ( | |
IntervalHourly = -1 * time.Hour | |
IntervalDaily = -24 * time.Hour | |
) | |
type Interval struct { | |
start time.Time | |
end time.Time | |
} | |
func NewInterval(d time.Duration, t time.Time) *Interval { | |
start := t.Add(d) | |
end := t | |
return &Interval{start, end} | |
} | |
func (i *Interval) Reduce(input chan *gonx.Entry, output chan *gonx.Entry) { | |
for entry := range input { | |
field, _ := entry.Field(FieldTimestamp) | |
t, _ := time.Parse(time.RFC3339Nano, field) | |
if i.inBounds(t) { | |
output <- entry | |
} | |
} | |
close(output) | |
} | |
func (i *Interval) inBounds(t time.Time) bool { | |
if t.Equal(i.start) { | |
return true | |
} | |
if t.After(i.start) && t.Before(i.end) { | |
return true | |
} | |
return false | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment