from .MerchantCalc import MerchantCalc, mch_detail_paths, mch_paths, detail_paths from .algorithm import calc_morder_lack from .DataStream import EMchPosmap as pos_map import time as time import json import redis import logging logger = logging.getLogger('MAmountCalc') def earliest_time(rclient: redis.client): result = {} min_time = int(time.time()) data = rclient.get('nc_refill-stat-earliest-ordertime') if data is not None: mchid_times = json.loads(data) for mchid,order_time in mchid_times.items(): mchid = int(mchid) order_time = int(order_time) result[mchid] = order_time if order_time < min_time: min_time = order_time return result, min_time else: return result, min_time class MAmountCalc(MerchantCalc): def _calc_handler(self, rclient): mchid_times,earliest = earliest_time(rclient) end_time = int(time.time()) reader = self._reader() days, start_time, end_time = self.calc_time(reader, earliest, end_time) day_stamp = days[0] tuple_pathes = reader.many_tuple_path(days) gen = detail_paths(reader, tuple_pathes, days) mamounts = dict() for _mchid, _card_type, _spec, _data in gen: if _mchid not in mchid_times: continue else: _start_time = mchid_times[_mchid] send_amounts, lack_amounts = calc_morder_lack(_data, pos_map, start_time - day_stamp, end_time - day_stamp) if _mchid not in mamounts: mamounts[_mchid] = {'send_amounts': send_amounts, 'lack_amounts': lack_amounts} else: mamounts[_mchid]['send_amounts'] += send_amounts mamounts[_mchid]['lack_amounts'] += lack_amounts result = dict() for _mchid, _val in mamounts.items(): _send_amounts = round(_val['send_amounts'], 2) _lack_amounts = round(_val['lack_amounts'], 2) result[_mchid] = {'send_amounts': _send_amounts, 'lack_amounts': _lack_amounts} val = json.dumps({'send_amounts': result, 'time': int(time.time())}) rclient.set('nc_refill-stat-merchant-sendamount',val) logger.debug(result) return 1