Skip to content

Commit b7cf9ac

Browse files
add doc strings
1 parent 6cd782d commit b7cf9ac

File tree

2 files changed

+109
-53
lines changed

2 files changed

+109
-53
lines changed
Lines changed: 40 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
from datetime import datetime
2-
from importlib.metadata import version
32

43
import romancal
5-
from rtb_db.constants.rfp_reef import FORMAT_DATE_TIME
4+
from rtb_db.constants.rfp_reef import DB_QC_INCOMPLETE
65
from rtb_db.table_defs.wfi_rfp.log import RFPLogProTable
76

7+
from wfi_reference_pipeline import __version__ as rfp_version
8+
89

910
class DBEntry:
1011
"""
@@ -15,33 +16,46 @@ class DBEntry:
1516
def __init__(self):
1617
self.rfp_log_pro = None # DB entry for Logistics Processing Table
1718

18-
def init_rfp_log_pro(self, ref_type, wfi_mode, reef_monitor):
19-
20-
current_time = datetime.now()
21-
start_time = current_time.strftime(FORMAT_DATE_TIME)
22-
__version__ = version(__package__ or "wfi_reference_pipeline")
19+
@staticmethod
20+
def get_date_time_formatted():
21+
return datetime.today().replace(microsecond=0)
2322

23+
def init_rfp_log_pro(self, ref_type, wfi_mode, reef_monitor):
24+
"""
25+
Initialize a logistics processing table object.
26+
27+
Parameters
28+
----------
29+
ref_type : str
30+
Reference type associated with this pipeline run.
31+
wfi_mode : str
32+
wfi mode associated with this pipeline run.
33+
reef_monitor : bool
34+
Expecting external monitoring for this run.
35+
"""
36+
start_time = self.get_date_time_formatted()
2437
self.rfp_log_pro = RFPLogProTable(ref_type=ref_type,
2538
start_time=start_time,
2639
wfi_mode=wfi_mode,
2740
reef_monitor=reef_monitor,
2841
rcal_version=romancal.__version__,
29-
rfp_version=__version__)
30-
31-
## SAPP TODO -
32-
# ITEMS NOT NEEDED AT INITIALIZATION:
33-
# pipeline: Mapped[str] = mapped_column(String()) # THIS ONE MAY NOT BE NEEDED
34-
# prep_start: Mapped[datetime] = mapped_column(DateTime())
35-
# prep_end: Mapped[datetime] = mapped_column(DateTime())
36-
# pipe_start: Mapped[datetime] = mapped_column(DateTime())
37-
# pipe_end: Mapped[datetime] = mapped_column(DateTime())
38-
# output_filename
39-
# end_time: Mapped[datetime] = mapped_column(END_DATETIME_COL, DateTime())
40-
# qc_status: Mapped[str] = mapped_column(QC_STATUS_COL, String())
41-
# crds_filename: Mapped[Optional[str]] = mapped_column(CRDS_FILENAME_COL, String())
42-
# crds_context: Mapped[str] = mapped_column(CRDS_CONTEXT_COL, String())
43-
# crds_end_time: Mapped[Optional[datetime]] = mapped_column(DateTime())
44-
# crds_start_time: Mapped[Optional[datetime]] = mapped_column(DateTime())
45-
# crds_delivered: Mapped[bool] = mapped_column(CRDS_DELIVERED_COL, Boolean())
46-
# _input_file_list: Mapped[str] = mapped_column(String())
47-
# input_file_list = String2ListVariable()
42+
rfp_version=rfp_version,
43+
qc_status=DB_QC_INCOMPLETE,
44+
)
45+
46+
# NOTE The following will need to eventually be added to the RFPLogProTable during the course of the pipeline run
47+
# pipeline_cmd: Mapped[str] = mapped_column(String())
48+
# prep_start: Mapped[datetime] = mapped_column(DateTime())
49+
# prep_end: Mapped[datetime] = mapped_column(DateTime())
50+
# pipe_start: Mapped[datetime] = mapped_column(DateTime())
51+
# pipe_end: Mapped[datetime] = mapped_column(DateTime())
52+
# output_filename
53+
# end_time: Mapped[datetime] = mapped_column(END_DATETIME_COL, DateTime())
54+
# qc_status: Mapped[str] = mapped_column(QC_STATUS_COL, String()) <---- Currently in but UPDATE THIS
55+
# crds_filename: Mapped[Optional[str]] = mapped_column(CRDS_FILENAME_COL, String())
56+
# crds_context: Mapped[str] = mapped_column(CRDS_CONTEXT_COL, String())
57+
# crds_end_time: Mapped[Optional[datetime]] = mapped_column(DateTime())
58+
# crds_start_time: Mapped[Optional[datetime]] = mapped_column(DateTime())
59+
# crds_delivered: Mapped[bool] = mapped_column(CRDS_DELIVERED_COL, Boolean())
60+
# _input_file_list: Mapped[str] = mapped_column(String())
61+
# input_file_list = String2ListVariable()

src/wfi_reference_pipeline/utilities/db_handler.py

Lines changed: 69 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,45 @@
1414

1515
class DBHandler:
1616
"""
17-
Utility class to safely handle access to the rtbdb
18-
This class is designed to ONLY be initialized in the Pipeline base class
19-
This class should be initialized as part of a base pipeline initialization procedure AFTER config values have been read and stored
17+
Utility class to safely handle access to the RTB database.
18+
19+
This class encapsulates connection setup and basic pipeline entry
20+
handling against the RTB DB via SQLAlchemy. It is intended to be
21+
constructed by the Pipeline base class, after configuration has been
22+
read and validated.
2023
"""
2124

2225
def __init__(self, ref_type, use_dsn, sql_server_str=None, sql_database_str=None, port=None, dsn_header_str=None):
26+
"""
27+
Initialize a DBHandler and immediately attempt to connect to the RTB database.
28+
29+
Parameters
30+
----------
31+
ref_type : str
32+
Reference type identifier used by the pipeline.
33+
use_dsn : bool
34+
If True connect using a DSN name else connect using explicit server/database parameters.
35+
sql_server_str : str, optional
36+
SQL Server hostname or address when use_dsn is False.
37+
sql_database_str : str, optional
38+
Database name when use_dsn is False.
39+
port : int, optional
40+
SQL Server port when use_dsn is False.
41+
dsn_header_str : str, optional
42+
DSN name to use when use_dsn is True.
43+
44+
Attributes
45+
----------
46+
ref_type : str
47+
sql_id : Any or None
48+
SQL identifier.
49+
db_engine : sqlalchemy.engine.Engine or None
50+
SQLAlchemy Engine once connected.
51+
db_entry : DBEntry
52+
Helper object to prepare and insert pipeline entries.
53+
"""
54+
55+
2356
if ref_type not in constants.WFI_REF_TYPES:
2457
raise ValueError(
2558
f"ref_type {ref_type} not valid, must be: {constants.WFI_REF_TYPES}"
@@ -32,16 +65,28 @@ def __init__(self, ref_type, use_dsn, sql_server_str=None, sql_database_str=None
3265

3366
self._connect(use_dsn, sql_server_str, sql_database_str, port, dsn_header_str)
3467

35-
def _connect(self, use_dsn, sql_server_str, sql_database_str, port, dsn_header_str):
36-
"""Confirm if the user has access to the desired database. If so, establish a SQLalchemy connection.
37-
Saves SQLalchemy engine to self.db_engine and saves the db availability boolean to self.use_db.
38-
39-
Returns
40-
-------
41-
self.use_db
42-
boolean to trigger if montor should attempt to use the database
4368

