diff --git a/app/api/resources/device.py b/app/api/resources/device.py index 0dc4cfa..ce123f5 100644 --- a/app/api/resources/device.py +++ b/app/api/resources/device.py @@ -104,8 +104,14 @@ class DeviceRecordingResource(ProtectedResource): @swag_from('swagger/get_device_recordings_spec.yaml') def get(self, device_id): validate_device_ownership(device_id) + request_args = request.args return RecordingsWrapperSchema().dump( - {'recordings': devices.get_device_recordings(device_id)}), 200 + {'recordings': + devices.get_device_recordings_filtered( + device_id, + request_args.get('record_type'), + request_args.get('start_date'), + request_args.get('end_date'))}), 200 @swag_from('swagger/create_device_recording_spec.yaml') def post(self, device_id): diff --git a/app/api/resources/swagger/get_device_recordings_spec.yaml b/app/api/resources/swagger/get_device_recordings_spec.yaml index a22f095..8485b74 100644 --- a/app/api/resources/swagger/get_device_recordings_spec.yaml +++ b/app/api/resources/swagger/get_device_recordings_spec.yaml @@ -9,6 +9,24 @@ parameters: required: true type: integer description: Id of the device + - in: path + name: record_type + required: false + schema: + type: integer + description: requested record_type + - in: path + name: start_date + required: false + schema: + type: string + description: start date of filter + - in: path + name: end_date + required: false + schema: + type: string + description: end date of filter responses: 200: description: Success diff --git a/app/devices/__init__.py b/app/devices/__init__.py index 30a5ec2..e939f9f 100644 --- a/app/devices/__init__.py +++ b/app/devices/__init__.py @@ -84,6 +84,31 @@ def get_device_recordings(device_id): return Recording.get_many(device_id=device_id) +def get_device_recordings_filtered(device_id, record_type=None, + start_date=None, end_date=None): + """ + Tries to get device recording for device with given parameters. Raises + error on failure + + :param device_id: Id of device + :param record_type: Type of recording + :param start_date: Lower date limit + :param end_date: Upper date limit + :type device_id: int + :type record_type: int + :type start_date: Date (string: %d-%m-%Y) + :type end_date: Date (string: %d-%m-%Y) + :returns: List of Recordings for given filters + :rtype: List of Recording + :raises: ValueError if device does not exist + """ + if not Device.exists(id=device_id): + raise ValueError("Device with id %s does not exist" % device_id) + + return Recording.get_many_filtered(device_id, record_type, + start_date, end_date) + + def get_device(device_id): """ Tries to get device with given parameters. Raises error on failure diff --git a/app/devices/models.py b/app/devices/models.py index 5489cf7..46cc9ae 100644 --- a/app/devices/models.py +++ b/app/devices/models.py @@ -1,4 +1,6 @@ +import time from datetime import datetime +import datetime as datetime_module from app.core import db from sqlalchemy.dialects.postgresql import JSON @@ -61,6 +63,33 @@ class Recording(db.Model): """ return Recording.query.filter_by(**kwargs).all() + @staticmethod + def get_many_filtered(device_id, record_type, date_start, date_end): + """ + Get many recording with given filters as a list + + Available filters: + * record_type + * recorded_at (upper and lower limit) + """ + query = Recording.query.filter(Recording.device_id == device_id) + if record_type is not None: + query = query.filter(Recording.record_type == record_type) + if date_start is not None: + lower_limit = time.mktime(datetime.strptime(date_start, + "%d-%m-%Y").timetuple()) + query = query.filter(Recording.recorded_at > + db.func.to_timestamp(lower_limit)) + if date_end is not None: + upper_limit = time.mktime( + (datetime.strptime( + date_end, + "%d-%m-%Y" + )+datetime_module.timedelta(days=1)).timetuple()) + query = query.filter(Recording.recorded_at < + db.func.to_timestamp(upper_limit)) + return query.all() + @staticmethod def get(**kwargs): """