|
@@ -15,13 +15,26 @@ from collections import (
|
|
|
OrderedDict,
|
|
|
)
|
|
|
|
|
|
-def get_where(product, unit, organic=None, limit='90 days'):
|
|
|
+def get_where(unit, product=None, category=None, group=None, organic=None, limit='90 days'):
|
|
|
where = [ ]
|
|
|
- where.append(SQL(' ').join([
|
|
|
- Identifier('products', 'name'),
|
|
|
- SQL('='),
|
|
|
- Literal(product)
|
|
|
- ]))
|
|
|
+ if product is not None:
|
|
|
+ where.append(SQL(' ').join([
|
|
|
+ Identifier('products', 'name'),
|
|
|
+ SQL('='),
|
|
|
+ Literal(product)
|
|
|
+ ]))
|
|
|
+ if category is not None:
|
|
|
+ where.append(SQL(' ').join([
|
|
|
+ Identifier('categories', 'name'),
|
|
|
+ SQL('='),
|
|
|
+ Literal(category)
|
|
|
+ ]))
|
|
|
+ if group is not None:
|
|
|
+ where.append(SQL(' ').join([
|
|
|
+ Identifier('groups', 'name'),
|
|
|
+ SQL('='),
|
|
|
+ Literal(group)
|
|
|
+ ]))
|
|
|
if organic is not None:
|
|
|
where.append(SQL(' ').join([
|
|
|
Identifier('organic'),
|
|
@@ -46,7 +59,7 @@ def get_where(product, unit, organic=None, limit='90 days'):
|
|
|
SQL("\n AND ").join(where),
|
|
|
])
|
|
|
|
|
|
-def get_historic_prices_statement(sort, product, unit, organic=None, limit='90 days'):
|
|
|
+def get_historic_prices_statement(unit, sort=None, product=None, category=None, group=None, organic=None, limit='90 days'):
|
|
|
partition = f"(PARTITION BY {'organic,' if organic is not None else ''} product_id, unit_id)"
|
|
|
organic_sort = f"{'organic,' if organic is not None else ''}"
|
|
|
_with = SQL("""WITH conv AS (
|
|
@@ -61,12 +74,16 @@ def get_historic_prices_statement(sort, product, unit, organic=None, limit='90 d
|
|
|
)""")
|
|
|
select = OrderedDict([
|
|
|
('id', Identifier('transactions','id')),
|
|
|
+ ('ts_raw', SQL("""(transactions.ts AT TIME ZONE 'UTC')::timestamp without time zone""")),
|
|
|
('%d/%m/%y %_I%P', SQL("""(transactions.ts AT TIME ZONE 'UTC')::timestamp without time zone""")),
|
|
|
('code', Identifier('stores', 'code')),
|
|
|
('$/unit', SQL("""TRUNC(price / (quantity * factor), 4)""")),
|
|
|
('avg', SQL(f"""TRUNC(sum(price) OVER {partition} / sum(quantity * factor) OVER {partition}, 4)""")),
|
|
|
('min', SQL(f"""TRUNC(min(price / (quantity * factor)) OVER {partition}, 4)""")),
|
|
|
('max', SQL(f"""TRUNC(max(price / (quantity * factor)) OVER {partition}, 4)""")),
|
|
|
+ ('product', Identifier('products','name')),
|
|
|
+ ('category', Identifier('categories', 'name')),
|
|
|
+ ('group', Identifier('groups', 'name')),
|
|
|
('organic', Identifier('organic')),
|
|
|
])
|
|
|
statement = SQL('\n').join([
|
|
@@ -114,12 +131,11 @@ def get_historic_prices_statement(sort, product, unit, organic=None, limit='90 d
|
|
|
key=Identifier('id'),
|
|
|
index=Identifier('group_id')
|
|
|
),
|
|
|
-
|
|
|
]),
|
|
|
]),
|
|
|
- get_where(product, unit, organic=organic, limit=limit),
|
|
|
- SQL('ORDER BY {organic_sort} {sort} {direction}, code, "$/unit" ASC, ts DESC').format(
|
|
|
- sort=Identifier(sort),
|
|
|
+ get_where(unit, product=product, category=category, group=group, organic=organic, limit=limit),
|
|
|
+ SQL('ORDER BY {organic_sort} {sort} code, product, category, "group", "$/unit" ASC, ts DESC').format(
|
|
|
+ sort=SQL(f'{Identifier(sort)} {direction},' if sort is not None else ''),
|
|
|
direction=SQL('DESC' if sort == 'ts' else 'ASC'),
|
|
|
organic_sort=SQL(organic_sort),
|
|
|
),
|