69+
def _connect(self, use_dsn, sql_server_str, sql_database_str, port, dsn_header_str):
4470
"""
71+
Establish a connection to the RTB database and store the SQLAlchemy Engine.
72+
73+
Depending on ``use_dsn``, this will either connect via a named DSN or
74+
directly using server parameters.
75+
76+
Parameters
77+
----------
78+
use_dsn : bool
79+
If True connect using DSN.
80+
sql_server_str : str, optional
81+
SQL Server string (non-DSN mode).
82+
sql_database_str : str, optional
83+
Database name (non-DSN mode).
84+
port : int, optional
85+
SQL Server port (non-DSN mode).
86+
dsn_header_str : str, optional
87+
DSN name (DSN mode).
88+
"""
89+
4590
try:
4691
if use_dsn:
4792
engine = connect_server(dsn_name=dsn_header_str)
@@ -54,16 +99,6 @@ def _connect(self, use_dsn, sql_server_str, sql_database_str, port, dsn_header_s
5499
engine = ensure_connection_is_engine(engine)
55100
self.db_engine = engine
56101

57-
# TODO - SAPP -> TEMP DEV CODE REMOVE
58-
print(f'SQL table names: {table_tools.table_names(engine)}\n')
59-
print(f'Table class names: {table_tools.table_class_names(engine)}\n')
60-
print(f'Table name from class: {RFPLogProTable.__tablename__}')
61-
print(f"Table class from name: {table_tools.get_table_class_from_tablename('wfi_rfp_log_pro')}")
62-
print(table_tools.get_full_table(engine, RFPLogProTable))
63-
#//////////////////////////////////////////////////////////////////
64-
65-
66-
67102
### TODO: This is not actually catching if the engine exists or not...
68103
### add raise exception to rtb-db login.connect_sqlalchemy
69104
except Exception as err:
@@ -74,11 +109,18 @@ def _connect(self, use_dsn, sql_server_str, sql_database_str, port, dsn_header_s
74109
)
75110

76111
def new_pipeline_db_entry(self, ref_type, wfi_mode, reef_monitor):
77-
# SAPP TODO - YOU ARE HERE!!!! GET AN INITIAL DATABASE ENTRY AND KEEP THE SQL ID
78-
# STORE EVERYTHING YOU HAVE ALREADY PERTAINING TO THIS PIPELINE RUN
79-
# CREATE A METHOD IN RTB_DB REPO THAT WILL ALLOW YOU TO UPDATE AN EXISTING ROW
80-
# ONCE YOU HAVE INITIAL UPDATE AND EXISTING ROW UPDATE, TRY RUNNING AND POPULATING EVERYTHING NEEDED FROM INITIATION THROUGH PREP PIPELINE
112+
"""
113+
Initialize and insert a new logistics processing table row for this pipeline.
114+
115+
Parameters
116+
----------
117+
ref_type : str
118+
Reference type associated with this pipeline run.
119+
wfi_mode : str
120+
wfi mode associated with this pipeline run.
121+
reef_monitor : bool
122+
Expecting external monitoring for this run.
123+
"""
81124

82125
self.db_entry.init_rfp_log_pro(ref_type, wfi_mode, reef_monitor)
83-
add_to_tables_from_class_list(self.engine, [self.db_entry.rfp_log_pro])
84-
#self.sql_id = self.rfp_log_pro.sql_id # VERIFY SQL_ID EXISTS
126+
add_to_tables_from_class_list(self.db_engine, [self.db_entry.rfp_log_pro])

0 commit comments

Comments
 (0)