ChSpeedRatioCalc.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. from .ChannelCalc import ChannelCalc, detail_pathes
  2. from .algorithm import calc_chspeed, calc_chratio, calc_commit
  3. from .DataStream import EChPosmap as pos_map
  4. from .ChannelReader import ChannelReader
  5. import time as time
  6. import json
  7. import redis
  8. import logging
  9. logger = logging.getLogger('ChSpeedCalc')
  10. class ChSpeedRatioCalc(ChannelCalc):
  11. def _calc_handler(self, rclient):
  12. logger.debug('_calc_handler')
  13. reader = self._reader()
  14. end_time = int(time.time())
  15. period = 900
  16. days, start_time, end_time = self.calc_time(reader, end_time - period, end_time)
  17. day_stamp = days[0]
  18. tuple_pathes = reader.many_tuple_path(days, card_types=set([4, 5, 6]))
  19. gen = detail_pathes(reader, tuple_pathes, days)
  20. start_pos = start_time - day_stamp
  21. end_pos = end_time - day_stamp
  22. result = dict()
  23. for _name, _card_type, _spec, _data in gen:
  24. speed = calc_chspeed(_data, pos_map, end_pos - 60, end_pos)
  25. ratio, ratio_commit, notify_time, succ_time = calc_chratio(_data, pos_map, end_pos - period, end_pos)
  26. monitor_commit = calc_commit(_data, pos_map, end_pos - 300, end_pos)
  27. key = f'{_name}-{_spec}-{_card_type}'
  28. result[key] = [speed, ratio, ratio_commit, notify_time, monitor_commit]
  29. logger.debug("%s-%d-%d speed=%d ratio=%.5f commit_count=%d notify_time=%.5f succ_time=%.5f monitor_commit=%d", _name, _card_type, _spec, speed, ratio,
  30. ratio_commit, notify_time, succ_time, monitor_commit)
  31. if len(result) != 0:
  32. rclient.set(f"nc_refill_channel_control_model", json.dumps(result))
  33. rclient.publish('refill', json.dumps({'type': 'channel_control', 'value': 0}))
  34. return 0.1