Make response for device recording query prettier

develop
Ensar Sarajčić 2018-10-30 21:46:33 +01:00
parent 141cf654ce
commit ba92a3bb66
4 changed files with 33 additions and 10 deletions

View File

@ -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')

View File

@ -17,4 +17,10 @@ responses:
200:
description: Success
schema:
type: array
type: object
required:
- content
properties:
content:
type: array

View File

@ -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

View File

@ -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