MerchantPainter.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. from .DataStream import EMchPosmap as pos_map, span_days, time_border, calc_interval
  2. from .PainterBase import PainterBase
  3. from .MerchantReader import MerchantReader
  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, calc_morder_send
  9. import time as time
  10. import logging
  11. logger = logging.getLogger('painter')
  12. _all_mchids = set()
  13. def add_mchid(mchid):
  14. if mchid not in _all_mchids:
  15. _all_mchids.add(mchid)
  16. def get_mchids():
  17. return list(_all_mchids)
  18. def allpathes(reader: MerchantReader, tuple_pathes: dict, days: list, spec=None):
  19. count = len(days)
  20. show_detail = True if len(list(tuple_pathes.keys())) == 1 else False
  21. if show_detail == False:
  22. all_datas = reader.init_data(count)
  23. else:
  24. all_datas = None
  25. for mchid, tup in tuple_pathes.items():
  26. add_mchid(mchid)
  27. ch_datas = reader.init_data(count)
  28. for _card_type, _spec in tup:
  29. if spec is not None and _spec != spec:
  30. continue
  31. if show_detail:
  32. detail_datas = reader.init_data(count)
  33. else:
  34. detail_datas = None
  35. for i, day in enumerate(days):
  36. data = reader.read(day, mchid, _card_type, _spec)
  37. if data is not None:
  38. column_pos = i * 86400
  39. view = ch_datas[:, column_pos:column_pos + 86400]
  40. view += data
  41. if show_detail:
  42. view = detail_datas[:, column_pos:column_pos + 86400]
  43. view += data
  44. if show_detail:
  45. yield mchid, _card_type, _spec, detail_datas
  46. if all_datas is not None:
  47. all_datas += ch_datas
  48. yield mchid, None, None, ch_datas
  49. if show_detail == False:
  50. yield 'all', None, None, all_datas
  51. class MerchantPainter(PainterBase):
  52. def _fig_funs(self):
  53. def create():
  54. fig = Figure(figsize=(19, 8))
  55. ax = fig.subplots()
  56. ax.set_title('success ratio')
  57. ax.set(xlabel='time', ylabel='ratio')
  58. return ax, fig
  59. def flush(ax, fig, xticks=None, xlables=None, yticks=None, ylables=None):
  60. ax.yaxis.set_major_formatter(ticker.PercentFormatter(xmax=1, decimals=4))
  61. if xticks is not None:
  62. ax.set_xticks(ticks=xticks)
  63. if xlables is not None:
  64. ax.set_xticklabels(xlables)
  65. if yticks is not None:
  66. ax.set_yticks(ticks=yticks)
  67. if ylables is not None:
  68. ax.set_yticklabels(ylables)
  69. fig.autofmt_xdate()
  70. ax.grid()
  71. fig.subplots_adjust(left=0.1, right=0.8, top=0.95, bottom=0.1)
  72. ax.legend(bbox_to_anchor=(1, 1), loc='upper left')
  73. buf = BytesIO()
  74. fig.savefig(buf, format="png")
  75. return buf
  76. return create, flush
  77. def _label(self, chname, succ, count, card_type=None, spec=None):
  78. _card_type = None
  79. if card_type == 1:
  80. _card_type = 'SY'
  81. elif card_type == 2:
  82. _card_type = 'SH'
  83. elif card_type == 4:
  84. _card_type = 'YD'
  85. elif card_type == 5:
  86. _card_type = 'LT'
  87. elif card_type == 6:
  88. _card_type = 'DX'
  89. elif card_type == 7:
  90. _card_type = 'TH'
  91. lable = f"{chname}"
  92. if _card_type is not None:
  93. lable += f"-{_card_type}"
  94. if spec is not None:
  95. lable += f"-{spec}"
  96. if count > 0:
  97. ratio = round(succ * 100 / count, 2)
  98. else:
  99. ratio = 0.00
  100. lable += f":{succ}/{count}={ratio}%"
  101. return lable,ratio