stanley-king 2 年之前
父节点
当前提交
d010fe6cf1
共有 6 个文件被更改,包括 133 次插入61 次删除
  1. 20 1
      plot/app.py
  2. 88 0
      plot/refill/ChannelCovSuccPainter.py
  3. 0 58
      plot/refill/MerchantCovSuccPainter.py
  4. 3 1
      plot/refill/__init__.py
  5. 13 0
      plot/refill/algorithm.py
  6. 9 1
      plot/testPlot.py

+ 20 - 1
plot/app.py

@@ -20,7 +20,7 @@ logging.basicConfig(filename='/var/www/html/data/log/flask.log',
                     level=logging.DEBUG)
 logger = logging.getLogger('plot')
 
-from refill import ChannelCumPainter, ChannelCovPainter
+from refill import ChannelCumPainter, ChannelCovPainter, ChannelCovSuccPainter
 from refill import MerchantCumRatioPainter, MerchantAmountPainter, MerchantCovRatioPainter
 from refill import filter_chname, filter_cardtype, filter_mchids, get_channels, get_mchids
 
@@ -86,6 +86,25 @@ def ch_covratio():
     except Exception as ex:
         return onError(ex)
 
+@app.route('/plot/ch_covsuccs')
+def ch_covsuccs():
+    try:
+        logger.debug('start ch_covratio')
+        start_time, end_time, card_types, spec, filter_wave = parse_parmeter()
+        chnames = request.args.get('chnames')
+        chnames = filter_chname(chnames)
+
+        painter = ChannelCovSuccPainter(start_time=start_time, end_time=end_time, chnames=chnames, card_types=card_types, spec=spec,
+                                    filter_wave=filter_wave)
+        buf, ratios = painter.paint()
+        data = base64.b64encode(buf.getbuffer()).decode("ascii")
+
+        return jsonify({'img': data, 'ratios': ratios, 'state': 'success'})
+    except Exception as ex:
+        return onError(ex)
+
+
+
 
 @app.route('/plot/channels')
 def channels():

+ 88 - 0
plot/refill/ChannelCovSuccPainter.py

@@ -0,0 +1,88 @@
+from .DataStream import EChPosmap as pos_map
+from .ChannelReader import ChannelReader
+from .ChannelPainter import ChannelPainter, ratio_pathes
+from matplotlib.figure import Figure
+from matplotlib import ticker
+from io import BytesIO
+import numpy as np
+from .algorithm import calc_cov_chsuccs
+import time as time
+
+import logging
+logger = logging.getLogger('ChannelCumPainter')
+
+class ChannelCovSuccPainter(ChannelPainter):
+    def __init__(self, start_time: int, end_time: int, chnames: set = None, card_types: set = None, spec: int = None, filter_wave: int = None):
+        self._reader = ChannelReader()
+        if filter_wave is None:
+            filter_wave = 3600
+        self._chnames, self._card_types, self._spec, self._filter_wave = chnames, card_types, spec, filter_wave
+        _days, self._start_time, self._end_time, self._interval = self.calc_time(self._reader, start_time, end_time)
+        pass
+
+    def paint(self):
+        reader = self._reader
+        self._days, _start_time, _end_time, _interval = self.calc_time(self._reader, self._start_time - self._filter_wave, self._end_time)
+
+        tuple_pathes = reader.many_tuple_path(self._days, self._chnames, self._card_types, self._spec)
+        gen = ratio_pathes(reader, tuple_pathes, self._days, self._spec)
+        if len(self._days) == 0:
+            return BytesIO()
+
+        day_stamp = self._days[0]
+        fig_create, fig_flush = self._fig_funs()
+        ax, fig = fig_create()
+        x = np.array([d - self._start_time for d in range(self._start_time, self._end_time)])
+        window = np.ones(self._filter_wave) / self._filter_wave
+
+        left_len = self._start_time - _start_time
+        right_len = _end_time - self._end_time
+
+        stime = lambda t: time.strftime('%d-%H:%M:%S', time.localtime(t))
+        logger.debug("near_stamp start_time %d, %s end_time=%s left_len=%d right_len=%d",
+                     self._start_time, stime(self._start_time), stime(self._end_time),left_len,right_len)
+
+        chname_ratios = []
+        for _chname, _card_type, _spec, _data in gen:
+            succ, commit = calc_cov_chsuccs(_data, pos_map, _start_time - day_stamp, _end_time - day_stamp, window, left_len,right_len)
+            label, ratio = self._label(chname=_chname, succ=succ, count=commit, card_type=_card_type, spec=_spec)
+            ax.plot(x, succ, ls='-', label=label)
+            ax.plot(x, commit, 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)
+
+        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
+        if card_type == 1:
+            _card_type = 'SY'
+        elif card_type == 2:
+            _card_type = 'SH'
+        elif card_type == 4:
+            _card_type = 'YD'
+        elif card_type == 5:
+            _card_type = 'LT'
+        elif card_type == 6:
+            _card_type = 'DX'
+        elif card_type == 7:
+            _card_type = 'TH'
+
+        lable = f"{chname}"
+        if _card_type is not None:
+            lable += f"-{_card_type}"
+
+        if spec is not None:
+            lable += f"-{spec}"
+
+        return lable, 0.0

+ 0 - 58
plot/refill/MerchantCovSuccPainter.py

