DataStream.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. import time
  2. from abc import ABCMeta, abstractmethod, ABC
  3. from datetime import timedelta
  4. from mpi4py import MPI
  5. import h5py
  6. from enum import IntEnum
  7. import threading
  8. __all__ = ['DataWriteStream', 'DataReadStream', 'day_stamp', 'EMchPosmap', 'EChPosmap', 'ENetPosmap', 'open_hdf5', 'time_border',
  9. 'calc_interval']
  10. import logging
  11. logger = logging.getLogger('stream')
  12. def day_stamp(stamp):
  13. import time as stime
  14. stamp = int(stamp)
  15. st_time = stime.gmtime(stamp + 8 * 3600)
  16. diff = timedelta(hours=st_time.tm_hour, minutes=st_time.tm_min, seconds=st_time.tm_sec)
  17. today = stamp - diff.total_seconds()
  18. return int(today)
  19. def open_hdf5(file, is_wirte):
  20. if is_wirte:
  21. # 不知道为何在内存中加载这么慢
  22. # return h5py.File(file, 'a', libver='latest', driver='core') # , swmr=True, backing_store=True
  23. # return h5py.File(file, driver='core',backing_store=True) # , swmr=True, backing_store=True
  24. #这种加载方式速度可以
  25. return h5py.File(file, 'a', libver='latest', rdcc_nbytes=1024 ** 3, rdcc_w0=0.25, rdcc_nslots=6151)
  26. else:
  27. return h5py.File(file, 'r') #, libver='latest') #plot/test_h5py.py
  28. def time_border(interval, time_stamp, lt):
  29. day = day_stamp(time_stamp)
  30. pos = time_stamp - day
  31. if lt:
  32. pos = pos - pos % interval
  33. elif pos % interval > 0:
  34. pos += interval - pos % interval
  35. else:
  36. pos = pos
  37. return pos + day
  38. def calc_interval(start, end):
  39. period = end - start
  40. segment = int(period / 24)
  41. if segment == 0:
  42. return 1
  43. else:
  44. intervals = [30, 60, 300, 600, 900, 1800, 3600, 7200, 10800, 14400, 18000, 21600, 25200, 28800, 32400, 36000, 39600, 43200,
  45. 86400, 172800]
  46. for i in intervals:
  47. if segment < i:
  48. return i
  49. pass
  50. def span_days(start_time, end_time):
  51. start_day = day_stamp(start_time)
  52. end_day = day_stamp(end_time)
  53. if end_day == end_time:
  54. end_day = end_day - 1
  55. days = list()
  56. while start_day <= end_day:
  57. days.append(start_day)
  58. start_day += 86400
  59. return days
  60. def ch_calc_cfgs():
  61. ratio_period = 1800
  62. speed_period = 300
  63. monitor_period = 600
  64. return ratio_period,speed_period,monitor_period
  65. class DataWriteStream(metaclass=ABCMeta):
  66. _version = 20200618
  67. _paths = dict()
  68. _lock = threading.Lock()
  69. def __init__(self, hfive):
  70. self._hfive = hfive
  71. # Getter function
  72. @property
  73. def file(self):
  74. return self._hfive
  75. # Setter function
  76. @file.setter
  77. def file(self, value):
  78. self._hfive = value
  79. @abstractmethod
  80. def write(self, method, params):
  81. pass
  82. def flush(self):
  83. hfive = self.file
  84. hfive.flush()
  85. def close(self):
  86. self._lock.acquire()
  87. if self._hfive is not None:
  88. self._hfive.close()
  89. self._hfive = None
  90. self._lock.release()
  91. pass
  92. class DataReadStream(metaclass=ABCMeta):
  93. _version = 20200618
  94. _days = list()
  95. def __init__(self, hfive):
  96. self._hfive = hfive
  97. self._days = self._getdays()
  98. stime = lambda t: time.strftime('%y-%m-%d %H:%M:%S', time.localtime(t))
  99. sdays = [stime(day) for day in self._days]
  100. logger.debug(sdays)
  101. def __del__(self):
  102. pass
  103. def days(self):
  104. return self._days
  105. # Getter function
  106. @property
  107. def file(self):
  108. return self._hfive
  109. # Setter function
  110. @file.setter
  111. def file(self, value):
  112. self._hfive = value
  113. def close(self):
  114. if self._hfive is not None:
  115. self._hfive.close()
  116. self._hfive = None
  117. def _root_path(self, day_stamp=None):
  118. if day_stamp is None:
  119. return f'/{self._version}/'
  120. else:
  121. return f'/{self._version}/{day_stamp}/'
  122. def datasets(self, day_stamp):
  123. def dir(group):
  124. result = []
  125. for name, sub in group.items():
  126. if isinstance(sub, h5py.Group):
  127. result.extend(dir(sub))
  128. else:
  129. result.append(sub.name)
  130. return result
  131. try:
  132. path = self._root_path(day_stamp)
  133. if path in self.file:
  134. group = self.file.require_group(path)
  135. days = dir(group)
  136. return days
  137. else:
  138. return []
  139. except Exception as ex:
  140. logger.error(ex)
  141. return []
  142. def _getdays(self):
  143. def sub(root):
  144. result = []
  145. try:
  146. for name, sub in root.items():
  147. if isinstance(sub, h5py.Group):
  148. result.append(name)
  149. except Exception as ex:
  150. logger.error(ex)
  151. finally:
  152. return result
  153. try:
  154. root_ptah = self._root_path()
  155. root = self.file.require_group(root_ptah)
  156. tmp = sub(root)
  157. days = [int(day) for day in tmp]
  158. days.sort()
  159. return days
  160. except Exception as ex:
  161. logger.error(ex)
  162. return []
  163. class EMchPosmap(IntEnum):
  164. submit_count = 0
  165. submit_amounts = 1
  166. succ_count = 2
  167. succ_mch_amounts = 3
  168. succ_ch_amounts = 4
  169. fail_count = 5
  170. fail_mch_amounts = 6
  171. @staticmethod
  172. def dim():
  173. return 7
  174. pass
  175. class EChPosmap(IntEnum):
  176. commit_count = 0
  177. commit_amounts = 1
  178. succ_count = 2
  179. succ_amounts = 3
  180. succ_periods = 4
  181. fail_count = 5
  182. fail_amounts = 6
  183. fail_periods = 7
  184. succ_mch_amounts = 8
  185. @staticmethod
  186. def dim():
  187. return 9
  188. @staticmethod
  189. def old_dim():
  190. return 8
  191. pass
  192. class ENetPosmap(IntEnum):
  193. succ_count = 0
  194. fail_count = 1
  195. @staticmethod
  196. def dim():
  197. return 2
  198. pass