Skip to content

Commit

Permalink
Restore back to base and improve inheritance
Browse files Browse the repository at this point in the history
  • Loading branch information
RobertoAldea committed Nov 29, 2023
1 parent d2a98a1 commit 36a65e1
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 80 deletions.
36 changes: 18 additions & 18 deletions code/drv_db/src/wattrex_driver_db/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
In this case is drv_db
'''
from .drv_db_engine import DrvDbSqlEngineC
from .drv_db_types import DrvDbCyclingModeE, DrvDbEquipStatusE, DrvDbExpStatusE, DrvDbTypeE, \
DrvDbCyclingLimitE, DrvDbMembraneTypeE, DrvDbElectrolyteTypeE, \
DrvDbBatteryTechE, DrvDbBipolarTypeE, DrvDbDeviceTypeE, \
DrvDbLeadAcidChemistryE, DrvDbLithiumChemistryE, DrvDbAvailableCuE, \
DrvDbPolarityE, DrvDbConnStatusE
from .drv_db_types import (DrvDbCyclingModeE, DrvDbEquipStatusE, DrvDbExpStatusE, DrvDbTypeE,
DrvDbCyclingLimitE, DrvDbMembraneTypeE, DrvDbElectrolyteTypeE,
DrvDbBatteryTechE, DrvDbBipolarTypeE, DrvDbDeviceTypeE,
DrvDbLeadAcidChemistryE, DrvDbLithiumChemistryE, DrvDbAvailableCuE,
DrvDbPolarityE, DrvDbConnStatusE)
from .drv_db_dao_base import DrvDbAlarmC
from .drv_db_dao_cache import (DrvDbCacheExperimentC, DrvDbCacheGenericMeasureC,
DrvDbCacheExtendedMeasureC, DrvDbCacheStatusC)
from .drv_db_dao_base import DrvDbAlarmC
from .drv_db_dao_master import (DrvDbBatteryC, DrvDbLithiumC, DrvDbLeadAcidC, DrvDbRedoxStackC,
DrvDbComputationalUnitC, DrvDbCyclerStationC, DrvDbCompatibleDeviceC, DrvDbUsedDeviceC,
DrvDbProfileC, DrvDbInstructionC, DrvDbMasterExperimentC,DrvDbAvailableMeasuresC,
Expand All @@ -21,17 +21,17 @@
transform_ext_meas_db)

__all__ = [
'DrvDbCacheExperimentC', 'DrvDbCacheGenericMeasureC', 'DrvDbAlarmC',\
'DrvDbCacheExtendedMeasureC', 'DrvDbCacheStatusC', 'DrvDbSqlEngineC', \
'DrvDbCyclingModeE', 'DrvDbEquipStatusE', 'DrvDbExpStatusE', 'DrvDbTypeE', \
'DrvDbCyclingLimitE', 'DrvDbMembraneTypeE', 'DrvDbElectrolyteTypeE', \
'DrvDbBatteryTechE', 'DrvDbBipolarTypeE', 'DrvDbDeviceTypeE', \
'DrvDbLeadAcidChemistryE', 'DrvDbLithiumChemistryE', 'DrvDbAvailableCuE', \
'DrvDbBatteryC', 'DrvDbLithiumC', 'DrvDbLeadAcidC', 'DrvDbRedoxStackC',\
'DrvDbComputationalUnitC', 'DrvDbCyclerStationC', 'DrvDbCompatibleDeviceC', 'DrvDbUsedDeviceC',\
'DrvDbProfileC', 'DrvDbInstructionC', 'DrvDbMasterExperimentC',\
'DrvDbMasterGenericMeasureC', 'DrvDbMasterExtendedMeasureC', 'DrvDbMasterStatusC',\
'DrvDbLinkConfigurationC', 'DrvDbPolarityE', 'DrvDbUsedMeasuresC', 'DrvDbDetectedDeviceC',\
'DrvDbAvailableMeasuresC', 'transform_experiment_db', 'transform_gen_meas_db',\
'DrvDbCacheExperimentC', 'DrvDbCacheGenericMeasureC', 'DrvDbAlarmC',
'DrvDbCacheExtendedMeasureC', 'DrvDbCacheStatusC', 'DrvDbSqlEngineC',
'DrvDbCyclingModeE', 'DrvDbEquipStatusE', 'DrvDbExpStatusE', 'DrvDbTypeE',
'DrvDbCyclingLimitE', 'DrvDbMembraneTypeE', 'DrvDbElectrolyteTypeE',
'DrvDbBatteryTechE', 'DrvDbBipolarTypeE', 'DrvDbDeviceTypeE',
'DrvDbLeadAcidChemistryE', 'DrvDbLithiumChemistryE', 'DrvDbAvailableCuE',
'DrvDbBatteryC', 'DrvDbLithiumC', 'DrvDbLeadAcidC', 'DrvDbRedoxStackC',
'DrvDbComputationalUnitC', 'DrvDbCyclerStationC', 'DrvDbCompatibleDeviceC', 'DrvDbUsedDeviceC',
'DrvDbProfileC', 'DrvDbInstructionC', 'DrvDbMasterExperimentC',
'DrvDbMasterGenericMeasureC', 'DrvDbMasterExtendedMeasureC', 'DrvDbMasterStatusC',
'DrvDbLinkConfigurationC', 'DrvDbPolarityE', 'DrvDbUsedMeasuresC', 'DrvDbDetectedDeviceC',
'DrvDbAvailableMeasuresC', 'transform_experiment_db', 'transform_gen_meas_db',
'transform_status_db', 'transform_ext_meas_db'
]
68 changes: 63 additions & 5 deletions code/drv_db/src/wattrex_driver_db/drv_db_dao_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@
####################### GENERIC IMPORTS #######################

####################### THIRD PARTY IMPORTS #######################
from sqlalchemy import Column, DateTime, ForeignKeyConstraint
from sqlalchemy.dialects.mysql import MEDIUMINT
from sqlalchemy import Column, DateTime, ForeignKey, String, Enum, ForeignKeyConstraint
from sqlalchemy.dialects.mysql import INTEGER, MEDIUMINT, SMALLINT
from sqlalchemy.orm import declarative_base
from sqlalchemy.ext.declarative import declared_attr

####################### SYSTEM ABSTRACTION IMPORTS #######################
from system_logger_tool import Logger, sys_log_logger_get_module_logger
Expand All @@ -24,7 +25,7 @@


####################### MODULE IMPORTS #######################
from .drv_db_dao_cache import DrvDbCacheExperimentC
from .drv_db_types import DrvDbExpStatusE, DrvDbEquipStatusE

####################### ENUMS #######################

Expand All @@ -33,14 +34,71 @@
Base = declarative_base()
metadata = Base.metadata

class DrvDbBaseExperimentC:
'''
Class method to create a simplified model of database GenericMeasures table.
'''
ExpID = Column(MEDIUMINT(unsigned=True), primary_key=True)
Name = Column(String(30), nullable=False)
Description = Column(String(250), nullable=False)
DateCreation = Column(DateTime, nullable=False)
DateBegin = Column(DateTime, nullable=True)
DateFinish = Column(DateTime, nullable=True)
Status = Column(Enum(*DrvDbExpStatusE.get_all_values()), nullable=False)

class DrvDbBaseGenericMeasureC:
'''
Class method to create a simplified model of database GenericMeasures table.
'''
MeasID = Column(INTEGER(unsigned=True), primary_key=True, nullable=False)
Timestamp = Column(DateTime, nullable=False)
Voltage = Column(MEDIUMINT(), nullable=False)
Current = Column(MEDIUMINT(), nullable=False)
Power = Column(INTEGER())

@declared_attr
def ExpID(cls):
log.critical(f"EXPID DrvDbBaseGenericMeasureC")
return Column(ForeignKey(DrvDbBaseExperimentC.ExpID), primary_key=True, nullable=False)

class DrvDbBaseExtendedMeasureC:
'''
Class method to create a base model of database ExtendedMeasures table.
'''
Value = Column(MEDIUMINT(), nullable=False)

@declared_attr
def ExpID(cls):
log.critical(f"EXPID DrvDbBaseExtendedMeasureC")
return Column(ForeignKey(DrvDbBaseExperimentC.ExpID), primary_key= True, nullable=False)

@declared_attr
def MeasID(cls):
return Column(ForeignKey(DrvDbBaseGenericMeasureC.MeasID), primary_key= True, nullable=False)

class DrvDbBaseStatusC:
'''
Class method to create a base model of database Status table.
'''

StatusID = Column(MEDIUMINT(unsigned=True), primary_key=True, nullable=False)
Timestamp = Column(DateTime, nullable=False)
Status = Column(Enum(*DrvDbEquipStatusE.get_all_values()), nullable=False)
ErrorCode = Column(SMALLINT(unsigned=True), nullable=False)

@declared_attr
def ExpID(cls):
log.critical(f"EXPID DrvDbBaseStatusC")
return Column(ForeignKey(DrvDbBaseExperimentC.ExpID), primary_key=True, nullable=False)

class DrvDbAlarmC(Base):
'''
Class method to create a base model of database Alarm table.
'''
__tablename__ = 'Alarm'
__table_args__ = (ForeignKeyConstraint(['ExpID'], [DrvDbCacheExperimentC.ExpID]),)
__table_args__ = (ForeignKeyConstraint(['ExpID'], [DrvDbBaseExperimentC.ExpID]),)

ExpID = Column(primary_key=True, nullable=False)
ExpID = Column(ForeignKey(DrvDbBaseExperimentC.ExpID), primary_key=True, nullable=False)
AlarmID = Column(MEDIUMINT(unsigned=True), primary_key=True, nullable=False)
Timestamp = Column(DateTime, nullable=False)
Code = Column(MEDIUMINT(unsigned=True), nullable=False)
Expand Down
60 changes: 17 additions & 43 deletions code/drv_db/src/wattrex_driver_db/drv_db_dao_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
'''
Create a ORM model of the defined database.
This document's base structure is generated automaticly using sqlacodegen extracting data from DB.\
This document's base structure is generated automaticly using sqlacodegen extracting data from DB.
Attributes in this script does not follow PEP8 snake_case naming convention.
sqlacodegen mysql+mysqlconnector://user:password@ip:port/db_name --outfile drv_db_dao.py
Expand All @@ -12,8 +12,8 @@
####################### GENERIC IMPORTS #######################

