Add orders field for custom query

develop
Ensar Sarajčić 2018-10-31 00:03:25 +01:00
parent 5e47e14ba6
commit 83c910956b
4 changed files with 37 additions and 6 deletions

View File

@ -38,6 +38,7 @@ class RecordingsQuerySchema(Schema):
selections = fields.Raw() selections = fields.Raw()
filters = fields.Raw() filters = fields.Raw()
groups = fields.Raw() groups = fields.Raw()
orders = fields.Raw()
class DeviceSecretSchema(BaseResourceSchema): class DeviceSecretSchema(BaseResourceSchema):

View File

@ -3,10 +3,11 @@ 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', 'hour', 'minute', 'second'] PERIODS = ['year', 'month', 'week', 'day', 'hour', 'minute', 'second']
ORDERS = ['asc', 'desc']
def run_query_on(query_object, field_provider, **kwargs): def run_query_on(query_object, field_provider, **kwargs):
selections, filters, groups = validate_selections(**kwargs) selections, filters, groups, orderings = validate_selections(**kwargs)
entities = [] entities = []
print('Starting with args: ' + str(kwargs)) print('Starting with args: ' + str(kwargs))
@ -42,6 +43,20 @@ def run_query_on(query_object, field_provider, **kwargs):
for group in groups.keys(): for group in groups.keys():
query_object = query_object.group_by('group_' + str(group)) query_object = query_object.group_by('group_' + str(group))
if orderings is not None:
for order in orderings.keys():
if ((selections is not None and
order not in selections.keys())
and
(groups is None or
order not in ['group_' + str(group) for group in
groups.keys()])):
raise ValueError(
'Invalid order! Must use one of' +
' selections or groups')
query_object = query_object.order_by(
order + ' ' + orderings[order])
return query_object return query_object
@ -83,6 +98,7 @@ def validate_selections(**kwargs):
selections = kwargs.get('selections') selections = kwargs.get('selections')
filters = kwargs.get('filters') filters = kwargs.get('filters')
groups = kwargs.get('groups') groups = kwargs.get('groups')
orderings = kwargs.get('orders')
if selections is None: if selections is None:
raise ValueError("Missing selections!") raise ValueError("Missing selections!")
@ -90,14 +106,21 @@ def validate_selections(**kwargs):
if is_group(**kwargs): if is_group(**kwargs):
for key in selections.keys(): for key in selections.keys():
if selections[key] not in GROUPS: if selections[key] not in GROUPS:
raise ValueError("Can only use sum, avg and count when\ raise ValueError("Can only use " + str(GROUPS) + " when " +
grouping!") "grouping!")
if filters is not None: if filters is not None:
for key in filters.keys(): for key in filters.keys():
for inner_key in filters[key].keys(): for inner_key in filters[key].keys():
if inner_key not in FILTERS: if inner_key not in FILTERS:
raise ValueError("Unknown filter: " + str( raise ValueError("Invalid filter (" + str(
inner_key)) inner_key) + "). Valid filters: " + str(FILTERS))
return selections, filters, groups if orderings is not None:
for key in orderings.keys():
if orderings[key] not in ORDERS:
raise ValueError("Invalid order type ("
+ orderings[key] + "). " +
"Valid types: " + str(ORDERS))
return selections, filters, groups, orderings

View File

@ -12,5 +12,7 @@
}, },
"group": { "group": {
"date": "month" "date": "month"
},
"order": {
} }
} }

View File

@ -167,6 +167,7 @@ definitions:
- selections - selections
- filters - filters
- groups - groups
- orders
properties: properties:
selections: selections:
type: object type: object
@ -180,6 +181,10 @@ definitions:
type: object type: object
description: GROUP BY part of query description: GROUP BY part of query
example: { "recorded_at": "year" } example: { "recorded_at": "year" }
orders:
type: object
description: ORDER BY part of query
example: { "group_recorded_at": "asc" }
RecordingCreation: RecordingCreation:
type: object type: object