ChannelCovPainter.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. from .DataStream import EChPosmap as pos_map
  2. from .ChannelReader import ChannelReader, ChPathFilter
  3. from .ChannelPainter import ChannelPainter, ratio_pathes
  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_cov_chratios, calc_count_cdf
  9. import time as time
  10. import logging
  11. logger = logging.getLogger('ChannelCumPainter')
  12. class ChannelCovPainter(ChannelPainter):
  13. def __init__(self, start_time: int, end_time: int, chnames: set = None, card_types: set = None, spec: int = None, filter_wave: int = None,
  14. all_show_type: int = 3):
  15. self._reader = ChannelReader()
  16. filter_wave = filter_wave or 3600
  17. only_all = all_show_type == 1
  18. self._chnames, self._card_types, self._spec, self._filter_wave, self._only_all, self._all_show_type \
  19. = chnames, card_types, spec, filter_wave, only_all, all_show_type
  20. days, self._start_time, self._end_time, self._interval = self.calc_time(self._reader, start_time, end_time)
  21. pass
  22. def _fig_create(self):
  23. fig = Figure(figsize=(16, 12))
  24. gs = fig.add_gridspec(nrows=3, height_ratios=(4, 4, 4))
  25. ax_succ_cdf = fig.add_subplot(gs[0, :], )
  26. ax_commit_cdf = fig.add_subplot(gs[1, :], sharex=ax_succ_cdf)
  27. ax_ratio = fig.add_subplot(gs[2, :], sharex=ax_succ_cdf)
  28. ax_succ_cdf.set_title('succ orders vs time (cdf)')
  29. ax_commit_cdf.set_title('commit orders vs time (cdf)')
  30. ax_ratio.set_title('success ratio vs time')
  31. return fig, ax_succ_cdf, ax_commit_cdf, ax_ratio
  32. def _flush(self, fig, ax_succ_cdf, ax_commit_cdf, ax_ratio, xticks, xlables, line_lables):
  33. def show_legent(ax, labels):
  34. size = len(line_lables)
  35. if size == 0:
  36. return False
  37. max_col = 8
  38. s = int(size / max_col)
  39. m = size % max_col
  40. if s == 0 and m > 0:
  41. ncol = m
  42. else:
  43. ncol = max_col
  44. leg = ax_ratio.legend(tuple(line_lables), loc='upper left', ncol=8, mode="expand", shadow=True, fancybox=False)
  45. leg.get_frame().set_alpha(0.2)
  46. return True
  47. ax_succ_cdf.grid()
  48. ax_commit_cdf.set_xticks(ticks=xticks)
  49. ax_commit_cdf.set_xticklabels(xlables)
  50. ax_commit_cdf.grid()
  51. ax_ratio.yaxis.set_major_formatter(ticker.PercentFormatter(xmax=1, decimals=4))
  52. ax_ratio.grid()
  53. show_legent(ax_ratio, line_lables)
  54. fig.autofmt_xdate()
  55. fig.tight_layout()
  56. buf = BytesIO()
  57. fig.savefig(buf, format="png")
  58. return buf
  59. def show_all(self):
  60. return self._all_show_type != 3
  61. def paint(self):
  62. reader = self._reader
  63. days, _start_time, _end_time, _interval = self.calc_time(self._reader, self._start_time - self._filter_wave, self._end_time)
  64. if len(days) == 0:
  65. return BytesIO()
  66. filter = ChPathFilter(self._chnames, self._card_types, self._spec, self._only_all)
  67. tuple_pathes = reader.many_tuple_path(days, self._chnames, self._card_types, self._spec)
  68. gen = ratio_pathes(reader, tuple_pathes, days, filter)
  69. day_stamp = days[0]
  70. x = np.array([d - self._start_time for d in range(self._start_time, self._end_time)])
  71. window = np.ones(self._filter_wave)
  72. left_len = self._start_time - _start_time
  73. right_len = 0
  74. stime = lambda t: time.strftime('%d-%H:%M:%S', time.localtime(t))
  75. logger.debug("start_time %d, %s end_time=%s left_len=%d right_len=%d",
  76. self._start_time, stime(self._start_time), stime(self._end_time), left_len, right_len)
  77. chname_ratios = []
  78. fig, ax_succ_cdf, ax_commit_cdf, ax_ratio = self._fig_create()
  79. line_lables = []
  80. index = 0;
  81. for _chname, _card_type, _spec, _data in gen:
  82. color = self.color(index)
  83. index += 1
  84. _succ, _commit, y, _succ_period, _fail_period = calc_cov_chratios(_data, pos_map, _start_time - day_stamp, _end_time - day_stamp, window,
  85. left_len, right_len)
  86. _succs, _commits = calc_count_cdf(_data, pos_map, _start_time - day_stamp, _end_time - day_stamp, left_len, right_len)
  87. label = self._label_simple(chname=_chname, filter=filter, card_type=_card_type, spec=_spec)
  88. line_lables.append(label)
  89. if _chname != 'all':
  90. show_plot = True
  91. elif self.show_all():
  92. show_plot = True
  93. else:
  94. show_plot = False
  95. if show_plot:
  96. ax_ratio.plot(x, y, ls='-', color=color)
  97. ax_succ_cdf.plot(x, _succs, ls='-', color=color)
  98. ax_commit_cdf.plot(x, _commits, ls='-', color=color)
  99. if _card_type is None and _spec is None:
  100. _ratio = _succ / (_commit + 0.00001)
  101. _ratio = round(_ratio, 5)
  102. chname_ratios.append((_chname, _ratio, _succ, _commit, _succ_period, _fail_period))
  103. xticks, xlables = self.clac_xlables(self._start_time, self._end_time, self._interval)
  104. buf = self._flush(fig, ax_succ_cdf, ax_commit_cdf, ax_ratio, xticks, xlables, line_lables)
  105. chname_ratios = sorted(chname_ratios, key=lambda x: x[1], reverse=True)
  106. result = {}
  107. ratios = {}
  108. for _chname, _ratio, _succ, _commit, _succ_period, _fail_period in chname_ratios:
  109. ratios[_chname] = [_ratio, _succ, _commit, _succ_period, _fail_period]
  110. result['detail'] = ratios
  111. return buf, result