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

Google Maps Geocoding API is overused? #492

Closed
Snowius opened this issue Apr 18, 2024 · 1 comment
Closed

Google Maps Geocoding API is overused? #492

Snowius opened this issue Apr 18, 2024 · 1 comment

Comments

@Snowius
Copy link

Snowius commented Apr 18, 2024

Hey, I'm using the calculate distance time feature with Google Maps and I'm noticing that my costs keeps going up daily even with the "caching" in place and the fact that my instances have been running for a few days so all the bags I have have already been cached supposedly. In fact, my geocoding API keeps being pinged for every single bag (whilst the direction API stalled and is not being called anymore since all is cached). Why is it done like this? I looked into the code and it's basically because we always check if the adress is valid before going into the cached places. Is it not an easy fix though?

location.py:

  def calculate_distance_time(self, destination: str, travel_mode: str) -> Union[DistanceTime, None]:
        """
        Calculates the distance and time taken to travel from origin to
        destination using the given mode of transportation.
        Returns distance and time in km and minutes respectively.
        """
        if not self.enabled:
            log.debug("Location service disabled")
            return None

        if not self._is_address_valid(destination):
            return None

        key = f"{destination}_{travel_mode}"

        # use cached value if available
        if key in self.distancetime_dict:
            return self.distancetime_dict[key]

I wonder if we can just take this code

        key = f"{destination}_{travel_mode}"

        # use cached value if available
        if key in self.distancetime_dict:
            return self.distancetime_dict[key]

and put it in front of this condition?

        if not self._is_address_valid(destination):
            return None

This way, we always check the dictionary before spamming the geocoding API with "is this adress valid?" every single time we try to get a notification about a new bag.

I'll probably make my own container with the fixed code but I figured it could be useful to change it in the main project as well.

Thoughts?

@Snowius
Copy link
Author

Snowius commented Apr 25, 2024

Update:

I've been running my two instances for a week now with the fixed code and it seems much better on the costs. As you can see here, I've created my instances a week ago on the 18th and they have been running since then (shows 6 but will be 7 in 3 hours when it hits 1 PM).

image

With this, here's my metrics for the Google Geocoding API

image

As you can see, since the 18th of April, the API stopped being used as much. In fact, once it already knows the information for all of my favorite locations, it will not send any more requests because all the locations will be cached in the program.

If you consider using this fix, it would surely benefit some people :)

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

No branches or pull requests

1 participant