Ejemplo n.º 1
0
 def test_from_proto_list(self):
     """
     Assert from_proto_list creates valid objects
     """
     protos = [
         StatPointProto(time=1, min=2, mean=3, max=4, count=5, stddev=6),
         StatPointProto(time=11,
                        min=12,
                        mean=13,
                        max=14,
                        count=15,
                        stddev=16)
     ]
     points = StatPoint.from_proto_list(protos)
     assert points[0].time == protos[0].time
     assert points[0].min == protos[0].min
     assert points[0].mean == protos[0].mean
     assert points[0].max == protos[0].max
     assert points[0].count == protos[0].count
     assert points[0].stddev == protos[0].stddev
     assert points[1].time == protos[1].time
     assert points[1].min == protos[1].min
     assert points[1].mean == protos[1].mean
     assert points[1].max == protos[1].max
     assert points[1].count == protos[1].count
     assert points[1].stddev == protos[1].stddev
Ejemplo n.º 2
0
    def windows(self, start, end, width, depth=0, version=0):
        """
        Read arbitrarily-sized windows of data from BTrDB.  StatPoint objects
        will be returned representing the data for each window.

        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)
        width : int
            The number of nanoseconds in each window, subject to the depth
            parameter.
        depth : int
            The precision of the window duration as a power of 2 in nanoseconds.
            E.g 30 would make the window duration accurate to roughly 1 second
        version : int
            The version of the stream to query.

        Returns
        -------
        tuple
            Returns a tuple containing windows of data.  Each window is a tuple
            containing data tuples.  Each data tuple contains a StatPoint and
            the stream version (tuple(tuple(StatPoint, int), ...)).

        Notes
        -----
        Windows returns arbitrary precision windows from BTrDB. It is slower
        than AlignedWindows, but still significantly faster than RawValues. Each
        returned window will be `width` nanoseconds long. `start` is inclusive,
        but `end` is exclusive (e.g if end < start+width you will get no
        results). That is, results will be returned for all windows that start
        at a time less than the end timestamp. If (`end` - `start`) is not a
        multiple of width, then end will be decreased to the greatest value less
        than end such that (end - start) is a multiple of `width` (i.e., we set
        end = start + width * floordiv(end - start, width). The `depth`
        parameter is an optimization that can be used to speed up queries on
        fast queries. Each window will be accurate to 2^depth nanoseconds. If
        depth is zero, the results are accurate to the nanosecond. On a dense
        stream for large windows, this accuracy may not be required. For example
        for a window of a day, +- one second may be appropriate, so a depth of
        30 can be specified. This is much faster to execute on the database
        side.

        """
        materialized = []
        start = to_nanoseconds(start)
        end = to_nanoseconds(end)

        windows = self._btrdb.ep.windows(self._uuid, start, end, width, depth,
                                         version)
        for stat_points, version in windows:
            for point in stat_points:
                materialized.append((StatPoint.from_proto(point), version))

        return tuple(materialized)
Ejemplo n.º 3
0
 def test_from_proto_creates_rawpoint(self):
     """
     Assert from_proto creates a new instance of StatPoint
     """
     proto = StatPointProto(time=1, min=2, mean=3, max=4, count=5, stddev=6)
     point = StatPoint.from_proto(proto)
     assert isinstance(point, StatPoint)
Ejemplo n.º 4
0
 def test_get_raises_indexerror(self):
     """
     Assert the __get__ raises IndexError
     """
     point = StatPoint(1, 2, 3, 4, 5, 6)
     with pytest.raises(IndexError):
         point[6]
     with pytest.raises(IndexError):
         point[-1]
