def get_sensor_data(self, id_sensor_data): query = self.session.query(SensorData).filter(SensorData.id_sensor_data == id_sensor_data) my_sensor_data = query.first() # Do something to convert bytes in the right format my_sensor_data.data = DataFormat.from_bytes(my_sensor_data.data, my_sensor_data.channel.id_data_format) return my_sensor_data
def add_recordset(self, participant: Participant, name, start_timestamp, end_timestamp, force=False): if not force: # Check if we already have a recordset for that period ''' query = self.session.query(Recordset)\ .filter(Recordset.participant == participant & ((Recordset.start_timestamp <= start_timestamp & Recordset.end_timestamp >= start_timestamp) | (Recordset.start_timestamp <= end_timestamp & Recordset.end_timestamp >= end_timestamp) | (Recordset.start_timestamp >= start_timestamp & Recordset.end_timestamp <= end_timestamp))) ''' query = self.session.query( Recordset).filter((Recordset.participant == participant) & (Recordset.name == name)) if query.first(): # Update start and end times, if needed. current_record = query.first() print("Recordset found: " + current_record.name) new_starttime = current_record.start_timestamp if start_timestamp < new_starttime: new_starttime = start_timestamp new_endtime = current_record.end_timestamp if end_timestamp > new_endtime: new_endtime = end_timestamp current_record.start_timestamp = new_starttime current_record.end_timestamp = new_endtime self.commit() return current_record # Create object record = Recordset(participant=participant, name=name, start_timestamp=start_timestamp, end_timestamp=end_timestamp) self.session.add(record) self.commit() return record
def add_channel(self, sensor, id_sensor_unit, id_data_format, label): # Check if that sensor is already present in the database query = self.session.query( Channel).filter((Channel.sensor == sensor) & (Channel.id_sensor_unit == id_sensor_unit) & (Channel.id_data_format == id_data_format) & (Channel.label == label)) if query.first(): #print("Channel " + label + " already present in DB!") return query.first() # Create object channel = Channel(sensor=sensor, id_sensor_unit=id_sensor_unit, id_data_format=id_data_format, label=label) self.session.add(channel) self.commit() return channel
def add_sensor(self, _id_sensor_type, _name, _hw_name, _location, _sampling_rate, _data_rate): # Check if that sensor is already present in the database query = self.session.query(Sensor).filter( (Sensor.id_sensor_type == _id_sensor_type) & (Sensor.location == _location) & (Sensor.name == _name) & (Sensor.hw_name == _hw_name) & (Sensor.sampling_rate == _sampling_rate) & (Sensor.data_rate) == _data_rate) if query.first(): #print("Sensor " + _name + " already present in DB!") return query.first() # Create object sensor = Sensor(id_sensor_type=_id_sensor_type, name=_name, hw_name=_hw_name, location=_location, sampling_rate=_sampling_rate, data_rate=_data_rate) self.session.add(sensor) self.commit() return sensor
def sa_get_relationship(session, model, name): """ Resolve the relationship on a SQLAlchemy model to either an object (in the case of one-to-one relationships) or a query to all of the objects (in the case of one-to-many relationships). :param session: The SQLAlchemy session to associate the query with. :param model: The SQLAlchemy model of the object associated with the relationship. :param name: The name of the relationship as it exists in the *model*. :return: Either the object or a SQLAlchemy query for the objects. """ mapper = sqlalchemy.inspect(model.__class__) relationship = mapper.relationships[name] foreign_model = db_models.database_tables[relationship.target.name].model query = session.query(foreign_model) if relationship.uselist: column_name = relationship.primaryjoin.right.name return query.filter(getattr(foreign_model, column_name) == model.id) column_name = relationship.primaryjoin.left.name query = query.filter(getattr(foreign_model, column_name) == getattr(model, relationship.primaryjoin.right.name)) return query.first()
def sa_get_relationship(session, model, name): """ Resolve the relationship on a SQLAlchemy model to either an object (in the case of one-to-one relationships) or a query to all of the objects (in the case of one-to-many relationships). :param session: The SQLAlchemy session to associate the query with. :param model: The SQLAlchemy model of the object associated with the relationship. :param name: The name of the relationship as it exists in the *model*. :return: Either the object or a SQLAlchemy query for the objects. """ mapper = sqlalchemy.inspect(model.__class__) relationship = mapper.relationships[name] foreign_model = db_models.database_tables[relationship.table.name].model query = session.query(foreign_model) if relationship.uselist: column_name = relationship.primaryjoin.right.name return query.filter(getattr(foreign_model, column_name) == model.id) column_name = relationship.primaryjoin.left.name query = query.filter( getattr(foreign_model, column_name) == getattr( model, relationship.primaryjoin.right.name)) return query.first()
def get_dataset(self): query = self.session.query(DataSet) return query.first()
def get_channel(self, id_channel): query = self.session.query(Channel).filter( Channel.id_channel == id_channel) return query.first()
def get_recordset(self, id_recordset): query = self.session.query(Recordset).filter( Recordset.id_recordset == id_recordset) print('get_recordset', query.first()) return query.first()
def get_sensor(self, id_sensor): query = self.session.query(Sensor).filter( Sensor.id_sensor == id_sensor) # print('first sensor', query.first()) return query.first()
def get_participant(self, id_participant): query = self.session.query(Participant).filter( Participant.id_participant == id_participant) return query.first()
def get_group(self, id_group): query = self.session.query(Group).filter(Group.id_group == id_group) # print('first group', query.first()) return query.first()