MerchantCumRatioPainter.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. from .DataStream import EMchPosmap as pos_map, span_days, time_border, calc_interval
  2. from .MerchantReader import MerchantReader
  3. from .MerchantPainter import MerchantPainter, allpathes
  4. from matplotlib.figure import Figure
  5. from matplotlib import ticker
  6. from io import BytesIO
  7. import numpy as np
  8. from .algorithm import calc_mchratios
  9. import time as time
  10. import logging
  11. logger = logging.getLogger('MerchantCumRatioPainter')
  12. class MerchantCumRatioPainter(MerchantPainter):
  13. def __init__(self, start_time: int, end_time: int, mchids: set = None, card_types: set = None, spec: int = None, filter_wave: int = None):
  14. self._reader = MerchantReader()
  15. self._mchids, self._card_types, self._spec, self._filter_wave = mchids, card_types, spec, filter_wave
  16. self._days, self._start_time, self._end_time, self._interval = self.calc_time(self._reader, start_time, end_time)
  17. pass
  18. def paint(self):
  19. tuple_pathes = self._reader.many_tuple_path(self._days, self._mchids, self._card_types, self._spec)
  20. gen = allpathes(self._reader, tuple_pathes, self._days, self._spec)
  21. if len(self._days) == 0:
  22. return BytesIO()
  23. day_stamp = self._days[0]
  24. fig_create, fig_flush = self._fig_funs()
  25. ax, fig = fig_create()
  26. x = np.array([d - self._start_time for d in range(self._start_time, self._end_time)])
  27. if self._filter_wave is not None and self._filter_wave > 1:
  28. window = np.ones(self._filter_wave) / self._filter_wave
  29. else:
  30. window = None
  31. mchid_ratios = []
  32. for _mchid, _card_type, _spec, _data in gen:
  33. succ, count, y = calc_mchratios(_data, pos_map, self._start_time - day_stamp, self._end_time - day_stamp)
  34. y = np.convolve(y, window, 'same') if window is not None else y
  35. label, ratio = self._label(chname=_mchid, succ=succ, count=count, card_type=_card_type, spec=_spec)
  36. ax.plot(x, y, ls='-', label=label)
  37. if _card_type is None and _spec is None and type(_mchid) is int:
  38. mchid_ratios.append((_mchid, ratio))
  39. xticks = [d - self._start_time for d in range(self._start_time, self._end_time + 1, self._interval)]
  40. xlables = [time.strftime('%d-%H:%M:%S', time.localtime(d + self._start_time)) for d in xticks]
  41. buf = fig_flush(ax, fig, xticks=xticks, xlables=xlables)
  42. mchid_ratios = sorted(mchid_ratios, key=lambda x: (x[1], x[0]), reverse=True)
  43. result = []
  44. for mchid,ratio in mchid_ratios:
  45. result.append(f'{mchid}:{ratio}')
  46. return buf, result