123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- from .MerchantCalc import MerchantCalc, mch_detail_paths, mch_paths
- from .algorithm import calc_mch_profit
- from .DataStream import EMchPosmap as pos_map
- from .DataStream import day_stamp
- import logging
- import time as time
- import logging
- import json
- logger = logging.getLogger('MProfitRatioCalc')
- def mixed_ratio(rclient):
- result = list()
- data = rclient.get('nc_refill-stat-merchant-mixed')
- if data is not None:
- mch_cfgs = json.loads(data)
- for mchid, val in mch_cfgs.items():
- mchid = int(mchid)
- period = int(val['period'])
- formula = val['profit_formula'].strip()
- result.append((mchid, period, formula))
- return result
- class MProfitRatioCalc(MerchantCalc):
- def _calc_handler(self, rclient):
- logger.debug('_calc_handler')
- mixed_ratios = mixed_ratio(rclient)
- reader = self._reader()
- gross = dict()
- detail = dict()
- for mchid, period, formula in mixed_ratios:
- logger.debug(f'mchid={mchid} period={period}')
- end_time = int(time.time())
- if period == 86401:
- cur_day = day_stamp(end_time)
- period = end_time - cur_day
- logger.debug(f'mchid={mchid} period={period}')
- days, start_time, end_time = self.calc_time(reader, end_time - period, end_time)
- if len(days) == 0:
- continue
- stamp = days[0]
- tuple_pathes = reader.many_tuple_path(days, mchids={mchid})
- gen = mch_detail_paths(reader, tuple_pathes, days)
- start = start_time - stamp
- end = end_time - stamp
- spec_amount = 0.0
- for _mchid, _card_type, _spec, _data in gen:
- submit_count, succ_count, fail_count, succ_ratio, profit = calc_mch_profit(_data, pos_map, start, end)
- if _card_type is None and _spec is None:
- if succ_count == 0:
- profit += 5
- spec_amount += 5
- profit_ratio = profit / (spec_amount + 0.000001)
- gross[_mchid] = [submit_count, succ_count, fail_count, succ_ratio, profit, round(profit_ratio, 5)]
- spec_amount = 0.0
- else:
- key = f"{_mchid}-{_card_type}-{_spec}"
- amount = succ_count * _spec
- spec_amount += amount
- profit_ratio = profit / (amount + 0.000001)
- detail[key] = [submit_count, succ_count, fail_count, succ_ratio, profit, round(profit_ratio, 5)]
- result = {'gross': gross, 'detail': detail}
- if len(gross) != 0 or len(detail) != 0:
- rclient.set(f"nc_refill_merchant_profit_ratio", json.dumps(result))
- rclient.publish('refill', json.dumps({'type': 'mch_profit_ratio', 'value': 0}))
- return 1
|