Browse Source

initial work - need UI and need to add GROUP BY in get_sessions_statement (or in the dataframe)

Daniel Sheffield 2 years ago
parent
commit
70f3abefc9
3 changed files with 13 additions and 4 deletions
  1. 2 1
      app/data/QueryManager.py
  2. 8 0
      app/data/TransactionView.py
  3. 3 3
      app/data/util.py

+ 2 - 1
app/data/QueryManager.py

@@ -32,6 +32,7 @@ display_map = {
     'price': lambda x: f'{x:.4f}',
     'quantity': lambda x: f'{x:.2f}' if x is not None else None,
     'organic': lambda x: 'yes' if x else 'no',
+    'tags': lambda x: '' if not x else x,
 }
 display_mapper: Callable[
     [Any, str], str
@@ -64,7 +65,7 @@ def get_session_transactions(cursor, statement, display):
         'id', 'ts', 'store', 'code', 'quantity',
     ], axis=1).to_string(header=[
         'Description', 'Volume', 'Unit', 'Price', '$/unit', 'Total',
-        'Group', 'Category', 'Product', 'Organic',
+        'Group', 'Category', 'Product', 'Organic', 'Tags'
     ], justify='justify-all', max_colwidth=60, index=False)
 
 

+ 8 - 0
app/data/TransactionView.py

@@ -66,6 +66,12 @@ OVER (
     ('category', Identifier('categories', 'name')),
     ('product', Identifier('products', 'name')),
     ('organic', Identifier('organic')),
+    # need a group by to aggregate
+    #('tags', SQL("""array_agg({tag_name})""").format(
+    #    tag_name=Identifier('tags','name'),
+    #))
+    ('tags', Identifier('tags','name')),
+
 ])
 
 JOINS = OrderedDict([
@@ -74,6 +80,8 @@ JOINS = OrderedDict([
     ('products', ('id', 'product_id')),
     ('categories', ('id', 'category_id')),
     ('groups', ('id', 'group_id')),
+    ('tags_map', ('transaction_id', ('transactions','id'))),
+    ('tags', ('id', ('tags_map','tag_id'))),
 ])
 
 def get_where(date, store, full_name=False, exact_time=False):

+ 3 - 3
app/data/util.py

@@ -29,10 +29,10 @@ def get_from(
         SQL("{table} ON {table_column} = {other_column}").format(
             table=Identifier(table),
             table_column=Identifier(table, table_column),
-            other_column=Identifier(other_column)
+            other_column=Identifier(*other_column) if isinstance(other_column,tuple) else Identifier(other_column)
         ) for table, (table_column, other_column) in table_to_join_on.items()
     ]
     return SQL('').join([SQL("""FROM {base}
-JOIN """).format(base=Identifier(base)),
+LEFT JOIN """).format(base=Identifier(base)),
         SQL("""
-JOIN """).join(joins)])
+LEFT JOIN """).join(joins)])