Browse Source

handle converting between different units in price_check.py and fix reconcile.py

Daniel Sheffield 3 years ago
parent
commit
ddc701c47b
3 changed files with 29 additions and 11 deletions
  1. 1 1
      db_utils.py
  2. 26 9
      price_view.py
  3. 2 1
      reconcile.py

+ 1 - 1
db_utils.py

@@ -105,7 +105,7 @@ class QueryManager(object):
         self.cursor = cursor
         self.activity_manager = activity_manager
     
-    def get_historic_prices(self, rating_cb, sort, product, unit, organic=None, limit='90 days'):
+    def get_historic_prices(self, rating_cb, sort, product, unit, organic=None, limit='365 days'):
         statement = get_historic_prices_statement(sort, product, unit, organic=organic, limit=limit)
         #print(self.cursor.mogrify(statement).decode('utf-8'))
         #input()

+ 26 - 9
price_view.py

@@ -22,11 +22,6 @@ def get_where(product, unit, organic=None, limit='90 days'):
         SQL('='),
         Literal(product)
     ]))
-    where.append(SQL(' ').join([
-        Identifier('units', 'name'),
-        SQL('='),
-        Literal(unit),
-    ]))
     if organic is not None:
         where.append(SQL(' ').join([
             Identifier('organic'),
@@ -39,6 +34,11 @@ def get_where(product, unit, organic=None, limit='90 days'):
             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      "),
@@ -48,6 +48,16 @@ def get_where(product, unit, organic=None, limit='90 days'):
 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')),
         #('ts', SQL('{identifier}::date').format(
@@ -60,10 +70,10 @@ def get_historic_prices_statement(sort, product, unit, organic=None, limit='90 d
         #('volume', Identifier('quantity')),
         #('unit', Identifier('units', 'name')),
         #('price', Identifier('price')),
-        ('$/unit', SQL("""TRUNC(price/quantity,4)""")),
-        ('avg', SQL(f"""TRUNC(sum(price) OVER {partition} / sum(quantity) OVER {partition}, 4)""")),
-        ('min', SQL(f"""TRUNC(min(price/quantity) OVER {partition}, 4)""")),
-        ('max', SQL(f"""TRUNC(max(price/quantity) OVER {partition}, 4)""")),
+        ('$/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)""")),
         #('total', SQL("""sum(transactions.price) OVER (PARTITION BY transactions.ts::date ORDER BY transactions.id ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)""")),
         #('group', Identifier('groups','name')),
         #('category', Identifier('categories','name')),
@@ -76,6 +86,7 @@ def get_historic_prices_statement(sort, product, unit, organic=None, limit='90 d
         #('price_max', SQL(f"""max(price/quantity) OVER {partition}""")),
     ])
     statement = SQL('\n').join([
+        _with,
         SQL('').join([
             SQL("SELECT"
                 "\n  "),
@@ -89,6 +100,11 @@ def get_historic_prices_statement(sort, product, unit, organic=None, limit='90 d
                 "\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'),
@@ -114,6 +130,7 @@ 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),

+ 2 - 1
reconcile.py

@@ -1,3 +1,4 @@
+#!/usr/bin/python3
 #
 # Copyright (c) Daniel Sheffield 2021
 #
@@ -15,7 +16,7 @@ import sys
 import os
 import psycopg2
 from db_utils import cursor_as_dict
-from txn_view import get_statement
+from txn_view import get_session_transactions_statement as get_statement
 
 try:
     from db_credentials import HOST, PASSWORD