2018-10-30 19:16:27 +00:00
|
|
|
from app.core import db
|
|
|
|
|
|
|
|
GROUPS = ['sum', 'avg', 'count']
|
|
|
|
FILTERS = ['$gt', '$lt', '$eq']
|
2018-10-30 20:46:33 +00:00
|
|
|
PERIODS = ['year', 'month', 'week', 'day', 'hour', 'minute', 'second']
|
2018-10-30 23:03:25 +00:00
|
|
|
ORDERS = ['asc', 'desc']
|
2018-10-30 19:16:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
def run_query_on(query_object, field_provider, **kwargs):
|
2018-11-03 13:53:26 +00:00
|
|
|
"""
|
|
|
|
Generates a query for target object based on query provided as kwargs
|
|
|
|
|
|
|
|
:param query_object: Initial query object as returned by SQLAlchemy for
|
|
|
|
target table
|
|
|
|
:type query_object: Query
|
|
|
|
:param field_provider: Function which provides fields based on name, with
|
|
|
|
optional parameter formatted which returns the field formatted using sql
|
|
|
|
functions
|
|
|
|
:type field_provider: func(col_name:String, formatted:Boolean)
|
|
|
|
"""
|
2018-10-30 23:03:25 +00:00
|
|
|
selections, filters, groups, orderings = validate_selections(**kwargs)
|
2018-10-30 19:16:27 +00:00
|
|
|
entities = []
|
|
|
|
|
|
|
|
if selections is not None:
|
2018-10-30 20:46:33 +00:00
|
|
|
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]
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2018-10-30 19:16:27 +00:00
|
|
|
for selection in selections.keys():
|
|
|
|
entities.append(get_column(selections[selection],
|
2018-11-03 14:32:46 +00:00
|
|
|
field_provider(selection)).label(selection))
|
2018-10-30 19:16:27 +00:00
|
|
|
|
|
|
|
query_object = query_object.with_entities(*entities)
|
|
|
|
|
|
|
|
if filters is not None:
|
|
|
|
for filtered_column in filters.keys():
|
|
|
|
for actual_filter in filters[filtered_column].keys():
|
|
|
|
query_object = query_object.filter(
|
|
|
|
get_filter(field_provider(filtered_column),
|
|
|
|
actual_filter,
|
|
|
|
filters[filtered_column][actual_filter]))
|
|
|
|
|
|
|
|
if groups is not None:
|
|
|
|
for group in groups.keys():
|
2018-10-30 20:46:33 +00:00
|
|
|
query_object = query_object.group_by('group_' + str(group))
|
2018-10-30 19:16:27 +00:00
|
|
|
|
2018-10-30 23:03:25 +00:00
|
|
|
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])
|
|
|
|
|
2018-10-30 19:16:27 +00:00
|
|
|
return query_object
|
|
|
|
|
|
|
|
|
|
|
|
def get_column(selection_name, field):
|
|
|
|
if selection_name == 'value':
|
|
|
|
return field
|
|
|
|
if selection_name == 'sum':
|
|
|
|
return db.func.sum(field)
|
|
|
|
if selection_name == 'avg':
|
|
|
|
return db.func.avg(field)
|
|
|
|
if selection_name == 'count':
|
|
|
|
return db.func.count(field)
|
|
|
|
|
|
|
|
|
|
|
|
def get_filter(filter_column, filter_key, filter_value):
|
|
|
|
if filter_key == '$gt':
|
|
|
|
return filter_column > filter_value
|
|
|
|
if filter_key == '$lt':
|
|
|
|
return filter_column < filter_value
|
|
|
|
if filter_key == '$eq':
|
|
|
|
return filter_column == filter_value
|
|
|
|
|
|
|
|
|
|
|
|
def get_group(group_column, group_value):
|
|
|
|
if group_value == 'value':
|
|
|
|
return group_column
|
|
|
|
if group_value in PERIODS:
|
|
|
|
return db.func.date_part(group_value, group_column)
|
|
|
|
# We now expect a date format
|
|
|
|
return db.func.to_char(group_column, group_value)
|
|
|
|
|
|
|
|
|
|
|
|
def is_group(**kwargs):
|
|
|
|
if kwargs.get('group') is not None:
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
def validate_selections(**kwargs):
|
|
|
|
selections = kwargs.get('selections')
|
|
|
|
filters = kwargs.get('filters')
|
|
|
|
groups = kwargs.get('groups')
|
2018-10-30 23:03:25 +00:00
|
|
|
orderings = kwargs.get('orders')
|
2018-10-30 19:16:27 +00:00
|
|
|
|
|
|
|
if selections is None:
|
|
|
|
raise ValueError("Missing selections!")
|
|
|
|
|
|
|
|
if is_group(**kwargs):
|
|
|
|
for key in selections.keys():
|
|
|
|
if selections[key] not in GROUPS:
|
2018-10-30 23:03:25 +00:00
|
|
|
raise ValueError("Can only use " + str(GROUPS) + " when " +
|
|
|
|
"grouping!")
|
2018-10-30 19:16:27 +00:00
|
|
|
|
|
|
|
if filters is not None:
|
|
|
|
for key in filters.keys():
|
|
|
|
for inner_key in filters[key].keys():
|
|
|
|
if inner_key not in FILTERS:
|
2018-10-30 23:03:25 +00:00
|
|
|
raise ValueError("Invalid filter (" + str(
|
|
|
|
inner_key) + "). Valid filters: " + str(FILTERS))
|
|
|
|
|
|
|
|
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))
|
2018-10-30 19:16:27 +00:00
|
|
|
|
2018-10-30 23:03:25 +00:00
|
|
|
return selections, filters, groups, orderings
|