Commit 2cf11c02 authored by liadomide's avatar liadomide

TVB-2598, TVB-2588 review pagination and potentially queries with many results

parent 6e08fe7a
......@@ -33,7 +33,7 @@ from sqlalchemy.exc import SQLAlchemyError
from sqlalchemy.orm.exc import NoResultFound
from tvb.core.entities.model.model_burst import BurstConfiguration
from tvb.core.entities.model.simulator.simulator import SimulatorIndex
from tvb.core.entities.storage.root_dao import RootDAO
from tvb.core.entities.storage.root_dao import RootDAO, DEFAULT_PAGE_SIZE
class BurstDAO(RootDAO):
......@@ -41,7 +41,7 @@ class BurstDAO(RootDAO):
DAO layer for Burst entities.
"""
def get_bursts_for_project(self, project_id, page_start=0, page_size=None, count=False):
def get_bursts_for_project(self, project_id, page_start=0, page_size=DEFAULT_PAGE_SIZE, count=False):
"""Get latest 50 BurstConfiguration entities for the current project"""
try:
bursts = self.session.query(BurstConfiguration
......
......@@ -46,7 +46,7 @@ from tvb.adapters.datatypes.db.surface import SurfaceIndex
from tvb.core.entities.model.model_datatype import *
from tvb.core.entities.model.model_operation import Operation, AlgorithmCategory, Algorithm, OperationGroup
from tvb.core.entities.model.model_burst import BurstConfiguration
from tvb.core.entities.storage.root_dao import RootDAO
from tvb.core.entities.storage.root_dao import RootDAO, DEFAULT_PAGE_SIZE
from tvb.core.neotraits.db import Base
......@@ -214,7 +214,7 @@ class DatatypeDAO(RootDAO):
return count
def get_all_datatypes(self, page_start=0, page_size=20):
def get_all_datatypes(self, page_start=0, page_size=DEFAULT_PAGE_SIZE):
"""
Return a list with all of the datatypes currently available in TVB. Is used by
the file storage update manager to upgrade from version to the next.
......
......@@ -40,12 +40,9 @@ from sqlalchemy import func as func
from sqlalchemy.exc import SQLAlchemyError
from sqlalchemy.orm.exc import NoResultFound
from sqlalchemy.sql.expression import case as case_, desc
from tvb.core.entities.model.model_datatype import DataType
from tvb.core.entities.model.model_operation import Operation, ResultFigure, Algorithm, AlgorithmCategory, \
OperationGroup, STATUS_FINISHED, STATUS_STARTED, STATUS_ERROR, STATUS_CANCELED, STATUS_PENDING, \
OperationProcessIdentifier
from tvb.core.entities.storage.root_dao import RootDAO
from tvb.core.entities.model.model_operation import *
from tvb.core.entities.storage.root_dao import RootDAO, DEFAULT_PAGE_SIZE
class OperationDAO(RootDAO):
......@@ -194,7 +191,8 @@ class OperationDAO(RootDAO):
return expected_hdd_size or 0
def get_filtered_operations(self, project_id, filter_chain, page_start=0, page_size=20, is_count=False):
def get_filtered_operations(self, project_id, filter_chain, page_start=0,
page_size=DEFAULT_PAGE_SIZE, is_count=False):
"""
:param project_id: current project ID
:param filter_chain: instance of FilterChain
......
......@@ -43,7 +43,7 @@ from tvb.basic.profile import TvbProfile
from tvb.core.entities.model.model_datatype import DataType, Links
from tvb.core.entities.model.model_operation import Operation
from tvb.core.entities.model.model_project import User, ROLE_ADMINISTRATOR, Project, User_to_Project
from tvb.core.entities.storage.root_dao import RootDAO
from tvb.core.entities.storage.root_dao import RootDAO, DEFAULT_PAGE_SIZE
class CaseDAO(RootDAO):
......@@ -93,7 +93,7 @@ class CaseDAO(RootDAO):
admins = self.session.query(User).filter_by(role=ROLE_ADMINISTRATOR).all()
return admins
def get_all_users(self, different_name=' ', page_start=0, page_size=20, is_count=False):
def get_all_users(self, different_name=' ', page_start=0, page_size=DEFAULT_PAGE_SIZE, is_count=False):
"""Retrieve all USERS in DB, except current user and system user."""
try:
sys_name = TvbProfile.current.web.admin.SYSTEM_USER_NAME
......@@ -103,10 +103,7 @@ class CaseDAO(RootDAO):
if is_count:
result = query.count()
else:
query = query.order_by(User.username)
if page_start is not None and page_size is not None:
query = query.offset(max(page_start, 0)).limit(max(page_size, 0))
result = query.all()
result = query.order_by(User.username).offset(max(page_start, 0)).limit(max(page_size, 0)).all()
return result
except NoResultFound:
self.logger.warning("No users found. Maybe database is empty.")
......@@ -219,7 +216,7 @@ class CaseDAO(RootDAO):
number = self.session.query(Project).filter_by(name=name).count()
return number
def get_all_projects(self, page_start=0, page_size=20, is_count=False):
def get_all_projects(self, page_start=0, page_size=DEFAULT_PAGE_SIZE, is_count=False):
"""
Retrieve all Project entities currently in the system.
WARNING: use this wisely, as it might easily overflow the system.
......@@ -231,7 +228,7 @@ class CaseDAO(RootDAO):
result = query.offset(max(page_start, 0)).limit(max(page_size, 0)).all()
return result
def get_projects_for_user(self, user_id, page_start=0, page_size=20, is_count=False):
def get_projects_for_user(self, user_id, page_start=0, page_size=DEFAULT_PAGE_SIZE, is_count=False):
"""
Return all projects a given user can access (administrator or not).
"""
......@@ -242,13 +239,11 @@ class CaseDAO(RootDAO):
User_to_Project.fk_user == user_id))
).filter(
or_(User.id == user_id, User_to_Project.fk_user == user_id)
).order_by(desc(Project.id))
).order_by(desc(Project.id))
if is_count:
result = query.count()
else:
if page_start is not None and page_size is not None:
query = query.offset(max(page_start, 0)).limit(max(page_size, 0))
result = query.all()
result = query.offset(max(page_start, 0)).limit(max(page_size, 0)).all()
[project.administrator.username for project in result]
return result
......
......@@ -43,6 +43,8 @@ from tvb.core.entities.storage.session_maker import SESSION_META_CLASS
from tvb.config import SIMULATION_DATATYPE_CLASS
DEFAULT_PAGE_SIZE = 200
class RootDAO(object, metaclass=SESSION_META_CLASS):
"""
......
......@@ -284,11 +284,11 @@ class ProjectService:
return available_projects, pages_no
@staticmethod
def retrieve_all_user_projects(user_id):
def retrieve_all_user_projects(user_id, page_start=0, page_size= PROJECTS_PAGE_SIZE):
"""
Return a list with all projects visible for current user, without pagination.
"""
return dao.get_projects_for_user(user_id, page_start=None, page_size=None)
return dao.get_projects_for_user(user_id, page_start=page_start, page_size=page_size)
@staticmethod
def get_linkable_projects_for_user(user_id, data_id):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment