# from . import DataHandler #此时是导入文件 from .DataStream import DataWriteStream, day_stamp from .DataStream import ENetPosmap as pos_map import numpy as np __all__ = ['NetchkWriter'] import logging log = logging.getLogger('writer') class NetchkWriter(DataWriteStream): def __init__(self, hfive): DataWriteStream.__init__(self, hfive) dim = pos_map.dim() self._cache = np.zeros((dim, 86400)) def write(self, method, params): flush = True if method == 'net_succ': self._onSucc(params) elif method == 'net_fail': self._onFail(params) else: flush = False if flush: hfive = self.file hfive.flush() def write_batch(self, batches): for path, items in batches.items(): self._write_batch(path, items) def _write_batch(self, path, messages): def _succ(input): return input['time'] def _fail(input): return input['time'] def _pos(time): today = day_stamp(time) return time - today dset = self._data_set(path=path) self._cache[:, :] = dset for method, params in messages: if method == 'net_succ': time = _succ(params) pos = _pos(time) self._cache[pos_map.succ_count, pos] += 1 elif method == 'net_fail': time = _fail(params) pos = _pos(time) self._cache[pos_map.fail_count, pos] += 1 dset[:, :] = self._cache hfive = self.file hfive.flush() def _onSucc(self, params): def parse(input): return input['channel_name'], input['time'] chname, time = parse(params) dset, pos = self.path_pos(chname, time) dset[pos_map.succ_count, pos] += 1 pass def _onFail(self, params): def parse(input): return input['channel_name'], input['time'] chname, time = parse(params) dset, pos = self.path_pos(chname, time) dset[pos_map.fail_count, pos] += 1 pass def get_path(self, method, params): def _parse(input): return input['channel_name'], input['time'] def _path(input): chname, time = _parse(input) today = day_stamp(time) path = f'/{self._version}/{today}/{chname}' return path if method in ['net_succ', 'net_fail']: path = _path(params) return path else: return None def path_pos(self, chname, time): today = day_stamp(time) path = f'/{self._version}/{today}/{chname}' log.debug("%s,%s", 'NetchkWriter', path) dset = self._data_set(path=path) return dset, time - today def _data_set(self, path): hfive = self.file if path not in hfive: dim = pos_map.dim() dset = hfive.create_dataset(path, (dim, 86400), chunks=(dim, 3600)) dset[:, :] = np.zeros((dim, 86400)) hfive.flush() else: dset = hfive[path] return dset