Ejemplo n.º 5
0
    def aligned_windows(self, start, end, pointwidth, version=0):
        """
        Read statistical aggregates of windows of data from BTrDB.

        Query BTrDB for aggregates (or roll ups or windows) of the time series
        with `version` between time `start` (inclusive) and `end` (exclusive) in
        nanoseconds. Each point returned is a statistical aggregate of all the
        raw data within a window of width 2**`pointwidth` nanoseconds. These
        statistical aggregates currently include the mean, minimum, and maximum
        of the data and the count of data points composing the window.

        Note that `start` is inclusive, but `end` is exclusive. That is, results
        will be returned for all windows that start in the interval [start, end).
        If end < start+2^pointwidth you will not get any results. If start and
        end are not powers of two, the bottom pointwidth bits will be cleared.
        Each window will contain statistical summaries of the window.
        Statistical points with count == 0 will be omitted.

        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)
        pointwidth : int
            Specify the number of ns between data points (2**pointwidth)
        version : int
            Version of the stream to query

        Returns
        -------
        tuple
            Returns a tuple containing windows of data.  Each window is a tuple
            containing data tuples.  Each data tuple contains a StatPoint and
            the stream version.

        Notes
        -----
        As the window-width is a power-of-two, it aligns with BTrDB internal
        tree data structure and is faster to execute than `windows()`.
        """
        materialized = []
        start = to_nanoseconds(start)
        end = to_nanoseconds(end)

        windows = self._btrdb.ep.alignedWindows(self._uuid, start, end,
                                                pointwidth, version)
        for stat_points, version in windows:
            for point in stat_points:
                materialized.append((StatPoint.from_proto(point), version))

        return tuple(materialized)
Ejemplo n.º 6
0
 def test_get_returns_correct_values(self):
     """
     Assert the __get__ returns correct values
     """
     point = StatPoint(1, 2, 3, 4, 5, 6)
     assert point[0] == 1
     assert point[1] == 2
     assert point[2] == 3
     assert point[3] == 4
     assert point[4] == 5
     assert point[5] == 6
Ejemplo n.º 7
0
 def test_from_proto(self):
     """
     Assert from_proto creates a correct object
     """
     proto = StatPointProto(time=1, min=2, mean=3, max=4, count=5, stddev=6)
     point = StatPoint.from_proto(proto)
     assert point.time == proto.time
     assert point.min == proto.min
     assert point.mean == proto.mean
     assert point.max == proto.max
     assert point.count == proto.count
     assert point.stddev == proto.stddev
Ejemplo n.º 8
0
    def windows(self, start, end, width, depth=0, version=0):
        """
        Read arbitrarily-sized windows of data from BTrDB.  StatPoint objects
        will be returned representing the data for each window.

        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)
        width : int
            The number of nanoseconds in each window.
        version : int
            The version of the stream to query.

        Returns
        -------
        tuple
            Returns a tuple containing windows of data.  Each window is a tuple
            containing data tuples.  Each data tuple contains a StatPoint and
            the stream version (tuple(tuple(StatPoint, int), ...)).

        Notes
        -----
        Windows returns arbitrary precision windows from BTrDB. It is slower
        than AlignedWindows, but still significantly faster than RawValues. Each
        returned window will be `width` nanoseconds long. `start` is inclusive,
        but `end` is exclusive (e.g if end < start+width you will get no
        results). That is, results will be returned for all windows that start
        at a time less than the end timestamp. If (`end` - `start`) is not a
        multiple of width, then end will be decreased to the greatest value less
        than end such that (end - start) is a multiple of `width` (i.e., we set
        end = start + width * floordiv(end - start, width). The `depth`
        parameter previously available has been deprecated. The only valid value
        for depth is now 0.

        """
        materialized = []
        start = to_nanoseconds(start)
        end = to_nanoseconds(end)

        windows = self._btrdb.ep.windows(self._uuid, start, end, width, depth,
                                         version)
        for stat_points, version in windows:
            for point in stat_points:
                materialized.append((StatPoint.from_proto(point), version))

        return tuple(materialized)
