nexusLIMS.db package

A module to handle communication with the NexusLIMS database.

Also performs basic database ORM tasks. The top-level module has a helper function to make a database query (make_db_query()), while the session_handler submodule is primarily concerned with mapping session log information from the database into python objects for use in other parts of the NexusLIMS backend.

nexusLIMS.db.make_db_query(query)[source]

Execute a query on the NexusLIMS database and return the results as a list.

Parameters

query (str) – The SQL query to execute

Returns

res_list – The results of the SQL query

Return type

list of tuple

Submodules

nexusLIMS.db.session_handler module

Classes and methods to interact with sessions from the NexusLIMS database.

class nexusLIMS.db.session_handler.Session(session_identifier: str, instrument: Instrument, dt_range: Tuple[datetime, datetime], user: str)[source]

Bases: object

A representation of a session in the NexusLIMS database.

A record of an individual session as read from the Nexus Microscopy facility session database. Created by combining two SessionLog objects with status "TO_BE_BUILT".

Parameters
  • session_identifier – The unique identifier for an individual session on an instrument

  • instrument – An object representing the instrument associated with this session

  • dt_range – A tuple of two datetime objects representing the start and end of this session )in that order

  • user (str) – The username associated with this session (may not be trustworthy)

insert_record_generation_event() dict[source]

Insert record generation event to session log.

Insert a log for this session into the session database with event_type “RECORD_GENERATION” and the current time (with local system timezone) as the timestamp.

Returns

res – A dictionary containing the results from the query to check that a RECORD_GENERATION event was added

Return type

dict

update_session_status(status)[source]

Update the status of this Session in the NexusLIMS database.

Specifically, update the record_status in any session logs for this Session.

Parameters

status (str) – One of "COMPLETED", "WAITING_FOR_END", "TO_BE_BUILT", "ERROR", "NO_FILES_FOUND", "NO_CONSENT", or "NO_RESERVATION" (the allowed values in the NexusLIMS database). Status value will be validated by a check constraint in the database

Returns

success – Whether the update operation was successful

Return type

bool

class nexusLIMS.db.session_handler.SessionLog(session_identifier: str, instrument: str, timestamp: str, event_type: str, user: str, record_status: Optional[str] = None)[source]

Bases: object

A log of the start or end of a Session.

A simple mapping of one row in the session_log table of the NexusLIMS database (all values are strings).

Parameters
  • session_identifier – A unique string that is consistent among a single record’s “START”, “END”, and “RECORD_GENERATION” events (often a UUID, but is not required to be so)

  • instrument – The instrument associated with this session (foreign key reference to the instruments table)

  • timestamp – The ISO format timestamp representing the date and time of the logged event

  • event_type – The type of log for this session (either “START”, “END”, or “RECORD_GENERATION”)

  • user – The username associated with this session (if known)

  • record_status – The status to use for this record (defaults to 'TO_BE_BUILT')

insert_log() bool[source]

Insert this log into the NexusLIMS database.

Inserts a log into the database with the information contained within this SessionLog’s attributes (used primarily for NEMO usage_event integration). It will check for the presence of a matching record first and warn without inserting anything if it finds one.

Returns

success – Whether or not the session log row was inserted successfully

Return type

bool

nexusLIMS.db.session_handler.db_query(query, args=None)[source]

Make a query on the NexusLIMS database.

nexusLIMS.db.session_handler.get_sessions_to_build() List[Session][source]

Get list of sessions that need to be built from the NexusLIMS database.

Query the NexusLIMS database for pairs of logs with status 'TO_BE_BUILT' and return the information needed to build a record for that session.

Returns

sessions – A list of Session objects containing the sessions that the need their record built. Will be an empty list if there’s nothing to do.

Return type

List[Session]