]> git.rm.cloudns.org Git - xonotic/xonstat.git/commitdiff
Add a paginated view for top servers by # of players.
authorAnt Zucaro <azucaro@gmail.com>
Sun, 27 Oct 2013 16:24:49 +0000 (12:24 -0400)
committerAnt Zucaro <azucaro@gmail.com>
Sun, 27 Oct 2013 16:24:49 +0000 (12:24 -0400)
xonstat/__init__.py
xonstat/templates/top_servers_by_players.mako [new file with mode: 0644]
xonstat/views/__init__.py
xonstat/views/main.py

index 6f21dd2d24a12221dcc04ecb9414322a3acbffd1..c066e2cb0567ac634e205643a6c6f4f72b15ab23 100644 (file)
@@ -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 (file)
index 0000000..72765f1
--- /dev/null
@@ -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>
+
+<%block name="title">
+Active Servers Index
+</%block>
+
+% if not top_servers:
+<h2>Sorry, no servers yet. Get playing!</h2>
+
+% else:
+##### ACTIVE SERVERS #####
+  <div class="span6 offset3">
+    <table class="table table-hover table-condensed">
+      <thead>
+        <tr>
+          <th style="width:40px;">#</th>
+          <th style="width:180px;">Server</th>
+          <th style="width:60px;">Games</th>
+        </tr>
+      </thead>
+      <tbody>
+      ##### 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:
+        <tr>
+          <td>${i}</td>
+          % if server_id != '-':
+          <td class="nostretch" style="max-width:180px;"><a href="${request.route_url('server_info', id=server_id)}" title="Go to the server info page for ${name}">${name}</a></td>
+          % else:
+          <td class="nostretch" style="max-width:180px;">${name}</td>
+          % endif
+          <td>${count}</td>
+        </tr>
+        <% i = i+1 %>
+      % endfor
+      </tbody>
+    </table>
+    <p class="note">*figures are from the past 7 days</p>
+  </div> <!-- /span4 -->
+% endif
+
+${navlinks("top_servers_by_players", top_servers.page, top_servers.last_page)}
+  </div> <!-- /span4 -->
+</div> <!-- /row -->
index 7eabd5b5540abfc103be6fd196a93eb0703b4af4..04f73709195ca75969ff892a6242c08d58da15b4 100644 (file)
@@ -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
index 465b7478a75168d06772846dbe0e514ff22badde..0320a599a9aa6554b45e00c29c6bc6345f85a6c8 100644 (file)
@@ -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}