Skip to content

Commit f917832

Browse files
committed
Revise code according to review.
1 parent 2a8a57a commit f917832

File tree

1 file changed

+15
-11
lines changed
  • sdks/python/apache_beam/examples/cookbook/ordered_window_elements

1 file changed

+15
-11
lines changed

sdks/python/apache_beam/examples/cookbook/ordered_window_elements/streaming.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
from apache_beam.typehints.typehints import TupleConstraint
3636
from apache_beam.utils.timestamp import MAX_TIMESTAMP
3737
from apache_beam.utils.timestamp import MIN_TIMESTAMP
38+
from apache_beam.utils.timestamp import Duration
3839
from apache_beam.utils.timestamp import DurationTypes # pylint: disable=unused-import
3940
from apache_beam.utils.timestamp import Timestamp
4041
from apache_beam.utils.timestamp import TimestampTypes # pylint: disable=unused-import
@@ -89,7 +90,7 @@ def __init__(
8990
self,
9091
duration: DurationTypes,
9192
slide_interval: DurationTypes,
92-
offset: TimestampTypes,
93+
offset: DurationTypes,
9394
allowed_lateness: DurationTypes,
9495
default_start_value,
9596
fill_start_if_missing: bool,
@@ -200,20 +201,23 @@ def process(
200201

201202
timer_started = timer_state.read()
202203
if not timer_started:
203-
timestamp_secs = timestamp.micros / 1e6
204+
offset_duration = Duration.of(self.offset)
205+
slide_duration = Duration.of(self.slide_interval)
206+
duration_duration = Duration.of(self.duration)
204207

205208
# Align the timestamp with the windowing scheme.
206-
aligned_timestamp = timestamp_secs - self.offset
209+
aligned_micros = (timestamp - offset_duration).micros
207210

208-
# Calculate the start of the last window that could contain this timestamp.
209-
last_window_start_aligned = ((aligned_timestamp // self.slide_interval) *
210-
self.slide_interval)
211-
last_window_start = last_window_start_aligned + self.offset
211+
# Calculate the start of the last window that could contain this timestamp
212+
last_window_start_aligned_micros = (
213+
(aligned_micros // slide_duration.micros) * slide_duration.micros)
212214

213-
n = (self.duration - 1) // self.slide_interval
215+
last_window_start = Timestamp(
216+
micros=last_window_start_aligned_micros) + offset_duration
217+
n = (duration_duration.micros - 1) // slide_duration.micros
214218
# Calculate the start of the first sliding window.
215-
first_slide_start = last_window_start - n * self.slide_interval
216-
first_slide_start_ts = Timestamp.of(first_slide_start)
219+
first_slide_start_ts = last_window_start - Duration(
220+
micros=n * slide_duration.micros)
217221

218222
# Set the initial timer to fire at the end of the first window plus
219223
# allowed lateness.
@@ -541,7 +545,7 @@ def __init__(
541545
self,
542546
duration: DurationTypes,
543547
slide_interval: Optional[DurationTypes] = None,
544-
offset: TimestampTypes = 0,
548+
offset: DurationTypes = 0,
545549
allowed_lateness: DurationTypes = 0,
546550
default_start_value=None,
547551
fill_start_if_missing: bool = False,

0 commit comments

Comments
 (0)