|
@@ -1,18 +1,14 @@
|
|
|
|
|
|
-from .DataStream import EMchPosmap as pos_map, span_days, time_border, calc_interval
|
|
|
|
|
|
+from .DataStream import EMchPosmap as pos_map, span_days
|
|
from .MerchantReader import MerchantReader
|
|
from .MerchantReader import MerchantReader
|
|
-from matplotlib.figure import Figure
|
|
|
|
-from matplotlib import ticker
|
|
|
|
-from io import BytesIO
|
|
|
|
-import numpy as np
|
|
|
|
-from .algorithm import calc_mchratios, calc_morder_send
|
|
|
|
-import time as stime
|
|
|
|
|
|
+from .algorithm import calc_morder_lack
|
|
|
|
+import time as time
|
|
import logging
|
|
import logging
|
|
|
|
+import json
|
|
import redis
|
|
import redis
|
|
|
|
|
|
logger = logging.getLogger('painter')
|
|
logger = logging.getLogger('painter')
|
|
|
|
|
|
-
|
|
|
|
def detail_paths(reader: MerchantReader, tuple_pathes: dict, days: list):
|
|
def detail_paths(reader: MerchantReader, tuple_pathes: dict, days: list):
|
|
count = len(days)
|
|
count = len(days)
|
|
|
|
|
|
@@ -28,6 +24,24 @@ def detail_paths(reader: MerchantReader, tuple_pathes: dict, days: list):
|
|
yield mchid, _card_type, _spec, detail_datas
|
|
yield mchid, _card_type, _spec, detail_datas
|
|
|
|
|
|
|
|
|
|
|
|
+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 MerchantCalc(object):
|
|
class MerchantCalc(object):
|
|
def __init__(self):
|
|
def __init__(self):
|
|
self._mQuit = False
|
|
self._mQuit = False
|
|
@@ -50,25 +64,63 @@ class MerchantCalc(object):
|
|
try:
|
|
try:
|
|
if client is None:
|
|
if client is None:
|
|
client = redis_client()
|
|
client = redis_client()
|
|
-
|
|
|
|
- self._ratios(client)
|
|
|
|
|
|
+ send_amounts = self._send_amounts(client)
|
|
|
|
+ val = json.dumps({'send_amounts': send_amounts, 'time': int(time.time())})
|
|
|
|
+ client.set('nc_refill-stat-merchant-sendamount',val)
|
|
except redis.RedisError as ex:
|
|
except redis.RedisError as ex:
|
|
logger.error(ex)
|
|
logger.error(ex)
|
|
except Exception as ex:
|
|
except Exception as ex:
|
|
logger.error(ex)
|
|
logger.error(ex)
|
|
|
|
+ finally:
|
|
|
|
+ time.sleep(10)
|
|
|
|
|
|
- def _ratios(self, rclient):
|
|
|
|
- def earliest_time():
|
|
|
|
- order_time = rclient.hget('nc_refill-stat-earliest_sending', 'order_time')
|
|
|
|
- if order_time is not None:
|
|
|
|
- return int(order_time)
|
|
|
|
- else:
|
|
|
|
- return int(stime.time() - 86400)
|
|
|
|
|
|
+ def calc_time(self, start_time: int, end_time: int):
|
|
|
|
+ reader = MerchantReader()
|
|
|
|
+ end_time = reader.near_stamp(end_time, False)
|
|
|
|
+ if end_time is None:
|
|
|
|
+ raise Exception('end_time data is empty')
|
|
|
|
+
|
|
|
|
+ start_time = reader.near_stamp(start_time, True)
|
|
|
|
+ if start_time is None:
|
|
|
|
+ raise Exception('start_time data is empty')
|
|
|
|
+
|
|
|
|
+ strtime = lambda t: time.strftime('%d-%H:%M:%S', time.localtime(t))
|
|
|
|
+ logger.debug("near_stamp start_time %s end_time=%s", strtime(start_time), strtime(end_time))
|
|
|
|
+
|
|
|
|
+ if start_time >= end_time:
|
|
|
|
+ raise Exception('start_time equal endtime')
|
|
|
|
|
|
- start_time = earliest_time()
|
|
|
|
- end_time = int(stime.time())
|
|
|
|
-
|
|
|
|
- logger.debug('start_time=%d end_time=%d', start_time, end_time)
|
|
|
|
|
|
+ days = span_days(start_time, end_time)
|
|
|
|
|
|
|
|
+ strtime = lambda t: time.strftime('%d-%H:%M:%S', time.localtime(t))
|
|
|
|
+ sdays = [strtime(day) for day in days]
|
|
|
|
+ logger.debug(sdays)
|
|
|
|
+
|
|
|
|
+ return reader,days, start_time, end_time
|
|
|
|
+ pass
|
|
|
|
+
|
|
|
|
+ def _send_amounts(self, rclient):
|
|
|
|
+ mchid_times,earliest = earliest_time(rclient)
|
|
|
|
+ end_time = int(time.time())
|
|
|
|
+ reader, days, start_time, end_time = self.calc_time(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
|
|
|
|
+ logger.debug(mamounts)
|
|
|
|
|
|
- pass
|
|
|
|
|
|
+ return mamounts
|