__init__.py 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. import asyncio
  2. import collections
  3. import logging
  4. import random
  5. from itertools import groupby
  6. from typing import List, Tuple, Dict, Union
  7. from .random_street_view import call_random_street_view, VALID_COUNTRIES as RSV_COUNTRIES
  8. from .urban_centers import urban_coord, VALID_COUNTRIES as URBAN_COUNTRIES
  9. from .shared import aiohttp_client, reverse_geocode
  10. from ..schemas import GameConfig, GenMethodEnum, CountryCode, CacheInfo, GeneratorInfo
  11. logger = logging.getLogger(__name__)
  12. generator_info = [
  13. GeneratorInfo(
  14. generation_method=GenMethodEnum.rsv,
  15. country_locks=RSV_COUNTRIES
  16. ),
  17. GeneratorInfo(
  18. generation_method=GenMethodEnum.urban,
  19. country_locks=URBAN_COUNTRIES
  20. ),
  21. ]
  22. cache_names = {
  23. GenMethodEnum.rsv: "RSV",
  24. GenMethodEnum.urban: "Urban",
  25. }
  26. class ExhaustedSourceError(Exception):
  27. pass
  28. class PointStore:
  29. def __init__(self,
  30. cache_targets: Dict[Tuple[GenMethodEnum, CountryCode], int],
  31. rsv_country_retries: int = 5,
  32. urban_country_pool_size: int = 30,
  33. urban_country_retries: int = 30,
  34. urban_city_retries: int = 50,
  35. urban_city_retries_per_random_country: int = 10):
  36. self.cache_targets = cache_targets
  37. self.rsv_country_retries = rsv_country_retries
  38. self.urban_country_pool_size = urban_country_pool_size
  39. self.urban_country_retries = urban_country_retries
  40. self.urban_city_retries = urban_city_retries
  41. self.urban_city_retries_per_random_country = urban_city_retries_per_random_country
  42. self.store = collections.defaultdict(collections.deque)
  43. async def _gen_rsv_point(self, country: CountryCode):
  44. # RSV point function returns a collection of points, which should be cached
  45. for actual_country, points in groupby(await call_random_street_view(country), key=lambda p: p[0]):
  46. # but these points need to be cached according to the actual reverse geocoded country they are in
  47. self.store[(GenMethodEnum.rsv, actual_country)].extend(points)
  48. stock = self.store[(GenMethodEnum.rsv, country)]
  49. if len(stock) > 0:
  50. return stock.popleft()
  51. async def _gen_urban_point(self, countries: List[CountryCode], city_retries: int):
  52. for country in countries:
  53. logger.info(f"Selecting urban centers from {country}")
  54. pt = await urban_coord(country, city_retries=city_retries)
  55. if pt is not None:
  56. if pt[0] == country:
  57. return pt
  58. else:
  59. # TODO technically this is slightly wasted effort in rare edge cases
  60. self.store[(GenMethodEnum.urban, pt[0])].append(pt)
  61. # TODO I think all of this logic still gets stuck in the trap of generating points even when there's a stock in some edge cases
  62. # this needs a rewrite but I'm not doing that now
  63. async def get_point(self, generator: GenMethodEnum, country: Union[CountryCode, None], force_generate: bool = False) -> Tuple[str, float, float]:
  64. if country is None:
  65. # generating points across the whole world
  66. # for current generators, this means selecting a country at random
  67. if generator == GenMethodEnum.rsv:
  68. for _ in range(self.rsv_country_retries):
  69. # try a few countries before giving up, just in case one has no data
  70. country = random.choice(RSV_COUNTRIES)
  71. point = await self._gen_rsv_point(country)
  72. if point is not None:
  73. return point
  74. elif generator == GenMethodEnum.urban:
  75. # try many countries since finding an urban center point is harder
  76. countries = random.sample(URBAN_COUNTRIES, k=min(self.urban_country_pool_size, len(URBAN_COUNTRIES)))
  77. point = await self._gen_urban_point(countries, self.urban_city_retries_per_random_country)
  78. if point is not None:
  79. return point
  80. # if nothing could be done - inform the caller
  81. raise ExhaustedSourceError
  82. # generating points for a specific country
  83. # if we already have a point ready, just return it immediately
  84. if not force_generate:
  85. stock = self.store[(generator, country)]
  86. if len(stock) > 0:
  87. return stock.popleft()
  88. # otherwise, need to actually generate a new point
  89. if generator == GenMethodEnum.rsv:
  90. point = await self._gen_rsv_point(country)
  91. if point is not None:
  92. return point
  93. elif generator == GenMethodEnum.urban:
  94. point = await self._gen_urban_point((country for _ in range(self.urban_country_retries)), self.urban_city_retries)
  95. if point is not None:
  96. return point
  97. # finally, if all that fails, just inform the caller
  98. raise ExhaustedSourceError
  99. async def get_points(self, config: GameConfig) -> List[Tuple[str, float, float]]:
  100. """
  101. Provide points according to the GameConfig.
  102. Return a list of valid geo points, as
  103. (2 character country code, latitude, longitude) tuples.
  104. In the event that the configured source cannot reasonably supply enough points,
  105. most likely due to time constraints, this will raise an ExhaustedSourceError.
  106. """
  107. # in the case of using the "difficulty tiered" generator there is some special logic
  108. if config.generation_method == GenMethodEnum.diff_tiered:
  109. # TODO - actually implement this, for now just fail
  110. raise ExhaustedSourceError
  111. try:
  112. point_tasks = [self.get_point(config.generation_method, config.country_lock) for _ in range(config.rounds)]
  113. gathered = asyncio.gather(*point_tasks)
  114. return await asyncio.wait_for(gathered, 60)
  115. # TODO - it would be nice to keep partially generated sets around if there's a timeout or exhaustion
  116. except asyncio.TimeoutError:
  117. raise ExhaustedSourceError
  118. def get_cache_info(self) -> List[CacheInfo]:
  119. """
  120. Get CacheInfo for all caches.
  121. """
  122. return [CacheInfo(cache_name=f"{cache_names[g]}-{c}", size=len(ps)) for (g, c), ps in self.store.items()]
  123. async def _restock_source_impl(self, generator: GenMethodEnum, country: CountryCode):
  124. key = (generator, country)
  125. target = self.cache_targets.get(key, 0)
  126. stock = self.store[key]
  127. while len(stock) < target: # this check allows for RSV to do its multi-point restock
  128. stock.append(await self.get_point(*key, force_generate=True))
  129. async def restock_source(self, config: GameConfig):
  130. """
  131. Restock any caches associated with the GameConfig.
  132. """
  133. if config.country_lock is None:
  134. return
  135. try:
  136. await self._restock_source_impl(config.generation_method, config.country_lock)
  137. except ExhaustedSourceError:
  138. # if the cache can't be restocked, that is bad, but not fatal
  139. logger.exception(f"Failed to fully restock point cache for {config}")
  140. async def restock_all(self, timeout: Union[int, float, None] = None):
  141. """
  142. Restock all caches.
  143. """
  144. restock_tasks = [self._restock_source_impl(gen, cc) for (gen, cc) in self.cache_targets.keys()]
  145. gathered = asyncio.gather(*restock_tasks)
  146. try:
  147. await asyncio.wait_for(gathered, timeout)
  148. except (asyncio.TimeoutError, ExhaustedSourceError):
  149. # if this task times out, it's fine, as it's just intended to be a best effort
  150. logger.exception("Failed to fully restock a point cache!")
  151. points = PointStore({
  152. (GenMethodEnum.urban, "us"): 10,
  153. })