]> de.git.xonotic.org Git - xonotic/xonstat.git/blobdiff - xonstat/views/map.py
Use pagination on the maps page instead of a full select count(*).
[xonotic/xonstat.git] / xonstat / views / map.py
index 506dcd97369f84723bf16a27c97138461666006f..ccc1529652295899a241e66efe62150cc5e3e8b1 100644 (file)
@@ -1,49 +1,69 @@
 import logging
-import sqlalchemy.sql.functions as func
-import sqlalchemy.sql.expression as expr
 from collections import namedtuple
 from datetime import datetime, timedelta
+
+import sqlalchemy.sql.expression as expr
+import sqlalchemy.sql.functions as func
+from pyramid.httpexceptions import HTTPNotFound
 from webhelpers.paginate import Page
-from xonstat.models import *
+from xonstat.models import DBSession, Server, Map, Game, PlayerGameStat, Player, PlayerCaptime
+from xonstat.models.map import MapCapTime
 from xonstat.util import page_url, html_colors
 from xonstat.views.helpers import RecentGame, recent_games_q
 
 log = logging.getLogger(__name__)
 
-def _map_index_data(request):
-    if request.params.has_key('page'):
-        current_page = request.params['page']
-    else:
-        current_page = 1
+# Defaults
+INDEX_COUNT = 20
 
-    try:
-        map_q = DBSession.query(Map).\
-                order_by(Map.map_id.desc())
 
-        maps = Page(map_q, current_page, items_per_page=25, url=page_url)
+class MapIndex(object):
+    """Returns a list of maps."""
 
-    except Exception as e:
-        maps = None
+    def __init__(self, request):
+        """Common parameter parsing."""
+        self.request = request
+        self.page = request.params.get("page", 1)
+        self.last = request.params.get("last", None)
 
-    return {'maps':maps, }
+        # all views share this data, so we'll pre-calculate
+        self.maps = self.map_index()
 
+    def map_index(self):
+        """Returns the raw data shared by all renderers."""
+        try:
+            map_q = DBSession.query(Map)
 
-def map_index(request):
-    """
-    Provides a list of all the current maps.
-    """
-    return _map_index_data(request)
+            if self.last:
+                map_q = map_q.filter(Map.map_id < self.last)
 
+            map_q = map_q.order_by(Map.map_id.desc()).limit(INDEX_COUNT)
+            maps = map_q.all()
 
-def map_index_json(request):
-    """
-    Provides a JSON-serialized list of all the current maps.
-    """
-    view_data = _map_index_data(request)
+        except Exception as e:
+            log.debug(e)
+            raise HTTPNotFound
+
+        return maps
 
-    maps = [m.to_dict() for m in view_data['maps']]
+    def html(self):
+        """For rendering this data using something HTML-based."""
+        # build the query string
+        query = {}
+        if len(self.maps) > 1:
+            query['last'] = self.maps[-1].map_id
 
-    return maps
+        return {
+            'maps': self.maps,
+            'query': query,
+        }
+
+    def json(self):
+        """For rendering this data using JSON."""
+        return {
+            'maps': [m.to_dict() for m in self.maps],
+            'last': self.last,
+        }
 
 
 def _map_info_data(request):
@@ -186,7 +206,7 @@ def map_captimes_data(request):
                 order_by(expr.asc(PlayerCaptime.fastest_cap))
 
     except Exception as e:
-        raise pyramid.httpexceptions.HTTPNotFound
+        raise httpexceptions.HTTPNotFound
 
     map_captimes = Page(mct_q, current_page, items_per_page=20, url=page_url)