def test_store_record(conn): with closing(conn.cursor()) as cursor: datasource = name_to_datasource(cursor, "test-source-003") attributes = [Attribute("a", "integer", "a attribute"), Attribute("b", "integer", "b attribute")] notificationstore = NotificationStore(datasource, attributes) notificationstore.create(cursor) datarecord = Record( entity_id=100, timestamp=datetime(2013, 6, 5, 12, 0, 0), attribute_names=["a", "b"], values=[1, 42] ) notificationstore.store_record(datarecord)(cursor)
def test_create(conn): with closing(conn.cursor()) as cursor: datasource = name_to_datasource(cursor, "test-source-001") notificationstore = NotificationStore(datasource, []) notificationstore.create(cursor) assert notificationstore.id is not None query = ( "SELECT datasource_id " "FROM notification.notificationstore " "WHERE id = %s") args = (notificationstore.id,) cursor.execute(query, args) eq_(cursor.rowcount, 1) datasource_id, = cursor.fetchone() eq_(datasource_id, datasource.id)
def store(self, column_names, fields, raw_data_rows): with closing(self.conn.cursor()) as cursor: datasource = DataSource.from_name(cursor, self.datasource) notificationstore = NotificationStore.load(cursor, datasource) rows = list(raw_data_rows) if notificationstore: datatype_dict = { attribute.name: attribute.data_type for attribute in notificationstore.attributes } def merge_datatypes(): for name in column_names: configured_descriptor = fields.get(name) notificationstore_type = datatype_dict[name] if configured_descriptor: if configured_descriptor.data_type.name != notificationstore_type: raise Exception("Attribute({} {}) type of notificationstore does not match configured" " type: {}".format(name, notificationstore_type, configured_descriptor.data_type.name)) yield configured_descriptor else: yield ColumnDescriptor(name, datatype_map[notificationstore_type], {}) column_descriptors = list(merge_datatypes()) else: deduced_datatype_names = deduce_data_types( map(operator.itemgetter(2), rows) ) def merge_datatypes(): for column_name, datatype_name in zip(column_names, deduced_datatype_names): configured_descriptor = fields.get(column_name) if configured_descriptor: yield configured_descriptor else: yield ColumnDescriptor(column_name, datatype_map[datatype_name], {}) column_descriptors = list(merge_datatypes()) attributes = [ Attribute(name, column_descriptor.data_type.name, '') for name, column_descriptor in zip(column_names, column_descriptors) ] notificationstore = NotificationStore( datasource, attributes ).create(cursor) self.conn.commit() parsers = [column_descriptor.string_parser() for column_descriptor in column_descriptors] for dn, timestamp, values in rows: record = Record( EntityDnRef(dn), timestamp, column_names, [parse(value) for parse, value in zip(parsers, values)] ) notificationstore.store_record(record)(cursor) self.conn.commit()
def f(cursor): return NotificationStore.load(cursor, datasource, entitytype)