Skip to content

Commit bcc8593

Browse files
committed
RDBC-766 Python client - time series 3 - stage #1 + test for roll-ups
1 parent 4d7afca commit bcc8593

File tree

5 files changed

+130
-13
lines changed

5 files changed

+130
-13
lines changed

ravendb/documents/operations/time_series.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ def to_json(self) -> Dict[str, Any]:
330330

331331
def append(self, append_operation: AppendOperation) -> None:
332332
if self._appends is None:
333-
self._appends = [] # todo: perf
333+
self._appends = [] # todo: perf
334334
filtered = self._appends
335335

336336
# if len(filtered) != 0:

ravendb/documents/session/document_session.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2314,7 +2314,7 @@ def get(
23142314
to_date: Optional[datetime] = None,
23152315
start: int = 0,
23162316
page_size: int = int_max,
2317-
):
2317+
) -> List[TypedTimeSeriesRollupEntry[_T_TS_Values_Bindable]]:
23182318
if self._not_in_cache(from_date, to_date):
23192319
results = self.get_time_series_and_includes(from_date, to_date, None, start, page_size)
23202320
return [TypedTimeSeriesRollupEntry.from_entry(self._object_type, x) for x in results]

ravendb/documents/session/time_series.py

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,37 +68,44 @@ def _create_instance(self) -> _T_Values:
6868
except Exception as e:
6969
raise RavenException(f"Unable to create instance of class: {self._object_type.__name__}", e)
7070

71-
def get_max(self) -> _T_Values:
71+
@property
72+
def max(self) -> _T_Values:
7273
if self._max is None:
7374
self._max = self._create_instance()
7475
return self._max
7576

76-
def get_min(self) -> _T_Values:
77+
@property
78+
def min(self) -> _T_Values:
7779
if self._min is None:
7880
self._min = self._create_instance()
7981
return self._min
8082

81-
def get_count(self) -> _T_Values:
83+
@property
84+
def count(self) -> _T_Values:
8285
if self._count is None:
8386
self._count = self._create_instance()
8487
return self._count
8588

86-
def get_first(self) -> _T_Values:
89+
@property
90+
def first(self) -> _T_Values:
8791
if self._first is None:
8892
self._first = self._create_instance()
8993
return self._first
9094

91-
def get_last(self) -> _T_Values:
95+
@property
96+
def last(self) -> _T_Values:
9297
if self._last is None:
9398
self._last = self._create_instance()
9499
return self._last
95100

96-
def get_sum(self) -> _T_Values:
101+
@property
102+
def sum(self) -> _T_Values:
97103
if self._sum is None:
98104
self._sum = self._create_instance()
99105
return self._sum
100106

101-
def get_average(self) -> _T_Values:
107+
@property
108+
def average(self) -> _T_Values:
102109
if self._average is not None:
103110
return self._average
104111

@@ -139,7 +146,9 @@ def _assign_rollup(self, target: List[float], source: _T_Values, offset: int) ->
139146
target[i * 6 + offset] = values[i]
140147

141148
@classmethod
142-
def from_entry(cls, ts_bindable_object_type: Type[_T_TSBindable], entry: TimeSeriesEntry):
149+
def from_entry(
150+
cls, ts_bindable_object_type: Type[_T_TSBindable], entry: TimeSeriesEntry
151+
) -> TypedTimeSeriesRollupEntry[_T_TSBindable]:
143152
result = TypedTimeSeriesRollupEntry(ts_bindable_object_type, entry.timestamp)
144153
result.rollup = True
145154
result.tag = entry.tag
@@ -153,6 +162,8 @@ def from_entry(cls, ts_bindable_object_type: Type[_T_TSBindable], entry: TimeSer
153162
result._sum = TimeSeriesValuesHelper.set_fields(ts_bindable_object_type, cls.extract_values(values, 4))
154163
result._count = TimeSeriesValuesHelper.set_fields(ts_bindable_object_type, cls.extract_values(values, 5))
155164

165+
return result
166+
156167
@staticmethod
157168
def extract_values(input: List[float], offset: int) -> List[float]:
158169
length = math.ceil((len(input) - offset) / 6.0)

ravendb/tests/jvm_migrated_tests/client_tests/time_series_tests/test_time_series_typed_session.py

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@
77
TimeSeriesCollectionConfiguration,
88
TimeSeriesConfiguration,
99
ConfigureTimeSeriesOperation,
10+
RawTimeSeriesPolicy,
1011
)
1112
from ravendb.documents.session.time_series import ITimeSeriesValuesBindable, TypedTimeSeriesEntry
1213
from ravendb.infrastructure.entities import User
1314
from ravendb.primitives.time_series import TimeValue
1415
from ravendb.tests.test_base import TestBase
15-
16+
from ravendb.tools.raven_test_helper import RavenTestHelper
1617

1718
document_id = "users/gracjan"
1819
company_id = "companies/1-A"
@@ -261,3 +262,54 @@ def test_can_execute_simple_rollup(self):
261262