Ejemplo n.º 9
0
    def test_from_proto_list_returns_list_of_statpoint(self):
        """
        Assert from_proto_list returns list of StatPoint
        """
        protos = [
            StatPointProto(time=1, min=2, mean=3, max=4, count=5, stddev=6),
            StatPointProto(time=11,
                           min=12,
                           mean=13,
                           max=14,
                           count=15,
                           stddev=16)
        ]
        points = StatPoint.from_proto_list(protos)

        assert len(points) == 2
        for point in points:
            assert isinstance(point, StatPoint)
Ejemplo n.º 10
0
def statpoint_streamset():
    rows = [
        [
            StatPoint(1500000000100000000, 0, 2.0, 2.5, 10, 1),
            StatPoint(1500000000100000000, 0, 4.0, 4.5, 11, 1),
            StatPoint(1500000000100000000, 0, 6.0, 6.5, 10, 1),
            StatPoint(1500000000100000000, 0, 8.0, 8.5, 11, 2)
        ],
        [
            StatPoint(1500000000300000000, 0, 3.0, 3.5, 11, 1),
            StatPoint(1500000000300000000, 0, 5.0, 5.5, 10, 1),
            StatPoint(1500000000300000000, 0, 7.0, 7.5, 10, 1),
            StatPoint(1500000000300000000, 0, 9.0, 9.5, 11, 2)
        ],
        [
            StatPoint(1500000000500000000, 0, 4.0, 4.5, 10, 1),
            StatPoint(1500000000500000000, 0, 6.0, 6.5, 11, 1),
            StatPoint(1500000000500000000, 0, 8.0, 8.5, 10, 1),
            StatPoint(1500000000500000000, 0, 10.0, 10.5, 11, 2)
        ],
        [
            StatPoint(1500000000700000000, 0, 5.0, 5.5, 11, 1),
            StatPoint(1500000000700000000, 0, 7.0, 7.5, 10, 1),
            StatPoint(1500000000700000000, 0, 9.0, 9.5, 10, 1),
            StatPoint(1500000000700000000, 0, 11.0, 11.5, 11, 2)
        ],
    ]
    values = [
        [
            StatPoint(1500000000100000000, 0, 2.0, 2.5, 10, 1),
            StatPoint(1500000000300000000, 0, 4.0, 4.5, 11, 1),
            StatPoint(1500000000500000000, 0, 6.0, 6.5, 10, 1),
            StatPoint(1500000000700000000, 0, 8.0, 8.5, 11, 2)
        ],
        [
            StatPoint(1500000000100000000, 1, 3.0, 3.5, 11, 1),
            StatPoint(1500000000300000000, 1, 5.0, 5.5, 10, 1),
            StatPoint(1500000000500000000, 1, 7.0, 7.5, 10, 1),
            StatPoint(1500000000700000000, 1, 9.0, 9.5, 11, 2)
        ],
        [
            StatPoint(1500000000100000000, 2, 4.0, 4.5, 10, 1),
            StatPoint(1500000000300000000, 2, 6.0, 6.5, 11, 1),
            StatPoint(1500000000500000000, 2, 8.0, 8.5, 10, 1),
            StatPoint(1500000000700000000, 2, 10.0, 10.5, 11, 2)
        ],
        [
            StatPoint(1500000000100000000, 3, 5.0, 5.5, 11, 1),
            StatPoint(1500000000300000000, 3, 7.0, 7.5, 10, 1),
            StatPoint(1500000000500000000, 3, 9.0, 9.5, 10, 1),
            StatPoint(1500000000700000000, 3, 11.0, 11.5, 11, 2)
        ],
    ]
    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)

    obj = StreamSet(streams)
    obj.rows = Mock(return_value=rows)
    obj.values = Mock(return_value=values)
    obj.pointwidth = 20
    return obj
Ejemplo n.º 11
0
 def test_create(self):
     """
     Ensure we can create the object
     """
     StatPoint(1, 1, 1, 1, 1, 1)