]> 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 1e937bf6a24ef6dda519c641b82f716f6ba35982..3f76f1e56168ce52fe037db2c843656b61baf963 100644 (file)
@@ -1,44 +1,70 @@
 import logging
-import sqlalchemy as sa
-import sqlalchemy.sql.functions as func
-import sqlalchemy.sql.expression as expr
-from beaker.cache import cache_regions, cache_region
-from collections import namedtuple
 from datetime import datetime, timedelta
-from pyramid.response import Response
-from xonstat.models import *
-from xonstat.util import *
-from xonstat.views.helpers import RecentGame, recent_games_q
-from webhelpers.paginate import Page
 
+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
 
 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
@@ -82,8 +108,8 @@ def get_summary_stats(scope="all"):
             )
 
     except Exception as e:
-        raise e
         stat_line = None
+        raise e
 
     return stat_line
 
@@ -111,125 +137,58 @@ def get_ranks(game_type_cd):
     return ranks
 
 
-def top_players_by_time_q(cutoff_days):
-    """
-    Query for the top players by the amount of time played during a date range.
-
-    Games older than cutoff_days days old are ignored.
-    """
-
-    # only games played during this range are considered
-    right_now = datetime.utcnow()
-    cutoff_dt = right_now - timedelta(days=cutoff_days)
-
-    top_players_q = DBSession.query(Player.player_id, Player.nick,
-            func.sum(PlayerGameStat.alivetime)).\
-            filter(Player.player_id == PlayerGameStat.player_id).\
-            filter(Player.player_id > 2).\
-            filter(expr.between(PlayerGameStat.create_dt, cutoff_dt, right_now)).\
-            order_by(expr.desc(func.sum(PlayerGameStat.alivetime))).\
-            group_by(Player.nick).\
-            group_by(Player.player_id)
-
-    return top_players_q
-
-
 @cache_region('hourly_term')
-def get_top_players_by_time(cutoff_days):
+def get_top_players_by_time(limit=None, start=None):
     """
     The top players by the amount of time played during a date range.
-
-    Games older than cutoff_days days old are ignored.
     """
-    # how many to retrieve
-    count = 10
-
-    # only games played during this range are considered
-    right_now = datetime.utcnow()
-    cutoff_dt = right_now - timedelta(days=cutoff_days)
-
-    top_players_q = top_players_by_time_q(cutoff_days)
-
-    top_players = top_players_q.limit(count).all()
-
-    top_players = [(player_id, html_colors(nick), score) \
-            for (player_id, nick, score) in top_players]
-
-    return top_players
-
+    q = DBSession.query(ActivePlayer)
 
-def top_servers_by_players_q(cutoff_days):
-    """
-    Query to get the top servers by the amount of players active
-    during a date range.
+    if start is not None:
+        q = q.filter(ActivePlayer.sort_order >= start)
 
-    Games older than cutoff_days days old are ignored.
-    """
-    # only games played during this range are considered
-    right_now = datetime.utcnow()
-    cutoff_dt = right_now - timedelta(days=cutoff_days)
+    q = q.order_by(ActivePlayer.sort_order)
 
-    top_servers_q = DBSession.query(Server.server_id, Server.name,
-        func.count()).\
-        filter(Game.server_id==Server.server_id).\
-        filter(expr.between(Game.create_dt, cutoff_dt, right_now)).\
-        order_by(expr.desc(func.count(Game.game_id))).\
-        group_by(Server.server_id).\
-        group_by(Server.name)
+    if limit is not None:
+        q = q.limit(limit)
 
-    return top_servers_q
+    return q.all()
 
 
 @cache_region('hourly_term')
-def get_top_servers_by_players(cutoff_days):
+def get_top_servers_by_play_time(limit=None, start=None):
     """
-    The top servers by the amount of players active during a date range.
-
-    Games older than cutoff_days days old are ignored.
+    The top servers by the cumulative amount of time played on them during a given interval.
     """
-    # how many to retrieve
-    count = 10
-
-    top_servers = top_servers_by_players_q(cutoff_days).limit(count).all()
+    q = DBSession.query(ActiveServer)
 
-    return top_servers
+    if start is not None:
+        q = q.filter(ActiveServer.sort_order >= start)
 
+    q = q.order_by(ActiveServer.sort_order)
 
-def top_maps_by_times_played_q(cutoff_days):
-    """
-    Query to retrieve the top maps by the amount of times it was played
-    during a date range.
-
-    Games older than cutoff_days days old are ignored.
-    """
-    # only games played during this range are considered
-    right_now = datetime.utcnow()
-    cutoff_dt = right_now - timedelta(days=cutoff_days)
-
-    top_maps_q = DBSession.query(Game.map_id, Map.name,
-            func.count()).\
-            filter(Map.map_id==Game.map_id).\
-            filter(expr.between(Game.create_dt, cutoff_dt, right_now)).\
-            order_by(expr.desc(func.count())).\
-            group_by(Game.map_id).\
-            group_by(Map.name)
+    if limit is not None:
+        q = q.limit(limit)
 
-    return top_maps_q
+    return q.all()
 
 
 @cache_region('hourly_term')
