X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=sfa%2Fiotlab%2Fiotlabpostgres.py;h=eed8769a4ef53a189510708e365dda5e2a0ac148;hb=0cc524b0816203ed36589fa68337f9291fad0b7b;hp=4e3c6df425ac468b57b48fed0d1afedbf06b6d92;hpb=b0995f45712c5ea9dd26fdf0c4347ccba2d77fce;p=sfa.git diff --git a/sfa/iotlab/iotlabpostgres.py b/sfa/iotlab/iotlabpostgres.py index 4e3c6df4..eed8769a 100644 --- a/sfa/iotlab/iotlabpostgres.py +++ b/sfa/iotlab/iotlabpostgres.py @@ -1,14 +1,17 @@ +""" +File defining classes to handle the table in the iotlab dedicated database. +""" + from sqlalchemy import create_engine from sqlalchemy.orm import sessionmaker - -from sfa.util.config import Config +# from sfa.util.config import Config from sfa.util.sfalogging import logger from sqlalchemy import Column, Integer, String from sqlalchemy import Table, MetaData from sqlalchemy.ext.declarative import declarative_base -from sqlalchemy.dialects import postgresql +# from sqlalchemy.dialects import postgresql from sqlalchemy.exc import NoSuchTableError @@ -16,148 +19,246 @@ from sqlalchemy.exc import NoSuchTableError #Dict holding the columns names of the table as keys #and their type, used for creation of the table slice_table = {'record_id_user': 'integer PRIMARY KEY references X ON DELETE \ -CASCADE ON UPDATE CASCADE','oar_job_id':'integer DEFAULT -1', \ -'record_id_slice':'integer', 'slice_hrn':'text NOT NULL'} + CASCADE ON UPDATE CASCADE', 'oar_job_id': 'integer DEFAULT -1', + 'record_id_slice': 'integer', 'slice_hrn': 'text NOT NULL'} #Dict with all the specific iotlab tables -tablenames_dict = {'iotlab_xp': slice_table} - +# tablenames_dict = {'lease_table': slice_table} -IotlabBase = declarative_base() +TestbedBase = declarative_base() -class IotlabXP (IotlabBase): - """ SQL alchemy class to manipulate slice_iotlab table in - iotlab_sfa database. +class LeaseTableXP (TestbedBase): + """ SQL alchemy class to manipulate the rows of the slice_iotlab table in + lease_table database. Handles the records representation and creates the + table if it does not exist yet. """ - __tablename__ = 'iotlab_xp' - + __tablename__ = 'lease_table' slice_hrn = Column(String) - job_id = Column(Integer, primary_key = True) - end_time = Column(Integer, nullable = False) - - - #oar_job_id = Column( Integer,default = -1) - #node_list = Column(postgresql.ARRAY(String), nullable =True) + experiment_id = Column(Integer, primary_key=True) + end_time = Column(Integer, nullable=False) - def __init__ (self, slice_hrn =None, job_id=None, end_time=None): + def __init__(self, slice_hrn=None, experiment_id=None, end_time=None): """ Defines a row of the slice_iotlab table """ if slice_hrn: self.slice_hrn = slice_hrn - if job_id : - self.job_id = job_id + if experiment_id: + self.experiment_id = experiment_id if end_time: self.end_time = end_time - def __repr__(self): """Prints the SQLAlchemy record to the format defined by the function. """ - result = " 0: + self.testbed_session.query(LeaseTableXP).filter(LeaseTableXP.experiment_id.in_(deleted_experiments)).delete(synchronize_session='fetch') + self.testbed_session.commit() + return + + def __init__(self, config, debug=False): + self.sl_base = TestbedBase + + # Check whether we already have an instance + if TestbedAdditionalSfaDB._connection_singleton is None: + TestbedAdditionalSfaDB._connection_singleton = \ + TestbedAdditionalSfaDB.Singleton(config, debug) + + # Store instance reference as the only member in the handle + self._EventHandler_singleton = \ + TestbedAdditionalSfaDB._connection_singleton + + def __getattr__(self, aAttr): """ - if self.iotlab_session is None: - Session = sessionmaker() - self.iotlab_session = Session(bind = self.iotlab_engine) - return self.iotlab_session + Delegate access to implementation. - def close_session(self): + :param aAttr: Attribute wanted. + :returns: Attribute """ - Closes connection to database. + return getattr(self._connection_singleton, aAttr) - """ - if self.iotlab_session is None: return - self.iotlab_session.close() - self.iotlab_session = None + # def __setattr__(self, aAttr, aValue): + # """Delegate access to implementation. + + # :param attr: Attribute wanted. + # :param value: Vaule to be set. + # :return: Result of operation. + # """ + # return setattr(self._connection_singleton, aAttr, aValue) + def exists(self, tablename): """ Checks if the table specified as tablename exists. + :param tablename: name of the table in the db that has to be checked. + :type tablename: string + :returns: True if the table exists, False otherwise. + :rtype: bool """ - + metadata = MetaData(bind=self.testbed_engine) try: - metadata = MetaData (bind=self.iotlab_engine) - table = Table (tablename, metadata, autoload=True) + table = Table(tablename, metadata, autoload=True) return True except NoSuchTableError: - logger.log_exc("SLABPOSTGRES tablename %s does not exists" \ - %(tablename)) + logger.log_exc("SLABPOSTGRES tablename %s does not exist" + % (tablename)) return False - def createtable(self): """ Creates all the table sof the engine. @@ -165,13 +266,8 @@ class IotlabDB: """ - logger.debug("SLABPOSTGRES createtable IotlabBase.metadata.sorted_tables \ - %s \r\n engine %s" %(IotlabBase.metadata.sorted_tables , iotlab_engine)) - IotlabBase.metadata.create_all(iotlab_engine) + logger.debug("IOTLABPOSTGRES createtable \ + TestbedBase.metadata.sorted_tables %s \r\n engine %s" + % (TestbedBase.metadata.sorted_tables, self.testbed_engine)) + TestbedBase.metadata.create_all(self.testbed_engine) return - - - -iotlab_alchemy = IotlabDB(Config()) -iotlab_engine = iotlab_alchemy.iotlab_engine -iotlab_dbsession = iotlab_alchemy.session()