|
@@ -8,6 +8,8 @@ from os import path
|
|
|
import re
|
|
|
from datetime import timedelta
|
|
|
import numpy as np
|
|
|
+from matplotlib.figure import Figure
|
|
|
+from io import BytesIO
|
|
|
|
|
|
|
|
|
class DataCenter(object):
|
|
@@ -15,37 +17,29 @@ class DataCenter(object):
|
|
|
'commit-succ': 0, 'commit-fail': 1, 'notify-succ': 2, 'notify-fail': 3, 'user_succ': 4
|
|
|
}
|
|
|
|
|
|
- def __init__(self):
|
|
|
+ def __init__(self, debug):
|
|
|
self._mquit = False
|
|
|
- file_name = '/var/www/html/data/stdata/data.hdf5'
|
|
|
- if path.exists(file_name):
|
|
|
- self._mHfive = h5py.File(file_name, 'r+')
|
|
|
+ if debug:
|
|
|
+ self._file_name = '/Users/stanley-king/work/PHPProject/stdata/data.hdf5'
|
|
|
else:
|
|
|
- self._mHfive = h5py.File(file_name, 'a')
|
|
|
-
|
|
|
- def __del__(self):
|
|
|
- self._mHfive.close()
|
|
|
-
|
|
|
- def start(self):
|
|
|
- self._mTrdReader = Thread(target=DataCenter.prepare_data, args=(self,))
|
|
|
- self._mTrdReader.start()
|
|
|
- self._mTrdReader.join()
|
|
|
- pass
|
|
|
+ self._file_name = '/var/www/html/data/stdata/data.hdf5'
|
|
|
|
|
|
def stop(self):
|
|
|
self._mquit = True
|
|
|
pass
|
|
|
|
|
|
- def wait(self):
|
|
|
- self._mTrdReader.join()
|
|
|
-
|
|
|
def prepare_data(self):
|
|
|
try:
|
|
|
+ if path.exists(self._file_name):
|
|
|
+ hfive = h5py.File(self._file_name, 'a')
|
|
|
+ else:
|
|
|
+ hfive = h5py.File(self._file_name, 'w')
|
|
|
# pool = redis.ConnectionPool(host='121.89.223.81', port=57649, db=0)
|
|
|
pool = redis.ConnectionPool(host='172.26.105.125', port=6379, db=0)
|
|
|
r = redis.Redis(connection_pool=pool)
|
|
|
- self.read_redis(r, 'nc_channel_monitor_commit', 'commit')
|
|
|
- self.read_redis(r, 'nc_channel_monitor_notify', 'notify')
|
|
|
+ self.read_redis(hfive, r, 'nc_channel_monitor_commit', 'commit')
|
|
|
+ self.read_redis(hfive, r, 'nc_channel_monitor_notify', 'notify')
|
|
|
+ hfive.close()
|
|
|
except Exception as ex:
|
|
|
print(ex)
|
|
|
# while not self._mquit:
|
|
@@ -58,20 +52,20 @@ class DataCenter(object):
|
|
|
# break
|
|
|
# except Exception as ex:
|
|
|
# print(ex)
|
|
|
- # finally:
|
|
|
- # time.sleep(60)
|
|
|
+ # finally:
|
|
|
+ # time.sleep(60)
|
|
|
pass
|
|
|
|
|
|
- def read_redis(self, redis, name, prefix):
|
|
|
+ def read_redis(self, hfive, redis, name, prefix):
|
|
|
i = 0
|
|
|
for item in redis.hscan_iter(name):
|
|
|
key = str(item[0], encoding="utf-8")
|
|
|
val = str(item[1], encoding="utf-8")
|
|
|
print(f'{prefix}:{i}')
|
|
|
i += 1
|
|
|
- self.parase(key, val, prefix)
|
|
|
+ self.parase(hfive, key, val, prefix)
|
|
|
|
|
|
- def parase(self, text, val, prefix):
|
|
|
+ def parase(self, hfive, text, val, prefix):
|
|
|
items = re.split(r'-', text)
|
|
|
if len(items) != 6:
|
|
|
return False
|
|
@@ -87,14 +81,14 @@ class DataCenter(object):
|
|
|
time = int(time)
|
|
|
today = self.day_stamp(time)
|
|
|
path = f'/{today}/{chname}/{quality}/{card_type}/{amount}'
|
|
|
- if path not in self._mHfive:
|
|
|
- self._mHfive[path] = np.zeros((5, 86400))
|
|
|
+ if path not in hfive:
|
|
|
+ hfive[path] = np.zeros((5, 86400))
|
|
|
|
|
|
diff = time - today
|
|
|
if diff < 0:
|
|
|
print(diff)
|
|
|
- self._mHfive[path][pos][diff] = int(val)
|
|
|
- print(path, pos, diff)
|
|
|
+ hfive[path][pos][diff] = int(val)
|
|
|
+ print(path, pos, diff, val,hfive[path][pos][diff])
|
|
|
pass
|
|
|
|
|
|
def day_stamp(self, stamp):
|
|
@@ -103,3 +97,97 @@ class DataCenter(object):
|
|
|
diff = timedelta(hours=x.tm_hour, minutes=x.tm_min, seconds=x.tm_sec)
|
|
|
today = stamp - diff.total_seconds()
|
|
|
return int(today)
|
|
|
+
|
|
|
+ def days(self, root):
|
|
|
+ result = []
|
|
|
+ try:
|
|
|
+ for name, sub in root.items():
|
|
|
+ if isinstance(sub, h5py.Group):
|
|
|
+ result.append(name)
|
|
|
+ except Exception as ex:
|
|
|
+ print(ex)
|
|
|
+ finally:
|
|
|
+ return result
|
|
|
+
|
|
|
+ def dir(self, group):
|
|
|
+ result = []
|
|
|
+ for name, sub in group.items():
|
|
|
+ if isinstance(sub, h5py.Group):
|
|
|
+ result.extend(self.dir(sub))
|
|
|
+ else:
|
|
|
+ result.append(sub.name)
|
|
|
+ return result
|
|
|
+
|
|
|
+ def draw_plot(self, start_time, **kwargs):
|
|
|
+ hfive = h5py.File(self._file_name, 'r')
|
|
|
+ try:
|
|
|
+ paths = self.datasets(hfive, start_time, **kwargs)
|
|
|
+
|
|
|
+ predata = np.zeros((5, 86400))
|
|
|
+ for data in self.read_data(hfive, paths):
|
|
|
+ predata = predata + data
|
|
|
+ print(type(data))
|
|
|
+ except Exception as ex:
|
|
|
+ print(ex)
|
|
|
+ finally:
|
|
|
+ hfive.close()
|
|
|
+
|
|
|
+ def read_data(self, hfive, paths):
|
|
|
+ for path in paths:
|
|
|
+ yield hfive[path]
|
|
|
+
|
|
|
+ def datasets(self, hfive, start_time, **kwargs):
|
|
|
+ day_stamp = self.day_stamp(start_time)
|
|
|
+ sday = f'{day_stamp}'
|
|
|
+ root = hfive.require_group('/')
|
|
|
+ days = self.days(root)
|
|
|
+ if sday not in days:
|
|
|
+ return False
|
|
|
+
|
|
|
+ group = hfive.require_group(sday)
|
|
|
+ dsets = self.dir(group)
|
|
|
+
|
|
|
+ chname = quality = card_type = amount = None
|
|
|
+ for key, val in kwargs.items():
|
|
|
+ if key == 'chname':
|
|
|
+ chname = val
|
|
|
+ elif key == 'quality':
|
|
|
+ quality = f'{val}'
|
|
|
+ elif key == 'card_type':
|
|
|
+ card_type = f'{val}'
|
|
|
+ elif key == 'amount':
|
|
|
+ amount = f'{val}'
|
|
|
+ else:
|
|
|
+ continue
|
|
|
+ return self._filter(dsets, chname=chname, quality=quality, card_type=card_type, amount=amount)
|
|
|
+
|
|
|
+ def _filter(self, dsets, chname=None, quality=None, card_type=None, amount=None):
|
|
|
+ result = []
|
|
|
+ for text in dsets:
|
|
|
+ items = re.split(r'/', text)
|
|
|
+ if len(items) != 6:
|
|
|
+ return False
|
|
|
+ (_, _sday, _chname, _quality, _card_type, _amount) = items
|
|
|
+ if (chname is not None) and (_chname != chname):
|
|
|
+ continue
|
|
|
+ if (quality is not None) and (_quality != quality):
|
|
|
+ continue
|
|
|
+ if (card_type is not None) and (_card_type != card_type):
|
|
|
+ continue
|
|
|
+ if (amount is not None) and (_amount != amount):
|
|
|
+ continue
|
|
|
+ result.append(text)
|
|
|
+
|
|
|
+ return result
|
|
|
+
|
|
|
+ def _draw_plot(self, **args):
|
|
|
+ fig = Figure()
|
|
|
+ ax = fig.subplots()
|
|
|
+ ax.plot([1, 2])
|
|
|
+
|
|
|
+ buf = BytesIO()
|
|
|
+ fig.savefig(buf, format="png")
|
|
|
+ return buf
|
|
|
+
|
|
|
+
|
|
|
+dataCenter = DataCenter(debug=False)
|