123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 |
- from psycopg2.sql import (
- Identifier,
- SQL,
- Literal,
- Placeholder,
- Composed,
- )
- from collections import (
- OrderedDict,
- )
- def get_where(product, unit, organic=None, limit='90 days'):
- where = [ ]
- where.append(SQL(' ').join([
- Identifier('products', 'name'),
- SQL('='),
- Literal(product)
- ]))
- if organic is not None:
- where.append(SQL(' ').join([
- Identifier('organic'),
- SQL('='),
- Literal(organic),
- ]))
- where.append(
- SQL("{ts} at time zone 'utc' BETWEEN now() - {interval} AND now()").format(
- ts=Identifier('ts'),
- interval=SQL("{literal}::interval").format(literal=Literal(limit))
- )
- )
- where.append(
- SQL("(SELECT id FROM units WHERE name = {unit}) = conv._to").format(
- unit=Literal(unit),
- )
- )
- return SQL('').join([
- SQL("WHERE"
- "\n "),
- SQL("\n AND ").join(where),
- ])
- def get_historic_prices_statement(sort, product, unit, 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 (
- SELECT
- _1._from, _1._to, factor FROM conversions AS _1
- UNION
- SELECT _2._to, _2._from, 1/_2.factor FROM conversions AS _2
- UNION
- SELECT _3._from, _3._from, 1 FROM conversions AS _3
- UNION
- SELECT _4.id AS _from, _4.id AS _to, 1 FROM units AS _4
- )""")
- select = OrderedDict([
- ('id', Identifier('transactions','id')),
-
-
-
- ('%d/%m %_I%P', Identifier('transactions', 'ts')),
-
- ('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)""")),
-
-
-
-
- ('organic', Identifier('organic')),
-
-
-
-
-
- ])
- statement = SQL('\n').join([
- _with,
- SQL('').join([
- SQL("SELECT"
- "\n "),
- SQL(','
- "\n ").join([
- SQL(' ').join([v, SQL('AS'), Identifier(f'{k}')]) for k, v in select.items()
- ])
- ]),
- SQL('').join([
- SQL("FROM"
- "\n "),
- SQL("\n JOIN ").join([
- SQL("transactions"),
- SQL("{table} ON ({table}.{key} = {index})").format(
- table=Identifier('conv'),
- key=Identifier('_from'),
- index=Identifier('unit_id'),
- ),
- SQL("{table} ON {table}.{key} = {index}").format(
- table=Identifier('units'),
- key=Identifier('id'),
- index=Identifier('unit_id'),
- ),
- SQL("{table} ON {table}.{key} = {index}").format(
- table=Identifier('stores'),
- key=Identifier('id'),
- index=Identifier('store_id')
- ),
- SQL("{table} ON {table}.{key} = {index}").format(
- table=Identifier('products'),
- key=Identifier('id'),
- index=Identifier('product_id')
- ),
- SQL("{table} ON {table}.{key} = {index}").format(
- table=Identifier('categories'),
- key=Identifier('id'),
- index=Identifier('category_id')
- ),
- SQL("{table} ON {table}.{key} = {index}").format(
- table=Identifier('groups'),
- 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),
- direction=SQL('DESC' if sort == 'ts' else 'ASC'),
- organic_sort=SQL(organic_sort),
- ),
- ])
- return statement
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
|