####################### THIRD PARTY IMPORTS #######################
from sqlalchemy import Column, Enum, PrimaryKeyConstraint, ForeignKeyConstraint, DateTime, String
from sqlalchemy.dialects.mysql import MEDIUMINT, INTEGER, SMALLINT
from sqlalchemy import Column, Enum
from sqlalchemy.dialects.mysql import MEDIUMINT
from sqlalchemy.orm import declarative_base

####################### SYSTEM ABSTRACTION IMPORTS #######################
Expand All @@ -24,7 +24,9 @@


####################### MODULE IMPORTS #######################
from .drv_db_types import DrvDbCyclingModeE, DrvDbEquipStatusE, DrvDbExpStatusE
from .drv_db_dao_base import DrvDbBaseExperimentC, DrvDbBaseGenericMeasureC,\
DrvDbBaseExtendedMeasureC, DrvDbBaseStatusC
from .drv_db_types import DrvDbCyclingModeE

####################### ENUMS #######################

Expand All @@ -33,65 +35,37 @@
Base = declarative_base()
metadata = Base.metadata

class DrvDbCacheExperimentC(Base): #pylint: disable=too-many-instance-attributes
class DrvDbCacheExperimentC(DrvDbBaseExperimentC, Base): #pylint: disable=too-many-instance-attributes
'''
Class method to create a simplified model of database Experiment table.
'''
__tablename__: str = 'Experiment'
__table_args__ = {}

