stanley-king 4 anos atrás
pai
commit
3e7df95848
3 arquivos alterados com 175 adições e 55 exclusões
  1. 116 28
      plot/DataCenter.py
  2. 15 1
      plot/app.py
  3. 44 26
      plot/thdf5.py

+ 116 - 28
plot/DataCenter.py

@@ -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)

+ 15 - 1
plot/app.py

@@ -2,16 +2,30 @@ import os
 from gevent import monkey
 from gevent.pywsgi import WSGIServer
 from flask import Flask
+
+import base64
+from io import BytesIO
+from matplotlib.figure import Figure
 import logging
 # from plog import initLog
 
+from DataCenter import dataCenter
 
 app = Flask(__name__)
 
 
 @app.route('/plot/index')
 def index():
-    return 'Hello, World!'
+    fig = Figure()
+    ax = fig.subplots()
+    ax.plot([1, 2])
+    # Save it to a temporary buffer.
+    buf = BytesIO()
+    fig.savefig(buf, format="png")
+    # Embed the result in the html output.
+    data = base64.b64encode(buf.getbuffer()).decode("ascii")
+    return f"<img src='data:image/png;base64,{data}'/>"
+
 
 if __name__ == "__main__":
     # logger = initLog()

+ 44 - 26
plot/thdf5.py

@@ -7,26 +7,23 @@ from datetime import timedelta
 import re
 import threading
 import numpy as np
-from DataCenter import DataCenter
+from DataCenter import dataCenter
+from matplotlib.figure import Figure
+from PIL import Image
+from io import BytesIO
+from PIL import Image
 
 class DataTest(unittest.TestCase):
-    def test_data(self):
-        data = DataCenter()
-        data.start()
-        data.wait()
-
     def test_parase(self):
         try:
-            data = DataCenter()
-            data.parase('succ-lingzh-1-4-50-1618184676', '1')
+            dataCenter.parase('succ-lingzh-1-4-50-1618184676', '1')
         except Exception as ex:
             print(ex)
 
-    #docker-compose run pythoncli python -m unittest thdf5.DataTest.test_predata
+    # docker-compose up pythoncli python -m unittest thdf5.DataTest.test_predata
     def test_predata(self):
         try:
-            data = DataCenter()
-            data.prepare_data()
+            dataCenter.prepare_data()
         except Exception as ex:
             print(ex)
 
@@ -55,35 +52,56 @@ class DataTest(unittest.TestCase):
         y = stamp - today
         pass
 
+    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 test_h5pyw(self):
         try:
-            file_name = '/var/www/html/data/stdata/test.hdf5'
-            f = h5py.File(file_name,'a')
-            print(f.keys())
-            f.create_group('/grp1/1/1/50')  # or f.create_group('grp1')
-            f.create_group('/grp1/1/2/50')  # or f.create_group('grp1')
-            f.create_group('/grp1/1/3/50')  # or f.create_group('grp1')
-            f.create_group('/grp1/1/4/50')  # or f.create_group('grp1')
+            file_name = '/var/www/html/data/stdata/data.hdf5'
+            f = h5py.File(file_name, 'a')
+            days = self.days(f)
             f.close()
         except Exception as ex:
             print(ex)
         pass
 
+    def test_data(self):
+        dataCenter.draw_plot(1618502478,chname='lingzh')
+
+    def dir(self, group):
+        result = []
+        for name, sub in group.items():
+            if isinstance(sub, h5py.Group):
+                result.extend(self.dir(sub))
+            else:
+                print(sub.name)
+                result.append(sub.name)
+
+        return result
+
     def test_h5pyr(self):
         try:
             file_name = '/var/www/html/data/stdata/data.hdf5'
             f = h5py.File(file_name, 'r')
-            days = f.keys()
-            data = np.zeros(())
-
-            for day in days:
-                    x = f.require_group(day)
-                    print(x.keys())
-                    print(x.name)
-
+            root = f.require_group('/')
+            result = self.dir(group=root)
             f.close()
         except Exception as ex:
             print(ex)
 
+    def test_fig(self):
+        buf = dataCenter.draw()
+        img = Image.open(buf)
+        img.show()
+
+
 if __name__ == '__main__':
     unittest.main()