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

Jormun: Fix journey's sections sorter #3894

Merged
merged 3 commits into from
Jan 10, 2023
Merged

Jormun: Fix journey's sections sorter #3894

merged 3 commits into from
Jan 10, 2023

Conversation

azime
Copy link
Contributor

@azime azime commented Jan 5, 2023

Jira: NAV-1678
The sort of sections is random when two sections have the same departure_date_time and arrival_date_time

Before correction:
image

Comment on lines 461 to 465
if a.begin_date_time == b.begin_date_time and a.end_date_time == b.end_date_time:
return -1 if a.destination.uri == b.origin.uri else 1
if a.begin_date_time != b.begin_date_time:
return -1 if a.begin_date_time < b.begin_date_time else 1
else:
Copy link
Contributor

@pbougue pbougue Jan 5, 2023

Choose a reason for hiding this comment

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

Smart to try to join sections 💡
But how about 3 sections having the same values, or 2 "distant" sections? Maybe raising would be better 🤷

Anyway, more efficient (and clearer IMO):

        if a.begin_date_time != b.begin_date_time:
            return -1 if a.begin_date_time < b.begin_date_time else 1
        elif a.end_date_time != b.end_date_time:
           return -1 if a.end_date_time < b.end_date_time else 1
        elif a.destination.uri == b.origin.uri:
           return -1
        elif a.origin.uri == b.destination.uri:
           return 1
        else:  # still not decided, but default to something stable. Or is there a return 0 possible ?
           return -1 if a.origin.uri < b.origin.uri else 1  # or should we explicitly raise?

Copy link
Contributor

@woshilapin woshilapin Jan 6, 2023

Choose a reason for hiding this comment

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

I like your proposition better, it's clearer.

However, about you last comment, if I'm not wrong, I wouldn't recommend to return 0 (even if it's probably possible). We want a stable sorting and depending on the sorting algorithm, the original order might or might not be preserved.

By the way, this class is called SectionSorter, but I don't think the naming is correct, it doesn't sort, it only compares. How about renaming it SectionComparator?

Copy link
Contributor

Choose a reason for hiding this comment

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

Now I am wondering why we need to sort sections 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@pbench In the protobuf you cannot add a section at the beginning

Copy link
Contributor

Choose a reason for hiding this comment

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

@woshilapin returning a raw value in the else case (no matter the value) is not stable, as it will depend on which is left or right member. Sorting on uri is stable but not relevant... I guess raising has my preference, but it's not strong.
✔️ Anyway, it's quite clear and way better now, so it's fine by me.

Naming proposition by @woshilapin is better but not mandatory IMO.
@pbench avoid that sorting by a stitching mechanism of section-groups would be more correct, but this is more work, so I'm OK to keep this.

@sonarqubecloud
Copy link

sonarqubecloud bot commented Jan 6, 2023

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 1 Code Smell

83.3% 83.3% Coverage
0.0% 0.0% Duplication

return -1
elif a.origin.uri == b.destination.uri:
return 1
else:
Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder if we should put a log in this else case since it's not supposed to happen. Might help to debug in case it actually happens.

Copy link
Contributor

Choose a reason for hiding this comment

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

Indeed, logging (warning or error) here would be nice 👍

@azime azime merged commit af69676 into dev Jan 10, 2023
@azime azime deleted the fix_section_sorter branch January 10, 2023 17:05
@pbougue pbougue changed the title Fix section sorter Jormun: Fix journey's sections sorter Jan 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants