refactored views in preparation for json output
authornyov <nyov@nexnode.net>
Wed, 16 May 2012 22:55:51 +0000 (00:55 +0200)
committernyov <nyov@nexnode.net>
Wed, 16 May 2012 23:09:54 +0000 (01:09 +0200)
xonstat/__init__.py
xonstat/views/__init__.py
xonstat/views/game.py
xonstat/views/main.py
xonstat/views/map.py
xonstat/views/player.py
xonstat/views/search.py
xonstat/views/server.py

index d3a6d98a5ca8e1e07d1fe185658c06bbe583acd2..5a164ef4c7dbcefbf0847ca1cd7ef26e2a412a23 100755 (executable)
@@ -42,7 +42,7 @@ def main(global_config, **settings):
         renderer="player_info.mako")
 
     config.add_route("player_accuracy", "/player/{id:\d+}/accuracy")
-    config.add_view(player_accuracy, route_name="player_accuracy",
+    config.add_view(player_accuracy_json, route_name="player_accuracy",
         renderer="json")
 
     # GAME ROUTES
index 5592f2cb4642027f90ea1d72530bd3c598db5b42..7f1de44581d1ff7e771bc48003f49006d6abefa4 100755 (executable)
@@ -1,8 +1,8 @@
 from xonstat.views.submission import stats_submit\r
 from xonstat.views.player import player_index, player_info, player_game_index\r
-from xonstat.views.player import player_accuracy\r
+from xonstat.views.player import player_accuracy_json\r
 from xonstat.views.game import game_index, game_info, rank_index\r
 from xonstat.views.map import map_info, map_index, map_index_json\r
 from xonstat.views.server import server_info, server_game_index, server_index\r
-from xonstat.views.search import *\r
+from xonstat.views.search import search_q, search\r
 from xonstat.views.main import main_index\r
index 336acbae99297951b7db1f78915bac19e75a9832..44e929e5a8ff0126b1c0276b091864293a373d4e 100755 (executable)
@@ -11,12 +11,7 @@ from xonstat.util import page_url
 log = logging.getLogger(__name__)\r
 \r
 \r
-def game_index(request):\r
-    """\r
-    Provides a list of current games, with the associated game stats.\r
-    These games are ordered by game_id, with the most current ones first.\r
-    Paginated.\r
-    """\r
+def _game_index_data(request):\r
     if request.params.has_key('page'):\r
         current_page = request.params['page']\r
     else:\r
@@ -40,10 +35,16 @@ def game_index(request):
             'pgstats':pgstats}\r
 \r
 \r
-def game_info(request):\r
+def game_index(request):\r
     """\r
-    List the game stats (scoreboard) for a particular game. Paginated.\r
+    Provides a list of current games, with the associated game stats.\r
+    These games are ordered by game_id, with the most current ones first.\r
+    Paginated.\r
     """\r
+    return _game_index_data(request)\r
+\r
+\r
+def _game_info_data(request):\r
     game_id = request.matchdict['id']\r
     try:\r
         notfound = False\r
@@ -95,10 +96,14 @@ def game_info(request):
             }\r
 \r
 \r
-def rank_index(request):\r
+def game_info(request):\r
     """\r
-    Provide a list of gametype ranks, paginated.\r
+    List the game stats (scoreboard) for a particular game. Paginated.\r
     """\r
+    return _game_info_data(request)\r
+\r
+\r
+def _rank_index_data(request):\r
     if request.params.has_key('page'):\r
         current_page = request.params['page']\r
     else:\r
@@ -119,3 +124,10 @@ def rank_index(request):
             'ranks':ranks,\r
             'game_type_cd':game_type_cd,\r
            }\r
+\r
+\r
+def rank_index(request):\r
+    """\r
+    Provide a list of gametype ranks, paginated.\r
+    """\r
+    return _rank_index_data(request)\r
index 51624d503d07d6c8c87df1b3b22b23e632fdc61c..5a8bad4be70aead377dbf2d384af734b831fb4e1 100755 (executable)
@@ -8,7 +8,7 @@ from xonstat.util import *
 
 log = logging.getLogger(__name__)
 
-def main_index(request):
+def _main_index_data(request):
     try: 
         leaderboard_lifetime = int(
                 request.registry.settings['xonstat.leaderboard_lifetime'])
@@ -23,40 +23,31 @@ def main_index(request):
             PlayerRank.elo).\
             filter(PlayerRank.game_type_cd=='duel').\
             order_by(PlayerRank.rank).\
-            limit(10).all()
+            limit(leaderboard_count).all()
 
     duel_ranks = [(player_id, html_colors(nick), elo) \
             for (player_id, nick, elo) in duel_ranks]
 
-    for i in range(leaderboard_count-len(duel_ranks)):
-        duel_ranks.append(('-', '-', '-'))
-
     # top ranked CTF-ers
     ctf_ranks = DBSession.query(PlayerRank.player_id, PlayerRank.nick, 
             PlayerRank.elo).\
             filter(PlayerRank.game_type_cd=='ctf').\
             order_by(PlayerRank.rank).\
-            limit(10).all()
+            limit(leaderboard_count).all()
 
     ctf_ranks = [(player_id, html_colors(nick), elo) \
             for (player_id, nick, elo) in ctf_ranks]
 
-    for i in range(leaderboard_count-len(ctf_ranks)):
-        ctf_ranks.append(('-', '-', '-'))
-
     # top ranked DM-ers
     dm_ranks = DBSession.query(PlayerRank.player_id, PlayerRank.nick, 
             PlayerRank.elo).\
             filter(PlayerRank.game_type_cd=='dm').\
             order_by(PlayerRank.rank).\
-            limit(10).all()
+            limit(leaderboard_count).all()
 
     dm_ranks = [(player_id, html_colors(nick), elo) \
             for (player_id, nick, elo) in dm_ranks]
 
-    for i in range(leaderboard_count-len(dm_ranks)):
-        dm_ranks.append(('-', '-', '-'))
-
     right_now = datetime.utcnow()
     back_then = datetime.utcnow() - timedelta(days=leaderboard_lifetime)
 
@@ -68,14 +59,11 @@ def main_index(request):
             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(10).all()
+            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]
 
-    for i in range(leaderboard_count-len(top_players)):
-        top_players.append(('-', '-', '-'))
-
     # top servers by number of total players played
     top_servers = DBSession.query(Server.server_id, Server.name, 
             func.count()).\
@@ -83,10 +71,7 @@ def main_index(request):
             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(10).all()
-
-    for i in range(leaderboard_count-len(top_servers)):
-        top_servers.append(('-', '-', '-'))
+            group_by(Server.name).limit(leaderboard_count).all()
 
     # top maps by total times played
     top_maps = DBSession.query(Game.map_id, Map.name, 
@@ -95,10 +80,7 @@ def main_index(request):
             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(10).all()
-
-    for i in range(leaderboard_count-len(top_maps)):
-        top_maps.append(('-', '-', '-'))
+            group_by(Map.name).limit(leaderboard_count).all()
 
     # recent games played in descending order
     recent_games = DBSession.query(Game, Server, Map, PlayerGameStat).\
@@ -109,9 +91,6 @@ def main_index(request):
             filter(expr.between(Game.create_dt, back_then, right_now)).\
             order_by(expr.desc(Game.start_dt)).limit(recent_games_count).all()
 
-    for i in range(recent_games_count-len(recent_games)):
-        recent_games.append(('-', '-', '-', '-'))
-
     return {'top_players':top_players,
             'top_servers':top_servers,
             'top_maps':top_maps,
@@ -120,3 +99,37 @@ def main_index(request):
             'ctf_ranks':ctf_ranks,
             'dm_ranks':dm_ranks,
             }
+
+
+def main_index(request):
+    """
+    Display the main page information.
+    """
+    mainindex_data =  _main_index_data(request)
+
+    # FIXME: code clone, should get these from _main_index_data
+    leaderboard_count = 10
+    recent_games_count = 20
+
+    for i in range(leaderboard_count-len(mainindex_data['duel_ranks'])):
+        mainindex_data['duel_ranks'].append(('-', '-', '-'))
+
+    for i in range(leaderboard_count-len(mainindex_data['ctf_ranks'])):
+        mainindex_data['ctf_ranks'].append(('-', '-', '-'))
+
+    for i in range(leaderboard_count-len(mainindex_data['dm_ranks'])):
+        mainindex_data['dm_ranks'].append(('-', '-', '-'))
+
+    for i in range(leaderboard_count-len(mainindex_data['top_players'])):
+        mainindex_data['top_players'].append(('-', '-', '-'))
+
+    for i in range(leaderboard_count-len(mainindex_data['top_servers'])):
+        mainindex_data['top_servers'].append(('-', '-', '-'))
+
+    for i in range(leaderboard_count-len(mainindex_data['top_maps'])):
+        mainindex_data['top_maps'].append(('-', '-', '-'))
+
+    for i in range(recent_games_count-len(mainindex_data['recent_games'])):
+        mainindex_data['recent_games'].append(('-', '-', '-', '-'))
+
+    return mainindex_data
index 6b947eb61ce08c8a2abfc719b0c3dd742373ce22..86e70ad3aff62bd68e060d7286649a78a4a212b8 100755 (executable)
@@ -11,9 +11,6 @@ from xonstat.util import page_url
 log = logging.getLogger(__name__)\r
 \r
 def _map_index_data(request):\r
-    """\r
-    Provides a list of all the current maps. \r
-    """\r
     if request.params.has_key('page'):\r
         current_page = request.params['page']\r
     else:\r
@@ -49,10 +46,7 @@ def map_index_json(request):
     return maps\r
 \r
 \r
-def map_info(request):\r
-    """\r
-    List the information stored about a given map. \r
-    """\r
+def _map_info_data(request):\r
     map_id = request.matchdict['id']\r
 \r
     try: \r
@@ -76,10 +70,6 @@ def map_info(request):
             filter(PlayerGameStat.rank==1).\\r
             order_by(expr.desc(Game.start_dt)).all()[0:recent_games_count]\r
 \r
-        for i in range(recent_games_count-len(recent_games)):\r
-            recent_games.append(('-', '-', '-', '-'))\r
-\r
-\r
         # top players by score\r
         top_scorers = DBSession.query(Player.player_id, Player.nick,\r
                 func.sum(PlayerGameStat.score)).\\r
@@ -91,14 +81,11 @@ def map_info(request):
                         (datetime.utcnow() - timedelta(days=leaderboard_lifetime))).\\r
                 order_by(expr.desc(func.sum(PlayerGameStat.score))).\\r
                 group_by(Player.nick).\\r
-                group_by(Player.player_id).all()[0:10]\r
+                group_by(Player.player_id).all()[0:leaderboard_count]\r
 \r
         top_scorers = [(player_id, html_colors(nick), score) \\r
                 for (player_id, nick, score) in top_scorers]\r
 \r
-        for i in range(leaderboard_count-len(top_scorers)):\r
-            top_scorers.append(('-', '-', '-'))\r
-\r
         # top players by playing time\r
         top_players = DBSession.query(Player.player_id, Player.nick, \r
                 func.sum(PlayerGameStat.alivetime)).\\r
@@ -110,14 +97,11 @@ def map_info(request):
                         (datetime.utcnow() - timedelta(days=leaderboard_lifetime))).\\r
                 order_by(expr.desc(func.sum(PlayerGameStat.alivetime))).\\r
                 group_by(Player.nick).\\r
-                group_by(Player.player_id).all()[0:10]\r
+                group_by(Player.player_id).all()[0:leaderboard_count]\r
 \r
         top_players = [(player_id, html_colors(nick), score) \\r
                 for (player_id, nick, score) in top_players]\r
 \r
-        for i in range(leaderboard_count-len(top_players)):\r
-            top_players.append(('-', '-', '-'))\r
-\r
         # top servers using/playing this map\r
         top_servers = DBSession.query(Server.server_id, Server.name, \r
                 func.count(Game.game_id)).\\r
@@ -127,10 +111,7 @@ def map_info(request):
                         (datetime.utcnow() - timedelta(days=leaderboard_lifetime))).\\r
                 order_by(expr.desc(func.count(Game.game_id))).\\r
                 group_by(Server.name).\\r
-                group_by(Server.server_id).all()[0:10]\r
-\r
-        for i in range(leaderboard_count-len(top_servers)):\r
-            top_servers.append(('-', '-', '-'))\r
+                group_by(Server.server_id).all()[0:leaderboard_count]\r
 \r
     except Exception as e:\r
         gmap = None\r
@@ -140,3 +121,28 @@ def map_info(request):
             'top_players':top_players,\r
             'top_servers':top_servers,\r
             }\r
+\r
+\r
+def map_info(request):\r
+    """\r
+    List the information stored about a given map.\r
+    """\r
+    mapinfo_data =  _map_info_data(request)\r
+\r
+    # FIXME: code clone, should get these from _map_info_data\r
+    leaderboard_count = 10\r
+    recent_games_count = 20\r
+\r
+    for i in range(recent_games_count-len(mapinfo_data['recent_games'])):\r
+        mapinfo_data['recent_games'].append(('-', '-', '-', '-'))\r
+\r
+    for i in range(leaderboard_count-len(mapinfo_data['top_scorers'])):\r
+        mapinfo_data['top_scorers'].append(('-', '-', '-'))\r
+\r
+    for i in range(leaderboard_count-len(mapinfo_data['top_players'])):\r
+        mapinfo_data['top_players'].append(('-', '-', '-'))\r
+\r
+    for i in range(leaderboard_count-len(mapinfo_data['top_servers'])):\r
+        mapinfo_data['top_servers'].append(('-', '-', '-'))\r
+\r
+    return mapinfo_data\r
index 3a35319d55db6f05ea34cb631a6521541567368c..af1337d3fc44a64b3eb796472437268b30703784 100755 (executable)
@@ -15,10 +15,7 @@ from xonstat.util import page_url
 log = logging.getLogger(__name__)\r
 \r
 \r
-def player_index(request):\r
-    """\r
-    Provides a list of all the current players. \r
-    """\r
+def _player_index_data(request):\r
     if request.params.has_key('page'):\r
         current_page = request.params['page']\r
     else:\r
@@ -41,7 +38,14 @@ def player_index(request):
            }\r
 \r
 \r
-def get_games_played(player_id):\r
+def player_index(request):\r
+    """\r
+    Provides a list of all the current players.\r
+    """\r
+    return _player_index_data(request)\r
+\r
+\r
+def _get_games_played(player_id):\r
     """\r
     Provides a breakdown by gametype of the games played by player_id.\r
 \r
@@ -64,7 +68,7 @@ def get_games_played(player_id):
 \r
 # TODO: should probably factor the above function into this one such that\r
 # total_stats['ctf_games'] is the count of CTF games and so on...\r
-def get_total_stats(player_id):\r
+def _get_total_stats(player_id):\r
     """\r
     Provides aggregated stats by player_id.\r
 \r
@@ -143,10 +147,7 @@ def get_accuracy_stats(player_id, weapon_cd, games):
     return (avg, accs)\r
 \r
 \r
-def player_info(request):\r
-    """\r
-    Provides detailed information on a specific player\r
-    """\r
+def _player_info_data(request):\r
     player_id = int(request.matchdict['id'])\r
     if player_id <= 2:\r
         player_id = -1;\r
@@ -156,10 +157,10 @@ def player_info(request):
                 filter(Player.active_ind == True).one()\r
 \r
         # games played, alivetime, wins, kills, deaths\r
-        total_stats = get_total_stats(player.player_id)\r
+        total_stats = _get_total_stats(player.player_id)\r
 \r
         # games breakdown - N games played (X ctf, Y dm) etc\r
-        (total_games, games_breakdown) = get_games_played(player.player_id)\r
+        (total_games, games_breakdown) = _get_games_played(player.player_id)\r
 \r
 \r
         # friendly display of elo information and preliminary status\r
@@ -216,12 +217,14 @@ def player_info(request):
             }\r
 \r
 \r
-def player_game_index(request):\r
+def player_info(request):\r
     """\r
-    Provides an index of the games in which a particular\r
-    player was involved. This is ordered by game_id, with\r
-    the most recent game_ids first. Paginated.\r
+    Provides detailed information on a specific player\r
     """\r
+    return _player_info_data(request)\r
+\r
+\r
+def _player_game_index_data(request):\r
     player_id = request.matchdict['player_id']\r
 \r
     if request.params.has_key('page'):\r
@@ -254,15 +257,17 @@ def player_game_index(request):
             'games':games,\r
             'pgstats':pgstats}\r
 \r
-def player_accuracy(request):\r
-    """\r
-    Provides a JSON response representing the accuracy for the given weapon.\r
 \r
-    Parameters:\r
-       weapon = which weapon to display accuracy for. Valid values are 'nex',\r
-                'shotgun', 'uzi', and 'minstanex'.\r
-       games = over how many games to display accuracy. Can be up to 50.\r
+def player_game_index(request):\r
     """\r
+    Provides an index of the games in which a particular\r
+    player was involved. This is ordered by game_id, with\r
+    the most recent game_ids first. Paginated.\r
+    """\r
+    return _player_game_index_data(request)\r
+\r
+\r
+def _player_accuracy_data(request):\r
     player_id = request.matchdict['id']\r
     allowed_weapons = ['nex', 'rifle', 'shotgun', 'uzi', 'minstanex']\r
     weapon_cd = 'nex'\r
@@ -298,3 +303,14 @@ def player_accuracy(request):
             'accs':accs\r
             }\r
 \r
+\r
+def player_accuracy_json(request):\r
+    """\r
+    Provides a JSON response representing the accuracy for the given weapon.\r
+\r
+    Parameters:\r
+       weapon = which weapon to display accuracy for. Valid values are 'nex',\r
+                'shotgun', 'uzi', and 'minstanex'.\r
+       games = over how many games to display accuracy. Can be up to 50.\r
+    """\r
+    return _player_accuracy_data(request)\r
index 8ce22004995080f869df9aa4b5219c193cefecad..2a791f331004b81131e302ab7b704a5a626ac03f 100755 (executable)
@@ -74,7 +74,7 @@ def search_q(nick=None, server_name=None, map_name=None, create_dt=None,
 
     return (result_type, q)
 
-def search(request):
+def _search_data(request):
     fs = None
     nick = None
     server_name = None
@@ -145,3 +145,7 @@ def search(request):
             'results':results,
             'query':query,
             }
+
+
+def search(request):
+    return _search_data(request)
index daa9885948f15f15fd27f23f5c58885fe73c3456..575d1b0d5ff102c7603d18caf1fd0948df17e227 100755 (executable)
@@ -11,10 +11,7 @@ from xonstat.util import page_url, html_colors
 \r
 log = logging.getLogger(__name__)\r
 \r
-def server_index(request):\r
-    """\r
-    Provides a list of all the current servers. \r
-    """\r
+def _server_index_data(request):\r
     if request.params.has_key('page'):\r
         current_page = request.params['page']\r
     else:\r
@@ -33,10 +30,14 @@ def server_index(request):
     return {'servers':servers, }\r
 \r
 \r
-def server_info(request):\r
+def server_index(request):\r
     """\r
