MTimesRatioCalc.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. from .MerchantCalc import MerchantCalc, mch_cardtype_paths
  2. from .algorithm import calc_mchratios_val
  3. from .DataStream import EMchPosmap as pos_map
  4. from .DataStream import day_stamp
  5. import logging
  6. import time as time
  7. import logging
  8. import json
  9. logger = logging.getLogger('MTimesRatioCalc')
  10. class MTimesRatioCalc(MerchantCalc):
  11. def _calc_handler(self, rclient):
  12. def calc_start_end(presecs,end):
  13. result = []
  14. for x in presecs:
  15. if end > x:
  16. result.append((x, end - x, end))
  17. else:
  18. result.append((x, 0, end))
  19. return result
  20. logger.debug('_calc_handler')
  21. reader = self._reader()
  22. end_time = int(time.time())
  23. start_time = day_stamp(end_time)
  24. days, start_time, end_time = self.calc_time(reader, start_time, end_time)
  25. if len(days) == 0:
  26. return 60
  27. day_start = days[0]
  28. start_end_tups = calc_start_end([900, 1800, 3600, 7200, 86400], end_time - day_start)
  29. tuple_pathes = reader.many_tuple_path(days)
  30. gen = mch_cardtype_paths(reader, tuple_pathes, days)
  31. card_types = {None: 'ALL', 4: 'YD', 5: 'LT', 6: 'DX'}
  32. result = dict()
  33. for _mchid, _card_type, _spec, _data in gen:
  34. for secs, start, end in start_end_tups:
  35. succs, commits, ratio = calc_mchratios_val(_data, pos_map, start, end)
  36. if _mchid not in result:
  37. result[_mchid] = dict()
  38. if _card_type not in card_types:
  39. continue
  40. name = card_types[_card_type]
  41. if name not in result[_mchid]:
  42. result[_mchid][name] = dict()
  43. result[_mchid][name][secs] = [succs, commits, ratio]
  44. if len(result) != 0:
  45. rclient.set(f"nc_merchant_card_type_ratios", json.dumps(result))
  46. return 60