-def get_top_maps_by_times_played(cutoff_days):
+def get_top_maps_by_games(limit=None, start=None):
     """
-    The top maps by the amount of times it was played during a date range.
-
-    Games older than cutoff_days days old are ignored.
+    The top maps by the number of games played during a date range.
     """
-    # how many to retrieve
-    count = 10
+    q = DBSession.query(ActiveMap)
+
+    if start is not None:
+        q = q.filter(ActiveMap.sort_order >= start)
 
-    top_maps = top_maps_by_times_played_q(cutoff_days).limit(count).all()
+    q = q.order_by(ActiveMap.sort_order)
 
-    return top_maps
+    if limit is not None:
+        q = q.limit(limit)
+
+    return q.all()
 
 
 def _main_index_data(request):
@@ -243,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
@@ -258,13 +217,13 @@ def _main_index_data(request):
     back_then = datetime.utcnow() - timedelta(days=leaderboard_lifetime)
 
     # top players by playing time
-    top_players = get_top_players_by_time(leaderboard_lifetime)
+    top_players = get_top_players_by_time(10)
 
-    # top servers by number of total players played
-    top_servers = get_top_servers_by_players(leaderboard_lifetime)
+    # top servers by number of games
+    top_servers = get_top_servers_by_play_time(10)
 
     # top maps by total times played
-    top_maps = get_top_maps_by_times_played(leaderboard_lifetime)
+    top_maps = get_top_maps_by_games(10)
 
     # recent games played in descending order
     rgs = recent_games_q(cutoff=back_then).limit(recent_games_count).all()
@@ -284,22 +243,7 @@ def main_index(request):
     """
     Display the main page information.
     """
-    mainindex_data =  _main_index_data(request)
-
-    # FIXME: code clone, should get these from _main_index_data
-    leaderboard_count = 10
-    recent_games_count = 20
-
-    for i in range(leaderboard_count-len(mainindex_data['top_players'])):
-        mainindex_data['top_players'].append(('-', '-', '-'))
-
-    for i in range(leaderboard_count-len(mainindex_data['top_servers'])):
-        mainindex_data['top_servers'].append(('-', '-', '-'))
-
-    for i in range(leaderboard_count-len(mainindex_data['top_maps'])):
-        mainindex_data['top_maps'].append(('-', '-', '-'))
-
-    return mainindex_data
+    return _main_index_data(request)
 
 
 def main_index_json(request):
@@ -309,43 +253,61 @@ def main_index_json(request):
     return [{'status':'not implemented'}]
 
 
-def top_players_by_time(request):
-    current_page = request.params.get('page', 1)
-
-    cutoff_days = int(request.registry.settings.\
-        get('xonstat.leaderboard_lifetime', 30))
-
-    top_players_q = top_players_by_time_q(cutoff_days)
-
-    top_players = Page(top_players_q, current_page, items_per_page=25, url=page_url)
-
-    top_players.items = [(player_id, html_colors(nick), score) \
-            for (player_id, nick, score) in top_players.items]
+def top_players_index(request):
+    try:
+        start = int(request.params.get('start', None))
+    except:
+        start = None
 
-    return {'top_players':top_players}
+    top_players = get_top_players_by_time(20, start)
 
+    # building a query string
+    query = {}
+    if len(top_players) > 1:
+        query['start'] = top_players[-1].sort_order + 1
 
-def top_servers_by_players(request):
-    current_page = request.params.get('page', 1)
+    return {
+            'top_players':top_players,
+            'query':query,
+            'start':start,
+            }
 
-    cutoff_days = int(request.registry.settings.\
-        get('xonstat.leaderboard_lifetime', 30))
 
-    top_servers_q = top_servers_by_players_q(cutoff_days)
+def top_servers_index(request):
+    try:
+        start = int(request.params.get('start', None))
+    except:
+        start = None
 
-    top_servers = Page(top_servers_q, current_page, items_per_page=25, url=page_url)
+    top_servers = get_top_servers_by_play_time(20, start)
 
-    return {'top_servers':top_servers}
+    # building a query string
+    query = {}
+    if len(top_servers) > 1:
+        query['start'] = top_servers[-1].sort_order + 1
 
+    return {
+            'top_servers':top_servers,
+            'query':query,
+            'start':start,
+            }
 
-def top_maps_by_times_played(request):
-    current_page = request.params.get('page', 1)
 
-    cutoff_days = int(request.registry.settings.\
-        get('xonstat.leaderboard_lifetime', 30))
+def top_maps_index(request):
+    try:
+        start = int(request.params.get('start', None))
+    except:
+        start = None
 
-    top_maps_q = top_maps_by_times_played_q(cutoff_days)
+    top_maps = get_top_maps_by_games(20, start)
 
-    top_maps = Page(top_maps_q, current_page, items_per_page=25, url=page_url)
+    # building a query string
+    query = {}
+    if len(top_maps) > 1:
+        query['start'] = top_maps[-1].sort_order + 1
 
-    return {'top_maps':top_maps}
+    return {
+            'top_maps':top_maps,
+            'query':query,
+            'start':start,
+            }