ExpID = Column(MEDIUMINT(unsigned=True), primary_key=True)
Name = Column(String(30), nullable=False)
Description = Column(String(250), nullable=False)
DateCreation = Column(DateTime, nullable=False)
DateBegin = Column(DateTime, nullable=True)
DateFinish = Column(DateTime, nullable=True)
Status = Column(Enum(*DrvDbExpStatusE.get_all_values()), nullable=False)
__tablename__: str = 'ExperimentCache'

CSID = Column(MEDIUMINT(unsigned=True), nullable=False)
BatID = Column(MEDIUMINT(unsigned=True), nullable=False)
ProfID = Column(MEDIUMINT(unsigned=True), nullable=False)

class DrvDbCacheGenericMeasureC(Base):
class DrvDbCacheGenericMeasureC(DrvDbBaseGenericMeasureC, Base):
'''
Class method to create a model of cache database GenericMeasures table.
'''
__tablename__ = 'GenericMeasures'
__table_args__ = (ForeignKeyConstraint(['ExpID'], [DrvDbCacheExperimentC.ExpID]),)

ExpID = Column(primary_key=True, nullable=False)
MeasID = Column(INTEGER(unsigned=True), primary_key=True, nullable=False)
Timestamp = Column(DateTime, nullable=False)
Voltage = Column(MEDIUMINT(), nullable=False)
Current = Column(MEDIUMINT(), nullable=False)
Power = Column(INTEGER())
__tablename__ = 'GenericMeasuresCache'

