1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
- from .MerchantCalc import MerchantCalc, mch_cardtype_paths
- from .algorithm import calc_mchratios_val
- 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('MTimesRatioCalc')
- class MTimesRatioCalc(MerchantCalc):
- def _calc_handler(self, rclient):
- def calc_start_end(presecs,end):
- result = []
- for x in presecs:
- if end > x:
- result.append((x, end - x, end))
- else:
- result.append((x, 0, end))
- return result
- logger.debug('_calc_handler')
- reader = self._reader()
- end_time = int(time.time())
- start_time = day_stamp(end_time)
- days, start_time, end_time = self.calc_time(reader, start_time, end_time)
- if len(days) == 0:
- return 60
- day_start = days[0]
- start_end_tups = calc_start_end([900, 1800, 3600, 7200, 86400], end_time - day_start)
- tuple_pathes = reader.many_tuple_path(days)
- gen = mch_cardtype_paths(reader, tuple_pathes, days)
- card_types = {None: 'ALL', 4: 'YD', 5: 'LT', 6: 'DX'}
- result = dict()
- for _mchid, _card_type, _spec, _data in gen:
- for secs, start, end in start_end_tups:
- succs, commits, ratio = calc_mchratios_val(_data, pos_map, start, end)
- if _mchid not in result:
- result[_mchid] = dict()
- if _card_type not in card_types:
- continue
- name = card_types[_card_type]
- if name not in result[_mchid]:
- result[_mchid][name] = dict()
- result[_mchid][name][secs] = [succs, commits, ratio]
- if len(result) != 0:
- rclient.set(f"nc_merchant_card_type_ratios", json.dumps(result))
- return 60
|