Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix default week selection #357

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 55 additions & 33 deletions stats/fvsl.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package stats

import (
"iter"
"slices"
"sort"
"time"

Expand All @@ -27,49 +29,69 @@ func NewFeedVersionServiceLevelsFromReader(reader adapters.Reader) ([]dmfr.FeedV
return results, nil
}

func ServiceLevelDefaultWeek(start tt.Date, end tt.Date, fvsls []dmfr.FeedVersionServiceLevel) (tt.Date, error) {
// Defaults
if start.IsZero() {
start = tt.NewDate(time.Date(0, 0, 0, 0, 0, 0, 0, time.UTC))
func serviceLevelDefaultWeek(fvsls []dmfr.FeedVersionServiceLevel, start tt.Date, end tt.Date) (tt.Date, error) {
fvDate := tt.Date{}
fvMax := 0
for fvsl := range serviceLevelDateFilter(
fvsls,
start,
end,
) {
if tot := fvsl.Total(); tot > fvMax {
fvMax = tot
fvDate = fvsl.StartDate
}
}
return fvDate, nil
}

func serviceLevelExpandWeeks(fvsls []dmfr.FeedVersionServiceLevel) iter.Seq[dmfr.FeedVersionServiceLevel] {
return func(yield func(dmfr.FeedVersionServiceLevel) bool) {
for _, fvsl := range fvsls {
s := fvsl.StartDate.Val
for s.Before(fvsl.EndDate.Val) {
fvslCopy := fvsl
fvslCopy.StartDate = tt.NewDate(s)
fvslCopy.EndDate = tt.NewDate(s.AddDate(0, 0, 7))
if !yield(fvslCopy) {
return
}
s = fvslCopy.EndDate.Val
}
}
}
if end.IsZero() {
end = tt.NewDate(time.Date(9999, 0, 0, 0, 0, 0, 0, time.UTC))
}

func serviceLevelDateFilter(fvsls []dmfr.FeedVersionServiceLevel, startDate tt.Date, endDate tt.Date) iter.Seq[dmfr.FeedVersionServiceLevel] {
if startDate.IsZero() {
startDate = tt.NewDate(time.Date(0, 0, 0, 0, 0, 0, 0, time.UTC))
}
d := tt.Date{}
// Get FVSLs in window
var fvsort []dmfr.FeedVersionServiceLevel
if start.IsZero() || end.Before(start) {
fvsort = fvsls[:]
} else {
if endDate.IsZero() {
endDate = tt.NewDate(time.Date(9999, 0, 0, 0, 0, 0, 0, time.UTC))
}
return func(yield func(dmfr.FeedVersionServiceLevel) bool) {
for _, fvsl := range fvsls {
if fvsl.EndDate.Before(start) {
// log.Traceln("fvsl ends before window:", fvsl.StartDate.String(), fvsl.EndDate.String())
if fvsl.StartDate.After(endDate) {
continue
}
if fvsl.StartDate.After(end) {
// log.Traceln("fvsl starts before window:", fvsl.StartDate.String(), fvsl.EndDate.String())
if fvsl.EndDate.Before(startDate) {
continue
}
fvsort = append(fvsort, fvsl)
yield(fvsl)
}
}
if len(fvsort) == 0 {
return d, nil
}
sort.Slice(fvsort, func(i, j int) bool {
a := fvsort[i].Total()
b := fvsort[j].Total()
if a == b {
return fvsort[i].StartDate.Before(fvsort[j].StartDate)
}

func serviceLevelDateSort(fvsls []dmfr.FeedVersionServiceLevel) []dmfr.FeedVersionServiceLevel {
fvsort := make([]dmfr.FeedVersionServiceLevel, len(fvsls))
copy(fvsort, fvsls)
slices.SortFunc(fvsort, func(a, b dmfr.FeedVersionServiceLevel) int {
if a.StartDate.Before(b.StartDate) {
return -1
}
return a > b
return 1
})
// log.Traceln("window start:", start.String(), "end:", end.String())
// for _, fvsl := range fvsort {
// log.Traceln("start:", fvsl.StartDate.String(), "end:", fvsl.EndDate.String(), "total:", fvsl.Total())
// }
// log.Traceln("d:", fvsort[0].StartDate.String())
return fvsort[0].StartDate, nil
return fvsort
}

func fromJulian(day int) time.Time {
Expand Down Expand Up @@ -230,5 +252,5 @@ func (pp *FeedVersionServiceLevelBuilder) ServiceLevels() ([]dmfr.FeedVersionSer
}

// Done
return results, nil
return serviceLevelDateSort(results), nil
}
57 changes: 55 additions & 2 deletions stats/fvsl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package stats
import (
"encoding/json"
"math/rand"
"slices"
"testing"
"time"

"github.com/interline-io/transitland-lib/dmfr"
"github.com/interline-io/transitland-lib/internal/testutil"
Expand Down Expand Up @@ -93,7 +95,6 @@ func TestNewFeedVersionServiceLevelsFromReader(t *testing.T) {
}

func TestServiceLevelDefaultWeek(t *testing.T) {

fvsls := []dmfr.FeedVersionServiceLevel{
{StartDate: pd("2022-01-03"), EndDate: pd("2022-01-09"), Monday: 1000},
{StartDate: pd("2022-01-10"), EndDate: pd("2022-01-16"), Monday: 2000},
Expand Down Expand Up @@ -138,11 +139,63 @@ func TestServiceLevelDefaultWeek(t *testing.T) {
}
// Shuffle
rand.Shuffle(len(tc.fvsls), func(i, j int) { tc.fvsls[i], tc.fvsls[j] = tc.fvsls[j], tc.fvsls[i] })
d, err := ServiceLevelDefaultWeek(tc.start, tc.end, tc.fvsls)
d, err := serviceLevelDefaultWeek(tc.fvsls, tc.start, tc.end)
if err != nil {
t.Fatal(err)
}
assert.EqualValues(t, tc.expect.String(), d.String())
})
}
}

func TestServiceLevelExpandWeeks(t *testing.T) {
fvsls := []dmfr.FeedVersionServiceLevel{
{StartDate: pd("2022-01-03"), EndDate: pd("2022-01-17"), Monday: 123},
{StartDate: pd("2022-01-17"), EndDate: pd("2022-01-23"), Monday: 45},
{StartDate: pd("2022-01-24"), EndDate: pd("2022-02-28"), Monday: 678},
}
totalCount := 0
totalService := 0
var lastWeek tt.Date
for fvsl := range serviceLevelExpandWeeks(fvsls) {
// fmt.Println("week:", fvsl.StartDate, "fvsl:", fvsl)
lastWeek = fvsl.StartDate
totalCount += 1
totalService += fvsl.Total()
}
assert.Equal(t, 8, totalCount)
assert.Equal(t, 3681, totalService)
assert.Equal(t, time.Date(2022, 2, 21, 0, 0, 0, 0, lastWeek.Val.Location()), lastWeek.Val)
}

func TestServiceLevelDateFilter(t *testing.T) {
fvsls := []dmfr.FeedVersionServiceLevel{
{StartDate: pd("2022-01-03"), EndDate: pd("2022-01-17"), Monday: 123},
{StartDate: pd("2022-01-17"), EndDate: pd("2022-01-23"), Monday: 45},
{StartDate: pd("2022-01-24"), EndDate: pd("2022-02-28"), Monday: 678},
}
t.Run("basic", func(t *testing.T) {
totalCount := 0
totalService := 0
for fvsl := range serviceLevelDateFilter(fvsls, pd("2022-01-20"), pd("2022-02-01")) {
// fmt.Println("week:", fvsl.StartDate, "fvsl:", fvsl)
totalCount += 1
totalService += fvsl.Total()
}
assert.Equal(t, 2, totalCount)
assert.Equal(t, 723, totalService)
})
t.Run("expanded", func(t *testing.T) {
fvslsExpanded := slices.Collect(serviceLevelExpandWeeks(fvsls))
totalCount := 0
totalService := 0
for fvsl := range serviceLevelDateFilter(fvslsExpanded, pd("2022-01-20"), pd("2022-02-20")) {
// fmt.Println("week:", fvsl.StartDate, "fvsl:", fvsl)
totalCount += 1
totalService += fvsl.Total()
}
assert.Equal(t, 5, totalCount)
assert.Equal(t, 2757, totalService)

})
}
2 changes: 1 addition & 1 deletion stats/fvsw_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func TestNewFeedVersionServiceWindowsFromReader(t *testing.T) {
t.Error(err)
}

if d, err := ServiceLevelDefaultWeek(fvsw.FeedStartDate, fvsw.FeedEndDate, fvsls); err != nil {
if d, err := serviceLevelDefaultWeek(fvsls, fvsw.FeedStartDate, fvsw.FeedEndDate); err != nil {
t.Error(err)
} else {
fvsw.FallbackWeek = d
Expand Down
2 changes: 1 addition & 1 deletion stats/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func NewFeedStatsFromReader(reader adapters.Reader) (FeedVersionStats, error) {
}

// Service window: Default week
ret.ServiceWindow.FallbackWeek, err = ServiceLevelDefaultWeek(ret.ServiceWindow.FeedStartDate, ret.ServiceWindow.FeedStartDate, ret.ServiceLevels)
ret.ServiceWindow.FallbackWeek, err = serviceLevelDefaultWeek(ret.ServiceLevels, ret.ServiceWindow.FeedStartDate, ret.ServiceWindow.FeedStartDate)
if err != nil {
return ret, err
}
Expand Down
Loading