InstrID = Column(MEDIUMINT(unsigned=True), nullable=False)
PowerMode = Column(Enum(*DrvDbCyclingModeE.get_all_values()), nullable= False)

class DrvDbCacheExtendedMeasureC(Base):
class DrvDbCacheExtendedMeasureC(DrvDbBaseExtendedMeasureC, Base):
'''
Class method to create a model of cache database ExtendedMeasures table.
'''
__tablename__ = 'ExtendedMeasures'
__table_args__ = (PrimaryKeyConstraint('ExpID', 'MeasID', 'UsedMeasID'),
ForeignKeyConstraint(['ExpID', 'MeasID'], [DrvDbCacheGenericMeasureC.ExpID,
DrvDbCacheGenericMeasureC.MeasID]),)

ExpID = Column(primary_key= True, nullable=False)
MeasID = Column(primary_key= True, nullable=False)
Value = Column(MEDIUMINT(), nullable=False)
__tablename__ = 'ExtendedMeasuresCache'

UsedMeasID = Column(MEDIUMINT(unsigned=True), primary_key=True, nullable=False)

class DrvDbCacheStatusC(Base):
class DrvDbCacheStatusC(DrvDbBaseStatusC, Base):
'''
Class method to create a base model of database Status table.
'''
__tablename__ = 'Status'
__table_args__ = (ForeignKeyConstraint(['ExpID'], [DrvDbCacheExperimentC.ExpID]),)

StatusID = Column(MEDIUMINT(unsigned=True), primary_key=True, nullable=False)
ExpID = Column(primary_key=True, nullable=False)
Timestamp = Column(DateTime, nullable=False)
Status = Column(Enum(*DrvDbEquipStatusE.get_all_values()), nullable=False)
ErrorCode = Column(SMALLINT(unsigned=True), nullable=False)
__tablename__ = 'StatusCache'

