Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 47 additions & 2 deletions btrdb/utils/general.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@
"""
from datetime import timedelta

from btrdb.utils.timez import to_nanoseconds


##########################################################################
## Functions
##########################################################################


def unpack_stream_descriptor(desc):
"""
Returns dicts for tags and annotations found in supplied stream
Expand Down Expand Up @@ -75,6 +76,50 @@ def from_nanoseconds(cls, nsec):
break
return cls(pos)

def for_aligned_windows(self, start, end):
"""
Returns the aligned_windows's first and last timestamps, as well as the number of windows,
based on a given pointwidth set.

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)


Returns
-------
aligned_start: int
First timestamp would be returned by `aligned_windows` that is inclusive of specified start
timestamp.
aligned_end: int
Last timestamp would be returned by `aligned_windows` that is inclusive of specified end
timestamp.
n_windows: int
The number of windows would be returned by `aligned_windows`.

Examples
--------
Querying `aligned_windows` of pointwidth of 30, spaning 1 day (~24 hours).

>>> start, end = "2016-03-01", "2016-03-02"
>>> pointwidth(30).for_aligned_windows(start, end)
(1456790399996657664, 1456876798632525824, 80466)
Comment thread
justinGilmer marked this conversation as resolved.
# output timestamp's `strftime` is:
# ['2016-02-29 23:59:59.996657664', '2016-03-01 23:59:58.632525824']

"""
start, end = to_nanoseconds(start), to_nanoseconds(end)
aligned_start = start - (start % self.nanoseconds)
n_windows = (end - aligned_start) // self.nanoseconds
aligned_end = aligned_start + (self.nanoseconds * (n_windows - 1))
return aligned_start, aligned_end, n_windows

def __init__(self, p):
self._pointwidth = int(p)

Expand Down
30 changes: 30 additions & 0 deletions tests/btrdb/utils/test_general.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,36 @@ def test_from_nanoseconds(self, nsec, expected):
"""
assert pointwidth.from_nanoseconds(nsec) == expected

@pytest.mark.parametrize(
"pw_time_range, expected",
[
(
(52, 1560127027000000000, 1702702800000000000),
(1558245471070191616, 1697857059518676992, 32),
),
(
(48, 1560127027000000000, 1702702800000000000),
(1559934320930455552, 1702360659146047488, 507),
),
(
(44, 1456876800000000000, 1464652800000000000),
(1456861702896222208, 1464619856941809664, 442),
),
(
(38, 1456876800000000000, 1464652800000000000),
(1456876546303197184, 1464652292534829056, 28289),
),
(
(32, 1456876800000000000, 1464652800000000000),
(1456876799706267648, 1464652795046002688, 1810491),
),
],
)
def test_for_aligned_windows(self, pw_time_range, expected):
pw, start, end = pw_time_range
result = pointwidth(pw).for_aligned_windows(start, end)
assert result == expected

def test_time_conversions(self):
"""
Test standard pointwidth time conversions
Expand Down