55from typing import Callable , Generator , List , Literal , Optional
66
77import ispyb
8-
9- # import ispyb.sqlalchemy
10- import sqlalchemy .orm
118import workflows .transport
129from fastapi import Depends
1310from ispyb .sqlalchemy import (
2926 ZcZocaloBuffer ,
3027 url ,
3128)
29+ from sqlalchemy import create_engine
30+ from sqlalchemy .orm import Session , sessionmaker
3231
3332from murfey .util import sanitise
3433from murfey .util .config import get_security_config
3837security_config = get_security_config ()
3938
4039try :
41- Session = sqlalchemy . orm . sessionmaker (
42- bind = sqlalchemy . create_engine (
40+ ISPyBSession = sessionmaker (
41+ bind = create_engine (
4342 url (credentials = security_config .ispyb_credentials ),
4443 connect_args = {"use_pure" : True },
4544 )
4645 )
4746 log .info ("Loaded ISPyB database session" )
4847except AttributeError :
4948 log .error ("Error loading ISPyB session" , exc_info = True )
50- Session = lambda : None
49+ ISPyBSession = lambda : None
5150
5251
5352def _send_using_new_connection (transport_type : str , queue : str , message : dict ) -> None :
@@ -94,7 +93,7 @@ def do_insert_data_collection_group(
9493 ** kwargs ,
9594 ):
9695 try :
97- with Session () as db :
96+ with ISPyBSession () as db :
9897 db .add (record )
9998 db .commit ()
10099 log .info (f"Created DataCollectionGroup { record .dataCollectionGroupId } " )
@@ -109,7 +108,7 @@ def do_insert_data_collection_group(
109108
110109 def do_insert_atlas (self , record : Atlas ):
111110 try :
112- with Session () as db :
111+ with ISPyBSession () as db :
113112 db .add (record )
114113 db .commit ()
115114 log .info (f"Created Atlas { record .atlasId } " )
@@ -126,7 +125,7 @@ def do_update_atlas(
126125 self , atlas_id : int , atlas_image : str , pixel_size : float , slot : int
127126 ):
128127 try :
129- with Session () as db :
128+ with ISPyBSession () as db :
130129 atlas = db .query (Atlas ).filter (Atlas .atlasId == atlas_id ).one ()
131130 atlas .atlasImage = atlas_image or atlas .atlasImage
132131 atlas .pixelSize = pixel_size or atlas .pixelSize
@@ -194,7 +193,7 @@ def do_insert_grid_square(
194193 pixelSize = grid_square_parameters .pixel_size ,
195194 )
196195 try :
197- with Session () as db :
196+ with ISPyBSession () as db :
198197 db .add (record )
199198 db .commit ()
200199 log .info (f"Created GridSquare { record .gridSquareId } " )
@@ -211,7 +210,7 @@ def do_update_grid_square(
211210 self , grid_square_id : int , grid_square_parameters : GridSquareParameters
212211 ):
213212 try :
214- with Session () as db :
213+ with ISPyBSession () as db :
215214 grid_square = (
216215 db .query (GridSquare )
217216 .filter (GridSquare .gridSquareId == grid_square_id )
@@ -302,7 +301,7 @@ def do_insert_foil_hole(
302301 pixelSize = foil_hole_parameters .pixel_size ,
303302 )
304303 try :
305- with Session () as db :
304+ with ISPyBSession () as db :
306305 db .add (record )
307306 db .commit ()
308307 log .info (f"Created FoilHole { record .foilHoleId } " )
@@ -322,7 +321,7 @@ def do_update_foil_hole(
322321 foil_hole_parameters : FoilHoleParameters ,
323322 ):
324323 try :
325- with Session () as db :
324+ with ISPyBSession () as db :
326325 foil_hole = (
327326 db .query (FoilHole ).filter (FoilHole .foilHoleId == foil_hole_id ).one ()
328327 )
@@ -380,7 +379,7 @@ def do_insert_data_collection(self, record: DataCollection, message=None, **kwar
380379 else "Created for Murfey"
381380 )
382381 try :
383- with Session () as db :
382+ with ISPyBSession () as db :
384383 record .comments = comment
385384 db .add (record )
386385 db .commit ()
@@ -396,7 +395,7 @@ def do_insert_data_collection(self, record: DataCollection, message=None, **kwar
396395
397396 def do_insert_sample_group (self , record : BLSampleGroup ) -> dict :
398397 try :
399- with Session () as db :
398+ with ISPyBSession () as db :
400399 db .add (record )
401400 db .commit ()
402401 log .info (f"Created BLSampleGroup { record .blSampleGroupId } " )
@@ -411,7 +410,7 @@ def do_insert_sample_group(self, record: BLSampleGroup) -> dict:
411410
412411 def do_insert_sample (self , record : BLSample , sample_group_id : int ) -> dict :
413412 try :
414- with Session () as db :
413+ with ISPyBSession () as db :
415414 db .add (record )
416415 db .commit ()
417416 log .info (f"Created BLSample { record .blSampleId } " )
@@ -434,7 +433,7 @@ def do_insert_sample(self, record: BLSample, sample_group_id: int) -> dict:
434433
435434 def do_insert_subsample (self , record : BLSubSample ) -> dict :
436435 try :
437- with Session () as db :
436+ with ISPyBSession () as db :
438437 db .add (record )
439438 db .commit ()
440439 log .info (f"Created BLSubSample { record .blSubSampleId } " )
@@ -449,7 +448,7 @@ def do_insert_subsample(self, record: BLSubSample) -> dict:
449448
450449 def do_insert_sample_image (self , record : BLSampleImage ) -> dict :
451450 try :
452- with Session () as db :
451+ with ISPyBSession () as db :
453452 db .add (record )
454453 db .commit ()
455454 log .info (f"Created BLSampleImage { record .blSampleImageId } " )
@@ -532,7 +531,7 @@ def do_update_processing_status(self, record: AutoProcProgram, **kwargs):
532531 return {"success" : False , "return_value" : None }
533532
534533 def do_buffer_lookup (self , app_id : int , uuid : int ) -> Optional [int ]:
535- with Session () as db :
534+ with ISPyBSession () as db :
536535 buffer_objects = (
537536 db .query (ZcZocaloBuffer )
538537 .filter_by (AutoProcProgramID = app_id , UUID = uuid )
@@ -543,8 +542,8 @@ def do_buffer_lookup(self, app_id: int, uuid: int) -> Optional[int]:
543542 return reference
544543
545544
546- def _get_session () -> Generator [Optional [sqlalchemy . orm . Session ], None , None ]:
547- db = Session ()
545+ def _get_session () -> Generator [Optional [Session ], None , None ]:
546+ db = ISPyBSession ()
548547 if db is None :
549548 yield None
550549 return
@@ -563,7 +562,7 @@ def get_session_id(
563562 proposal_code : str ,
564563 proposal_number : str ,
565564 visit_number : str ,
566- db : sqlalchemy . orm . Session | None ,
565+ db : Session | None ,
567566) -> int | None :
568567
569568 # Log received lookup parameters
@@ -596,9 +595,7 @@ def get_session_id(
596595 return res
597596
598597
599- def get_proposal_id (
600- proposal_code : str , proposal_number : str , db : sqlalchemy .orm .Session
601- ) -> int :
598+ def get_proposal_id (proposal_code : str , proposal_number : str , db : Session ) -> int :
602599 query = (
603600 db .query (Proposal )
604601 .filter (
@@ -610,7 +607,7 @@ def get_proposal_id(
610607 return query [0 ].proposalId
611608
612609
613- def get_sub_samples_from_visit (visit : str , db : sqlalchemy . orm . Session ) -> List [Sample ]:
610+ def get_sub_samples_from_visit (visit : str , db : Session ) -> List [Sample ]:
614611 proposal_id = get_proposal_id (visit [:2 ], visit .split ("-" )[0 ][2 :], db )
615612 samples = (
616613 db .query (BLSampleGroup , BLSampleGroupHasBLSample , BLSample , BLSubSample )
@@ -635,9 +632,7 @@ def get_sub_samples_from_visit(visit: str, db: sqlalchemy.orm.Session) -> List[S
635632 return res
636633
637634
638- def get_all_ongoing_visits (
639- microscope : str , db : sqlalchemy .orm .Session | None
640- ) -> list [Visit ]:
635+ def get_all_ongoing_visits (microscope : str , db : Session | None ) -> list [Visit ]:
641636 if db is None :
642637 print ("No database found" )
643638 return []
@@ -676,7 +671,7 @@ def get_all_ongoing_visits(
676671
677672def get_data_collection_group_ids (session_id ):
678673 query = (
679- Session ()
674+ ISPyBSession ()
680675 .query (DataCollectionGroup )
681676 .filter (
682677 DataCollectionGroup .sessionId == session_id ,
0 commit comments