def filter_samples(date_from=datetime.datetime(1970, 1, 1, 0, 0).strftime("%Y-%m-%dT%H:%M"), date_to=_get_date(), workstation_id=-1, sensor_id=-1): samples = [] if date_from == "" or date_from is None: date_from = datetime.datetime(1970, 1, 1, 0, 0).strftime("%Y-%m-%dT%H:%M") if date_to == "" or date_to is None: date_to = _get_date() if sensor_id != -1: for sample in Sample.select().where((Sample.timestamp >= date_from) & (Sample.timestamp <= date_to) & (Sample.sensor_id == sensor_id)): samples.append(model_to_dict(sample)) elif sensor_id == -1 and workstation_id != -1: # sq_sensors = Sensor.select(Sensor.sensor_id).where(Sensor.station_id == workstation_id) # (Sample.sensor_id__in == sq_sensors) sq = Sample.select().join(Sensor, on=(Sample.sensor_id == Sensor.sensor_id)).where((Sample.timestamp >= date_from) & (Sample.timestamp <= date_to) & (Sensor.station_id == workstation_id)) print(sq.sql()[0]) for sample in sq: samples.append(model_to_dict(sample)) elif sensor_id == -1 and workstation_id == -1: for sample in Sample.select().where((Sample.timestamp >= date_from) & (Sample.timestamp <= date_to)): samples.append(model_to_dict(sample)) print(str(samples)) return samples
def get_sample_by_sensor(sensor_id): samples = [] for sample in Sample.select().where(Sample.sensor_id == sensor_id): samples.append(model_to_dict(sample)) return samples
def create_sample(s_value, sensor_id, s_date=_get_date()): if s_date is None or s_date == "": s_date = _get_date() Sample.create(value=s_value, sensor_id=sensor_id, timestamp=s_date)
def put_sample(sample_id, s_value, s_date=_get_date()): # method for editing sample_to_update = Sample.select().where(Sample.sample_id == sample_id).get() sample_to_update.value = s_value sample_to_update.timestamp = s_date sample_to_update.save()
def get_sample(sample_id): return model_to_dict(Sample.get_by_id(sample_id))
def insert_data(): workstations = [ {'station_name': 'Station1', 'station_description': 'Station1 Description'}, {'station_name': 'Station2', 'station_description': 'Station2 Description'}, {'station_name': 'Station3', 'station_description': 'Station3 Description'}, {'station_name': 'Station4', 'station_description': 'Station4 Description'}, {'station_name': 'Station5', 'station_description': 'Station5 Description'} ] Workstation.insert_many(workstations).execute() sensors = [ {'sensor_name': 'Sensor1', 'sensor_description': 'Sensor1 Description', 'station_id': 1}, {'sensor_name': 'Sensor2', 'sensor_description': 'Sensor2 Description', 'station_id': 2}, {'sensor_name': 'Sensor3', 'sensor_description': 'Sensor3 Description', 'station_id': 3}, {'sensor_name': 'Sensor4', 'sensor_description': 'Sensor4 Description', 'station_id': 4}, {'sensor_name': 'Sensor5', 'sensor_description': 'Sensor5 Description', 'station_id': 5}, {'sensor_name': 'Sensor6', 'sensor_description': 'Sensor6 Description', 'station_id': 5}, {'sensor_name': 'Sensor7', 'sensor_description': 'Sensor7 Description', 'station_id': 4} ] Sensor.insert_many(sensors).execute() samples = [ {'value': 0.1, 'timestamp': _get_date(), 'sensor_id': 7}, {'value': 0.2, 'timestamp': _get_date(), 'sensor_id': 6}, {'value': 0.3, 'timestamp': _get_date(), 'sensor_id': 5}, {'value': 0.4, 'timestamp': _get_date(), 'sensor_id': 4}, {'value': 0.5, 'timestamp': _get_date(), 'sensor_id': 3}, {'value': 0.6, 'timestamp': _get_date(), 'sensor_id': 2}, {'value': 0.7, 'timestamp': _get_date(), 'sensor_id': 1}, {'value': 0.8, 'timestamp': _get_date(), 'sensor_id': 1}, {'value': 0.9, 'timestamp': _get_date(), 'sensor_id': 2}, {'value': 1.099999, 'timestamp': _get_date(), 'sensor_id': 3} ] Sample.insert_many(samples).execute() print("workstations, sensors, samples added!")
def get_all_samples(): samples = [] for sample in Sample.select(): samples.append(model_to_dict(sample)) return samples