]> de.git.xonotic.org Git - xonotic/xonstat.git/blobdiff - xonstat/views/main.py
Add a view for top maps by times played.
[xonotic/xonstat.git] / xonstat / views / main.py
index 0320a599a9aa6554b45e00c29c6bc6345f85a6c8..76eeb073aac229a113e1c15e5e0bce9b72ea7607 100644 (file)
@@ -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}