]> git.rm.cloudns.org Git - xonotic/xonstat.git/commitdiff
Add a separate JSON view for summary stats.
authorAnt Zucaro <azucaro@gmail.com>
Wed, 14 Mar 2018 02:10:48 +0000 (22:10 -0400)
committerAnt Zucaro <azucaro@gmail.com>
Wed, 14 Mar 2018 02:10:48 +0000 (22:10 -0400)
xonstat/__init__.py
xonstat/views/__init__.py
xonstat/views/main.py

index 5055661bd34b82f339f24291f203a348dd91bb2c..877e4b88be04a502834e8c5613a0408b1d5a352e 100644 (file)
@@ -44,6 +44,10 @@ def main(global_config, **settings):
     config.add_route("main_index", "/")
     config.add_view(main_index, route_name="main_index", renderer="main_index.mako")
 
+    config.add_route("summary_stats_json", "/summary")
+    config.add_view(view=summary_stats_json, route_name="summary_stats_json", renderer="json",
+                    accept="application/json")
+
     # MAIN SUBMISSION ROUTE
     config.add_route("submit_stats", "stats/submit")
     config.add_view(submit_stats, route_name="submit_stats", renderer="submit_stats.mako")
index 830eabd47f59842c5941d03cc39b0edf28a88f56..2b726d0d979bb32a35f5933c5d122008bdbc5cf2 100644 (file)
@@ -26,7 +26,7 @@ from xonstat.views.search import search_json
 from xonstat.views.exceptions   import notfound
 
 from xonstat.views.main   import main_index, top_players_index, top_servers_index
-from xonstat.views.main   import top_maps_index
+from xonstat.views.main   import top_maps_index, summary_stats_json
 
 from xonstat.views.admin   import forbidden, login, merge
 
index c002f51e75027ed4c6f817c97e5ccfe2c2e08630..9f3f11d607f3715880d172c3a995a09186e11599 100644 (file)
@@ -2,6 +2,7 @@ import logging
 from datetime import datetime, timedelta
 
 from beaker.cache import cache_region
+from sqlalchemy import text
 from xonstat.models import DBSession, PlayerRank, ActivePlayer, ActiveServer, ActiveMap
 from xonstat.views.helpers import RecentGame, recent_games_q
 
@@ -9,29 +10,51 @@ log = logging.getLogger(__name__)
 
 
 @cache_region('hourly_term')
-def get_summary_stats(scope="all"):
+def summary_stats_data(scope="all"):
     """
-    Gets the following aggregate statistics according to the provided scope:
+    Gets the summary stats (number of active players, the game type, and the number of games)
+    for a given scope.
 
-        - the number of active players
-        - the number of games per game type
-
-    Scope can be "all" or "day".
-
-    The fetched information is summarized into a string which is passed
-    directly to the template.
+    :param scope: The scope to fetch from the table. May be "all" or "day".
+    :return: list[tuple]
     """
     if scope not in ["all", "day"]:
         scope = "all"
 
+    sql = text("SELECT num_players, game_type_cd, num_games, create_dt refresh_dt "
+               "FROM summary_stats_mv "
+               "WHERE scope = :scope "
+               "ORDER BY sort_order ")
+
+    try:
+        ss = DBSession.query("num_players", "game_type_cd", "num_games", "refresh_dt").\
+                from_statement(sql).params(scope=scope).all()
+
+        return ss
+    except Exception as e:
+        log.error(e)
+        return []
+
+
+def summary_stats_json(request):
+    ss = summary_stats_data(request.params.get("scope", "all"))
+    return [
+        {
+            "players": r.num_players,
+            "game_type_cd": r.game_type_cd,
+            "games": r.num_games,
+            "refresh_dt": r.refresh_dt.isoformat(),
+        }
+        for r in ss]
+
+
+@cache_region('hourly_term')
+def summary_stats_string(scope="all"):
+    """
+    Assembles the summary stats data into a readable line for direct inclusion in templates.
+    """
     try:
-        ss = DBSession.query("num_players", "game_type_cd", "num_games").\
-                from_statement(
-                        "SELECT num_players, game_type_cd, num_games "
-                        "FROM summary_stats_mv "
-                        "WHERE scope = :scope "
-                        "ORDER BY sort_order "
-                ).params(scope=scope).all()
+        ss = summary_stats_data(scope)
 
         i = 1
         total_games = 0
@@ -169,8 +192,8 @@ def _main_index_data(request):
     recent_games_count = 20
 
     # summary statistics for the tagline
-    stat_line = get_summary_stats("all")
-    day_stat_line = get_summary_stats("day")
+    stat_line = summary_stats_string("all")
+    day_stat_line = summary_stats_string("day")
 
 
     # the three top ranks tables