stanley-king 2 лет назад
Родитель
Сommit
8de82ce03f
4 измененных файлов с 69 добавлено и 0 удалено
  1. 19 0
      plot/chspeedctl.py
  2. 42 0
      plot/refill/ChSpeedControlCalc.py
  3. 7 0
      plot/refill/DataStream.py
  4. 1 0
      plot/refill/__init__.py

+ 19 - 0
plot/chspeedctl.py

@@ -0,0 +1,19 @@
+from refill import opt_parse, ChSpeedControlCalc
+import signal as sig
+import sys,getopt
+
+import logging
+logging.basicConfig(filename='/var/www/html/data/log/statcalc.log',
+                    format='%(levelname)10s  %(asctime)s  %(name)10s %(thread)d %(message)s',
+                    level=logging.DEBUG)
+logger = logging.getLogger('channel_speed_ratio')
+
+if __name__ == '__main__':
+    try:
+        rhost, rport = opt_parse()
+        calc = ChSpeedControlCalc()
+        calc.set_redis(rhost, rport)
+        sig.signal(sig.SIGINT, lambda: calc.stop())
+        calc.run()
+    except Exception as ex:
+        logger.error(ex)

+ 42 - 0
plot/refill/ChSpeedControlCalc.py

@@ -0,0 +1,42 @@
+from .ChannelCalc import ChannelCalc, detail_pathes
+from .algorithm import calc_chspeed, calc_chratio, calc_commit
+from .DataStream import EChPosmap as pos_map
+from .DataStream import ch_calc_cfgs
+from .ChannelReader import ChannelReader
+import time as time
+import json
+import redis
+
+import logging
+logger = logging.getLogger('ChSpeedControlCalc')
+
+class ChSpeedControlCalc(ChannelCalc):
+
+    def _calc_handler(self, rclient):
+        reader = self._reader()
+        end_time = int(time.time())
+        period = 1800
+        days, start_time, end_time = self.calc_time(reader, end_time - period, end_time)
+
+        day_stamp = days[0]
+        tuple_pathes = reader.many_tuple_path(days, card_types=set([4, 5, 6]))
+        gen = detail_pathes(reader, tuple_pathes, days)
+
+        start_pos = start_time - day_stamp
+        end_pos = end_time - day_stamp
+
+        result = dict()
+        for _name, _card_type, _spec, _data in gen:
+            speed = calc_chspeed(_data, pos_map, end_pos - 300, end_pos) / 5
+            ratio, ratio_commit, notify_time, succ_time = calc_chratio(_data, pos_map, end_pos - period, end_pos)
+            monitor_commit = calc_commit(_data, pos_map, end_pos - 600, end_pos) / 5
+
+            key = f'{_name}-{_spec}-{_card_type}'
+            result[key] = [speed, ratio, ratio_commit, notify_time, monitor_commit]
+            logger.debug("%s-%d-%d speed=%d ratio=%.5f commit_count=%d notify_time=%.5f succ_time=%.5f monitor_commit=%d", _name, _card_type, _spec, speed, ratio,
+                         ratio_commit, notify_time, succ_time, monitor_commit)
+
+        if len(result) != 0:
+            rclient.set(f"nc_refill_channel_control_speed", json.dumps(result))
+            rclient.publish('refill', json.dumps({'type': 'channel_speed', 'value': 0}))
+        return 60

+ 7 - 0
plot/refill/DataStream.py

@@ -76,6 +76,13 @@ def span_days(start_time, end_time):
         start_day += 86400
     return days
 
+def ch_calc_cfgs():
+    ratio_period = 1800
+    speed_period = 300
+    monitor_period = 600
+
+    return ratio_period,speed_period,monitor_period
+
 
 class DataWriteStream(metaclass=ABCMeta):
     _version = 20200618

+ 1 - 0
plot/refill/__init__.py

@@ -26,6 +26,7 @@ from .MAmountCalc import MAmountCalc
 from .MProfitRatioCalc import MProfitRatioCalc
 from .MTimesRatioCalc import MTimesRatioCalc
 from .ChSpeedRatioCalc import ChSpeedRatioCalc
+from .ChSpeedControlCalc import ChSpeedControlCalc
 
 from .WriterConsumer import WriterConsumer