DataStream.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. return h5py.File(file, 'a')
  22. else:
  23. return h5py.File(file, 'r')
  24. def time_border(interval, time_stamp, lt):
  25. day = day_stamp(time_stamp)
  26. pos = time_stamp - day
  27. if lt:
  28. pos = pos - pos % interval
  29. elif pos % interval > 0:
  30. pos += interval - pos % interval
  31. else:
  32. pos = pos
  33. return pos + day
  34. def calc_interval(start, end):
  35. period = end - start
  36. segment = int(period / 24)
  37. if segment == 0:
  38. return 1
  39. else:
  40. intervals = [30, 60, 300, 600, 900, 1800, 3600, 7200, 14400, 21600, 28800, 36000, 43200, 86400, 172800]
  41. for i in intervals:
  42. if segment < i:
  43. return i
  44. pass
  45. def span_days(start_time, end_time):
  46. start_day = day_stamp(start_time)
  47. end_day = day_stamp(end_time)
  48. days = list()
  49. while start_day <= end_day:
  50. days.append(start_day)
  51. start_day += 86400
  52. return days
  53. class DataWriteStream(metaclass=ABCMeta):
  54. _version = 20200618
  55. _paths = dict()
  56. _lock = threading.Lock()
  57. def __init__(self, hfive):
  58. self._hfive = hfive
  59. # Getter function
  60. @property
  61. def file(self):
  62. return self._hfive
  63. # Setter function
  64. @file.setter
  65. def file(self, value):
  66. self._hfive = value
  67. @abstractmethod
  68. def write(self, method, params):
  69. pass
  70. def close(self):
  71. self._lock.acquire()
  72. if self._hfive is not None:
  73. self._hfive.close()
  74. self._hfive = None
  75. self._lock.release()
  76. pass
  77. class DataReadStream(metaclass=ABCMeta):
  78. _version = 20200618
  79. _days = list()
  80. def __init__(self, hfive):
  81. self._hfive = hfive
  82. self._days = self._getdays()
  83. logger.debug(self._days)
  84. def __del__(self):
  85. pass
  86. def days(self):
  87. return self._days
  88. # Getter function
  89. @property
  90. def file(self):
  91. return self._hfive
  92. # Setter function
  93. @file.setter
  94. def file(self, value):
  95. self._hfive = value
  96. def close(self):
  97. if self._hfive is not None:
  98. self._hfive.close()
  99. self._hfive = None
  100. def _root_path(self, day_stamp=None):
  101. if day_stamp is None:
  102. return f'/{self._version}/'
  103. else:
  104. return f'/{self._version}/{day_stamp}/'
  105. def datasets(self, day_stamp):
  106. def dir(group):
  107. result = []
  108. for name, sub in group.items():
  109. if isinstance(sub, h5py.Group):
  110. result.extend(dir(sub))
  111. else:
  112. result.append(sub.name)
  113. return result
  114. try:
  115. path = self._root_path(day_stamp)
  116. if path in self.file:
  117. group = self.file.require_group(path)
  118. days = dir(group)
  119. return days
  120. else:
  121. return []
  122. except Exception as ex:
  123. logger.error(ex)
  124. return []
  125. def _getdays(self):
  126. def sub(root):
  127. result = []
  128. try:
  129. for name, sub in root.items():
  130. if isinstance(sub, h5py.Group):
  131. result.append(name)
  132. except Exception as ex:
  133. logger.error(ex)
  134. finally:
  135. return result
  136. try:
  137. root_ptah = self._root_path()
  138. root = self.file.require_group(root_ptah)
  139. tmp = sub(root)
  140. days = [int(day) for day in tmp]
  141. days.sort()
  142. return days
  143. except Exception as ex:
  144. logger.error(ex)
  145. return []
  146. def near_stamp(self, time_stamp, left=True):
  147. if len(self._days) == 0:
  148. return None
  149. if time_stamp > int(time.time()):
  150. time_stamp = int(time.time())
  151. if left:
  152. min = self._days[0]
  153. time_stamp = min if time_stamp < min else time_stamp
  154. else:
  155. max = self._days[-1] + 86400 - 1
  156. time_stamp = max if time_stamp > max else time_stamp
  157. return time_stamp
  158. class EMchPosmap(IntEnum):
  159. submit_count = 0
  160. submit_amounts = 1
  161. succ_count = 2
  162. succ_mch_amounts = 3
  163. succ_ch_amounts = 4
  164. fail_count = 5
  165. fail_mch_amounts = 6
  166. @staticmethod
  167. def dim():
  168. return 7
  169. pass
  170. class EChPosmap(IntEnum):
  171. commit_count = 0
  172. commit_amounts = 1
  173. succ_count = 2
  174. succ_amounts = 3
  175. succ_periods = 4
  176. fail_count = 5
  177. fail_amounts = 6
  178. fail_periods = 7
  179. @staticmethod
  180. def dim():
  181. return 8
  182. pass
  183. class ENetPosmap(IntEnum):
  184. succ_count = 0
  185. fail_count = 1
  186. @staticmethod
  187. def dim():
  188. return 2
  189. pass