def test_repr_str(self): """ Assert __repr__ and __str__ work correctly """ point = RawPoint(1, 10) expected = "RawPoint(1, 10)" assert str(point) == expected assert point.__repr__() == expected
def test_to_proto(self): """ Assert the to_proto """ point = RawPoint(1, 10) proto = RawPoint.to_proto(point) assert proto.time == 1 assert proto.value == 10 assert proto.__class__ == btrdb_pb2.RawPoint
def test_next_key_ready_if_none_are(self): """ Assert next_key_ready returns None if nothing is ready """ buffer = PointBuffer(3) assert buffer.next_key_ready() == None buffer.add_point(0, RawPoint(time=1000, value="leopard")) buffer.add_point(2, RawPoint(time=1000, value="giraffe")) buffer.add_point(0, RawPoint(time=2000, value="horse")) buffer.add_point(0, RawPoint(time=3000, value="pig")) assert buffer.next_key_ready() == None
def test_equals(self): """ Assert the __eq__ works correctly """ point1 = RawPoint(1, 1) point2 = RawPoint(1, 1) point3 = RawPoint(1, 2) point4 = RawPoint(2, 1) assert point1 == point2 assert point1 != point3 assert point1 != point4
def test_to_proto_list(self): """ Assert the to_proto """ points = [RawPoint(1, 10), RawPoint(2, 20)] protos = RawPoint.to_proto_list(points) assert protos[0].time == 1 assert protos[0].value == 10 assert protos[0].__class__ == btrdb_pb2.RawPoint assert protos[1].time == 2 assert protos[1].value == 20 assert protos[1].__class__ == btrdb_pb2.RawPoint
def test_is_ready(self): """ Assert is_ready returns correct value """ buffer = PointBuffer(2) buffer.add_point(0, RawPoint(time=1000, value="zebra")) buffer.add_point(1, RawPoint(time=1000, value="giraffe")) buffer.add_point(0, RawPoint(time=2000, value="horse")) assert buffer.is_ready(1000) == True assert buffer.is_ready(2000) == False buffer.deactivate(1) assert buffer.is_ready(2000) == True
def test_get_returns_correct_values(self): """ Assert the __get__ returns correct values """ point = RawPoint(1, 10) assert point[0] == 1 assert point[1] == 10
def test_from_proto_creates_rawpoint(self): """ Assert from_proto creates a new instance of RawPoint """ proto = RawPointProto(time=1, value=10) point = RawPoint.from_proto(proto) assert isinstance(point, RawPoint)
def test_create(self): """ Ensure we can create the object """ point = RawPoint(1, 2) assert point.time == 1 assert point.value == 2
def test_from_proto(self): """ Assert from_proto creates a correct object """ proto = RawPointProto(time=1, value=10) point = RawPoint.from_proto(proto) assert point.time == proto.time assert point.value == proto.value
def insert(self, uu, values): protoValues = RawPoint.to_proto_list(values) params = btrdb_pb2.InsertParams(uuid=uu.bytes, sync=False, values=protoValues) result = self.stub.Insert(params) BTrDBError.checkProtoStat(result.stat) return result.versionMajor
def test_get_raises_indexerror(self): """ Assert the __get__ raises IndexError """ point = RawPoint(1, 1) with pytest.raises(IndexError): point[2] with pytest.raises(IndexError): point[-1]
def test_next_key_ready(self): """ Assert next_key_ready returns correct key """ buffer = PointBuffer(3) buffer.add_point(0, RawPoint(time=1000, value="zebra")) buffer.add_point(1, RawPoint(time=1000, value="leopard")) buffer.add_point(2, RawPoint(time=1000, value="giraffe")) buffer.add_point(0, RawPoint(time=2000, value="horse")) buffer.add_point(0, RawPoint(time=3000, value="pig")) assert buffer.next_key_ready() == 1000 buffer = PointBuffer(2) buffer.add_point(0, RawPoint(time=1000, value="horse")) buffer.add_point(0, RawPoint(time=2000, value="zebra")) buffer.add_point(0, RawPoint(time=3000, value="pig")) buffer.add_point(1, RawPoint(time=2000, value="leopard")) assert buffer.next_key_ready() == 1000
def test_from_proto_list(self): """ Assert from_proto_list creates valid objects """ protos = [ RawPointProto(time=1, value=10), RawPointProto(time=2, value=20) ] points = RawPoint.from_proto_list(protos) assert points[0].time == protos[0].time assert points[0].value == protos[0].value assert points[1].time == protos[1].time assert points[1].value == protos[1].value
def test_from_proto_list_returns_list_of_rawpoint(self): """ Assert from_proto_list returns list of RawPoint """ protos = [ RawPointProto(time=1, value=10), RawPointProto(time=2, value=20) ] points = RawPoint.from_proto_list(protos) assert len(points) == 2 for point in points: assert isinstance(point, RawPoint)
def insert(self, uu, values, policy): policy_map = { 'never': btrdb_pb2.MergePolicy.NEVER, 'equal': btrdb_pb2.MergePolicy.EQUAL, 'retain': btrdb_pb2.MergePolicy.RETAIN, 'replace': btrdb_pb2.MergePolicy.REPLACE, } protoValues = RawPoint.to_proto_list(values) params = btrdb_pb2.InsertParams(uuid=uu.bytes, sync=False, values=protoValues, merge_policy=policy_map[policy]) result = self.stub.Insert(params) BTrDBError.checkProtoStat(result.stat) return result.versionMajor
def insert(self, uu, values, policy): policy_map = { "never": btrdb_pb2.MergePolicy.NEVER, "equal": btrdb_pb2.MergePolicy.EQUAL, "retain": btrdb_pb2.MergePolicy.RETAIN, "replace": btrdb_pb2.MergePolicy.REPLACE, } protoValues = RawPoint.to_proto_list(values) params = btrdb_pb2.InsertParams( uuid=uu.bytes, sync=False, values=protoValues, merge_policy=policy_map[policy], ) result = self.stub.Insert(params) check_proto_stat(result.stat) return result.versionMajor
def values(self, start, end, version=0): """ Read raw values from BTrDB between time [a, b) in nanoseconds. RawValues queries BTrDB for the raw time series data points between `start` and `end` time, both in nanoseconds since the Epoch for the specified stream `version`. Parameters ---------- start : int or datetime like object The start time in nanoseconds for the range to be queried. (see :func:`btrdb.utils.timez.to_nanoseconds` for valid input types) end : int or datetime like object The end time in nanoseconds for the range to be queried. (see :func:`btrdb.utils.timez.to_nanoseconds` for valid input types) version: int The version of the stream to be queried Returns ------ list Returns a list of tuples containing a RawPoint and the stream version (list(tuple(RawPoint,int))). Notes ----- Note that the raw data points are the original values at the sensor's native sampling rate (assuming the time series represents measurements from a sensor). This is the lowest level of data with the finest time granularity. In the tree data structure of BTrDB, this data is stored in the vector nodes. """ materialized = [] start = to_nanoseconds(start) end = to_nanoseconds(end) point_windows = self._btrdb.ep.rawValues(self._uuid, start, end, version) for point_list, version in point_windows: for point in point_list: materialized.append((RawPoint.from_proto(point), version)) return materialized
def nearest(self, time, version, backward=False): """ Finds the closest point in the stream to a specified time. Return the point nearest to the specified `time` in nanoseconds since Epoch in the stream with `version` while specifying whether to search forward or backward in time. If `backward` is false, the returned point will be >= `time`. If backward is true, the returned point will be < `time`. The version of the stream used to satisfy the query is returned. Parameters ---------- time : int or datetime like object The time (in nanoseconds since Epoch) to search near (see :func:`btrdb.utils.timez.to_nanoseconds` for valid input types) version : int Version of the stream to use in search backward : boolean True to search backwards from time, else false for forward Returns ------- tuple The closest data point in the stream and the version of the stream the value was retrieved at (tuple(RawPoint, int)). """ try: rp, version = self._btrdb.ep.nearest(self._uuid, to_nanoseconds(time), version, backward) except BTrDBError as exc: if exc.code != 401: raise return None return RawPoint.from_proto(rp), version
def test_next_key_ready_with_inactive(self): """ Assert next_key_ready returns correct key with inactive stream """ buffer = PointBuffer(3) buffer.deactivate(1) buffer.add_point(0, RawPoint(time=1000, value="leopard")) buffer.add_point(2, RawPoint(time=1000, value="leopard")) buffer.add_point(0, RawPoint(time=2000, value="leopard")) assert buffer.next_key_ready() == 1000 # assert first key is 500 even though it was exhausted buffer = PointBuffer(3) buffer.add_point(1, RawPoint(time=500, value="leopard")) buffer.deactivate(1) buffer.add_point(0, RawPoint(time=1000, value="leopard")) buffer.add_point(2, RawPoint(time=1000, value="leopard")) assert buffer.next_key_ready() == 500
def streamset(): # list of mock streams streams = [] for idx in range(4): stream = Mock(Stream) type(stream).collection = PropertyMock(return_value="test") type(stream).name = PropertyMock(return_value="stream{}".format(idx)) streams.append(stream) rows = [ (None, RawPoint(1500000000000000000, 1.0), RawPoint(1500000000000000000, 1.0), RawPoint(1500000000000000000, 1.0)), (RawPoint(1500000000100000000, 2.0), None, RawPoint(1500000000100000000, 2.0), RawPoint(1500000000100000000, 2.0)), (None, RawPoint(1500000000200000000, 3.0), None, RawPoint(1500000000200000000, 3.0)), (RawPoint(1500000000300000000, 4.0), None, RawPoint(1500000000300000000, 4.0), RawPoint(1500000000300000000, 4.0)), (None, RawPoint(1500000000400000000, 5.0), RawPoint(1500000000400000000, 5.0), RawPoint(1500000000400000000, 5.0)), (RawPoint(1500000000500000000, 6.0), None, None, RawPoint(1500000000500000000, 6.0)), (None, RawPoint(1500000000600000000, 7.0), RawPoint(1500000000600000000, 7.0), RawPoint(1500000000600000000, 7.0)), (RawPoint(1500000000700000000, 8.0), None, RawPoint(1500000000700000000, 8.0), RawPoint(1500000000700000000, 8.0)), (None, RawPoint(1500000000800000000, 9.0), RawPoint(1500000000800000000, 9.0), RawPoint(1500000000800000000, 9.0)), (RawPoint(1500000000900000000, 10.0), None, RawPoint(1500000000900000000, 10.0), RawPoint(1500000000900000000, 10.0)), ] values = [[ RawPoint(1500000000100000000, 2.0), RawPoint(1500000000300000000, 4.0), RawPoint(1500000000500000000, 6.0), RawPoint(1500000000700000000, 8.0), RawPoint(1500000000900000000, 10.0) ], [ RawPoint(1500000000000000000, 1.0), RawPoint(1500000000200000000, 3.0), RawPoint(1500000000400000000, 5.0), RawPoint(1500000000600000000, 7.0), RawPoint(1500000000800000000, 9.0) ], [ RawPoint(1500000000000000000, 1.0), RawPoint(1500000000100000000, 2.0), RawPoint(1500000000300000000, 4.0), RawPoint(1500000000400000000, 5.0), RawPoint(1500000000600000000, 7.0), RawPoint(1500000000700000000, 8.0), RawPoint(1500000000800000000, 9.0), RawPoint(1500000000900000000, 10.0) ], [ RawPoint(1500000000000000000, 1.0), RawPoint(1500000000100000000, 2.0), RawPoint(1500000000200000000, 3.0), RawPoint(1500000000300000000, 4.0), RawPoint(1500000000400000000, 5.0), RawPoint(1500000000500000000, 6.0), RawPoint(1500000000600000000, 7.0), RawPoint(1500000000700000000, 8.0), RawPoint(1500000000800000000, 9.0), RawPoint(1500000000900000000, 10.0) ]] obj = StreamSet(streams) obj.rows = Mock(return_value=rows) obj.values = Mock(return_value=values) return obj
}, { 'time': 1500000000800000000, 'test/stream0': None, 'test/stream1': 9.0, 'test/stream2': 9.0, 'test/stream3': 9.0 }, { 'time': 1500000000900000000, 'test/stream0': 10.0, 'test/stream1': None, 'test/stream2': 10.0, 'test/stream3': 10.0 }], "to_array": [ np.array([ RawPoint(1500000000100000000, 2.0), RawPoint(1500000000300000000, 4.0), RawPoint(1500000000500000000, 6.0), RawPoint(1500000000700000000, 8.0), RawPoint(1500000000900000000, 10.0) ]), np.array([ RawPoint(1500000000000000000, 1.0), RawPoint(1500000000200000000, 3.0), RawPoint(1500000000400000000, 5.0), RawPoint(1500000000600000000, 7.0), RawPoint(1500000000800000000, 9.0) ]), np.array([ RawPoint(1500000000000000000, 1.0), RawPoint(1500000000100000000, 2.0),