|
@@ -1,15 +1,17 @@
|
|
|
-from .DataStream import EMchPosmap as pos_map, day_stamp, span_days, time_border, calc_interval
|
|
|
+from .DataStream import EMchPosmap as pos_map, span_days, time_border, calc_interval
|
|
|
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
|
|
|
+from .algorithm import calc_mchratios, calc_morder_send
|
|
|
import time as time
|
|
|
|
|
|
import logging
|
|
|
+
|
|
|
logger = logging.getLogger('painter')
|
|
|
|
|
|
+
|
|
|
def allpathes(reader: MerchantReader, tuple_pathes: dict, days: list, spec=None):
|
|
|
count = len(days)
|
|
|
show_detail = True if len(list(tuple_pathes.keys())) == 1 else False
|
|
@@ -54,11 +56,21 @@ class MerchantPainter(object):
|
|
|
self._reader = MerchantReader()
|
|
|
_start_time, _end_time, self._mchids, self._card_types, self._spec, self._filter_wave = start_time, end_time, mchids, card_types, spec, filter_wave
|
|
|
|
|
|
- start_time = self._reader.near_stamp(_start_time,True)
|
|
|
- end_time = self._reader.near_stamp(_end_time,False)
|
|
|
+ if _end_time is None:
|
|
|
+ _end_time = int(time.time())
|
|
|
+ end_time = self._reader.near_stamp(_end_time, False)
|
|
|
+ if end_time is None:
|
|
|
+ raise Exception('data is empty')
|
|
|
+
|
|
|
+ if _start_time is None or start_time > end_time:
|
|
|
+ _start_time = end_time - 7200
|
|
|
+
|
|
|
+ start_time = self._reader.near_stamp(_start_time, True)
|
|
|
+ if start_time is None:
|
|
|
+ raise Exception('data is empty')
|
|
|
|
|
|
stime = lambda t: time.strftime('%d-%H:%M:%S', time.localtime(t))
|
|
|
- logger.debug("start_time=%s end_time=%s",stime(start_time) ,stime(end_time))
|
|
|
+ logger.debug("start_time %d , %s end_time=%s", start_time, stime(start_time), stime(end_time))
|
|
|
|
|
|
interval = calc_interval(start_time, end_time)
|
|
|
start_time = time_border(interval, start_time, True)
|
|
@@ -77,10 +89,18 @@ class MerchantPainter(object):
|
|
|
ax.set(xlabel='time', ylabel='ratio')
|
|
|
return ax, fig
|
|
|
|
|
|
- def flush(ax, fig, ticks, lables):
|
|
|
+ def flush(ax, fig, xticks=None, xlables=None, yticks=None, ylables=None):
|
|
|
ax.yaxis.set_major_formatter(ticker.PercentFormatter(xmax=1, decimals=4))
|
|
|
- ax.set_xticks(ticks=ticks)
|
|
|
- ax.set_xticklabels(lables)
|
|
|
+ if xticks is not None:
|
|
|
+ ax.set_xticks(ticks=xticks)
|
|
|
+ if xlables is not None:
|
|
|
+ ax.set_xticklabels(xlables)
|
|
|
+
|
|
|
+ if yticks is not None:
|
|
|
+ ax.set_yticks(ticks=yticks)
|
|
|
+ if ylables is not None:
|
|
|
+ ax.set_yticklabels(ylables)
|
|
|
+
|
|
|
fig.autofmt_xdate()
|
|
|
ax.grid()
|
|
|
fig.subplots_adjust(left=0.1, right=0.8, top=0.95, bottom=0.1)
|
|
@@ -113,10 +133,10 @@ class MerchantPainter(object):
|
|
|
y = np.convolve(y, window, 'same') if window is not None else y
|
|
|
ax.plot(x, y, ls='-', label=self._label(chname=_mchid, succ=succ, count=count, card_type=_card_type, spec=_spec))
|
|
|
|
|
|
- ticks = [d - self._start_time for d in range(self._start_time, self._end_time + 1, self._interval)]
|
|
|
- xlables = [time.strftime('%d-%H:%M:%S', time.localtime(d + self._start_time)) for d in ticks]
|
|
|
+ xticks = [d - self._start_time for d in range(self._start_time, self._end_time + 1, self._interval)]
|
|
|
+ xlables = [time.strftime('%d-%H:%M:%S', time.localtime(d + self._start_time)) for d in xticks]
|
|
|
|
|
|
- return fig_flush(ax, fig, ticks, xlables)
|
|
|
+ return fig_flush(ax, fig, xticks=xticks, xlables=xlables)
|
|
|
|
|
|
def _label(self, chname, succ, count, card_type=None, spec=None):
|
|
|
_card_type = None
|
|
@@ -148,4 +168,66 @@ class MerchantPainter(object):
|
|
|
|
|
|
return lable
|
|
|
|
|
|
+ def _fig_bar_funs(self):
|
|
|
+ def create():
|
|
|
+ fig = Figure(figsize=(19, 8))
|
|
|
+ ax = fig.subplots()
|
|
|
+ ax.set_title('success ratio')
|
|
|
+ ax.set(xlabel='time', ylabel='ratio')
|
|
|
+ return ax, fig
|
|
|
+
|
|
|
+ def flush(ax, fig, xticks=None, xlables=None, yticks=None, ylables=None):
|
|
|
+ if xticks is not None:
|
|
|
+ ax.set_xticks(ticks=xticks)
|
|
|
+ if xlables is not None:
|
|
|
+ ax.set_xticklabels(xlables)
|
|
|
+
|
|
|
+ if yticks is not None:
|
|
|
+ ax.set_yticks(ticks=yticks)
|
|
|
+ if ylables is not None:
|
|
|
+ ax.set_yticklabels(ylables)
|
|
|
+
|
|
|
+ ax.grid()
|
|
|
+ fig.autofmt_xdate()
|
|
|
+
|
|
|
+ buf = BytesIO()
|
|
|
+ fig.savefig(buf, format="png")
|
|
|
+ return buf
|
|
|
+
|
|
|
+ return create, flush
|
|
|
+
|
|
|
+ def paint_refilling(self):
|
|
|
+ tuple_pathes = self._reader.many_tuple_path(self._days, self._mchids, self._card_types, self._spec)
|
|
|
+ gen = allpathes(self._reader, tuple_pathes, self._days, self._spec)
|
|
|
+ if len(self._days) == 0:
|
|
|
+ return BytesIO()
|
|
|
+
|
|
|
+ day_stamp = self._days[0]
|
|
|
+ fig_create, fig_flush = self._fig_bar_funs()
|
|
|
+ ax, fig = fig_create()
|
|
|
+
|
|
|
+ lables = list()
|
|
|
+ datas = list()
|
|
|
+ for _mchid, _card_type, _spec, _data in gen:
|
|
|
+ if _mchid == 'all':
|
|
|
+ continue
|
|
|
+
|
|
|
+ lables.append(f"{_mchid}")
|
|
|
+ ret = calc_morder_send(_data, pos_map, self._start_time - day_stamp, self._end_time - day_stamp)
|
|
|
+ datas.append(ret)
|
|
|
+
|
|
|
+ send_count, submit_count, succ_count, fail_count, amounts, lack = zip(*datas)
|
|
|
+ width = 0.20
|
|
|
+ x_asix = np.arange(len(lables))
|
|
|
+
|
|
|
+ rect_send = ax.bar(x_asix - width * 0.5, list(send_count), width, label='send', align='center')
|
|
|
+ rect_submit = ax.bar(x_asix - width * 1.5, list(submit_count), width, label='summit',align='center')
|
|
|
+ rect_succ = ax.bar(x_asix + width * 0.5, list(succ_count), width, label='succ',align='center')
|
|
|
+ rect_fail = ax.bar(x_asix + width * 1.5, list(fail_count), width, label='fail', align='center')
|
|
|
+
|
|
|
+ ax.bar_label(rect_send, padding=3)
|
|
|
+ # ax.bar_label(rect_submit, padding=3)
|
|
|
+ # ax.bar_label(rect_succ, padding=3)
|
|
|
+ # ax.bar_label(rect_fail, padding=3)
|
|
|
|
|
|
+ return fig_flush(ax, fig, xticks=x_asix, xlables=lables)
|