Browse Source

Adding a new endpoint to check cached point counts

Kirk Trombley 5 years ago
parent
commit
7cd12b39cb
5 changed files with 62 additions and 28 deletions
  1. 10 0
      README.md
  2. 1 1
      server/Dockerfile
  3. 14 0
      server/extra_api.py
  4. 3 27
      server/game_api.py
  5. 34 0
      server/sources.py

+ 10 - 0
README.md

@@ -26,6 +26,16 @@ POST /score
         "score": number,
         "distance": number
     }
+GET /caches
+    Returns 200 and {
+        "caches": [
+            {
+                "generationMethod": string,
+                "onlyAmerica": boolean,
+                "size": number
+            }, ...
+        ]
+    }
 PUT /game
     Accepts {
         "timer": number,

+ 1 - 1
server/Dockerfile

@@ -14,6 +14,6 @@ COPY requirements.txt ./
 
 RUN pip3 install -r requirements.txt
 
-COPY app.py db.py extra_api.py game_api.py lib.py ./
+COPY app.py db.py extra_api.py game_api.py sources.py lib.py ./
 
 CMD gunicorn --workers=1 --bind=0.0.0.0:5000 app:app

+ 14 - 0
server/extra_api.py

@@ -1,6 +1,7 @@
 from flask import Blueprint, abort, request, jsonify
 
 import lib
+from sources import sources
 
 extra = Blueprint("extra", __name__)
 
@@ -26,3 +27,16 @@ def check_score():
         "score": score,
         "distance": distance,
     })
+
+
+@extra.route("caches", methods=["GET"])
+def get_cached_points():
+    return jsonify({
+        "caches": [
+            {
+                "generationMethod": gm,
+                "onlyAmerica": oa,
+                "size": len(ps.stock)
+            } for ((gm, oa), ps) in sources.items()
+        ]
+    })

+ 3 - 27
server/game_api.py

@@ -1,36 +1,12 @@
-import threading
-
 from flask import Blueprint, abort, request, jsonify
 
 import db
 import lib
+from sources import sources, restock_all
 
-game = Blueprint("game", __name__)
+restock_all()
 
-stock_target = 20
-# (gen_method, only_america): lib.PointSource
-sources = {
-    ("MAPCRUNCH", False): lib.MapCrunchPointSource(stock_target=stock_target, max_retries=1000, only_america=False),
-    ("MAPCRUNCH", True): lib.MapCrunchPointSource(stock_target=stock_target, max_retries=1000, only_america=True),
-    ("RANDOMSTREETVIEW", False): lib.RSVPointSource(stock_target=stock_target, only_america=False),
-    ("RANDOMSTREETVIEW", True): lib.RSVPointSource(stock_target=stock_target, only_america=True),
-    ("URBAN", False): lib.UrbanPointSource(
-        stock_target=stock_target,
-        max_retries=100,
-        retries_per_point=30,
-        max_dist_km=25,
-        usa_chance=0.1
-    ),
-    ("URBAN", True): lib.UrbanPointSource(
-        stock_target=stock_target,
-        max_retries=100,
-        retries_per_point=30,
-        max_dist_km=25,
-        usa_chance=1.0
-    )
-}
-for src in sources.values():
-    threading.Thread(target=src.restock).start()
+game = Blueprint("game", __name__)
 
 
 def require_game(game_id):

+ 34 - 0
server/sources.py

@@ -0,0 +1,34 @@
+import threading
+
+import lib
+
+stock_target = 20
+# (gen_method, only_america): lib.PointSource
+sources = {
+    ("MAPCRUNCH", False): lib.MapCrunchPointSource(stock_target=stock_target, max_retries=1000, only_america=False),
+    ("MAPCRUNCH", True): lib.MapCrunchPointSource(stock_target=stock_target, max_retries=1000, only_america=True),
+    ("RANDOMSTREETVIEW", False): lib.RSVPointSource(stock_target=stock_target, only_america=False),
+    ("RANDOMSTREETVIEW", True): lib.RSVPointSource(stock_target=stock_target, only_america=True),
+    ("URBAN", False): lib.UrbanPointSource(
+        stock_target=stock_target,
+        max_retries=100,
+        retries_per_point=30,
+        max_dist_km=25,
+        usa_chance=0.1
+    ),
+    ("URBAN", True): lib.UrbanPointSource(
+        stock_target=stock_target,
+        max_retries=100,
+        retries_per_point=30,
+        max_dist_km=25,
+        usa_chance=1.0
+    )
+}
+
+
+def restock_all():
+  """
+  Restock all the configured sources above
+  """
+  for src in sources.values():
+      threading.Thread(target=src.restock).start()