From ff605d0e0bac10b82fb256d2f3bdc936874ab31f Mon Sep 17 00:00:00 2001 From: Ant Zucaro Date: Sun, 27 Oct 2013 16:32:45 -0400 Subject: [PATCH] Add a view for top maps by times played. --- xonstat/__init__.py | 3 ++ .../templates/top_maps_by_times_played.mako | 50 +++++++++++++++++++ xonstat/views/__init__.py | 2 +- xonstat/views/main.py | 43 ++++++++++++---- 4 files changed, 88 insertions(+), 10 deletions(-) create mode 100644 xonstat/templates/top_maps_by_times_played.mako diff --git a/xonstat/__init__.py b/xonstat/__init__.py index c066e2c..b2314b3 100644 --- a/xonstat/__init__.py +++ b/xonstat/__init__.py @@ -87,6 +87,9 @@ def main(global_config, **settings): 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") + config.add_route("top_maps_by_times_played", "/topmaps") + config.add_view(top_maps_by_times_played, route_name="top_maps_by_times_played", renderer="top_maps_by_times_played.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_maps_by_times_played.mako b/xonstat/templates/top_maps_by_times_played.mako new file mode 100644 index 0000000..8de413e --- /dev/null +++ b/xonstat/templates/top_maps_by_times_played.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('maps')} + + +<%block name="title"> +Active Maps Index + + +% if not top_maps: +

Sorry, no maps yet. Get playing!

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

*figures are from the past 7 days

+
+% endif + +${navlinks("top_maps_by_times_played", top_maps.page, top_maps.last_page)} + + diff --git a/xonstat/views/__init__.py b/xonstat/views/__init__.py index 04f7370..4e65f2c 100644 --- a/xonstat/views/__init__.py +++ b/xonstat/views/__init__.py @@ -27,4 +27,4 @@ from xonstat.views.search import search_json from xonstat.views.exceptions import notfound from xonstat.views.main import main_index, top_players_by_time, top_servers_by_players -from xonstat.views.main import top_servers_by_players +from xonstat.views.main import top_servers_by_players, top_maps_by_times_played diff --git a/xonstat/views/main.py b/xonstat/views/main.py index 0320a59..76eeb07 100644 --- a/xonstat/views/main.py +++ b/xonstat/views/main.py @@ -173,27 +173,39 @@ def get_top_servers_by_players(cutoff_days): return top_servers -@cache_region('hourly_term') -def top_maps_by_times_played(cutoff_days): +def top_maps_by_times_played_q(cutoff_days): """ - The top maps by the amount of times it was played during a date range. + 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. """ - # 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_maps = DBSession.query(Game.map_id, Map.name, + 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).limit(count).all() + group_by(Map.name) + + return top_maps_q + + +@cache_region('hourly_term') +def get_top_maps_by_times_played(cutoff_days): + """ + The top maps by the amount of times it was played during a date range. + + Games older than cutoff_days days old are ignored. + """ + # how many to retrieve + count = 10 + + top_maps = top_maps_by_times_played_q(cutoff_days).limit(count).all() return top_maps @@ -231,7 +243,7 @@ def _main_index_data(request): top_servers = get_top_servers_by_players(leaderboard_lifetime) # top maps by total times played - top_maps = top_maps_by_times_played(leaderboard_lifetime) + top_maps = get_top_maps_by_times_played(leaderboard_lifetime) # recent games played in descending order rgs = recent_games_q(cutoff=back_then).limit(recent_games_count).all() @@ -302,3 +314,16 @@ def top_servers_by_players(request): top_servers = Page(top_servers_q, current_page, items_per_page=25, url=page_url) return {'top_servers':top_servers} + + +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)) + + top_maps_q = top_maps_by_times_played_q(cutoff_days) + + top_maps = Page(top_maps_q, current_page, items_per_page=25, url=page_url) + + return {'top_maps':top_maps} -- 2.39.2