Skip to content

Commit

Permalink
don't show wrong way around departures in planner
Browse files Browse the repository at this point in the history
  • Loading branch information
AaronClaydon committed Dec 13, 2024
1 parent 8ed37b6 commit e937bed
Showing 1 changed file with 22 additions and 6 deletions.
28 changes: 22 additions & 6 deletions pkg/dataaggregator/source/journeyplanner/journeyplan.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"sort"
"time"

"github.com/kr/pretty"
"github.com/travigo/travigo/pkg/ctdf"
"github.com/travigo/travigo/pkg/dataaggregator"
"github.com/travigo/travigo/pkg/dataaggregator/query"
Expand All @@ -20,7 +21,7 @@ func (s Source) JourneyPlanQuery(q query.JourneyPlan) (*ctdf.JourneyPlanResults,

departureBoard, err := dataaggregator.Lookup[[]*ctdf.DepartureBoard](query.DepartureBoard{
Stop: q.OriginStop,
Count: q.Count,
Count: q.Count * 4,
StartDateTime: q.StartDateTime,
Filter: &bson.M{"path.destinationstopref": q.DestinationStop.PrimaryIdentifier},
})
Expand All @@ -34,23 +35,32 @@ func (s Source) JourneyPlanQuery(q query.JourneyPlan) (*ctdf.JourneyPlanResults,
return departureBoard[i].Time.Before(departureBoard[j].Time)
})

// Once sorted cut off any records higher than our max count
if len(departureBoard) > q.Count {
departureBoard = departureBoard[:q.Count]
}

// Turn the departure board into a journey plan
journeyPlanResults := &ctdf.JourneyPlanResults{
JourneyPlans: []ctdf.JourneyPlan{},
OriginStop: *q.OriginStop,
DestinationStop: *q.DestinationStop,
}

currentFound := 0

for _, departure := range departureBoard {
if currentFound >= q.Count {
break
}

startTime := departure.Time
var arrivalTime time.Time

seenOrigin := false

for _, item := range departure.Journey.Path {
pretty.Println(item.OriginStopRef, q.OriginStop.PrimaryIdentifier, q.OriginStop.OtherIdentifiers)
if item.OriginStopRef == q.OriginStop.PrimaryIdentifier || slices.Contains[[]string](q.OriginStop.OtherIdentifiers, item.OriginStopRef) {
pretty.Println("cum")
seenOrigin = true
}

if item.DestinationStopRef == q.DestinationStop.PrimaryIdentifier || slices.Contains[[]string](q.DestinationStop.OtherIdentifiers, item.DestinationStopRef) {
refTime := item.DestinationArrivalTime
dateTime := q.StartDateTime
Expand All @@ -65,6 +75,11 @@ func (s Source) JourneyPlanQuery(q query.JourneyPlan) (*ctdf.JourneyPlanResults,
}
}

// If we've not seen origin by the time we've seen destination then this journey is running in the wrong direction
if !seenOrigin {
continue
}

journeyPlan := ctdf.JourneyPlan{
RouteItems: []ctdf.JourneyPlanRouteItem{
{
Expand All @@ -82,6 +97,7 @@ func (s Source) JourneyPlanQuery(q query.JourneyPlan) (*ctdf.JourneyPlanResults,
}

journeyPlanResults.JourneyPlans = append(journeyPlanResults.JourneyPlans, journeyPlan)
currentFound += 1
}

return journeyPlanResults, nil
Expand Down

0 comments on commit e937bed

Please sign in to comment.