From c1b360424f8e8031d614792131636c8f5c85d20f Mon Sep 17 00:00:00 2001 From: Ant Zucaro Date: Sun, 27 Oct 2013 12:24:49 -0400 Subject: [PATCH] Add a paginated view for top servers by # of players. --- xonstat/__init__.py | 3 ++ xonstat/templates/top_servers_by_players.mako | 50 +++++++++++++++++++ xonstat/views/__init__.py | 3 +- xonstat/views/main.py | 43 ++++++++++++---- 4 files changed, 89 insertions(+), 10 deletions(-) create mode 100644 xonstat/templates/top_servers_by_players.mako diff --git a/xonstat/__init__.py b/xonstat/__init__.py index 6f21dd2..c066e2c 100644 --- a/xonstat/__init__.py +++ b/xonstat/__init__.py @@ -84,6 +84,9 @@ def main(global_config, **settings): config.add_route("top_players_by_time", "/topactive") config.add_view(top_players_by_time, route_name="top_players_by_time", renderer="top_players_by_time.mako") + config.add_route("top_servers_by_players", "/topservers") + config.add_view(top_servers_by_players, route_name="top_servers_by_players", renderer="top_servers_by_players.mako") + # GAME ROUTES config.add_route("game_info", "/game/{id:\d+}") config.add_view(game_info, route_name="game_info", renderer="game_info.mako") diff --git a/xonstat/templates/top_servers_by_players.mako b/xonstat/templates/top_servers_by_players.mako new file mode 100644 index 0000000..72765f1 --- /dev/null +++ b/xonstat/templates/top_servers_by_players.mako @@ -0,0 +1,50 @@ +<%inherit file="base.mako"/> +<%namespace name="nav" file="nav.mako" /> +<%namespace file="navlinks.mako" import="navlinks" /> + +<%block name="navigation"> +${nav.nav('servers')} + + +<%block name="title"> +Active Servers Index + + +% if not top_servers: +

Sorry, no servers yet. Get playing!

+ +% else: +##### ACTIVE SERVERS ##### +
+ + + + + + + + + + ##### this is to get around the actual row_number/rank of the server not being in the actual query + <% i = 1 + (top_servers.page-1) * 25%> + % for (server_id, name, count) in top_servers.items: + + + % if server_id != '-': + + % else: + + % endif + + + <% i = i+1 %> + % endfor + +
#ServerGames
${i}${name}${name}${count}
+

*figures are from the past 7 days

+
+% endif + +${navlinks("top_servers_by_players", top_servers.page, top_servers.last_page)} + + diff --git a/xonstat/views/__init__.py b/xonstat/views/__init__.py index 7eabd5b..04f7370 100644 --- a/xonstat/views/__init__.py +++ b/xonstat/views/__init__.py @@ -26,4 +26,5 @@ from xonstat.views.search import search_json from xonstat.views.exceptions import notfound -from xonstat.views.main import main_index, top_players_by_time +from xonstat.views.main import main_index, top_players_by_time, top_servers_by_players +from xonstat.views.main import top_servers_by_players diff --git a/xonstat/views/main.py b/xonstat/views/main.py index 465b747..0320a59 100644 --- a/xonstat/views/main.py +++ b/xonstat/views/main.py @@ -136,27 +136,39 @@ def get_top_players_by_time(cutoff_days): return top_players -@cache_region('hourly_term') -def top_servers_by_players(cutoff_days): +def top_servers_by_players_q(cutoff_days): """ - The top servers by the amount of players active during a date range. + Query to get the top servers by the amount of players active + 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_servers = DBSession.query(Server.server_id, Server.name, + 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).limit(count).all() + group_by(Server.name) + + return top_servers_q + + +@cache_region('hourly_term') +def get_top_servers_by_players(cutoff_days): + """ + The top servers by the amount of players active during a date range. + + Games older than cutoff_days days old are ignored. + """ + # how many to retrieve + count = 10 + + top_servers = top_servers_by_players_q(cutoff_days).limit(count).all() return top_servers @@ -216,7 +228,7 @@ def _main_index_data(request): top_players = get_top_players_by_time(leaderboard_lifetime) # top servers by number of total players played - top_servers = top_servers_by_players(leaderboard_lifetime) + top_servers = get_top_servers_by_players(leaderboard_lifetime) # top maps by total times played top_maps = top_maps_by_times_played(leaderboard_lifetime) @@ -277,3 +289,16 @@ def top_players_by_time(request): for (player_id, nick, score) in top_players.items] return {'top_players':top_players} + + +def top_servers_by_players(request): + current_page = request.params.get('page', 1) + + cutoff_days = int(request.registry.settings.\ + get('xonstat.leaderboard_lifetime', 30)) + + top_servers_q = top_servers_by_players_q(cutoff_days) + + top_servers = Page(top_servers_q, current_page, items_per_page=25, url=page_url) + + return {'top_servers':top_servers} -- 2.39.2