123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- import redis
- import time as time
- import logging
- import json
- from .DataStream import span_days
- import logging
- logger = logging.getLogger('CalcBase')
- class CalcBase(object):
- def __init__(self):
- self._mQuit = False
- self._mRHost = ''
- self._mRPort = 6379
- def set_redis(self, rhost, rport):
- self._mRHost = rhost
- self._mRPort = rport
- def stop(self):
- self._mquit = True
- def _calc_handler(self, rclient):
- pass
- def _reader(self):
- return None
- def run(self):
- def redis_client():
- pool = redis.ConnectionPool(host=self._mRHost, port=self._mRPort)
- client = redis.Redis(connection_pool=pool)
- return client
- client = None
- loop = 0;
- while self._mQuit == False:
- try:
- if client is None:
- client = redis_client()
- self._calc_handler(client)
- except redis.RedisError as ex:
- logger.error(ex)
- except Exception as ex:
- logger.error(ex)
- finally:
- time.sleep(1)
- loop += 1
- def calc_time(self, reader, start_time: int, end_time: int):
- 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')
- 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 days, start_time, end_time
|