NetchkWriter.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. # from . import DataHandler #此时是导入文件
  2. from .DataStream import DataWriteStream, day_stamp
  3. from .DataStream import ENetPosmap as pos_map
  4. import numpy as np
  5. __all__ = ['NetchkWriter']
  6. import logging
  7. log = logging.getLogger('writer')
  8. class NetchkWriter(DataWriteStream):
  9. def write(self, method, params):
  10. flush = True
  11. if method == 'net_succ':
  12. self._onSucc(params)
  13. elif method == 'net_fail':
  14. self._onFail(params)
  15. else:
  16. flush = False
  17. if flush:
  18. hfive = self.file
  19. hfive.flush()
  20. def _onSucc(self, params):
  21. def parse(input):
  22. return input['channel_name'], input['time']
  23. chname, time = parse(params)
  24. dset, pos = self.path_pos(chname, time)
  25. dset[pos_map.succ_count, pos] += 1
  26. pass
  27. def _onFail(self, params):
  28. def parse(input):
  29. return input['channel_name'], input['time']
  30. chname, time = parse(params)
  31. dset, pos = self.path_pos(chname, time)
  32. dset[pos_map.fail_count, pos] += 1
  33. pass
  34. def path_pos(self, chname, time):
  35. today = day_stamp(time)
  36. path = f'/{self._version}/{today}/{chname}'
  37. log.debug("%s,%s", 'NetchkWriter', path)
  38. hfive = self.file
  39. if path not in hfive:
  40. dim = pos_map.dim()
  41. dset = hfive.create_dataset(path, (dim, 86400), chunks=(dim, 3600))
  42. dset[:, :] = np.zeros((dim, 86400))
  43. hfive.flush()
  44. else:
  45. dset = hfive[path]
  46. return dset, time - today