def test_value_accumulator_with_one_value_is_stored_as_single_float64(): acc = ValueAccumulator() acc.add(4.2) data = acc.to_bytes() assert struct.unpack('d', data)[0] == 4.2 restored_acc = ValueAccumulator.from_bytes(data) assert acc == restored_acc
def test_value_accumulator_can_be_stored_as_bytes(): values = [4.2, -1.3, 10.8] acc = ValueAccumulator(values) data = acc.to_bytes() assert isinstance(data, bytes) restored_acc = ValueAccumulator.from_bytes(data) assert acc == restored_acc
def decode(key: bytes, value: bytes) -> Optional[MetricsEvent]: key = int.from_bytes(key, byteorder='big', signed=False) ts = datetime.utcfromtimestamp((key >> KvStoreMetricsFormat.seq_bits) / 1000000.0) name = int.from_bytes(value[:32], byteorder='big', signed=False) if name not in MetricsName.__members__.values(): return None name = MetricsName(name) data = value[32:] if len(data) == 8: value = struct.unpack('d', data)[0] else: value = ValueAccumulator.from_bytes(data) return MetricsEvent(ts, name, value)