]> de.git.xonotic.org Git - xonotic/xonstat.git/blobdiff - xonstat/views/main.py
Remove more refs to sqlahelper.
[xonotic/xonstat.git] / xonstat / views / main.py
index c842a554421565b605d7f0e37f8dfd2c9c4d980d..3f76f1e56168ce52fe037db2c843656b61baf963 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,61 @@ 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
+    :param scope: The scope to fetch from the table. May be "all" or "day".
+    :return: list[tuple]
+    """
+    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 ")
 
-    Scope can be "all" or "day".
+    try:
+        ss = DBSession.query("num_players", "game_type_cd", "num_games", "refresh_dt").\
+                from_statement(sql).params(scope=scope).all()
 
-    The fetched information is summarized into a string which is passed
-    directly to the template.
-    """
+        return ss
+    except Exception as e:
+        log.error(e)
+        return []
+
+
+def summary_stats_json(request):
+    scope = request.params.get("scope", "all")
     if scope not in ["all", "day"]:
         scope = "all"
 
+    ss = summary_stats_data(scope)
+
+    # default values
+    players = 0
+    last_refreshed = "unknown"
+    games = []
+
+    if len(ss) > 0:
+        players = ss[0].num_players
+        last_refreshed = ss[0].refresh_dt.isoformat()
+        games = [{"game_type_cd": r.game_type_cd, "num_games": r.num_games} for r in ss]
+
+    return {
+        "players": players,
+        "scope": scope,
+        "last_refreshed": last_refreshed,
+        "games": games,
+    }
+
+
+@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
@@ -123,9 +156,9 @@ def get_top_players_by_time(limit=None, start=None):
 
 
 @cache_region('hourly_term')
-def get_top_servers_by_games(limit=None, start=None):
+def get_top_servers_by_play_time(limit=None, start=None):
     """
-    The top servers by the number of games played during a date range.
+    The top servers by the cumulative amount of time played on them during a given interval.
     """
     q = DBSession.query(ActiveServer)
 
@@ -169,8 +202,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
@@ -187,7 +220,7 @@ def _main_index_data(request):
     top_players = get_top_players_by_time(10)
 
     # top servers by number of games
-    top_servers = get_top_servers_by_games(10)
+    top_servers = get_top_servers_by_play_time(10)
 
     # top maps by total times played
     top_maps = get_top_maps_by_games(10)
@@ -246,7 +279,7 @@ def top_servers_index(request):
     except:
         start = None
 
-    top_servers = get_top_servers_by_games(20, start)
+    top_servers = get_top_servers_by_play_time(20, start)
 
     # building a query string
     query = {}