ChannelSpeedAnalyzePainter.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. from .DataStream import EChPosmap as pos_map
  2. from .ChannelReader import ChannelReader, ChPathFilter
  3. from .ChannelPainter import ChannelPainter, detail_pathes, get_channels
  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_chspeed_ratio
  9. import time as time
  10. import logging
  11. logger = logging.getLogger('ChannelSpeedAnalyzePainter')
  12. class ChannelSpeedAnalyzePainter(ChannelPainter):
  13. def __init__(self, start_time: int, end_time: int, chnames: set = None, card_types: set = None, spec: int = None, period: int = 60):
  14. self._reader = ChannelReader()
  15. self._chnames, self._card_types, self._spec, self._period = chnames, card_types, spec, period
  16. days, self._start_time, self._end_time, self._interval = self.calc_time(self._reader, start_time, end_time)
  17. secs = self._end_time - self._start_time
  18. if secs % period > 0:
  19. self._start_time = self._start_time -(period - secs % period)
  20. pass
  21. def _succ_hist(self, y, ax_histx):
  22. vals,pos,_1 = ax_histx.hist(y)
  23. if len(pos) >= 2:
  24. interval = (pos[1] - pos[0]) / 5
  25. else:
  26. interval = 0
  27. for i in range(len(vals)):
  28. ax_histx.annotate(text=int(vals[i]),xy = (pos[i] + interval,vals[i]))
  29. ax_histx.grid()
  30. def _fig_create(self):
  31. fig = Figure(figsize=(16, 6))
  32. gs = fig.add_gridspec(nrows=2, ncols=4, height_ratios=(2, 4), width_ratios=(4, 4, 4, 4))
  33. ax_hist_succs = fig.add_subplot(gs[0, :])
  34. ax_scatter = fig.add_subplot(gs[1, 0])
  35. ax_succ_cdf = fig.add_subplot(gs[1, 1])
  36. ax_ratio_cdf = fig.add_subplot(gs[1, 2])
  37. ax_ratio_latest = fig.add_subplot(gs[1, 3])
  38. return fig, ax_hist_succs, ax_scatter, ax_succ_cdf, ax_ratio_cdf, ax_ratio_latest
  39. def _flush(self, fig, ax_scatter, ax_succ_cdf, ax_hist_succs, ax_ratio_cdf, ax_ratio_latest):
  40. ax_scatter.grid()
  41. ax_hist_succs.grid()
  42. ax_ratio_cdf.grid()
  43. ax_ratio_latest.grid()
  44. ax_succ_cdf.grid()
  45. fig.tight_layout()
  46. buf = BytesIO()
  47. fig.savefig(buf, format="png")
  48. return buf
  49. def _succ_cdf(self, succs, commits, ax_count, ax_ratio,ax_latest):
  50. min = np.min(commits)
  51. max = np.max(commits)
  52. x = [i for i in range(0, max + 1, 1)]
  53. y = []
  54. for i in x:
  55. pos = np.where(commits == i)
  56. succ = succs[pos]
  57. count = np.sum(succ)
  58. y.append(count)
  59. y = np.array(y)
  60. succ = np.sum(y)
  61. y = np.cumsum(y)
  62. ax_count.plot(x, y, ls='-')
  63. ratio = y / (succ + 0.00000001)
  64. ax_ratio.plot(x, ratio, ls='-')
  65. pos = np.where(y == succ)
  66. if len(pos[0]) > 1:
  67. end = pos[0][1]
  68. x = x[:end]
  69. ratio = ratio[:end]
  70. ax_latest.plot(x, ratio, ls='-')
  71. pass
  72. def paint(self):
  73. reader = self._reader
  74. days, _start_time, _end_time, _interval = self.calc_time(self._reader, self._start_time, self._end_time)
  75. if len(days) == 0:
  76. return BytesIO()
  77. tuple_pathes = reader.many_tuple_path(days, self._chnames, self._card_types, self._spec)
  78. filter = ChPathFilter(self._chnames, self._card_types, self._spec)
  79. gen = detail_pathes(reader, tuple_pathes, days)
  80. day_stamp = days[0]
  81. channels = {}
  82. commits = []
  83. succs = []
  84. ratios = []
  85. fig, ax_hist_succs, ax_scatter, ax_succ_cdf, ax_ratio_cdf, ax_ratio_latest = self._fig_create()
  86. for _chname, _card_type, _spec, _data in gen:
  87. logger.debug(f'chname={_chname} card_type={_card_type} spec={_spec}')
  88. if _chname != 'all' and _card_type is not None and _spec is not None:
  89. _commits, _succs, _ratios = calc_chspeed_ratio(_data, pos_map, _start_time - day_stamp, _end_time - day_stamp, self._period)
  90. ax_scatter.scatter(_commits, _succs)
  91. commits.extend(_commits)
  92. succs.extend(_succs)
  93. ratios.extend(ratios)
  94. if len(succs) > 0 and len(commits) > 0:
  95. succs = np.array(succs).astype(int)
  96. commits = np.array(commits).astype(int)
  97. self._succ_hist(succs,ax_hist_succs)
  98. self._succ_cdf(succs,commits,ax_succ_cdf,ax_ratio_cdf,ax_ratio_latest)
  99. buf = self._flush(fig, ax_scatter, ax_succ_cdf, ax_hist_succs, ax_ratio_cdf, ax_ratio_latest)
  100. result = []
  101. channels = get_channels()
  102. for name in channels:
  103. result.append(f'{name}:0.00')
  104. return buf,result