@@ -94,14 +94,14 @@ def process_window(
94
94
95
95
for (window_start , window_end ), aggregated_value , _ in windows :
96
96
# Calculate the time gap between the new event and the session's last activity
97
- # window_end is stored as last_event_timestamp + 1, so subtract 1 to get actual last event time
98
- session_last_activity = window_end - 1
97
+ # window_end now directly represents the timestamp of the last event
98
+ session_last_activity = window_end
99
99
time_gap = timestamp_ms - session_last_activity
100
100
101
101
# Check if this session can be extended
102
102
if time_gap <= timeout_ms + grace_ms and timestamp_ms >= window_start :
103
103
session_start = window_start
104
- session_end = timestamp_ms + 1
104
+ session_end = timestamp_ms
105
105
can_extend_session = True
106
106
existing_aggregated = aggregated_value
107
107
old_window_to_delete = (window_start , window_end )
@@ -110,7 +110,7 @@ def process_window(
110
110
# If no extendable session found, start a new one
111
111
if not can_extend_session :
112
112
session_start = timestamp_ms
113
- session_end = timestamp_ms + 1
113
+ session_end = timestamp_ms # End time is the timestamp of the last event
114
114
115
115
# Process the event for this session
116
116
updated_windows : list [WindowKeyResult ] = []
@@ -138,7 +138,7 @@ def process_window(
138
138
updated_windows .append (
139
139
(
140
140
key ,
141
- self ._results (aggregated , [], session_start , session_end - 1 ),
141
+ self ._results (aggregated , [], session_start , session_end ),
142
142
)
143
143
)
144
144
@@ -221,18 +221,18 @@ def expire_by_key(
221
221
windows_to_delete = []
222
222
for (window_start , window_end ), aggregated , _ in all_windows :
223
223
# Session expires when the session end time + timeout has passed the expiry threshold
224
- # window_end is stored as last_event_timestamp + 1, so we subtract 1 and add timeout_ms
225
- last_event_timestamp = window_end - 1
226
- if last_event_timestamp + self ._timeout_ms <= expiry_threshold :
224
+ # window_end directly represents the timestamp of the last event
225
+ if window_end + self ._timeout_ms <= expiry_threshold :
227
226
collected = []
228
227
if collect :
229
- collected = state .get_from_collection (window_start , window_end )
228
+ # window_end is now the timestamp of the last event, so we need +1 to include it
229
+ collected = state .get_from_collection (window_start , window_end + 1 )
230
230
231
231
windows_to_delete .append ((window_start , window_end ))
232
232
count += 1
233
233
yield (
234
234
key ,
235
- self ._results (aggregated , collected , window_start , window_end - 1 ),
235
+ self ._results (aggregated , collected , window_start , window_end ),
236
236
)
237
237
238
238
# Clean up expired windows
0 commit comments