stanley-king vor 2 Jahren
Ursprung
Commit
1c01ed400e
3 geänderte Dateien mit 19 neuen und 12 gelöschten Zeilen
  1. 5 7
      plot/app.py
  2. 13 3
      plot/refill/ChannelPainter.py
  3. 1 2
      plot/refill/MerchantPainter.py

+ 5 - 7
plot/app.py

@@ -55,8 +55,7 @@ def parse_parmeter():
 
 def onError(ex):
     logger.error(ex)
-    data = base64.b64encode(b'').decode("ascii")
-    return f"<img src='data:image/png;base64,{data}'/>"
+    return jsonify({'state': 'fail'})
 
 
 @app.route('/plot/ch_ratio')
@@ -68,10 +67,10 @@ def ch_ratio():
         chnames = filter_chname(chnames)
 
         painter = ChannelPainter(start_time=start_time, end_time=end_time, chnames=chnames, card_types=card_types, spec=spec, filter_wave=filter_wave)
-        buf = painter.paint()
+        buf, ratios = painter.paint()
         data = base64.b64encode(buf.getbuffer()).decode("ascii")
 
-        return f"<img src='data:image/png;base64,{data}'/>"
+        return jsonify({'img': data, 'ratios': ratios, 'state': 'success'})
     except Exception as ex:
         return onError(ex)
 
@@ -105,11 +104,10 @@ def mch_ratio():
         mchids = filter_mchids(mchids)
 
         painter = MerchantPainter(start_time=start_time, end_time=end_time, mchids=mchids, card_types=card_types, spec=spec, filter_wave=filter_wave)
-        buf,mch_ratios = painter.paint_ratios()
+        buf, ratios = painter.paint_ratios()
         data = base64.b64encode(buf.getbuffer()).decode("ascii")
 
-        return jsonify({'img': data, 'mch_ratios': mch_ratios})
-        # return f"<img src='data:image/png;base64,{data}'/>"
+        return jsonify({'img': data, 'ratios': ratios, 'state': 'success'})
     except Exception as ex:
         return onError(ex)
 

+ 13 - 3
plot/refill/ChannelPainter.py

@@ -129,15 +129,25 @@ class ChannelPainter(object):
         else:
             window = None
 
+        chname_ratios = []
         for _chname, _card_type, _spec, _data in gen:
             succ, count, y = calc_chratios(_data, pos_map, self._start_time - day_stamp, self._end_time - day_stamp)
             y = np.convolve(y, window, 'same') if window is not None else y
-            ax.plot(x, y, ls='-', label=self._label(chname=_chname, succ=succ, count=count, card_type=_card_type, spec=_spec))
+            label, ratio = self._label(chname=_chname, succ=succ, count=count, card_type=_card_type, spec=_spec)
+            ax.plot(x, y, ls='-', label=label)
+            if _card_type is None and _spec is None and _chname != 'all':
+                chname_ratios.append((_chname, ratio))
 
         ticks = [d - self._start_time for d in range(self._start_time, self._end_time + 1, self._interval)]
         xlables = [time.strftime('%d-%H:%M:%S', time.localtime(d + self._start_time)) for d in ticks]
+        buf = fig_flush(ax, fig, ticks, xlables)
 
-        return fig_flush(ax, fig, ticks, xlables)
+        chname_ratios = sorted(chname_ratios, key=lambda x: (x[1], x[0]), reverse=True)
+        result = []
+        for name, ratio in chname_ratios:
+            result.append(f'{name}:{ratio}')
+
+        return buf, result
 
     def _label(self, chname, succ, count, card_type=None, spec=None):
         _card_type = None
@@ -167,4 +177,4 @@ class ChannelPainter(object):
             ratio = 0.00
         lable += f":{succ}/{count}={ratio}%"
 
-        return lable
+        return lable, ratio

+ 1 - 2
plot/refill/MerchantPainter.py

@@ -158,12 +158,11 @@ class MerchantPainter(object):
         buf = fig_flush(ax, fig, xticks=xticks, xlables=xlables)
 
         mchid_ratios = sorted(mchid_ratios, key=lambda x: (x[1], x[0]), reverse=True)
-
         result = []
         for mchid,ratio in mchid_ratios:
             result.append(f'{mchid}:{ratio}')
 
-        return buf,result
+        return buf, result
 
     def _label(self, chname, succ, count, card_type=None, spec=None):
         _card_type = None