|
@@ -9,6 +9,11 @@ import logging
|
|
|
log = logging.getLogger('writer')
|
|
|
|
|
|
class ChannelWriter(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 == 'ch_commit':
|
|
@@ -25,6 +30,45 @@ class ChannelWriter(DataWriteStream):
|
|
|
hfive.flush()
|
|
|
pass
|
|
|
|
|
|
+ def write_batch(self, batches):
|
|
|
+ for path, items in batches.items():
|
|
|
+ self._write_batch(path, items)
|
|
|
+
|
|
|
+ def _write_batch(self, path, messages):
|
|
|
+ def _commit(params):
|
|
|
+ return input['time'], input['channel_amount']
|
|
|
+ def _succ(input):
|
|
|
+ return input['time'], input['channel_amount'], input['period']
|
|
|
+ def _fail(params):
|
|
|
+ return input['time'], input['channel_amount'], input['period']
|
|
|
+ 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 == 'ch_commit':
|
|
|
+ time, channel_amount = _commit(params)
|
|
|
+ pos = _pos(time)
|
|
|
+ rows = [pos_map.commit_count, pos_map.commit_amounts]
|
|
|
+ self._cache[rows, pos] += [1, channel_amount]
|
|
|
+ elif method == 'ch_succ':
|
|
|
+ time, channel_amount, period = _succ(params)
|
|
|
+ pos = _pos(time)
|
|
|
+ rows = [pos_map.succ_count, pos_map.succ_amounts, pos_map.succ_periods]
|
|
|
+ self._cache[rows, pos] += [1, channel_amount, period]
|
|
|
+ elif method == 'ch_fail':
|
|
|
+ time, channel_amount, period = _fail(params)
|
|
|
+ pos = _pos(time)
|
|
|
+ rows = [pos_map.fail_count, pos_map.fail_amounts, pos_map.fail_periods]
|
|
|
+ self._cache[rows, pos] += [1, channel_amount, period]
|
|
|
+
|
|
|
+ dset[:, :] = self._cache
|
|
|
+ hfive = self.file
|
|
|
+ hfive.flush()
|
|
|
+
|
|
|
def _onCommit(self, params):
|
|
|
def parse(input):
|
|
|
return input['channel_name'], input['time'], input['spec'], input['card_type'], input['channel_amount']
|
|
@@ -35,9 +79,6 @@ class ChannelWriter(DataWriteStream):
|
|
|
rows = [pos_map.commit_count, pos_map.commit_amounts]
|
|
|
vals = [1, channel_amount]
|
|
|
dset[rows, pos] += vals
|
|
|
-
|
|
|
- # dset[pos_map.commit_count, pos] += 1
|
|
|
- # dset[pos_map.commit_amounts, pos] += channel_amount
|
|
|
pass
|
|
|
|
|
|
def _onSucc(self, params):
|
|
@@ -50,10 +91,6 @@ class ChannelWriter(DataWriteStream):
|
|
|
rows = [pos_map.succ_count, pos_map.succ_amounts, pos_map.succ_periods]
|
|
|
vals = [1, channel_amount, period]
|
|
|
dset[rows, pos] += vals
|
|
|
-
|
|
|
- # dset[pos_map.succ_count, pos] += 1
|
|
|
- # dset[pos_map.succ_amounts, pos] += channel_amount
|
|
|
- # dset[pos_map.succ_periods, pos] += period
|
|
|
pass
|
|
|
|
|
|
def _onFail(self, params):
|
|
@@ -66,17 +103,45 @@ class ChannelWriter(DataWriteStream):
|
|
|
rows = [pos_map.fail_count, pos_map.fail_amounts, pos_map.fail_periods]
|
|
|
vals = [1, channel_amount, period]
|
|
|
dset[rows, pos] += vals
|
|
|
-
|
|
|
- # dset[pos_map.fail_count, pos] += 1
|
|
|
- # dset[pos_map.fail_amounts, pos] += channel_amount
|
|
|
- # dset[pos_map.fail_periods, pos] += period
|
|
|
pass
|
|
|
|
|
|
+ def get_path(self, method, params):
|
|
|
+ def _path(chname, time, spec, card_type):
|
|
|
+ today = day_stamp(time)
|
|
|
+ path = f'/{self._version}/{today}/{chname}/{card_type}/{spec}'
|
|
|
+ return path
|
|
|
+ def _submit(input):
|
|
|
+ return input['channel_name'], input['time'], input['spec'], input['card_type']
|
|
|
+ def _succ(input):
|
|
|
+ return input['channel_name'], input['time'], input['spec'], input['card_type']
|
|
|
+ def _fail(input):
|
|
|
+ return input['channel_name'], input['time'], input['spec'], input['card_type']
|
|
|
+
|
|
|
+ if method == 'ch_commit':
|
|
|
+ func = _submit
|
|
|
+ elif method == 'ch_succ':
|
|
|
+ func = _succ
|
|
|
+ elif method == 'ch_fail':
|
|
|
+ func = _fail
|
|
|
+ else:
|
|
|
+ func = None
|
|
|
+
|
|
|
+ if func is not None:
|
|
|
+ chname, time, spec, card_type = func(params)
|
|
|
+ path = _path(chname, time, spec, card_type)
|
|
|
+ return path
|
|
|
+ else:
|
|
|
+ return None
|
|
|
+
|
|
|
def path_pos(self, chname, time, spec, card_type):
|
|
|
today = day_stamp(time)
|
|
|
path = f'/{self._version}/{today}/{chname}/{card_type}/{spec}'
|
|
|
log.debug("%s,%s", 'ChannelWriter', 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()
|
|
@@ -85,5 +150,4 @@ class ChannelWriter(DataWriteStream):
|
|
|
hfive.flush()
|
|
|
else:
|
|
|
dset = hfive[path]
|
|
|
-
|
|
|
- return dset, time - today
|
|
|
+ return dset
|