]> git.rm.cloudns.org Git - xonotic/xonstat.git/commitdiff
Refactor the "top <blah>" tables on the front page, cache them.
authorAnt Zucaro <azucaro@gmail.com>
Sat, 22 Dec 2012 21:57:46 +0000 (16:57 -0500)
committerAnt Zucaro <azucaro@gmail.com>
Sat, 22 Dec 2012 21:57:46 +0000 (16:57 -0500)
The main_index view was getting a little long, so I factored out
the "top <blah>" data queries into separate functions. I cached
those functions under an hourly term while I was at it.

xonstat/views/main.py

index f8fb9ea032e63118bd31f4f8ea0f7adbb4c5e956..885110f8cb0b164bf566cd2cdaaddd298128b69f 100644 (file)
@@ -83,6 +83,86 @@ def get_ranks(game_type_cd):
 
     return ranks
 
+
+@cache_region('hourly_term')
+def top_players_by_time(cutoff_days):
+    """
+    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 = 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).limit(count).all()
+
+    top_players = [(player_id, html_colors(nick), score) \
+            for (player_id, nick, score) in top_players]
+
+    return top_players
+
+
+@cache_region('hourly_term')
+def 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
+
+    # 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, 
+        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()
+
+    return top_servers
+
+
+@cache_region('hourly_term')
+def 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
+
+    # 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,
+            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()
+
+    return top_maps
+
+
 def _main_index_data(request):
     try:
         leaderboard_lifetime = int(
@@ -110,35 +190,13 @@ def _main_index_data(request):
     back_then = datetime.utcnow() - timedelta(days=leaderboard_lifetime)
 
     # top players by playing time
-    top_players = 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, back_then, right_now)).\
-            order_by(expr.desc(func.sum(PlayerGameStat.alivetime))).\
-            group_by(Player.nick).\
-            group_by(Player.player_id).limit(leaderboard_count).all()
-
-    top_players = [(player_id, html_colors(nick), score) \
-            for (player_id, nick, score) in top_players]
+    top_players = top_players_by_time(leaderboard_lifetime)
 
     # top servers by number of total players played
-    top_servers = DBSession.query(Server.server_id, Server.name, 
-            func.count()).\
-            filter(Game.server_id==Server.server_id).\
-            filter(expr.between(Game.create_dt, back_then, right_now)).\
-            order_by(expr.desc(func.count(Game.game_id))).\
-            group_by(Server.server_id).\
-            group_by(Server.name).limit(leaderboard_count).all()
+    top_servers = top_servers_by_players(leaderboard_lifetime)
 
     # top maps by total times played
-    top_maps = DBSession.query(Game.map_id, Map.name, 
-            func.count()).\
-            filter(Map.map_id==Game.map_id).\
-            filter(expr.between(Game.create_dt, back_then, right_now)).\
-            order_by(expr.desc(func.count())).\
-            group_by(Game.map_id).\
-            group_by(Map.name).limit(leaderboard_count).all()
+    top_maps = 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()