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