-    List the stored information about a given server.\r
+    Provides a list of all the current servers.\r
     """\r
+    return _server_index_data(request)\r
+\r
+\r
+def _server_info_data(request):\r
     server_id = request.matchdict['id']\r
 \r
     try: \r
@@ -60,10 +61,7 @@ def server_info(request):
                     (datetime.utcnow() - timedelta(days=leaderboard_lifetime))).\\r
                 order_by(expr.desc(func.count())).\\r
                 group_by(Game.map_id).\\r
-                group_by(Map.name).all()[0:10]\r
-\r
-        for i in range(leaderboard_count-len(top_maps)):\r
-            top_maps.append(('-', '-', '-'))\r
+                group_by(Map.name).all()[0:leaderboard_count]\r
 \r
         # top players by score\r
         top_scorers = DBSession.query(Player.player_id, Player.nick, \r
@@ -76,14 +74,11 @@ def server_info(request):
                         (datetime.utcnow() - timedelta(days=leaderboard_lifetime))).\\r
                 order_by(expr.desc(func.sum(PlayerGameStat.score))).\\r
                 group_by(Player.nick).\\r
-                group_by(Player.player_id).all()[0:10]\r
+                group_by(Player.player_id).all()[0:leaderboard_count]\r
 \r
         top_scorers = [(player_id, html_colors(nick), score) \\r
                 for (player_id, nick, score) in top_scorers]\r
 \r
-        for i in range(leaderboard_count-len(top_scorers)):\r
-            top_scorers.append(('-', '-', '-'))\r
-\r
         # top players by playing time\r
         top_players = DBSession.query(Player.player_id, Player.nick, \r
                 func.sum(PlayerGameStat.alivetime)).\\r
@@ -95,14 +90,11 @@ def server_info(request):
                         (datetime.utcnow() - timedelta(days=leaderboard_lifetime))).\\r
                 order_by(expr.desc(func.sum(PlayerGameStat.alivetime))).\\r
                 group_by(Player.nick).\\r
-                group_by(Player.player_id).all()[0:10]\r
+                group_by(Player.player_id).all()[0:leaderboard_count]\r
 \r
         top_players = [(player_id, html_colors(nick), score) \\r
                 for (player_id, nick, score) in top_players]\r
 \r
-        for i in range(leaderboard_count-len(top_players)):\r
-            top_players.append(('-', '-', '-'))\r
-\r
         # recent games played in descending order\r
         recent_games = DBSession.query(Game, Server, Map, PlayerGameStat).\\r
             filter(Game.server_id==Server.server_id).\\r
@@ -112,9 +104,6 @@ def server_info(request):
             filter(Server.server_id==server.server_id).\\r
             order_by(expr.desc(Game.start_dt)).all()[0:recent_games_count]\r
 \r
-        for i in range(recent_games_count-len(recent_games)):\r
-            recent_games.append(('-', '-', '-', '-'))\r
-\r
     except Exception as e:\r
         server = None\r
         recent_games = None\r
@@ -128,10 +117,32 @@ def server_info(request):
             }\r
 \r
 \r
-def server_game_index(request):\r
+def server_info(request):\r
     """\r
-    List the games played on a given server. Paginated.\r
+    List the stored information about a given server.\r
     """\r
+    serverinfo_data =  _server_info_data(request)\r
+\r
+    # FIXME: code clone, should get these from _server_info_data\r
+    leaderboard_count = 10\r
+    recent_games_count = 20\r
+\r
+    for i in range(leaderboard_count-len(serverinfo_data['top_maps'])):\r
+        serverinfo_data['top_maps'].append(('-', '-', '-'))\r
+\r
+    for i in range(leaderboard_count-len(serverinfo_data['top_scorers'])):\r
+        serverinfo_data['top_scorers'].append(('-', '-', '-'))\r
+\r
+    for i in range(leaderboard_count-len(serverinfo_data['top_players'])):\r
+        serverinfo_data['top_players'].append(('-', '-', '-'))\r
+\r
+    for i in range(recent_games_count-len(serverinfo_data['recent_games'])):\r
+        serverinfo_data['recent_games'].append(('-', '-', '-', '-'))\r
+\r
+    return serverinfo_data\r
+\r
+\r
+def _server_game_index_data(request):\r
     server_id = request.matchdict['server_id']\r
     current_page = request.matchdict['page']\r
 \r
@@ -152,3 +163,10 @@ def server_game_index(request):
 \r
     return {'games':games,\r
             'server':server}\r
+\r
+\r
+def server_game_index(request):\r
+    """\r
+    List the games played on a given server. Paginated.\r
+    """\r
+    return _server_game_index_data(request)\r