Skip to content

Instantly share code, notes, and snippets.

@przmv
Created May 25, 2015 09:14
Show Gist options
  • Save przmv/be48438dc738ed4a9957 to your computer and use it in GitHub Desktop.
Save przmv/be48438dc738ed4a9957 to your computer and use it in GitHub Desktop.
gonx interval reducer
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