ChannelCovPainter.py 5.4 KB

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