commit
4e1669cb42
|
@ -115,7 +115,8 @@ class DeviceRecordingQueryResource(ProtectedResource):
|
||||||
def post(self, args, device_id):
|
def post(self, args, device_id):
|
||||||
validate_device_ownership(device_id)
|
validate_device_ownership(device_id)
|
||||||
try:
|
try:
|
||||||
return devices.run_custom_query(device_id, args), 200
|
return {'content':
|
||||||
|
devices.run_custom_query(device_id, args)}, 200
|
||||||
except ValueError as e:
|
except ValueError as e:
|
||||||
abort(400, message=str(e), status='error')
|
abort(400, message=str(e), status='error')
|
||||||
|
|
||||||
|
|
|
@ -17,4 +17,10 @@ responses:
|
||||||
200:
|
200:
|
||||||
description: Success
|
description: Success
|
||||||
schema:
|
schema:
|
||||||
|
type: object
|
||||||
|
required:
|
||||||
|
- content
|
||||||
|
properties:
|
||||||
|
content:
|
||||||
type: array
|
type: array
|
||||||
|
|
||||||
|
|
|
@ -284,7 +284,13 @@ def run_custom_query(device_id, request):
|
||||||
resulting_query = jsonql.run_query_on(Recording.query.with_entities(),
|
resulting_query = jsonql.run_query_on(Recording.query.with_entities(),
|
||||||
recording_field_provider,
|
recording_field_provider,
|
||||||
**request)
|
**request)
|
||||||
print("Resulting query: " + str(resulting_query))
|
final_query = resulting_query.filter(Recording.device_id == device_id)
|
||||||
result = resulting_query.filter(Recording.device_id == device_id).all()
|
resulting_columns = final_query.column_descriptions
|
||||||
print("RESULT: " + str(result))
|
result = final_query.all()
|
||||||
return result
|
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
|
||||||
|
|
|
@ -2,7 +2,7 @@ from app.core import db
|
||||||
|
|
||||||
GROUPS = ['sum', 'avg', 'count']
|
GROUPS = ['sum', 'avg', 'count']
|
||||||
FILTERS = ['$gt', '$lt', '$eq']
|
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):
|
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))
|
print('Starting with args: ' + str(kwargs))
|
||||||
|
|
||||||
if selections is not None:
|
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():
|
for selection in selections.keys():
|
||||||
entities.append(get_column(selections[selection],
|
entities.append(get_column(selections[selection],
|
||||||
field_provider(selection)))
|
field_provider(selection)).label(selection))
|
||||||
|
|
||||||
print('New entities: ' + str(entities))
|
print('New entities: ' + str(entities))
|
||||||
query_object = query_object.with_entities(*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:
|
if groups is not None:
|
||||||
for group in groups.keys():
|
for group in groups.keys():
|
||||||
query_object = query_object.group_by(
|
query_object = query_object.group_by('group_' + str(group))
|
||||||
get_group(field_provider(group), groups[group]))
|
|
||||||
|
|
||||||
return query_object
|
return query_object
|
||||||
|
|
||||||
|
|
|
@ -2,7 +2,7 @@ import os
|
||||||
|
|
||||||
# App configuration
|
# App configuration
|
||||||
DEBUG = os.environ['DEBUG']
|
DEBUG = os.environ['DEBUG']
|
||||||
APP_VERSION = '0.3.4'
|
APP_VERSION = '0.3.5'
|
||||||
|
|
||||||
# Define the application directory
|
# Define the application directory
|
||||||
BASE_DIR = os.path.abspath(os.path.dirname(__file__))
|
BASE_DIR = os.path.abspath(os.path.dirname(__file__))
|
||||||
|
|
Loading…
Reference in New Issue