Skip to content

Commit 6e23e9d

Browse files
committed
Add shim to ensure that manager itself is not reinstantiated, align with existing manager pattern
1 parent 0834a34 commit 6e23e9d

3 files changed

Lines changed: 43 additions & 18 deletions

File tree

lib/galaxy/managers/history_graph.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
TruncationInfo,
4545
)
4646
from galaxy.security.idencoding import IdEncodingHelper
47+
from galaxy.structured_app import MinimalManagerApp
4748
from galaxy.tool_util.toolbox import AbstractToolBox
4849

4950
log = logging.getLogger(__name__)
@@ -54,6 +55,35 @@
5455
MAX_LIMIT = 1000
5556

5657

58+
class HistoryGraphManager:
59+
def __init__(self, app: MinimalManagerApp):
60+
self.app = app
61+
62+
def build(
63+
self,
64+
sa_session: Session,
65+
history_id: int,
66+
limit: int = 500,
67+
include_deleted: bool = False,
68+
seed: Optional[str] = None,
69+
direction: Literal["backward", "forward", "both"] = "both",
70+
depth: int = 5,
71+
seed_scope_hid: Optional[int] = None,
72+
) -> HistoryGraphResponse:
73+
return HistoryGraphBuilder(
74+
sa_session=sa_session,
75+
security=self.app.security,
76+
toolbox=self.app.toolbox,
77+
history_id=history_id,
78+
limit=limit,
79+
include_deleted=include_deleted,
80+
seed=seed,
81+
direction=direction,
82+
depth=depth,
83+
seed_scope_hid=seed_scope_hid,
84+
).build()
85+
86+
5787
class HistoryGraphBuilder:
5888
"""Builds a provenance graph from selected history items.
5989

lib/galaxy/webapps/galaxy/services/histories.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
HistoryManager,
4141
HistorySerializer,
4242
)
43-
from galaxy.managers.history_graph import HistoryGraphBuilder
43+
from galaxy.managers.history_graph import HistoryGraphManager
4444
from galaxy.managers.users import UserManager
4545
from galaxy.model import (
4646
HistoryDatasetAssociation,
@@ -131,6 +131,7 @@ def __init__(
131131
deserializer: HistoryDeserializer,
132132
citations_manager: CitationsManager,
133133
history_export_manager: HistoryExportManager,
134+
history_graph_manager: HistoryGraphManager,
134135
filters: HistoryFilters,
135136
short_term_storage_allocator: ShortTermStorageAllocator,
136137
notification_service: NotificationService,
@@ -142,6 +143,7 @@ def __init__(
142143
self.deserializer = deserializer
143144
self.citations_manager = citations_manager
144145
self.history_export_manager = history_export_manager
146+
self.history_graph_manager = history_graph_manager
145147
self.filters = filters
146148
self.shareable_service = ShareableHistoryService(self.manager, self.serializer, notification_service)
147149
self.short_term_storage_allocator = short_term_storage_allocator
@@ -397,19 +399,16 @@ def graph(
397399
) -> HistoryGraphResponse:
398400
history = self.manager.get_accessible(history_id, trans.user, current_history=trans.history)
399401
seed_scope_hid = self._resolve_seed_scope_hid(trans, history.id, seed_scope) if seed_scope else None
400-
builder = HistoryGraphBuilder(
402+
return self.history_graph_manager.build(
401403
sa_session=trans.sa_session,
402-
security=self.security,
403404
history_id=history.id,
404405
limit=limit,
405-
toolbox=trans.app.toolbox,
406406
include_deleted=include_deleted,
407407
seed=seed,
408408
direction=direction,
409409
depth=depth,
410410
seed_scope_hid=seed_scope_hid,
411411
)
412-
return builder.build()
413412

414413
def _resolve_seed_scope_hid(self, trans: ProvidesHistoryContext, history_id: int, seed_scope: str) -> int:
415414
db_id = self.security.decode_id(seed_scope[1:])

test/unit/app/managers/test_HistoryGraphBuilder.py

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from galaxy.managers.datasets import DatasetManager
1111
from galaxy.managers.hdas import HDAManager
1212
from galaxy.managers.histories import HistoryManager
13-
from galaxy.managers.history_graph import HistoryGraphBuilder
13+
from galaxy.managers.history_graph import HistoryGraphManager
1414
from .base import (
1515
BaseTestCase,
1616
CreatesCollectionsMixin,
@@ -35,6 +35,8 @@ def set_up_managers(self):
3535
self.hda_manager = self.app[HDAManager]
3636
self.history_manager = self.app[HistoryManager]
3737
self.collection_manager = self.app[DatasetCollectionManager]
38+
self.app._toolbox = None # type: ignore[assignment]
39+
self.history_graph_manager = self.app[HistoryGraphManager]
3840

3941
def _build_graph(
4042
self,
@@ -45,17 +47,15 @@ def _build_graph(
4547
limit=500,
4648
seed_scope_hid=None,
4749
):
48-
builder = HistoryGraphBuilder(
50+
return self.history_graph_manager.build(
4951
sa_session=self.trans.sa_session,
50-
security=self.app.security,
5152
history_id=history.id,
5253
limit=limit,
5354
seed=seed,
5455
direction=direction,
5556
depth=depth,
5657
seed_scope_hid=seed_scope_hid,
5758
)
58-
return builder.build()
5959

6060
def _encode(self, prefix, db_id):
6161
return f"{prefix}{self.app.security.encode_id(db_id)}"
@@ -580,13 +580,11 @@ def test_deleted_items_with_include_deleted(self):
580580
assert len(graph.nodes) == 0
581581

582582
# With include_deleted: included
583-
builder = HistoryGraphBuilder(
583+
graph = self.history_graph_manager.build(
584584
sa_session=self.trans.sa_session,
585-
security=self.app.security,
586585
history_id=history.id,
587586
include_deleted=True,
588587
)
589-
graph = builder.build()
590588
assert len(graph.nodes) == 1
591589

592590
def test_expanding_limit_generally_additive(self):
@@ -1015,13 +1013,11 @@ def test_n2_ambiguous_hdca_producer_has_node_but_no_edge(self):
10151013
hdca.implicit_collection_jobs_id = icj.id # implicit branch
10161014
self.trans.sa_session.flush()
10171015

1018-
builder = HistoryGraphBuilder(
1016+
graph = self.history_graph_manager.build(
10191017
sa_session=self.trans.sa_session,
1020-
security=self.app.security,
10211018
history_id=history.id,
10221019
limit=500,
10231020
)
1024-
graph = builder.build()
10251021

10261022
hdca_enc = self._encode("c", hdca.id)
10271023
node_ids = {n.id for n in graph.nodes}
@@ -1181,15 +1177,15 @@ def set_up_managers(self):
11811177
self.hda_manager = self.app[HDAManager]
11821178
self.history_manager = self.app[HistoryManager]
11831179
self.collection_manager = self.app[DatasetCollectionManager]
1180+
self.app._toolbox = None # type: ignore[assignment]
1181+
self.history_graph_manager = self.app[HistoryGraphManager]
11841182

11851183
def _build_graph(self, history, **kwargs):
1186-
builder = HistoryGraphBuilder(
1184+
return self.history_graph_manager.build(
11871185
sa_session=self.trans.sa_session,
1188-
security=self.app.security,
11891186
history_id=history.id,
11901187
**kwargs,
11911188
)
1192-
return builder.build()
11931189

11941190
def _encode(self, prefix, db_id):
11951191
return f"{prefix}{self.app.security.encode_id(db_id)}"

0 commit comments

Comments
 (0)