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

DISCO-3033 refactor city based suggest #667

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

misaniwere
Copy link
Contributor

@misaniwere misaniwere commented Oct 17, 2024

References

JIRA: DISCO-3033

Description

  • Follow up refactor based on this discussion
  • This PR makes some improvements on the city-based suggest logic for overwriting geolocation for the accuweather provider. Mainly, moving logic away from the provider and adding a helper function to the Location model to return preferred location based on custom values. Also added some more validation to query params.

PR Review Checklist

Put an x in the boxes that apply

  • This PR conforms to the Contribution Guidelines
  • The PR title starts with the JIRA issue reference, format [DISCO-####], and has the same title (if applicable)
  • [load test: (abort|warn)] keywords are applied (if applicable)
  • Documentation has been updated (if applicable)
  • Functional and performance test coverage has been expanded and maintained (if applicable)

@misaniwere misaniwere force-pushed the DISCO-3033-city-suggest-refactor branch from e5810d3 to c9bc733 Compare October 17, 2024 21:49
@misaniwere misaniwere requested a review from a team October 17, 2024 21:51
Copy link
Contributor

@ncloudioj ncloudioj left a comment

Choose a reason for hiding this comment

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

Looks good, thanks! r+wc.

def prioritize_custom_location(self) -> "Location":
"""Update city, region, and country with custom fields if all custom fields exist."""
if all([self.city_custom, self.regions_custom, self.country_custom]):
self.city = self.city_custom
Copy link
Contributor

Choose a reason for hiding this comment

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

Overwriting could be problematic as we will lose the original geolocation which is used by other consumers such as the logging middleware. I think we can either return a tuple of (country, regions, city) or just return a new Location with country, regions, and city are set to the custom counterparts.

Copy link
Contributor Author

@misaniwere misaniwere Oct 18, 2024

Choose a reason for hiding this comment

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

this should be fine as we're invoking this method on a copy of the geolocation and not the original object.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

another way we could try is moving this helper function out of the model class and into backend.py and having it return a new location with the overwrite so as not to mess with the middleware. what do you think @ncloudioj ?


validate_suggest_custom_location_params(city, region, country)

geolocation = request.scope[ScopeKey.GEOLOCATION].model_copy(
Copy link
Contributor

Choose a reason for hiding this comment

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

As of now, requests w/ the custom location is much fewer than those w/o it. So let's update it conditionally.

if country and region and city:
    geolocation = request.scope[ScopeKey.GEOLOCATION].model_copy(...)
else:
    geolocation = request.scope[ScopeKey.GEOLOCATION]
    

Copy link
Contributor Author

Choose a reason for hiding this comment

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

hmm since we're overwriting city, region & country down the line, I feel more comfortable working with the copy in this case. also we do some validation right before this so it either errors out before getting here if we have missing fields or the values are all None which is the default.

merino/web/api_v1.py Outdated Show resolved Hide resolved
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.

2 participants