12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- from .MerchantCalc import MerchantCalc, mch_detail_paths, mch_paths
- from .algorithm import calc_mch_profit
- from .DataStream import EMchPosmap as pos_map
- 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:
- end_time = int(time.time())
- days, start_time, end_time = self.calc_time(reader, end_time - period, end_time)
- if len(days) == 0:
- continue
- day_stamp = days[0]
- tuple_pathes = reader.many_tuple_path(days, mchids={mchid})
- gen = mch_detail_paths(reader, tuple_pathes, days)
- start = start_time - day_stamp
- end = end_time - day_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:
- 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
|