diff --git a/internal/clock/swcache.go b/internal/clock/swcache.go index 436905be..4a214ee2 100644 --- a/internal/clock/swcache.go +++ b/internal/clock/swcache.go @@ -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}). @@ -103,11 +111,6 @@ 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 } @@ -115,8 +118,6 @@ func (f *ServiceWindowCache) queryFv(ctx context.Context, fvid int) (ServiceWind 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 { @@ -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}). @@ -141,41 +147,43 @@ 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) { @@ -183,9 +191,9 @@ func (f *ServiceWindowCache) queryFvsl(ctx context.Context, fvid int) (ServiceWi } 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) {