From ba92a3bb662deedbd63ee308c1e9fddf87eebd2f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ensar=20Saraj=C4=8Di=C4=87?= Date: Tue, 30 Oct 2018 21:46:33 +0100 Subject: [PATCH 1/2] Make response for device recording query prettier --- app/api/resources/device.py | 3 ++- .../create_device_recording_query_spec.yaml | 8 +++++++- app/devices/api.py | 14 ++++++++++---- app/jsonql/api.py | 18 ++++++++++++++---- 4 files changed, 33 insertions(+), 10 deletions(-) diff --git a/app/api/resources/device.py b/app/api/resources/device.py index 50382c9..9a17b30 100644 --- a/app/api/resources/device.py +++ b/app/api/resources/device.py @@ -115,7 +115,8 @@ class DeviceRecordingQueryResource(ProtectedResource): def post(self, args, device_id): validate_device_ownership(device_id) try: - return devices.run_custom_query(device_id, args), 200 + return {'content': + devices.run_custom_query(device_id, args)}, 200 except ValueError as e: abort(400, message=str(e), status='error') diff --git a/app/api/resources/swagger/create_device_recording_query_spec.yaml b/app/api/resources/swagger/create_device_recording_query_spec.yaml index 74b3e36..453ea36 100644 --- a/app/api/resources/swagger/create_device_recording_query_spec.yaml +++ b/app/api/resources/swagger/create_device_recording_query_spec.yaml @@ -17,4 +17,10 @@ responses: 200: description: Success schema: - type: array + type: object + required: + - content + properties: + content: + type: array + diff --git a/app/devices/api.py b/app/devices/api.py index 8808c45..3c63856 100644 --- a/app/devices/api.py +++ b/app/devices/api.py @@ -284,7 +284,13 @@ def run_custom_query(device_id, request): resulting_query = jsonql.run_query_on(Recording.query.with_entities(), recording_field_provider, **request) - print("Resulting query: " + str(resulting_query)) - result = resulting_query.filter(Recording.device_id == device_id).all() - print("RESULT: " + str(result)) - return result + final_query = resulting_query.filter(Recording.device_id == device_id) + resulting_columns = final_query.column_descriptions + result = final_query.all() + formatted_result = [] + for row in result: + formatted_row = {} + for idx, col in enumerate(row): + formatted_row[resulting_columns[idx]['name']] = col + formatted_result.append(formatted_row) + return formatted_result diff --git a/app/jsonql/api.py b/app/jsonql/api.py index 99e12d4..0b7efc9 100644 --- a/app/jsonql/api.py +++ b/app/jsonql/api.py @@ -2,7 +2,7 @@ from app.core import db GROUPS = ['sum', 'avg', 'count'] FILTERS = ['$gt', '$lt', '$eq'] -PERIODS = ['year', 'month', 'week', 'day'] +PERIODS = ['year', 'month', 'week', 'day', 'hour', 'minute', 'second'] def run_query_on(query_object, field_provider, **kwargs): @@ -12,9 +12,20 @@ def run_query_on(query_object, field_provider, **kwargs): print('Starting with args: ' + str(kwargs)) if selections is not None: + if groups is not None: + for group in groups.keys(): + entities.append( + get_group( + field_provider(group), + groups[group] + ).label( + 'group_' + str(group) # + '_' + groups[group] + ) + ) + for selection in selections.keys(): entities.append(get_column(selections[selection], - field_provider(selection))) + field_provider(selection)).label(selection)) print('New entities: ' + str(entities)) query_object = query_object.with_entities(*entities) @@ -29,8 +40,7 @@ def run_query_on(query_object, field_provider, **kwargs): if groups is not None: for group in groups.keys(): - query_object = query_object.group_by( - get_group(field_provider(group), groups[group])) + query_object = query_object.group_by('group_' + str(group)) return query_object From 5e47e14ba63cf0793b769e217fc17359b2a221b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ensar=20Saraj=C4=8Di=C4=87?= Date: Tue, 30 Oct 2018 21:47:16 +0100 Subject: [PATCH 2/2] Version 0.3.5 --- config.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config.py b/config.py index 0bff46a..16725e8 100644 --- a/config.py +++ b/config.py @@ -2,7 +2,7 @@ import os # App configuration DEBUG = os.environ['DEBUG'] -APP_VERSION = '0.3.4' +APP_VERSION = '0.3.5' # Define the application directory BASE_DIR = os.path.abspath(os.path.dirname(__file__))