Skip to content

Cleanup itinerary, make it immutable #6535

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

Merged
merged 31 commits into from
Mar 14, 2025

Conversation

t2gran
Copy link
Member

@t2gran t2gran commented Mar 12, 2025

Summary

Cleanup Itinerary code. This started with an few errors in our logs at entur, see #6534. The error happens in the mapping in the GraphQL code, but the a negative cost should not be allowed on the itinerary. This PR clean up the Itinerary and
make it immutable except the lifecycle systemNotices field.

I have added a few TODOs for further cleanup. Also the builder setters and Itinerary getter for generalizedCost should use the type-safe Cost type - I changed the internal representation from int to Cost, but has not converted the external representation everywhere.

Notable changes

  • Itinerary used the Cost type internally
  • The elevation gained/lost is computed in two phases and is fragile to changes in leg
  • Itinerary is now immutable except for the systemNotices - which is used for life-cycle in filter-chain. This is intended.
  • All Itinerary field and method names are updated to follow the naming conventions

Review

  • Esay to review commit by commit, but as a whole works too.
  • I will fix all minor comments in a follow-up PR to make the review of this go fast - there are a lot of changes and I do not want to have this PR pending for a long time.

Issue

This is related to #6534

Unit tests

✅ I have extracted some code and added unit-test for it, other than that I relay on existing tests.

Documentation

✅ The JavaDoc is updated in a few places.

Changelog

🟥 Not relevant

Bumping the serialization version id

✅ The Cost class is changed

t2gran added 26 commits March 7, 2025 17:00
To avoid rounding errors we should use centi-seconds
in cost. This is the same resolution used in Raptor.
When legs are modified the totals are recalculated,
when this happen we need to add the original elevation
edge calculation together with the new totals from
elevation profile.
@t2gran t2gran added Technical Debt bump serialization id Add this label if you want the serialization id automatically bumped after merging the PR Skip Changelog labels Mar 12, 2025
@t2gran t2gran added this to the 2.7 (next release) milestone Mar 12, 2025
@t2gran t2gran requested a review from a team as a code owner March 12, 2025 10:28
@t2gran t2gran added the Entur Test This is currently being tested at Entur label Mar 12, 2025
Copy link

codecov bot commented Mar 12, 2025

Codecov Report

Attention: Patch coverage is 88.53119% with 57 lines in your changes missing coverage. Please review.

Project coverage is 70.24%. Comparing base (8a7a685) to head (867ff36).
Report is 47 commits behind head on dev-2.x.

Files with missing lines Patch % Lines
...iority/TransitGroupPriorityItineraryDecorator.java 7.14% 12 Missing and 1 partial ⚠️
...entripplanner/routing/algorithm/RoutingWorker.java 59.09% 9 Missing ⚠️
...r/routing/algorithm/mapping/ItinerariesHelper.java 11.11% 8 Missing ⚠️
...ssibilityscore/DecorateWithAccessibilityScore.java 82.60% 3 Missing and 1 partial ⚠️
...planner/ext/realtimeresolver/RealtimeResolver.java 78.94% 2 Missing and 2 partials ⚠️
...opconsolidation/DecorateConsolidatedStopNames.java 71.42% 2 Missing and 2 partials ⚠️
...g/opentripplanner/model/plan/ItineraryBuilder.java 94.80% 4 Missing ⚠️
...algorithm/mapping/RaptorPathToItineraryMapper.java 89.28% 1 Missing and 2 partials ⚠️
...rg/opentripplanner/ext/fares/DecorateWithFare.java 66.66% 1 Missing and 1 partial ⚠️
...tripplanner/ext/fares/ItineraryFaresDecorator.java 88.88% 1 Missing and 1 partial ⚠️
... and 4 more
Additional details and impacted files
@@              Coverage Diff              @@
##             dev-2.x    #6535      +/-   ##
=============================================
+ Coverage      70.22%   70.24%   +0.02%     
- Complexity     18331    18367      +36     
=============================================
  Files           2085     2087       +2     
  Lines          77308    77383      +75     
  Branches        7840     7840              
=============================================
+ Hits           54286    54359      +73     
+ Misses         20250    20249       -1     
- Partials        2772     2775       +3     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@t2gran t2gran modified the milestones: 2.7, 2.8 (next release) Mar 12, 2025
@t2gran t2gran changed the title Fix gen cost itinerary Cleanup itinerary, make it immutable Mar 12, 2025
Copy link
Member

@leonardehrenfried leonardehrenfried left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I read through the code and even though I could find a few typos I chose to leave them as is and we will clean the up later.

I also ran the IBI smoke test suite on this PR and it passed so I'm pretty confident that no bugs were introduced.

And there is a fix for the tests that you had to disable: leonardehrenfried@61eab43

You may want to cherry-pick (not merge) it. If not we can do it afterwards.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice job. This is a bit too big to do a thorough review but I commented on a few minor things that can be addressed later.


assertEquals(1, itineraries.size());
itinerary = itineraries.getFirst();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: I think it would be nicer to use separate variables for the original and the modified itineraries

@@ -428,8 +445,8 @@ public void setElevationGained(Double elevationGained) {
* <p>
* -1 indicate that the cost is not set/computed.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment should be updated


public List<ScheduledTransitLeg> getScheduledTransitLegs() {
return getLegs()
public List<ScheduledTransitLeg> findScheduledTransitLegs() {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using "find" here seems strange to me. Shouldn't it rather be listScheduledTransitLegs()?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree, this was a mistake.

.parallelStream()
.map(leg -> decorateLegWithRideEstimate(i, leg, service))
.toList();

i.setLegs(legs);
return builder.withLegs(ignore -> legs).build();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why don't we run the decoration in this lambda?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because we are updating the existing legs, not setting them. You always start with creating a builder passing in the legs. So, I wanted to make sure the developer setting the legs we careful - not just replacing the new legs, witch may cause information loss.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will add a comment on it.

/**
* The fare products of this itinerary.
*/
public ItineraryFares fare() {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be called fares() to be consistent with the type name?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The typename is wrong - it is not a collection of fares it is THE itinerary fare. I can change the typename in a followup PR.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sometimes 'Information' or 'Info' is added to the class name in cases where the plural is used in this way make it easier to read/talk in natural english. E.g. ItineraryFareInfo

}

static RoutingResult empty() {
return new RoutingResult(null, null);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this class would be simpler to follow if you used Collections.emptyList() instead of null parameters.

@t2gran
Copy link
Member Author

t2gran commented Mar 14, 2025

I read through the code and even though I could find a few typos I chose to leave them as is and we will clean the up later.

@leonardehrenfried Feel free to comment on them, then I can fix them in a follow up PR.

@t2gran
Copy link
Member Author

t2gran commented Mar 14, 2025

@habrahamsson-skanetrafiken thank you for the feedback, I will make the fixes in a follwup PR as soon as this is merged.

There was a merge conflict so I need the approvals again :-)

@t2gran t2gran merged commit 9e67f60 into opentripplanner:dev-2.x Mar 14, 2025
6 checks passed
@t2gran t2gran deleted the fix_gen_cost_itinerary branch March 14, 2025 12:20
t2gran pushed a commit that referenced this pull request Mar 14, 2025
t2gran added a commit to entur/OpenTripPlanner that referenced this pull request Mar 20, 2025
t2gran added a commit that referenced this pull request Mar 20, 2025
Fix NPE in RoutingWorker after #6535 was merged
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bump serialization id Add this label if you want the serialization id automatically bumped after merging the PR Entur Test This is currently being tested at Entur Skip Changelog Technical Debt
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants