def __init__(self, connection, tables_def = None, **kwargs): """Initialise the historian. The historian makes two connections to the data store. Both of these connections are available across the main and processing thread of the historian. topic_map and topic_meta are used as cache for the meta data and topic maps. :param connection: dictionary that contains necessary information to establish a connection to the sql database. The dictionary should contain two entries - 1. 'type' - describe the type of database (sqlite or mysql) 2. 'params' - parameters for connecting to the database. :param tables_def: optional parameter. dictionary containing the names to be used for historian tables. Should contain the following keys 1. "table_prefix": - if specified tables names are prefixed with this value followed by a underscore 2."data_table": name of the table that stores historian data, 3."topics_table": name of the table that stores the list of topics for which historian contains data data 4. "meta_table": name of the table that stores the metadata data for topics :param kwargs: additional keyword arguments. """ self.connection = connection self.tables_def, self.table_names = self.parse_table_def(tables_def) self.topic_id_map = {} self.topic_name_map = {} self.topic_meta = {} self.agg_topic_id_map = {} database_type = self.connection['type'] self.db_functs_class = sqlutils.get_dbfuncts_class(database_type) # Create two instance so connection is shared within a single thread. # This is because sqlite only supports sharing of connection within # a single thread. # historian_setup and publish_to_historian happens in background thread # everything else happens in the MainThread # One utils class instance( hence one db connection) for main thread self.main_thread_dbutils = self.db_functs_class( self.connection['params'], self.table_names) # One utils class instance( hence one db connection) for main thread # this gets initialized in the bg_thread within historian_setup self.bg_thread_dbutils = None super(SQLHistorian, self).__init__(**kwargs)
def historian_setup(self): thread_name = threading.currentThread().getName() _log.debug("historian_setup on Thread: {}".format(thread_name)) database_type = self.config['connection']['type'] self.tables_def, table_names = self.parse_table_def(self.config) db_functs_class = sqlutils.get_dbfuncts_class(database_type) self.reader = db_functs_class(self.config['connection']['params'], table_names) self.writer = db_functs_class(self.config['connection']['params'], table_names) self.reader.setup_historian_tables() topic_id_map, topic_name_map = self.reader.get_topic_map() self.topic_id_map.update(topic_id_map) self.topic_name_map.update(topic_name_map) self.agg_topic_id_map = self.reader.get_agg_topic_map()
def historian_setup(self): thread_name = threading.currentThread().getName() _log.debug("historian_setup on Thread: {}".format(thread_name)) database_type = self.config['connection']['type'] self.tables_def, table_names = self.parse_table_def(self.config) db_functs_class = sqlutils.get_dbfuncts_class(database_type) self.reader = db_functs_class(self.config['connection']['params'], table_names) self.writer = db_functs_class(self.config['connection']['params'], table_names) self.writer.setup_historian_tables() topic_id_map, topic_name_map = self.reader.get_topic_map() self.topic_id_map.update(topic_id_map) self.topic_name_map.update(topic_name_map) self.agg_topic_id_map = self.reader.get_agg_topic_map()
def configure(self, config_name, action, config): if not config or not isinstance(config, dict): raise ValueError("Configuration should be a valid json") # 1. Check connection to db instantiate db functions class connection = config.get('connection', None) assert connection is not None database_type = connection.get('type', None) assert database_type is not None params = connection.get('params', None) assert params is not None class_name = sqlutils.get_dbfuncts_class(database_type) self.dbfuncts_class = class_name(connection['params'], None) self.dbfuncts_class.setup_aggregate_historian_tables( self.volttron_table_defs) super(SQLAggregateHistorian, self).configure( config_name, action, config)
def configure(self, config_name, action, config): if not config or not isinstance(config, dict): raise ValueError("Configuration should be a valid json") # 1. Check connection to db instantiate db functions class connection = config.get('connection', None) assert connection is not None database_type = connection.get('type', None) assert database_type is not None params = connection.get('params', None) assert params is not None class_name = sqlutils.get_dbfuncts_class(database_type) self.dbfuncts_class = class_name(connection['params'], None) self.dbfuncts_class.setup_aggregate_historian_tables( self.volttron_table_defs) super(SQLAggregateHistorian, self).configure(config_name, action, config)
def get_dbfuncts_object(self): db_functs_class = sqlutils.get_dbfuncts_class(self.connection['type']) return db_functs_class(self.connection['params'], self.table_names)