Add optional filters for device recordings
parent
15ffd04281
commit
3e0218b0d8
|
@ -104,8 +104,14 @@ class DeviceRecordingResource(ProtectedResource):
|
||||||
@swag_from('swagger/get_device_recordings_spec.yaml')
|
@swag_from('swagger/get_device_recordings_spec.yaml')
|
||||||
def get(self, device_id):
|
def get(self, device_id):
|
||||||
validate_device_ownership(device_id)
|
validate_device_ownership(device_id)
|
||||||
|
request_args = request.args
|
||||||
return RecordingsWrapperSchema().dump(
|
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')
|
@swag_from('swagger/create_device_recording_spec.yaml')
|
||||||
def post(self, device_id):
|
def post(self, device_id):
|
||||||
|
|
|
@ -9,6 +9,24 @@ parameters:
|
||||||
required: true
|
required: true
|
||||||
type: integer
|
type: integer
|
||||||
description: Id of the device
|
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:
|
responses:
|
||||||
200:
|
200:
|
||||||
description: Success
|
description: Success
|
||||||
|
|
|
@ -84,6 +84,31 @@ def get_device_recordings(device_id):
|
||||||
return Recording.get_many(device_id=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):
|
def get_device(device_id):
|
||||||
"""
|
"""
|
||||||
Tries to get device with given parameters. Raises error on failure
|
Tries to get device with given parameters. Raises error on failure
|
||||||
|
|
|
@ -1,4 +1,6 @@
|
||||||
|
import time
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
import datetime as datetime_module
|
||||||
from app.core import db
|
from app.core import db
|
||||||
from sqlalchemy.dialects.postgresql import JSON
|
from sqlalchemy.dialects.postgresql import JSON
|
||||||
|
|
||||||
|
@ -61,6 +63,33 @@ class Recording(db.Model):
|
||||||
"""
|
"""
|
||||||
return Recording.query.filter_by(**kwargs).all()
|
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
|
@staticmethod
|
||||||
def get(**kwargs):
|
def get(**kwargs):
|
||||||
"""
|
"""
|
||||||
|
|
Loading…
Reference in New Issue