@@ -1,58 +0,0 @@
-from .DataStream import EMchPosmap as pos_map, span_days, time_border, calc_interval
-from .MerchantReader import MerchantReader
-from .MerchantPainter import MerchantPainter, allpathes
-from matplotlib.figure import Figure
-from matplotlib import ticker
-from io import BytesIO
-import numpy as np
-from .algorithm import calc_cov_mchratios
-import time as time
-
-import logging
-logger = logging.getLogger('MerchantCovRatioPainter')
-
-class MerchantCovRatioPainter(MerchantPainter):
-    def __init__(self, start_time: int, end_time: int, mchids: set = None, card_types: set = None, spec: int = None, filter_wave: int = None):
-        self._reader = MerchantReader()
-        if filter_wave is None:
-            filter_wave = 3600
-        self._mchids, self._card_types, self._spec, self._filter_wave = mchids, card_types, spec, filter_wave
-        _days, self._start_time, self._end_time, self._interval = self.calc_time(self._reader, start_time, end_time)
-        pass
-
-    def paint(self):
-        reader = self._reader
-        self._days, _start_time, _end_time, _interval = self.calc_time(self._reader, self._start_time - self._filter_wave, self._end_time)
-        tuple_pathes = self._reader.many_tuple_path(self._days, self._mchids, self._card_types, self._spec)
-        gen = allpathes(self._reader, tuple_pathes, self._days, self._spec)
-        if len(self._days) == 0:
-            return BytesIO()
-
-        day_stamp = self._days[0]
-        fig_create, fig_flush = self._fig_funs()
-        ax, fig = fig_create()
-
-        x = np.array([d - self._start_time for d in range(self._start_time, self._end_time)])
-        window = np.ones(self._filter_wave)
-
-        left_len = self._start_time - _start_time
-        right_len = _end_time - self._end_time
-
-        mchid_ratios = []
-        for _mchid, _card_type, _spec, _data in gen:
-            succ, count, y = calc_cov_mchratios(_data, pos_map, _start_time - day_stamp, _end_time - day_stamp, window, left_len, right_len)
-            label, ratio = self._label(chname=_mchid, 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 type(_mchid) is int:
-                mchid_ratios.append((_mchid, ratio))
-
-        xticks = [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 xticks]
-        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

+ 3 - 1
plot/refill/__init__.py

@@ -10,6 +10,8 @@ from .ChannelReader import ChannelReader
 
 from .ChannelCumPainter import ChannelCumPainter
 from .ChannelCovPainter import ChannelCovPainter
+from .ChannelCovSuccPainter import ChannelCovSuccPainter
+
 from .ChannelPainter import get_channels
 from .MerchantPainter import get_mchids
 from .MerchantCumRatioPainter import MerchantCumRatioPainter
@@ -26,7 +28,7 @@ from .server_util import opt_parse
 __all__ = ['DataWriteStream', 'DataReadStream',
            'MerchantWriter', 'ChannelWriter', 'NetchkWriter', 'WriteConsumer',
            'MerchantReader', 'NetchkReader', 'ChannelReader',
-           'ChannelCumPainter', 'ChannelCovPainter',
+           'ChannelCumPainter', 'ChannelCovPainter', 'ChannelCovSuccPainter',
            'MerchantCumRatioPainter', 'MerchantAmountPainter', 'MerchantCovRatioPainter',
            'get_channels', 'get_mchids',
            'queueListener', 'open_hdf5', 'day_stamp', 'time_border',

+ 13 - 0
plot/refill/algorithm.py

@@ -37,6 +37,19 @@ def calc_cov_chratios(data, pos_map, start, end, window, left_len,right_len):
 
     return int(succs), int(succs + fails), y
 
+def calc_cov_chsuccs(data, pos_map, start, end, window, left_len,right_len):
+    view = data[[pos_map.succ_count, pos_map.fail_count, pos_map.commit_count], :]
+    view = view[:, start:end]
+    succ = view[0, :]
+    commit = view[1, :]
+    succ = np.convolve(succ, window, 'same')
+    commit = np.convolve(commit, window, 'same')
+
+    succ = succ[left_len:end - start - right_len]
+    commit = commit[left_len:end - start - right_len]
+
+    return succ, commit
+
 
 def calc_chspeed(data, pos_map, start, end):
     view = data[[pos_map.commit_count], :]

+ 9 - 1
plot/testPlot.py

@@ -51,7 +51,7 @@ class MyTestCase(unittest.TestCase):
         painter = ChannelCumPainter(start_time=start_time, end_time=end_time, chnames=set(), card_types={4, 5, 6})
         painter.paint()
 
-    def test_chcov(self):
+    def test_chcov_ratio(self):
         from refill import ChannelCovPainter
 
         start_time = int(time.time()) - 10 * 86400 - 3600
@@ -59,6 +59,14 @@ class MyTestCase(unittest.TestCase):
         painter = ChannelCovPainter(start_time=start_time, end_time=end_time, chnames=set(), card_types={4, 5, 6},filter_wave=3600)
         painter.paint()
 
+    def test_chcov_succ(self):
+        from refill import ChannelCovSuccPainter
+
+        start_time = int(time.time()) - 20 * 86400 - 3600
+        end_time = int(time.time()) - 20 * 86400
+        painter = ChannelCovSuccPainter(start_time=start_time, end_time=end_time, chnames=set(), card_types={4, 5, 6},filter_wave=3600)
+        painter.paint()
+
     def test_mch_ratio_painter(self):
         from refill import MerchantCumRatioPainter