DevID = Column(MEDIUMINT(unsigned=True), nullable=False)
24 changes: 10 additions & 14 deletions code/drv_db/src/wattrex_driver_db/drv_db_dao_master.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
DrvDbCyclingModeE, DrvDbDeviceTypeE, DrvDbElectrolyteTypeE,
DrvDbLeadAcidChemistryE, DrvDbLithiumChemistryE, DrvDbMembraneTypeE,
DrvDbAvailableCuE, DrvDbPolarityE, DrvDbConnStatusE)
from .drv_db_dao_cache import (DrvDbCacheStatusC, DrvDbCacheExperimentC, DrvDbCacheExtendedMeasureC,
DrvDbCacheGenericMeasureC)
from .drv_db_dao_base import (DrvDbBaseStatusC, DrvDbBaseExperimentC, DrvDbBaseExtendedMeasureC,
DrvDbBaseGenericMeasureC)

####################### ENUMS #######################

Expand Down Expand Up @@ -234,48 +234,44 @@ class DrvDbInstructionC(Base):
LimitType = Column(Enum(*(DrvDbCyclingLimitE.get_all_values())), nullable=False)
LimitPoint = Column(MEDIUMINT(), nullable=False)

class DrvDbMasterExperimentC(DrvDbCacheExperimentC): #pylint: disable=too-many-instance-attributes
class DrvDbMasterExperimentC(DrvDbBaseExperimentC, Base): #pylint: disable=too-many-instance-attributes
'''
Class method to create a DRVDB model of database Experiment table.
'''
__tablename__ = 'Experiment'
__table_args__ = (ForeignKeyConstraint(['CSID'], [DrvDbCyclerStationC.CSID]),
ForeignKeyConstraint(['BatID'], [DrvDbBatteryC.BatID]),
ForeignKeyConstraint(['ProfID'],[DrvDbProfileC.ProfID]),
{'extend_existing': True},)
ForeignKeyConstraint(['ProfID'],[DrvDbProfileC.ProfID]),)

CSID = Column(nullable=False)
BatID = Column(nullable=False)
ProfID = Column(nullable=False)


class DrvDbMasterGenericMeasureC(DrvDbCacheGenericMeasureC): #pylint: disable=too-many-instance-attributes
class DrvDbMasterGenericMeasureC(DrvDbBaseGenericMeasureC, Base): #pylint: disable=too-many-instance-attributes
'''
Class method to create a model of cache database GenericMeasures table.
'''
__tablename__ = 'GenericMeasures'
__table_args__ = (ForeignKeyConstraint(['InstrID'], [DrvDbInstructionC.InstrID]),
{'extend_existing': True},)
__table_args__ = (ForeignKeyConstraint(['InstrID'], [DrvDbInstructionC.InstrID]),)

InstrID = Column(nullable=False)

class DrvDbMasterExtendedMeasureC(DrvDbCacheExtendedMeasureC):
class DrvDbMasterExtendedMeasureC(DrvDbBaseExtendedMeasureC, Base):
'''
Class method to create a DRVDB model of database ExtendedMeasures table.
'''
__tablename__ = 'ExtendedMeasures'
__table_args__ = (ForeignKeyConstraint(['UsedMeasID'], [DrvDbUsedMeasuresC.UsedMeasID]),
{'extend_existing': True},)
__table_args__ = (ForeignKeyConstraint(['UsedMeasID'], [DrvDbUsedMeasuresC.UsedMeasID]),)

UsedMeasID = Column(primary_key=True, nullable=False)

class DrvDbMasterStatusC(DrvDbCacheStatusC):
class DrvDbMasterStatusC(DrvDbBaseStatusC, Base):
'''
Class method to create a DRVDB model of database Status table.
'''
__tablename__ = 'Status'
__table_args__ = (ForeignKeyConstraint(['DevID'], [DrvDbUsedDeviceC.DevID]),
{'extend_existing': True},)
__table_args__ = (ForeignKeyConstraint(['DevID'], [DrvDbUsedDeviceC.DevID]),)

DevID = Column(primary_key=True, nullable=False)

Expand Down

0 comments on commit 36a65e1

Please sign in to comment.