Skip to content

Commit

Permalink
Fix SA2.0 ORM usage in galaxy.webapps/reports [partially]
Browse files Browse the repository at this point in the history
  • Loading branch information
jdavcs committed Sep 25, 2023
1 parent 0e4df32 commit 2084da7
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 48 deletions.
12 changes: 7 additions & 5 deletions lib/galaxy/webapps/reports/controllers/jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -1288,25 +1291,24 @@ 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)


# ---- Utility methods -------------------------------------------------------


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):
"""
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
69 changes: 35 additions & 34 deletions lib/galaxy/webapps/reports/controllers/system.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
desc,
false,
null,
select,
true,
)
from sqlalchemy.orm import joinedload
Expand Down Expand Up @@ -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
Expand All @@ -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:
Expand All @@ -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
Expand All @@ -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,
Expand All @@ -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


Expand Down
19 changes: 11 additions & 8 deletions lib/galaxy/webapps/reports/controllers/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion lib/galaxy/webapps/reports/controllers/workflows.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))

0 comments on commit 2084da7

Please sign in to comment.