Skip to content

Commit

Permalink
feat: timeseries: add TimeSeriesSet.SplitTimesLowerBound()
Browse files Browse the repository at this point in the history
  • Loading branch information
grokify committed Jul 25, 2021
1 parent 4a24de3 commit d856dd7
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
28 changes: 26 additions & 2 deletions data/timeseries/time_series_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/grokify/simplego/sort/sortutil"
"github.com/grokify/simplego/time/month"
"github.com/grokify/simplego/time/timeslice"
"github.com/grokify/simplego/time/timeutil"
"github.com/grokify/simplego/type/stringsutil"
"github.com/pkg/errors"
Expand Down Expand Up @@ -143,7 +144,7 @@ func (set *TimeSeriesSet) Item(seriesName, rfc3339 string) (TimeItem, error) {
return item, nil
}

func (set *TimeSeriesSet) TimeSlice(sortAsc bool) sortutil.TimeSlice {
func (set *TimeSeriesSet) TimeSlice(sortAsc bool) timeslice.TimeSlice {
times := []time.Time{}
for _, ds := range set.Series {
for _, item := range ds.ItemMap {
Expand All @@ -165,7 +166,7 @@ func (set *TimeSeriesSet) TimeStrings() []string {
}

func (set *TimeSeriesSet) MinMaxTimes() (time.Time, time.Time) {
values := sortutil.TimeSlice{}
values := timeslice.TimeSlice{}
for _, ds := range set.Series {
min, max := ds.MinMaxTimes()
values = append(values, min, max)
Expand Down Expand Up @@ -194,6 +195,29 @@ func (set *TimeSeriesSet) MinMaxValuesFloat64() (float64, float64) {
return values[0], values[len(values)-1]
}

func (set *TimeSeriesSet) SplitTimesLowerBound(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)
if err != nil {
return sets, err
}
timeItem.SeriesName = seriesName
timeItem.SeriesSetName = timeBucket.Format(time.RFC3339)
timeItem.Time = timeBucket
sets.AddItems(timeItem)
}
}
return sets, nil
}

type RowInt64 struct {
Name string
DisplayName string
Expand Down
7 changes: 4 additions & 3 deletions data/timeseries/time_series_sets.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ import (
)

type TimeSeriesSets struct {
Name string
SetsMap map[string]TimeSeriesSet
Interval timeutil.Interval
Name string
SetsMap map[string]TimeSeriesSet
KeyIsTime bool
Interval timeutil.Interval
}

func NewTimeSeriesSets(name string) TimeSeriesSets {
Expand Down

0 comments on commit d856dd7

Please sign in to comment.