Skip to content

Commit

Permalink
[WIP] Improve service window calculations
Browse files Browse the repository at this point in the history
  • Loading branch information
irees committed Dec 4, 2024
1 parent 71bb881 commit 0fa880f
Showing 1 changed file with 44 additions and 36 deletions.
80 changes: 44 additions & 36 deletions internal/clock/swcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,15 @@ func (f *ServiceWindowCache) queryFv(ctx context.Context, fvid int) (ServiceWind
DefaultTimezone tt.String
}
fvq := sq.StatementBuilder.
Select("fv.fetched_at", "fvsw.feed_start_date", "fvsw.feed_end_date", "fvsw.earliest_calendar_date", "fvsw.latest_calendar_date", "fvsw.fallback_week", "fvsw.default_timezone").
Select(
"fv.fetched_at",
"fvsw.feed_start_date",
"fvsw.feed_end_date",
"fvsw.earliest_calendar_date",
"fvsw.latest_calendar_date",
"fvsw.fallback_week",
"fvsw.default_timezone",
).
From("feed_versions fv").
LeftJoin("feed_version_service_windows fvsw on fvsw.feed_version_id = fv.id").
Where(sq.Eq{"fvsw.feed_version_id": fvid}).
Expand All @@ -103,20 +111,13 @@ func (f *ServiceWindowCache) queryFv(ctx context.Context, fvid int) (ServiceWind
ret.StartDate = fiData.FeedStartDate.Val
ret.EndDate = fiData.FeedEndDate.Val
}
// else {
// fmt.Println("using calendar start/end")
// ret.StartDate = fiData.EarliestCalendarDate.Val
// ret.EndDate = fiData.LatestCalendarDate.Val
// }
ret.Location, _ = f.tzCache.Location(fiData.DefaultTimezone.Val)
return ret, nil
}

func (f *ServiceWindowCache) queryFvsl(ctx context.Context, fvid int) (ServiceWindow, error) {
ret := ServiceWindow{}
minServiceRatio := 0.75
startDate := time.Time{}
endDate := time.Time{}

// Get FVSLs
type fvslEnt struct {
Expand All @@ -126,7 +127,12 @@ func (f *ServiceWindowCache) queryFvsl(ctx context.Context, fvid int) (ServiceWi
TotalService tt.Int
}
fvslQuery := sq.StatementBuilder.
Select("fv.fetched_at", "fvsl.start_date", "fvsl.end_date", "monday + tuesday + wednesday + thursday + friday + saturday + sunday as total_service").
Select(
"fv.fetched_at",
"fvsl.start_date",
"fvsl.end_date",
"monday + tuesday + wednesday + thursday + friday + saturday + sunday as total_service",
).
From("feed_versions fv").
Join("feed_version_service_levels fvsl on fvsl.feed_version_id = fv.id").
Where(sq.Eq{"route_id": nil}).
Expand All @@ -141,51 +147,53 @@ func (f *ServiceWindowCache) queryFvsl(ctx context.Context, fvid int) (ServiceWi
return ret, nil
}

// Check if we have feed infos, otherwise calculate based on fetched week or highest service week
// Get the week which includes fetched_at date, and the highest service week
// Get the highest service week
highestIdx := 0
highestService := -1
fetchedWeek := -1
fetchedAt := fvslEnts[0].FetchedAt.Val
highestService := fvslEnts[0].TotalService.Float()
for i, ent := range fvslEnts {
sd := ent.StartDate.Val
ed := ent.EndDate.Val
if (sd.Before(fetchedAt) || sd.Equal(fetchedAt)) && (ed.After(fetchedAt) || ed.Equal(fetchedAt)) {
fetchedWeek = i
}
if ent.TotalService.Int() > highestService {
if sl := ent.TotalService.Float(); sl > highestService {
highestIdx = i
highestService = ent.TotalService.Int()
highestService = sl
}
}
if fetchedWeek < 0 {
// fmt.Println("fetched week not in fvsls, using highest week:", highestIdx, highestService)
fetchedWeek = highestIdx
} else {
// fmt.Println("using fetched week:", fetchedWeek)
if highestService == 0 {
return ret, nil
}
// If the fetched week has bad service, use highest week
if float64(fvslEnts[fetchedWeek].TotalService.Val)/float64(highestService) < minServiceRatio {
// fmt.Println("fetched week has poor service ratio, falling back to highest week:", fetchedWeek)
fetchedWeek = highestIdx

// Get the week containing fetched_at, defaulting to the highest service week
selectedWeek := highestIdx
fetchedAt := fvslEnts[0].FetchedAt.Val
for i, ent := range fvslEnts {
if ent.StartDate.Val.After(fetchedAt) {
continue
}
if ent.EndDate.Val.Before(fetchedAt) {
continue
}
if ent.TotalService.Float()/highestService < minServiceRatio {
// fmt.Println("fetched week has poor service ratio, falling back to highest week:", i)
continue
}
// fmt.Println("using fetched week:", i)
selectedWeek = i
}

// Expand window in both directions from chosen week
startDate = fvslEnts[fetchedWeek].StartDate.Val
endDate = fvslEnts[fetchedWeek].EndDate.Val
for i := fetchedWeek; i < len(fvslEnts); i++ {
startDate := fvslEnts[selectedWeek].StartDate.Val
endDate := fvslEnts[selectedWeek].EndDate.Val
for i := selectedWeek; i < len(fvslEnts); i++ {
ent := fvslEnts[i]
if float64(ent.TotalService.Val)/float64(highestService) < minServiceRatio {
if ent.TotalService.Float()/highestService < minServiceRatio {
break
}
if ent.StartDate.Val.Before(startDate) {
startDate = ent.StartDate.Val
}
endDate = ent.EndDate.Val
}
for i := fetchedWeek - 1; i > 0; i-- {
for i := selectedWeek - 1; i > 0; i-- {
ent := fvslEnts[i]
if float64(ent.TotalService.Val)/float64(highestService) < minServiceRatio {
if ent.TotalService.Float()/highestService < minServiceRatio {
break
}
if ent.EndDate.Val.After(endDate) {
Expand Down

0 comments on commit 0fa880f

Please sign in to comment.