@@ -66,73 +66,56 @@ def process_window(
66
66
latest_timestamp = max (timestamp_ms , state_ts )
67
67
68
68
# Calculate session expiry threshold
69
- session_expiry_threshold = latest_timestamp - grace_ms
69
+ expiry_threshold = latest_timestamp - grace_ms
70
70
71
71
# Check if the event is too late
72
- if timestamp_ms < session_expiry_threshold :
73
- late_by_ms = session_expiry_threshold - timestamp_ms
72
+ if timestamp_ms < expiry_threshold :
74
73
self ._on_expired_window (
75
74
value = value ,
76
75
key = key ,
77
76
start = timestamp_ms ,
78
- end = timestamp_ms , # End time is the timestamp of the last event
77
+ end = timestamp_ms ,
79
78
timestamp_ms = timestamp_ms ,
80
- late_by_ms = late_by_ms ,
79
+ late_by_ms = expiry_threshold - timestamp_ms ,
81
80
)
82
81
return [], []
83
82
84
- # Look for an existing session that can be extended
85
- can_extend_session = False
86
- existing_aggregated = None
87
- old_window_to_delete = None
88
-
89
83
# Search for active sessions that can accommodate the new event
90
- search_start = max (0 , timestamp_ms - timeout_ms * 2 )
91
- windows = state .get_windows (
92
- search_start , timestamp_ms + timeout_ms + 1 , backwards = True
93
- )
94
-
95
- for (window_start , window_end ), aggregated_value , _ in windows :
84
+ for (window_start , window_end ), aggregated_value , _ in state .get_windows (
85
+ start_from_ms = max (0 , timestamp_ms - timeout_ms * 2 ),
86
+ start_to_ms = timestamp_ms + timeout_ms + 1 ,
87
+ backwards = True ,
88
+ ):
96
89
# Calculate the time gap between the new event and the session's last activity
97
90
# window_end now directly represents the timestamp of the last event
98
- session_last_activity = window_end
99
- time_gap = timestamp_ms - session_last_activity
91
+ time_gap = timestamp_ms - window_end
100
92
101
93
# Check if this session can be extended
102
94
if time_gap <= timeout_ms + grace_ms and timestamp_ms >= window_start :
95
+ extend_session = True
103
96
session_start = window_start
104
- # Only update end time if the new event is newer than the current end time
97
+ # Only update end time if the new event is greater than the current end time
105
98
session_end = max (window_end , timestamp_ms )
106
- can_extend_session = True
107
99
existing_aggregated = aggregated_value
108
- old_window_to_delete = (window_start , window_end )
100
+ # Delete the old window if extending an existing session
101
+ state .delete_window (window_start , window_end )
109
102
break
110
-
111
- # If no extendable session found, start a new one
112
- if not can_extend_session :
113
- session_start = timestamp_ms
114
- session_end = timestamp_ms # End time is the timestamp of the last event
103
+ else :
104
+ # If no extendable session found, start a new one
105
+ extend_session = False
106
+ session_start = session_end = timestamp_ms
115
107
116
108
# Process the event for this session
117
109
updated_windows : list [WindowKeyResult ] = []
118
110
119
- # Delete the old window if extending an existing session
120
- if can_extend_session and old_window_to_delete :
121
- old_start , old_end = old_window_to_delete
122
- state .delete_window (old_start , old_end )
123
-
124
111
# Add to collection if needed
125
112
if collect :
126
- state .add_to_collection (
127
- value = self ._collect_value (value ),
128
- id = timestamp_ms ,
129
- )
113
+ state .add_to_collection (value = self ._collect_value (value ), id = timestamp_ms )
130
114
131
115
# Update the session window aggregation
132
- aggregated = None
133
116
if aggregate :
134
117
current_value = (
135
- existing_aggregated if can_extend_session else self ._initialize_value ()
118
+ existing_aggregated if extend_session else self ._initialize_value ()
136
119
)
137
120
138
121
aggregated = self ._aggregate_value (current_value , value , timestamp_ms )
@@ -142,6 +125,8 @@ def process_window(
142
125
self ._results (aggregated , [], session_start , session_end ),
143
126
)
144
127
)
128
+ else :
129
+ aggregated = None
145
130
146
131
state .update_window (
147
132
session_start , session_end , value = aggregated , timestamp_ms = timestamp_ms
@@ -150,12 +135,10 @@ def process_window(
150
135
# Expire old sessions
151
136
if self ._closing_strategy == ClosingStrategy .PARTITION :
152
137
expired_windows = self .expire_by_partition (
153
- transaction , session_expiry_threshold , collect
138
+ transaction , expiry_threshold , collect
154
139
)
155
140
else :
156
- expired_windows = self .expire_by_key (
157
- key , state , session_expiry_threshold , collect
158
- )
141
+ expired_windows = self .expire_by_key (key , state , expiry_threshold , collect )
159
142
160
143
return updated_windows , expired_windows
161
144
0 commit comments