func New(dataPoints []*data.Point, interval time.Duration) *Aggregator { if len(dataPoints) == 0 { return &Aggregator{} } data.Sort(dataPoints) buckets := [][]*data.Point{} startTime := dataPoints[0].StartTime currentBucketTime := startTime nextBucketTime := startTime.Add(interval) bucketIndex := 0 buckets = append(buckets, nil) for _, dp := range dataPoints { for dp.StartTime.After(nextBucketTime) { currentBucketTime = nextBucketTime bucketIndex += 1 buckets = append(buckets, nil) nextBucketTime = currentBucketTime.Add(interval) } buckets[bucketIndex] = append(buckets[bucketIndex], dp) } return &Aggregator{buckets: buckets, interval: interval} }
Context("when the input is badly formatted", func() { It("returns an error", func() { _, err := data.Parse(badValuesInput) Expect(err).To(HaveOccurred()) }) }) Context("when the input is empty", func() { It("returns an error", func() { _, err := data.Parse("") Expect(err).To(HaveOccurred()) }) }) }) Describe("Sort", func() { It("sorts the data by start time", func() { p1 := &data.Point{time.Unix(1478020922, 270882000), time.Duration(19100000)} p2 := &data.Point{time.Unix(1478020922, 288598000), time.Duration(6300000)} p3 := &data.Point{time.Unix(1478020923, 0), time.Duration(10000000)} p4 := &data.Point{time.Unix(1478020922, 270864000), time.Duration(19900000)} dataPoints := []*data.Point{p1, p2, p3, p4} data.Sort(dataPoints) Expect(dataPoints).To(Equal([]*data.Point{p4, p1, p2, p3})) }) }) })