MerchantPainter.py 4.3 KB

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