-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: timeseries: add
TimeSeriesSet.NewTimesUpperBound()
- Loading branch information
Showing
1 changed file
with
61 additions
and
0 deletions.
There are no files selected for viewing
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
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 | ||
} |