This summarizes a point of concern about our meeting today for new sensors added in the future.
Problem
Currently, in the database schema created by init_database.py, one column is created with enums:
|
egauge.script.orm_egauge.setup() |
(@matthew-schultz I didn't realize you were using egauge orm here. Since we are moving this to a single file it's not too bad but if we didn't make in time it would cause confusion down the road on "why egauge and not the orm of another sensor".)
The database is created as part of a setup function leveraging sql alchemy library:
|
def setup(): |
|
""" |
|
Use defined classes to create tables in the database named in config file |
|
""" |
|
config_path = str(Path(os.path.dirname(os.path.realpath(__file__))).parent.parent) + "/config.txt" |
|
with open(config_path, "r") as file: |
|
#prepend '[DEFAULT]\n' since ConfigParser requires section headers in config files |
|
config_string = '[DEFAULT]\n' + file.read() |
|
config = configparser.ConfigParser() |
|
config.read_string(config_string) |
|
db_url = "postgresql:///" + config['DEFAULT']['db'] |
|
db = create_engine(db_url) |
|
BASE.metadata.create_all(db) |
And in particular, the point of concern lies within this class of the data model in the same file:
|
class ScriptFolderEnum(enum.Enum): |
|
""" |
|
This class defines strings that could be inserted into sensor_info.sensor_type |
|
""" |
|
egauge = "egauge" |
|
hobo = "hobo" |
|
webctrl = "webctrl" |
-
According to this, the possible values of sensors are defined at the moment the database is initialized. Adding a new folder sensor, therefore requires not only code be added, but manually editing every database that exists on the server to add the new sensor. There is also no clear way that a new programmer will know they have to edit this column.
-
There is also a concern if the responsibility of verifying, before adding to the crontab, if the script even exist. Currently, verification does not exist. init_crontab.py passively assumes, since the column sensor_type is enum, that the value will never be inserted incorrectly.
Solution
Not sure yet. The current implementation makes sense because values are inserted directly in the database, and an enum shows on Navicat as a dropdown menu instead of letting Eileen edit the text, which a typo would suffice to mess the crontab file. However, in the long-run due to 1) this solution may prove confusing to someone adding a new sensor or troublesome as they will have to manually edit the database. Code changes alone are insufficient.
We would like a solution similar to what is observed on init_crontab.py on what concerns adding a new folder:
|
for script_folder in script_folders: |
|
# use ".value" to access value of script_folder enum |
|
script_folder = script_folder.value |
|
# hobo has a different script name |
|
if script_folder == 'hobo': |
|
script_filename = 'extract_hobo.py' |
|
else: |
|
script_filename = 'api_' + script_folder + '.py' |
|
database_active_scripts.append(script_filename) |
So long a new script is added following the convention api_ etc, modifying init_crontab.py is even unnecessary altogether!
This summarizes a point of concern about our meeting today for new sensors added in the future.
Problem
Currently, in the database schema created by
init_database.py, one column is created with enums:lonoa/init_database.py
Line 44 in 65609c5
(@matthew-schultz I didn't realize you were using egauge orm here. Since we are moving this to a single file it's not too bad but if we didn't make in time it would cause confusion down the road on "why egauge and not the orm of another sensor".)
The database is created as part of a setup function leveraging sql alchemy library:
lonoa/egauge/script/orm_egauge.py
Lines 171 to 183 in 65609c5
And in particular, the point of concern lies within this class of the data model in the same file:
lonoa/egauge/script/orm_egauge.py
Lines 74 to 80 in 65609c5
According to this, the possible values of sensors are defined at the moment the database is initialized. Adding a new folder sensor, therefore requires not only code be added, but manually editing every database that exists on the server to add the new sensor. There is also no clear way that a new programmer will know they have to edit this column.
There is also a concern if the responsibility of verifying, before adding to the crontab, if the script even exist. Currently, verification does not exist.
init_crontab.pypassively assumes, since the column sensor_type is enum, that the value will never be inserted incorrectly.Solution
Not sure yet. The current implementation makes sense because values are inserted directly in the database, and an enum shows on Navicat as a dropdown menu instead of letting Eileen edit the text, which a typo would suffice to mess the crontab file. However, in the long-run due to 1) this solution may prove confusing to someone adding a new sensor or troublesome as they will have to manually edit the database. Code changes alone are insufficient.
We would like a solution similar to what is observed on
init_crontab.pyon what concerns adding a new folder:lonoa/init_crontab.py
Lines 54 to 62 in 65609c5
So long a new script is added following the convention
api_etc, modifyinginit_crontab.pyis even unnecessary altogether!