NetcheckPainter.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. from .DataStream import ENetPosmap as pos_map, day_stamp, span_days, time_border, calc_interval
  2. from .NetchkReader import NetchkReader
  3. from .PainterBase import PainterBase
  4. from matplotlib.figure import Figure
  5. from matplotlib import ticker
  6. from io import BytesIO
  7. import numpy as np
  8. import time as time
  9. import logging
  10. logger = logging.getLogger('ChannelPainter')
  11. _all_net_channels = set()
  12. def _add_net_channel(channel):
  13. if channel not in _all_net_channels:
  14. _all_net_channels.add(channel)
  15. def get_net_channels():
  16. logger.debug(_all_net_channels)
  17. return list(_all_net_channels)
  18. def net_pathes(reader: NetchkReader, names: set, days: list):
  19. count = len(days)
  20. if len(names) > 1:
  21. _all_data = reader.init_data(count)
  22. else:
  23. _all_data = None
  24. for name in names:
  25. _add_net_channel(name)
  26. _ch_data = reader.init_data(count)
  27. for i, day in enumerate(days):
  28. data = reader.read(day, name)
  29. if data is not None:
  30. column_pos = i * 86400
  31. view = _ch_data[:, column_pos:column_pos + 86400]
  32. view += data
  33. if _all_data is not None:
  34. _all_data += _ch_data
  35. yield name, _ch_data
  36. if _all_data is not None:
  37. yield 'all', _all_data
  38. class NetcheckPainter(PainterBase):
  39. def _fig_funs(self):
  40. def create():
  41. fig = Figure(figsize=(19, 8))
  42. ax = fig.subplots()
  43. ax.set_title('success ratio')
  44. ax.set(xlabel='time', ylabel='ratio')
  45. return ax, fig
  46. def flush(ax, fig, ticks, lables):
  47. ax.yaxis.set_major_formatter(ticker.PercentFormatter(xmax=1, decimals=4))
  48. ax.set_xticks(ticks=ticks)
  49. ax.set_xticklabels(lables)
  50. fig.autofmt_xdate()
  51. ax.grid()
  52. # fig.subplots_adjust(left=0.1, right=0.8, top=0.95, bottom=0.1)
  53. ax.legend(bbox_to_anchor=(1, 1), loc='upper left')
  54. fig.tight_layout()
  55. buf = BytesIO()
  56. fig.savefig(buf, format="png")
  57. return buf
  58. return create, flush
  59. def _label(self, chname, succ, count):
  60. lable = f"{chname}"
  61. if count > 0:
  62. ratio = round(succ * 100 / count, 5)
  63. else:
  64. ratio = 0.00
  65. lable += f":{succ}/{count}={ratio}%"
  66. return lable, ratio