Skip to content

Commit 50220b2

Browse files
create db_handler and connect engine
1 parent dec80ac commit 50220b2

File tree

5 files changed

+104
-6
lines changed

5 files changed

+104
-6
lines changed

src/wfi_reference_pipeline/config/config_access.py

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ def get_logging_config(config_file="config.yml"):
8181
Parameters
8282
----------
8383
config_file : str, optional
84-
The name of the configuration file to load, by default "crds_submission_config.yml".
84+
The name of the configuration file to load, by default "config.yml".
8585
8686
Returns
8787
-------
@@ -101,7 +101,7 @@ def get_data_files_config(config_file="config.yml"):
101101
Parameters
102102
----------
103103
config_file : str, optional
104-
The name of the configuration file to load, by default "crds_submission_config.yml".
104+
The name of the configuration file to load, by default "config.yml".
105105
106106
Returns
107107
-------
@@ -112,6 +112,24 @@ def get_data_files_config(config_file="config.yml"):
112112
_validate_config(settings, CONFIG_SCHEMA)
113113
return settings["data_files"]
114114

115+
def get_db_config(config_file="config.yml"):
116+
"""Get configuration settings from config.yml for database
117+
Validate that the settings are in the correct format before returning
118+
119+
Parameters
120+
----------
121+
config_file : str, optional
122+
The name of the configuration file to load, by default config.yml.
123+
124+
Returns
125+
-------
126+
dict
127+
A dictionary containing the configuration settings.
128+
"""
129+
settings = _get_config(config_file)
130+
_validate_config(settings, CONFIG_SCHEMA)
131+
return settings["database"]
132+
115133

116134
def get_pipelines_config(ref_type, config_file="pipelines_config.yml"):
117135
"""Get configuration settings from pipelines_config.yml for any reference type
@@ -122,7 +140,7 @@ def get_pipelines_config(ref_type, config_file="pipelines_config.yml"):
122140
ref_type : CONSTANT
123141
The defined reference type from constants.py
124142
config_file : str, optional
125-
The name of the configuration file to load, by default "crds_submission_config.yml".
143+
The name of the configuration file to load, by default "pipelines_config.yml".
126144
127145
Returns
128146
-------
@@ -152,7 +170,7 @@ def get_quality_control_config(ref_type, detector=None, config_file=None):
152170
ref_type : CONSTANT
153171
The defined reference type from constants.py
154172
config_file : str, optional
155-
The name of the configuration file to load, by default "crds_submission_config.yml".
173+
The name of the configuration file to load, by default None.
156174
157175
Returns
158176
-------

src/wfi_reference_pipeline/config/example_config.yml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,9 @@ data_files:
1717
# crds_ready_dir: string location to store CRDS ready deliverable reference files
1818
ingest_dir: "/PATH/TO/DATAFILES/INGEST"
1919
prep_dir: "/PATH/TO/DATAFILES/PREPPED"
20-
crds_ready_dir: "/PATH/TO/DATAFILES/CRDS_READY"
20+
crds_ready_dir: "/PATH/TO/DATAFILES/CRDS_READY"
21+
22+
database:
23+
use_rtbdb: false
24+
sql_server_str: "<SERVER_NAME.stsci.edu>"
25+
sql_database_str: "<DATABASE_NAME"

src/wfi_reference_pipeline/pipelines/pipeline.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
from abc import ABC, abstractmethod
33
from pathlib import Path
44

5-
from wfi_reference_pipeline.config.config_access import get_data_files_config
5+
from wfi_reference_pipeline.config.config_access import get_data_files_config, get_db_config
66
from wfi_reference_pipeline.constants import REF_TYPE_DARK, WFI_DETECTORS
7+
from wfi_reference_pipeline.utilities.db_handler import DBHandler
78
from wfi_reference_pipeline.utilities.file_handler import FileHandler
89
from wfi_reference_pipeline.utilities.logging_functions import configure_logging
910
from wfi_reference_pipeline.utilities.quality_control.dark_quality_control import (
@@ -52,6 +53,11 @@ def __init__(self, ref_type, detector):
5253
self.file_handler = FileHandler(
5354
self.ref_type, self.prep_path, self.pipeline_out_path
5455
)
56+
self._db_config = get_db_config()
57+
if self._db_config["use_rtbdb"]:
58+
self.db_handler = DBHandler(self.ref_type, self._db_config["sql_server_str"], self._db_config["sql_database_str"])
59+
else:
60+
self.db_handler = None
5561

5662
@abstractmethod
5763
def select_uncal_files(self):
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import logging
2+
3+
from rtb_db.utilities.login import connect_server
4+
from rtb_db.utilities.table_tools import ensure_connection_is_engine, table_names
5+
6+
from wfi_reference_pipeline import constants
7+
8+
9+
class DBHandler:
10+
"""
11+
Utility class to safely handle access to the rtbdb
12+
This class is designed to ONLY be initialized in the Pipeline base class
13+
This class should be initialized as part of a base pipeline initialization procedure AFTER config values have been read and stored
14+
"""
15+
16+
def __init__(self, ref_type, sql_server_str, sql_database_str):
17+
if ref_type not in constants.WFI_REF_TYPES:
18+
raise ValueError(
19+
f"ref_type {ref_type} not valid, must be: {constants.WFI_REF_TYPES}"
20+
)
21+
22+
self.ref_type = ref_type
23+
self.sql_engine = None
24+
self.sql_server_str = sql_server_str
25+
self.sql_database_str = sql_database_str
26+
self.connect()
27+
28+
def connect(self):
29+
"""Confirm if the user has access to the desired database. If so, establish a SQLalchemy connection.
30+
Saves SQLalchemy engine to self.sql_engine and saves the db availability boolean to self.use_db.
31+
32+
Returns
33+
-------
34+
self.use_db
35+
boolean to trigger if montor should attempt to use the database
36+
37+
"""
38+
try:
39+
engine = connect_server(driver="FreeTDS",
40+
server=self.sql_server_str,
41+
port=1433,
42+
tds_version=7.1,
43+
database=self.sql_database_str)
44+
45+
engine = ensure_connection_is_engine(engine)
46+
self.sql_engine = engine
47+
48+
# TODO - SAPP -> GET TABLE NAMES
49+
50+
# ensure metrics table exists on the active database server
51+
# assert set(self.sql_table_names).issubset(table_names(self.sql_engine)) # TODO figure this out, not currently storing sql_table_names
52+
53+
54+
### TODO: This is not actually catching if the engine exists or not...
55+
### add raise exception to rtb-db login.connect_sqlalchemy
56+
except Exception as err:
57+
logging.warning('Unable to connect to RTB database.')
58+
logging.error(f"Received {err}")
59+
return
60+

src/wfi_reference_pipeline/utilities/schemas.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,15 @@
2525
},
2626
"required": ["ingest_dir", "prep_dir", "crds_ready_dir"],
2727
},
28+
"database": {
29+
"type": "object",
30+
"properties": {
31+
"use_rtbdb": {"type": "boolean"},
32+
"sql_server_str": {"type": "string"},
33+
"sql_database_str": {"type": "string"},
34+
},
35+
"required": ["use_rtbdb", "sql_server_str", "sql_database_str"],
36+
},
2837
},
2938
# List which entries are needed (all of them)
3039
"required": ["logging", "data_files"],

0 commit comments

Comments
 (0)