From 2084da724335c3f02a627d201082a5c675760d16 Mon Sep 17 00:00:00 2001 From: John Davis Date: Thu, 10 Aug 2023 18:47:43 -0400 Subject: [PATCH] Fix SA2.0 ORM usage in galaxy.webapps/reports [partially] --- .../webapps/reports/controllers/jobs.py | 12 ++-- .../webapps/reports/controllers/system.py | 69 ++++++++++--------- .../webapps/reports/controllers/users.py | 19 ++--- .../webapps/reports/controllers/workflows.py | 2 +- 4 files changed, 54 insertions(+), 48 deletions(-) diff --git a/lib/galaxy/webapps/reports/controllers/jobs.py b/lib/galaxy/webapps/reports/controllers/jobs.py index ede8c033c843..1c6e9a9c2ec6 100644 --- a/lib/galaxy/webapps/reports/controllers/jobs.py +++ b/lib/galaxy/webapps/reports/controllers/jobs.py @@ -18,12 +18,15 @@ and_, not_, or_, + select, ) from galaxy import ( model, util, ) +from galaxy.model import Job +from galaxy.model.repositories import get_user_by_email from galaxy.web.legacy_framework import grids from galaxy.webapps.base.controller import ( BaseUIController, @@ -1288,7 +1291,7 @@ def tool_per_month(self, trans, **kwd): @web.expose def job_info(self, trans, **kwd): message = "" - job = trans.sa_session.query(model.Job).get(trans.security.decode_id(kwd.get("id", ""))) + job = trans.sa_session.get(Job, trans.security.decode_id(kwd.get("id", ""))) return trans.fill_template("/webapps/reports/job_info.mako", job=job, message=message) @@ -1296,7 +1299,7 @@ def job_info(self, trans, **kwd): def get_job(trans, id): - return trans.sa_session.query(trans.model.Job).get(trans.security.decode_id(id)) + return trans.sa_session.get(Job, trans.security.decode_id(id)) def get_monitor_id(trans, monitor_email): @@ -1304,9 +1307,8 @@ def get_monitor_id(trans, monitor_email): A convenience method to obtain the monitor job id. """ monitor_user_id = None - monitor_row = ( - trans.sa_session.query(trans.model.User.id).filter(trans.model.User.table.c.email == monitor_email).first() - ) + stmt = select(trans.model.User.id).filter(trans.model.User.email == monitor_email).limit(1) + monitor_row = trans.sa_session.scalars(stmt).first() if monitor_row is not None: monitor_user_id = monitor_row[0] return monitor_user_id diff --git a/lib/galaxy/webapps/reports/controllers/system.py b/lib/galaxy/webapps/reports/controllers/system.py index e26446c75d2d..0b18f18f2d5d 100644 --- a/lib/galaxy/webapps/reports/controllers/system.py +++ b/lib/galaxy/webapps/reports/controllers/system.py @@ -11,6 +11,7 @@ desc, false, null, + select, true, ) from sqlalchemy.orm import joinedload @@ -74,13 +75,14 @@ def userless_histories(self, trans, **kwd): cutoff_time = datetime.utcnow() - timedelta(days=userless_histories_days) history_count = 0 dataset_count = 0 - for history in trans.sa_session.query(model.History).filter( + stmt = select(model.History).filter( and_( - model.History.table.c.user_id == null(), - model.History.table.c.deleted == true(), + model.History.user_id == null(), + model.History.deleted == true(), model.History.update_time < cutoff_time, ) - ): + ) + for history in trans.sa_session.scalars(stmt).all(): for dataset in history.datasets: if not dataset.deleted: dataset_count += 1 @@ -106,18 +108,21 @@ def deleted_histories(self, trans, **kwd): history_count = 0 dataset_count = 0 disk_space = 0 - histories = ( - trans.sa_session.query(model.History) + + stmt = ( + select(model.History) .filter( and_( - model.History.table.c.deleted == true(), - model.History.table.c.purged == false(), + model.History.deleted == true(), + model.History.purged == false(), model.History.update_time < cutoff_time, ) ) .options(joinedload(model.History.datasets)) + .unique() ) + histories = trans.sa_session.scalars(stmt).all() for history in histories: for hda in history.datasets: if not hda.dataset.purged: @@ -144,13 +149,14 @@ def deleted_datasets(self, trans, **kwd): cutoff_time = datetime.utcnow() - timedelta(days=deleted_datasets_days) dataset_count = 0 disk_space = 0 - for dataset in trans.sa_session.query(model.Dataset).filter( + stmt = select(model.Dataset).filter( and_( - model.Dataset.table.c.deleted == true(), - model.Dataset.table.c.purged == false(), - model.Dataset.table.c.update_time < cutoff_time, + model.Dataset.deleted == true(), + model.Dataset.purged == false(), + model.Dataset.update_time < cutoff_time, ) - ): + ) + for dataset in trans.sa_session.scalars(stmt).all(): dataset_count += 1 try: disk_space += dataset.file_size @@ -167,28 +173,22 @@ def deleted_datasets(self, trans, **kwd): @web.expose def dataset_info(self, trans, **kwd): message = "" - dataset = trans.sa_session.query(model.Dataset).get(trans.security.decode_id(kwd.get("id", ""))) + dataset = trans.sa_session.get(model.Dataset, trans.security.decode_id(kwd.get("id", ""))) # Get all associated hdas and lddas that use the same disk file. - associated_hdas = ( - trans.sa_session.query(trans.model.HistoryDatasetAssociation) - .filter( - and_( - trans.model.HistoryDatasetAssociation.deleted == false(), - trans.model.HistoryDatasetAssociation.dataset_id == dataset.id, - ) + stmt = select(trans.model.HistoryDatasetAssociation).filter( + and_( + trans.model.HistoryDatasetAssociation.deleted == false(), + trans.model.HistoryDatasetAssociation.dataset_id == dataset.id, ) - .all() ) - associated_lddas = ( - trans.sa_session.query(trans.model.LibraryDatasetDatasetAssociation) - .filter( - and_( - trans.model.LibraryDatasetDatasetAssociation.deleted == false(), - trans.model.LibraryDatasetDatasetAssociation.dataset_id == dataset.id, - ) + associated_hdas = trans.sa_session.scalars(stmt).all() + stmt = select(trans.model.LibraryDatasetDatasetAssociation).filter( + and_( + trans.model.LibraryDatasetDatasetAssociation.deleted == false(), + trans.model.LibraryDatasetDatasetAssociation.dataset_id == dataset.id, ) - .all() ) + associated_lddas = trans.sa_session.scalars(stmt).all() return trans.fill_template( "/webapps/reports/dataset_info.mako", dataset=dataset, @@ -208,11 +208,12 @@ def disk_usage(self, trans, **kwd): disk_usage = self.get_disk_usage(file_path) min_file_size = 2**32 # 4 Gb file_size_str = nice_size(min_file_size) - datasets = ( - trans.sa_session.query(model.Dataset) - .filter(and_(model.Dataset.table.c.purged == false(), model.Dataset.table.c.file_size > min_file_size)) - .order_by(desc(model.Dataset.table.c.file_size)) + stmt = ( + select(model.Dataset) + .filter(and_(model.Dataset.purged == false(), model.Dataset.file_size > min_file_size)) + .order_by(desc(model.Dataset.file_size)) ) + datasets = trans.sa_session.scalars(stmt).all() return file_path, disk_usage, datasets, file_size_str diff --git a/lib/galaxy/webapps/reports/controllers/users.py b/lib/galaxy/webapps/reports/controllers/users.py index bfc9cb233f83..8798ea32c6a0 100644 --- a/lib/galaxy/webapps/reports/controllers/users.py +++ b/lib/galaxy/webapps/reports/controllers/users.py @@ -9,7 +9,11 @@ import sqlalchemy as sa from markupsafe import escape -from sqlalchemy import false +from sqlalchemy import ( + false, + func, + select, +) import galaxy.model from galaxy import util @@ -27,7 +31,8 @@ class Users(BaseUIController, ReportQueryBuilder): @web.expose def registered_users(self, trans, **kwd): message = escape(util.restore_text(kwd.get("message", ""))) - num_users = trans.sa_session.query(galaxy.model.User).count() + stmt = select(func.count(galaxy.model.User.id)) + num_users = trans.sa_session.scalar(stmt) return trans.fill_template("/webapps/reports/registered_users.mako", num_users=num_users, message=message) @web.expose @@ -165,11 +170,9 @@ def name_to_num(name): days_not_logged_in = 0 cutoff_time = datetime.utcnow() - timedelta(days=int(days_not_logged_in)) users = [] - for user in ( - trans.sa_session.query(galaxy.model.User) - .filter(galaxy.model.User.table.c.deleted == false()) - .order_by(galaxy.model.User.table.c.email) - ): + + stmt = select(galaxy.model.User).filter(galaxy.model.User.deleted == false()).order_by(galaxy.model.User.email) + for user in trans.sa_session.scalars(stmt).all(): current_galaxy_session = user.current_galaxy_session if current_galaxy_session: last_galaxy_session = current_galaxy_session @@ -204,7 +207,7 @@ def user_disk_usage(self, trans, **kwd): user_cutoff = int(kwd.get("user_cutoff", 60)) # disk_usage isn't indexed - all_users = trans.sa_session.query(galaxy.model.User).all() + all_users = trans.sa_session.scalars(select(galaxy.model.User)).all() sort_attrgetter = operator.attrgetter(str(sort_id)) users = sorted(all_users, key=lambda x: sort_attrgetter(x) or 0, reverse=_order) if user_cutoff: diff --git a/lib/galaxy/webapps/reports/controllers/workflows.py b/lib/galaxy/webapps/reports/controllers/workflows.py index 999a25fe9897..2291b7a499f2 100644 --- a/lib/galaxy/webapps/reports/controllers/workflows.py +++ b/lib/galaxy/webapps/reports/controllers/workflows.py @@ -491,4 +491,4 @@ def per_workflow(self, trans, **kwd): def get_workflow(trans, id): - return trans.sa_session.query(trans.model.Workflow).get(trans.security.decode_id(id)) + return trans.sa_session.get(model.Workflow, trans.security.decode_id(id))