|
@@ -4,9 +4,11 @@ import seaborn as sns
|
|
|
import seaborn as sns
|
|
|
import matplotlib.pyplot as plt
|
|
|
import os
|
|
|
+import sys
|
|
|
from sqlite3 import Cursor
|
|
|
import psycopg
|
|
|
from db_credentials import HOST, PASSWORD
|
|
|
+ALL_UNITS = {'g','kg','mL','L','Pieces','Bunches','Bags'}
|
|
|
host = f'host={HOST}'
|
|
|
password = f'password={PASSWORD}'
|
|
|
user = os.getenv('USER')
|
|
@@ -21,27 +23,51 @@ for k in ('group', 'category', 'product', 'unit'):
|
|
|
names = [ o for o in options ]
|
|
|
matches = '\t'.join(names)
|
|
|
print(f'{k.title()} names: {matches}')
|
|
|
- fields[k] = input(f'{k.title()}: ')
|
|
|
- if (fields[k] == ''):
|
|
|
- continue
|
|
|
- options = query_manager.unique_suggestions(k, **fields)
|
|
|
- while (len(options) != 1):
|
|
|
+ while (len(options) >=1):
|
|
|
+ v = fields[k] if k in fields else ''
|
|
|
+ fields[k] = v + input(f"{k.title()}: {v}")
|
|
|
options = query_manager.unique_suggestions(k, **fields)
|
|
|
+ if k == 'unit':
|
|
|
+ options = sorted(set(options) | ALL_UNITS)
|
|
|
+
|
|
|
+ if fields[k] == '':
|
|
|
+ choice = ''
|
|
|
+ break
|
|
|
+ elif len(options) == 1:
|
|
|
+ choice = options[0]
|
|
|
+ break
|
|
|
+ elif fields[k].lower() in map(lambda x: x.lower(), options):
|
|
|
+ choice = next(
|
|
|
+ filter(lambda x: x.lower() == fields[k].lower(), options)
|
|
|
+ )
|
|
|
+ break
|
|
|
+ elif len(options) == 0 and k == 'unit':
|
|
|
+ choice = fields[k]
|
|
|
+ break
|
|
|
matches = '\t'.join(options)
|
|
|
print(f'Matches ({k}): {matches}')
|
|
|
- fields[k] += input(f'{k.title()}: {fields[k]}')
|
|
|
- fields[k] = options[0] if options[0] in names else ''
|
|
|
+
|
|
|
+ if k != 'unit':
|
|
|
+ fields[k] = (fields[k] and choice) or ''
|
|
|
+ else:
|
|
|
+ fields[k] = choice
|
|
|
if fields[k] == '':
|
|
|
- print(f'Ignoring {k} {options[0]} as it does not exist')
|
|
|
+ print(f'Ignoring {k} {choice} as it does not exist')
|
|
|
else:
|
|
|
print(f'Selected {k}: {fields[k]}')
|
|
|
print()
|
|
|
|
|
|
fields = dict((k,v or None) for k,v in fields.items())
|
|
|
-
|
|
|
-print(fields)
|
|
|
unit = fields['unit'] or 'kg'
|
|
|
+if unit not in ALL_UNITS:
|
|
|
+ print(f'Invalid unit: {unit}')
|
|
|
+ exit(2)
|
|
|
+
|
|
|
+print(f'Getting data for selection:\n ')
|
|
|
+print(f'\n '.join([f'{k.title()}: {v}' for k,v in fields.items()]))
|
|
|
d = query_manager.get_historic_prices_data(unit, **dict((k,v) for k,v in fields.items() if k != 'unit'))
|
|
|
+if d.empty:
|
|
|
+ sys.exit(1)
|
|
|
sns.set_theme()
|
|
|
d = d.pivot_table(index=['ts_raw',], columns=['product',], values=['$/unit'], aggfunc='mean')
|
|
|
d.columns = d.columns.droplevel()
|