Der-Henning / tgtg

Scanner for Too Good To Go Notifications
https://github.com/Der-Henning/tgtg/wiki
GNU General Public License v3.0
357 stars 59 forks source link

Google Maps Geocoding API is overused? #492

Closed Snowius closed 2 months ago

Snowius commented 2 months ago

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 commented 2 months ago

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 :)