Skip to content

Commit

Permalink
feat: timeseries: add TimeSeriesSet.NewTimesUpperBound()
Browse files Browse the repository at this point in the history
  • Loading branch information
grokify committed Jul 25, 2021
1 parent 132a079 commit f187e16
Showing 1 changed file with 61 additions and 0 deletions.
61 changes: 61 additions & 0 deletions data/timeseries/time_series_set_xf.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package timeseries

import (
"sort"
"time"

"github.com/grokify/simplego/time/timeslice"
"github.com/pkg/errors"
)

/*
func (set *TimeSeriesSet) NewTimesLowerBound(times ...time.Time) (TimeSeriesSets, error) {
min, _ := set.MinMaxTimes()
times = append(times, min)
timeSlice := timeslice.TimeSlice(times)
sort.Sort(timeSlice)
timeSlice = timeSlice.Dedupe()
sets := NewTimeSeriesSets("time sets by times")
sets.KeyIsTime = true
for seriesName, ts := range set.Series {
for _, timeItem := range ts.ItemMap {
timeBucket, err := timeSlice.RangeLower(timeItem.Time, true)
if err != nil {
return sets, err
}
timeItem.SeriesName = seriesName
timeItem.SeriesSetName = timeBucket.Format(time.RFC3339)
timeItem.Time = timeBucket
sets.AddItems(timeItem)
}
}
return sets, nil
}
*/

func (set *TimeSeriesSet) NewTimesUpperBound(times ...time.Time) (TimeSeriesSet, error) {
_, max := set.MinMaxTimes()
times = append(times, max)
timeSlice := timeslice.TimeSlice(times)
sort.Sort(timeSlice)
timeSlice = timeSlice.Dedupe()
newSet := NewTimeSeriesSet(set.Name)
for seriesName, series := range set.Series {
for _, timeItem := range series.ItemMap {
if len(timeItem.SeriesName) == 0 {
timeItem.SeriesName = seriesName
}
if timeItem.SeriesName != seriesName {
return newSet, errors.New("timeItem.SeriesName != TimeSeriesSet seriesName")
}
tRangeUpper, err := timeSlice.RangeUpper(timeItem.Time, true)
if err != nil {
panic("time item greater than set max time")
}
timeItem.Time = tRangeUpper
newSet.AddItems(timeItem)
}
}

return newSet, nil
}

0 comments on commit f187e16

Please sign in to comment.