262263
ts3 = session.time_series_for(document_id, p3.get_time_series_name(ts_name1)).get()
263264
self.assertEqual(len(ts1) // 4, len(ts3))
265+
266+
def test_can_work_with_rollup_time_series_2(self):
267+
raw_hours = 24
268+
raw = RawTimeSeriesPolicy(TimeValue.of_hours(raw_hours))
269+
270+
p1 = TimeSeriesPolicy("By6Hours", TimeValue.of_hours(6), TimeValue.of_hours(raw_hours * 4))
271+
p2 = TimeSeriesPolicy("By1Day", TimeValue.of_days(1), TimeValue.of_hours(raw_hours * 5))
272+
p3 = TimeSeriesPolicy("By30Minutes", TimeValue.of_minutes(30), TimeValue.of_hours(raw_hours * 2))
273+
p4 = TimeSeriesPolicy("By1Hour", TimeValue.of_hours(1), TimeValue.of_hours(raw_hours * 3))
274+
275+
time_series_collection_configuration = TimeSeriesCollectionConfiguration()
276+
time_series_collection_configuration.raw_policy = raw
277+
time_series_collection_configuration.policies = [p1, p2, p3, p4]
278+
279+
config = TimeSeriesConfiguration()
280+
config.collections = {"users": time_series_collection_configuration}
281+
config.policy_check_frequency = timedelta(seconds=1)
282+
283+
self.store.maintenance.send(ConfigureTimeSeriesOperation(config))
284+
self.store.time_series.register_type(User, StockPrice)
285+
286+
total = TimeValue.of_days(12).value // 60
287+
base_line = RavenTestHelper.utc_today() - timedelta(days=12)
288+
289+
with self.store.open_session() as session:
290+
session.store(User(name="Karmel"), "users/karmel")
291+
292+
ts = session.typed_time_series_for(StockPrice, "users/karmel")
293+
for i in range(total + 1):
294+
open = i
295+
close = i + 100_000
296+
high = i + 200_000
297+
low = i + 300_000
298+
volume = i + 400_000
299+
ts.append(
300+
base_line + timedelta(minutes=i), StockPrice(open, close, high, low, volume), "watches/fitbit"
301+
)
302+
303+
session.save_changes()
304+
305+
time.sleep(1.5) # wait for rollups
306+
307+
with self.store.open_session() as session:
308+
ts1 = session.time_series_rollup_for(StockPrice, "users/karmel", p1.name)
309+
r = ts1.get()[0]
310+
self.assertIsNotNone(r.first)
311+
self.assertIsNotNone(r.last)
312+
self.assertIsNotNone(r.min)
313+
self.assertIsNotNone(r.max)
314+
self.assertIsNotNone(r.count)
315+
self.assertIsNotNone(r.average)

ravendb/tests/jvm_migrated_tests/issues_tests/test_ravenDB_16060.py

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -436,13 +436,15 @@ def test_can_serve_time_series_from_cache_rollup(self):
436436
session.store(User(name="Karmel"), "users/karmel")
437437

438438
ts = session.typed_time_series_for(StockPrice, "users/karmel")
439-
for i in range(total+1):
439+
for i in range(total + 1):
440440
open = i
441441
close = i + 100_000
442442
high = i + 200_000
443443
low = i + 300_000
444444
volume = i + 400_000
445-
ts.append(base_line + timedelta(minutes=i), StockPrice(open, close, high, low, volume), "watches/fitbit")
445+
ts.append(
446+
base_line + timedelta(minutes=i), StockPrice(open, close, high, low, volume), "watches/fitbit"
447+
)
446448

447449
session.save_changes()
448450

@@ -458,3 +460,55 @@ def test_can_serve_time_series_from_cache_rollup(self):
458460
res = ts.get(base_line, base_line + timedelta(days=365))
459461
self.assertEqual(16, len(res))
460462
self.assertEqual(1, session.advanced.number_of_requests)
463+
464+
def test_can_include_typed_time_series_rollup(self):
465+
raw = RawTimeSeriesPolicy(TimeValue.of_hours(24))
466+
467+
p1 = TimeSeriesPolicy("By6Hours", TimeValue.of_hours(6), TimeValue.of_days(4))
468+
p2 = TimeSeriesPolicy("By1Day", TimeValue.of_days(1), TimeValue.of_days(5))
469+
p3 = TimeSeriesPolicy("By30Minutes", TimeValue.of_minutes(30), TimeValue.of_days(2))
470+
p4 = TimeSeriesPolicy("By1Hour", TimeValue.of_hours(1), TimeValue.of_days(3))
471+
472+
time_series_collection_configuration = TimeSeriesCollectionConfiguration()
473+
time_series_collection_configuration.raw_policy = raw
474+
time_series_collection_configuration.policies = [p1, p2, p3, p4]
475+
476+
config = TimeSeriesConfiguration()
477+
config.collections = {"users": time_series_collection_configuration}
478+
config.policy_check_frequency = timedelta(seconds=1)
479+
480+
self.store.maintenance.send(ConfigureTimeSeriesOperation(config))
481+
self.store.time_series.register_type(User, StockPrice)
482+
483+
total = TimeValue.of_days(12).value
484+
base_line = RavenTestHelper.utc_today() - timedelta(days=12)
485+
486+
with self.store.open_session() as session:
487+
session.store(User(name="Karmel"), "users/karmel")
488+
489+
ts = session.typed_time_series_for(StockPrice, "users/karmel")
490+
for i in range(total + 1):
491+
open = i
492+
close = i + 100_000
493+
high = i + 200_000
494+
low = i + 300_000
495+
volume = i + 400_000
496+
ts.append(
497+
base_line + timedelta(minutes=i), StockPrice(open, close, high, low, volume), "watches/fitbit"
498+
)
499+
500+
session.save_changes()
501+
502+
time.sleep(1.2)
503+
504+
with self.store.open_session() as session:
505+
user = (
506+
session.query(object_type=User)
507+
.include(lambda i: i.include_time_series(f"stockPrices@{p1.name}"))
508+
.first()
509+
)
510+
511+
# should not go to server
512+
res = session.time_series_rollup_for(StockPrice, user.Id, p1.name).get()
513+
self.assertEqual(16, len(res))
514+
self.assertEqual(1, session.advanced.number_of_requests)

0 commit comments

Comments
 (0)