123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- from .DataStream import EMchPosmap as pos_map, span_days, time_border, calc_interval
- from .MerchantReader import MerchantReader
- from .MerchantPainter import MerchantPainter, mch_paths
- from matplotlib.figure import Figure
- from matplotlib import ticker
- from io import BytesIO
- import numpy as np
- from .algorithm import calc_morder_send
- import time as time
- import logging
- logger = logging.getLogger('painter')
- class MerchantAmountPainter(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()
- self._mchids, self._card_types, self._spec, self._filter_wave = mchids, card_types, spec, filter_wave
- self._days, self._start_time, self._end_time, self._interval = self.calc_time(self._reader, start_time, end_time)
- pass
- def _fig_bar_funs(self):
- def create():
- fig = Figure(figsize=(19, 16))
- ax_count, ax_amount = fig.subplots(2, 1)
- ax_count.set_title('sending order count monitor')
- ax_count.set(xlabel='merchant id', ylabel='count')
- ax_amount.set_title('sending order amount monitor')
- ax_amount.set(xlabel='merchant id', ylabel='amount')
- return ax_count, ax_amount, fig
- def end(ax, xticks=None, xlables=None, yticks=None, ylables=None):
- if xticks is not None:
- ax.set_xticks(ticks=xticks)
- if xlables is not None:
- ax.set_xticklabels(xlables)
- if yticks is not None:
- ax.set_yticks(ticks=yticks)
- if ylables is not None:
- ax.set_yticklabels(ylables)
- ax.legend()
- ax.grid()
- def flush(fig):
- fig.tight_layout()
- buf = BytesIO()
- fig.savefig(buf, format="png")
- return buf
- return create, end,flush
- def paint(self):
- tuple_pathes = self._reader.many_tuple_path(self._days, self._mchids, self._card_types)
- gen = mch_paths(self._reader, tuple_pathes, self._days)
- if len(self._days) == 0:
- return BytesIO()
- day_stamp = self._days[0]
- fig_create, fig_end, fig_flush = self._fig_bar_funs()
- ax_count, ax_amount, fig = fig_create()
- lables = list()
- datas = list()
- mchids = list()
- for _mchid, _card_type, _spec, _data in gen:
- lables.append(f"{_mchid}")
- logger.debug(_mchid)
- mchids.append(_mchid)
- ret = calc_morder_send(_data, pos_map, self._start_time - day_stamp, self._end_time - day_stamp)
- datas.append(ret)
- send_count, submit_count, succ_count, fail_count, submit_amount, succ_amount, fail_amount, send_amount, lack = ret
- logger.debug("send=%d submit=%d succ=%d fail=%d",send_count, submit_count, succ_count, fail_count)
- cratio = succ_count / (succ_count + fail_count + 0.0001)
- aratio = succ_amount / (succ_amount + fail_amount + 0.0001)
- logger.debug("cratio=%.2f aratio=%.2f", cratio, aratio)
- send_count, submit_count, succ_count, fail_count, submit_amount, succ_amount, fail_amount, send_amount, lack = zip(*datas)
- self.draw_count(ax_count, lables, send_count, submit_count, succ_count, fail_count, fig_end)
- self.draw_amount(ax_amount, lables, submit_amount, succ_amount, fail_amount, send_amount, lack, fig_end)
- return fig_flush(fig), mchids
- def draw_count(self, ax, lables, send_count, submit_count, succ_count, fail_count, fig_end):
- width = 0.24
- x_asix = np.arange(len(lables))
- rect_send = ax.bar(x_asix - width * 0.5, list(send_count), width, label='sending', align='center')
- rect_submit = ax.bar(x_asix - width * 1.5, list(submit_count), width, label='summit',align='center')
- rect_succ = ax.bar(x_asix + width * 0.5, list(succ_count), width, label='succ',align='center')
- rect_fail = ax.bar(x_asix + width * 1.5, list(fail_count), width, label='fail', align='center')
- alpha = 45
- ax.bar_label(rect_send, padding=0, rotation=alpha, fmt='%d')
- ax.bar_label(rect_submit, padding=0, rotation=alpha, fmt='%d')
- ax.bar_label(rect_succ, padding=0, rotation=alpha, fmt='%d')
- ax.bar_label(rect_fail, padding=0, rotation=alpha, fmt='%d')
- fig_end(ax, xticks=x_asix, xlables=lables)
- pass
- def draw_amount(self, ax, lables, submit_amount, succ_amount, fail_amount, send_amount, lack, fig_end):
- width = 0.18
- x_asix = np.arange(len(lables))
- rect_submit = ax.bar(x_asix - width * 2, list(submit_amount), width, label='summit',align='center')
- rect_send = ax.bar(x_asix - width, list(send_amount), width, label='sending', align='center')
- rect_msucc = ax.bar(x_asix, list(lack), width, label='may succ', align='center')
- rect_succ = ax.bar(x_asix + width, list(succ_amount), width, label='succ',align='center')
- rect_fail = ax.bar(x_asix + width * 2, list(fail_amount), width, label='fail', align='center')
- alpha = 45
- ax.bar_label(rect_submit, padding=0, rotation=alpha, fmt='%.2f')
- ax.bar_label(rect_send, padding=0, rotation=alpha, fmt='%.2f')
- ax.bar_label(rect_msucc, padding=0, rotation=alpha, fmt='%.2f')
- ax.bar_label(rect_succ, padding=0, rotation=alpha, fmt='%.2f')
- ax.bar_label(rect_fail, padding=0, rotation=alpha, fmt='%.2f')
- fig_end(ax, xticks=x_asix, xlables=lables)
- pass
|