MProfitRatioCalc.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. from .MerchantCalc import MerchantCalc, mch_detail_paths, mch_paths
  2. from .algorithm import calc_mch_profit
  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('MProfitRatioCalc')
  10. def mixed_ratio(rclient):
  11. result = list()
  12. data = rclient.get('nc_refill-stat-merchant-mixed')
  13. if data is not None:
  14. mch_cfgs = json.loads(data)
  15. for mchid, val in mch_cfgs.items():
  16. mchid = int(mchid)
  17. period = int(val['period'])
  18. formula = val['profit_formula'].strip()
  19. result.append((mchid, period, formula))
  20. return result
  21. class MProfitRatioCalc(MerchantCalc):
  22. def _calc_handler(self, rclient):
  23. logger.debug('_calc_handler')
  24. mixed_ratios = mixed_ratio(rclient)
  25. reader = self._reader()
  26. gross = dict()
  27. detail = dict()
  28. for mchid, period, formula in mixed_ratios:
  29. logger.debug(f'mchid={mchid} period={period}')
  30. end_time = int(time.time())
  31. # if period == 86400:
  32. # cur_day = day_stamp(end_time)
  33. # period = end_time - cur_day
  34. # logger.debug(f'mchid={mchid} period={period}')
  35. days, start_time, end_time = self.calc_time(reader, end_time - period, end_time)
  36. if len(days) == 0:
  37. continue
  38. stamp = days[0]
  39. tuple_pathes = reader.many_tuple_path(days, mchids={mchid})
  40. gen = mch_detail_paths(reader, tuple_pathes, days)
  41. start = start_time - stamp
  42. end = end_time - stamp
  43. spec_amount = 0.0
  44. for _mchid, _card_type, _spec, _data in gen:
  45. submit_count, succ_count, fail_count, succ_ratio, profit = calc_mch_profit(_data, pos_map, start, end)
  46. if _card_type is None and _spec is None:
  47. if succ_count == 0:
  48. profit += 10
  49. spec_amount += 10
  50. profit_ratio = profit / (spec_amount + 0.000001)
  51. gross[_mchid] = [submit_count, succ_count, fail_count, succ_ratio, profit, round(profit_ratio, 5)]
  52. spec_amount = 0.0
  53. else:
  54. key = f"{_mchid}-{_card_type}-{_spec}"
  55. amount = succ_count * _spec
  56. spec_amount += amount
  57. if succ_count == 0:
  58. profit += 10
  59. amount += 10
  60. profit_ratio = profit / (amount + 0.000001)
  61. detail[key] = [submit_count, succ_count, fail_count, succ_ratio, profit, round(profit_ratio, 5)]
  62. result = {'gross': gross, 'detail': detail}
  63. if len(gross) != 0 or len(detail) != 0:
  64. rclient.set(f"nc_refill_merchant_profit_ratio", json.dumps(result))
  65. rclient.publish('refill', json.dumps({'type': 'mch_profit_ratio', 'value': 0}))
  66. return 1