18
18
import json
19
19
import logging
20
20
import re
21
+ from typing import List
21
22
import uuid as uuidlib
22
23
import warnings
23
24
from collections import deque
@@ -532,9 +533,7 @@ def arrow_insert(self, data: pa.Table, merge: str = "never") -> int:
532
533
version = []
533
534
for tab in table_slices :
534
535
version .append (
535
- self ._btrdb .ep .arrowInsertValues (
536
- uu = self .uuid , values = tab , policy = merge
537
- )
536
+ self ._btrdb .ep .arrowInsertValues (uu = self .uuid , values = tab , policy = merge )
538
537
)
539
538
return max (version )
540
539
@@ -768,10 +767,12 @@ def arrow_values(self, start, end, version: int = 0) -> pa.Table:
768
767
if len (tables ) > 0 :
769
768
return pa .concat_tables (tables )
770
769
else :
771
- schema = pa .schema ([
772
- pa .field ('time' , pa .timestamp ('ns' , tz = 'UTC' ), nullable = False ),
773
- pa .field ('value' , pa .float64 (), nullable = False ),
774
- ])
770
+ schema = pa .schema (
771
+ [
772
+ pa .field ("time" , pa .timestamp ("ns" , tz = "UTC" ), nullable = False ),
773
+ pa .field ("value" , pa .float64 (), nullable = False ),
774
+ ]
775
+ )
775
776
return pa .Table .from_arrays ([pa .array ([]), pa .array ([])], schema = schema )
776
777
777
778
def aligned_windows (self , start , end , pointwidth , version = 0 ):
@@ -879,20 +880,24 @@ def arrow_aligned_windows(
879
880
logger .debug (f"For stream - { self .uuid } - { self .name } " )
880
881
start = to_nanoseconds (start )
881
882
end = to_nanoseconds (end )
882
- tables = list (self ._btrdb .ep .arrowAlignedWindows (
883
- self .uuid , start = start , end = end , pointwidth = pointwidth , version = version
884
- ))
883
+ tables = list (
884
+ self ._btrdb .ep .arrowAlignedWindows (
885
+ self .uuid , start = start , end = end , pointwidth = pointwidth , version = version
886
+ )
887
+ )
885
888
if len (tables ) > 0 :
886
889
return pa .concat_tables (tables )
887
890
else :
888
- schema = pa .schema ([
889
- pa .field ('time' , pa .timestamp ('ns' , tz = 'UTC' ), nullable = False ),
890
- pa .field ('mean' , pa .float64 (), nullable = False ),
891
- pa .field ('min' , pa .float64 (), nullable = False ),
892
- pa .field ('max' , pa .float64 (), nullable = False ),
893
- pa .field ('count' , pa .uint64 (), nullable = False ),
894
- pa .field ('stddev' , pa .float64 (), nullable = False ),
895
- ])
891
+ schema = pa .schema (
892
+ [
893
+ pa .field ("time" , pa .timestamp ("ns" , tz = "UTC" ), nullable = False ),
894
+ pa .field ("mean" , pa .float64 (), nullable = False ),
895
+ pa .field ("min" , pa .float64 (), nullable = False ),
896
+ pa .field ("max" , pa .float64 (), nullable = False ),
897
+ pa .field ("count" , pa .uint64 (), nullable = False ),
898
+ pa .field ("stddev" , pa .float64 (), nullable = False ),
899
+ ]
900
+ )
896
901
return pa .Table .from_arrays ([pa .array ([]) for _ in range (5 )], schema = schema )
897
902
898
903
def windows (self , start , end , width , depth = 0 , version = 0 ):
@@ -986,25 +991,29 @@ def arrow_windows(
986
991
raise NotImplementedError (_arrow_not_impl_str .format ("arrow_windows" ))
987
992
start = to_nanoseconds (start )
988
993
end = to_nanoseconds (end )
989
- tables = list (self ._btrdb .ep .arrowWindows (
990
- self .uuid ,
991
- start = start ,
992
- end = end ,
993
- width = width ,
994
- depth = 0 ,
995
- version = version ,
996
- ))
994
+ tables = list (
995
+ self ._btrdb .ep .arrowWindows (
996
+ self .uuid ,
997
+ start = start ,
998
+ end = end ,
999
+ width = width ,
1000
+ depth = 0 ,
1001
+ version = version ,
1002
+ )
1003
+ )
997
1004
if len (tables ) > 0 :
998
1005
return pa .concat_tables (tables )
999
1006
else :
1000
- schema = pa .schema ([
1001
- pa .field ('time' , pa .timestamp ('ns' , tz = 'UTC' ), nullable = False ),
1002
- pa .field ('mean' , pa .float64 (), nullable = False ),
1003
- pa .field ('min' , pa .float64 (), nullable = False ),
1004
- pa .field ('max' , pa .float64 (), nullable = False ),
1005
- pa .field ('count' , pa .uint64 (), nullable = False ),
1006
- pa .field ('stddev' , pa .float64 (), nullable = False ),
1007
- ])
1007
+ schema = pa .schema (
1008
+ [
1009
+ pa .field ("time" , pa .timestamp ("ns" , tz = "UTC" ), nullable = False ),
1010
+ pa .field ("mean" , pa .float64 (), nullable = False ),
1011
+ pa .field ("min" , pa .float64 (), nullable = False ),
1012
+ pa .field ("max" , pa .float64 (), nullable = False ),
1013
+ pa .field ("count" , pa .uint64 (), nullable = False ),
1014
+ pa .field ("stddev" , pa .float64 (), nullable = False ),
1015
+ ]
1016
+ )
1008
1017
return pa .Table .from_arrays ([pa .array ([]) for _ in range (5 )], schema = schema )
1009
1018
1010
1019
def nearest (self , time , version , backward = False ):
@@ -1085,8 +1094,14 @@ class StreamSetBase(Sequence):
1085
1094
A lighweight wrapper around a list of stream objects
1086
1095
"""
1087
1096
1088
- def __init__ (self , streams ):
1089
- self ._streams = streams
1097
+ def __init__ (self , streams : List [Stream ]):
1098
+ self ._streams : List [Stream ] = []
1099
+ for stream in streams :
1100
+ if not isinstance (stream , Stream ):
1101
+ raise BTRDBTypeError (
1102
+ f"streams must be of type Stream { stream } , { type (stream )} "
1103
+ )
1104
+ self ._streams .append (stream )
1090
1105
if len (self ._streams ) < 1 :
1091
1106
raise ValueError (
1092
1107
f"Trying to create streamset with an empty list of streams { self ._streams } ."
@@ -1541,7 +1556,7 @@ def _streamset_data(self, as_iterators=False):
1541
1556
_ = params .pop ("sampling_frequency" , None )
1542
1557
versions = self ._pinned_versions
1543
1558
if versions == None :
1544
- versions = {s .uuid : 0 for s in self }
1559
+ versions = {s .uuid : 0 for s in self }
1545
1560
1546
1561
if self .pointwidth is not None :
1547
1562
# create list of stream.aligned_windows data
@@ -1734,12 +1749,12 @@ def values(self):
1734
1749
result .append ([point [0 ] for point in stream_data ])
1735
1750
return result
1736
1751
1737
- def arrow_values (self , name_callable = lambda s : s .collection + '/' + s .name ):
1752
+ def arrow_values (self , name_callable = lambda s : s .collection + "/" + s .name ):
1738
1753
"""Return a pyarrow table of stream values based on the streamset parameters."""
1739
1754
params = self ._params_from_filters ()
1740
1755
versions = self ._pinned_versions
1741
1756
if versions == None :
1742
- versions = {s .uuid : 0 for s in self }
1757
+ versions = {s .uuid : 0 for s in self }
1743
1758
1744
1759
if params .get ("sampling_frequency" , None ) is None :
1745
1760
_ = params .pop ("sampling_frequency" , None )
@@ -1797,13 +1812,20 @@ def arrow_values(self, name_callable=lambda s : s.collection + '/' + s.name):
1797
1812
table = list (self ._btrdb .ep .arrowMultiValues (** params ))
1798
1813
if len (table ) > 0 :
1799
1814
data = pa .concat_tables (table )
1800
- data = data .rename_columns (["time" ] + [name_callable (s ) for s in self ._streams ])
1815
+ data = data .rename_columns (
1816
+ ["time" ] + [name_callable (s ) for s in self ._streams ]
1817
+ )
1801
1818
else :
1802
1819
schema = pa .schema (
1803
- [pa .field ('time' , pa .timestamp ('ns' , tz = 'UTC' ), nullable = False )]
1804
- + [pa .field (name_callable (s ), pa .float64 (), nullable = False ) for s in self ._streams ],
1820
+ [pa .field ("time" , pa .timestamp ("ns" , tz = "UTC" ), nullable = False )]
1821
+ + [
1822
+ pa .field (name_callable (s ), pa .float64 (), nullable = False )
1823
+ for s in self ._streams
1824
+ ],
1825
+ )
1826
+ data = pa .Table .from_arrays (
1827
+ [pa .array ([]) for i in range (1 + len (self ._streams ))], schema = schema
1805
1828
)
1806
- data = pa .Table .from_arrays ([pa .array ([]) for i in range (1 + len (self ._streams ))], schema = schema )
1807
1829
return data
1808
1830
1809
1831
def __repr__ (self ):
@@ -1828,6 +1850,15 @@ def __getitem__(self, item):
1828
1850
1829
1851
return self ._streams [item ]
1830
1852
1853
+ def __contains__ (self , item ):
1854
+ if isinstance (item , str ):
1855
+ for stream in self ._streams :
1856
+ if str (stream .uuid ()) == item :
1857
+ return True
1858
+ return False
1859
+
1860
+ return item in self ._streams
1861
+
1831
1862
def __len__ (self ):
1832
1863
return len (self ._streams )
1833
1864
@@ -1865,12 +1896,14 @@ def __init__(
1865
1896
if self .start is not None and self .end is not None and self .start >= self .end :
1866
1897
raise BTRDBValueError ("`start` must be strictly less than `end` argument" )
1867
1898
1899
+
1868
1900
def _to_period_ns (fs : int ):
1869
1901
"""Convert sampling rate to sampling period in ns."""
1870
1902
period = 1 / fs
1871
1903
period_ns = period * 1e9
1872
1904
return int (period_ns )
1873
1905
1906
+
1874
1907
def _coalesce_table_deque (tables : deque ):
1875
1908
main_table = tables .popleft ()
1876
1909
idx = 0
0 commit comments