Przeglądaj źródła

fixup various bugs with current price badge

Daniel Sheffield 2 lat temu
rodzic
commit
ba971fcbb6

+ 12 - 1
app/activities/PriceCheck.py

@@ -112,10 +112,21 @@ class PriceCheck(FocusWidget):
             quantity = None
 
         if None in (sort, product, unit):
+            self.text_fields['dbview'].set_text('')
+            self.rating.update_rating(None, None, None, unit)
             return
 
         df = self.query_manager.get_historic_prices_data(unit, sort=sort, product=product, organic=organic)
-        _avg, _min, _max = [ x for x in df[['avg','min','max']].iloc[0].apply(float) ]
+        if df.empty:
+            self.text_fields['dbview'].set_text('')
+            self.rating.update_rating(None, None, None, unit)
+            return
+        
+        assert len(df['avg'].unique()) == 1, f"There should be only one average price: {df['avg'].unique()}"
+        
+        _avg, _min, _max = [
+          float(x) for x in df[['avg','min','max']].iloc[0]
+        ]
         self.rating.update_rating(_avg, _min, _max, unit, price=price, quantity=quantity),
 
         self.text_fields['dbview'].set_text(

+ 16 - 6
app/activities/Rating.py

@@ -22,6 +22,9 @@ class Rating(object):
 
     def update_rating(self, _avg, _min, _max, unit, price=None, quantity=None):
         if None in (_avg, _min, _max):
+            self.text_fields['rating'].set_text('')
+            self.text_fields['spread'].set_text('')
+            self.text_fields['marker'].set_text('')
             return
         current = None if None in (price, quantity or None) else float(price/quantity)
         size = 14
@@ -29,31 +32,38 @@ class Rating(object):
         rating = [' ']*len(chars)
         _min, _max = min(_min, current or _min), max(_max, current or _max)
         ls = np.linspace(_min, _max, len(chars))
-        if current is not None:
+        if current is not None and _avg is not None:
             if current <= _avg:
                 p = 'badge_good'
             else:
                 p = 'badge_bad'
         else:
             p = 'badge_neutral'
+        
         for idx, (e, a) in enumerate(zip(ls, ls[1:])):
             if e <= _avg <= a:
                 for c, (_idx,_) in zip(''.join(list(f'{_avg:>5.2f}')[:idx+2]), filter(lambda x: idx-2 < x[0] and x[0]>0, enumerate(chars))):
                     chars[_idx] = (p, c)
-                chars[0] = '|'
-                chars[-1] = '|'
-
+            
             if current is not None and e <= current < a:
                 rating[idx] = '^'
+        
+        chars[0] = '|'
+        chars[-1] = '|'
 
         if current is not None:
-            self.text_fields['rating'].set_text(f"{current:>5.2f}/{unit} {current/_avg - 1:>4.0%}")
+            self.text_fields['rating'].set_text(
+                f"{current:>5.2f}/{unit} {current/_avg - 1:>4.0%}"
+            )
 
         if _min != _max:
             if current == _max:
                 rating[-1] = '^'
-            self.text_fields['spread'].set_text([f"{_min:>5.2f}", ('badge_highlight', chars), f"{_max:<5.2f}"])
             self.text_fields['marker'].set_text(f"{' '*5}{''.join(rating)}{' '*5}")
+            self.text_fields['spread'].set_text([
+                f"{_min:>5.2f}", ('badge_highlight', chars), f"{_max:<5.2f}"
+            ])
         else:
             self.text_fields['spread'].set_text('')
             self.text_fields['marker'].set_text('')
+        

+ 11 - 2
app/activities/TransactionEditor.py

@@ -109,7 +109,7 @@ class TransactionEditor(FocusWidget):
                 date or None, store or None
             ) else ''
         )
-        self.update_historic_prices(self.data)
+        self.update_historic_prices(data)
         return self
 
     def update_historic_prices(self, data):
@@ -127,10 +127,19 @@ class TransactionEditor(FocusWidget):
             quantity = None
 
         if None in (product, unit):
+            self.rating.update_rating(None, None, None, unit)
             return
 
         df = self.query_manager.get_historic_prices_data(unit, product=product, organic=organic)
-        _avg, _min, _max = [ x for x in df[['avg','min','max']].iloc[0].apply(float) ]
+        if df.empty:
+            self.rating.update_rating(None, None, None, unit)
+            return
+        
+        assert len(df['avg'].unique()) == 1, f"There should be only one average price: {df['avg'].unique()}"
+        
+        _avg, _min, _max = [
+          float(x) for x in df[['avg','min','max']].iloc[0]
+        ]
         self.rating.update_rating(_avg, _min, _max, unit, price=price, quantity=quantity),
 
 

+ 1 - 1
app/price_view.py

@@ -60,7 +60,7 @@ def get_where(unit, product=None, category=None, group=None, organic=None, limit
     ])
 
 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)"
+    partition = f"(PARTITION BY {'organic,' if organic is not None else ''} product_id)"
     organic_sort = f"{'organic,' if organic is not None else ''}"
     sort_sql = SQL('').join([
         SQL('{sort} {direction},').format(