ChConsumeCalc.py 1.9 KB

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