-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
53515e5
commit 89c3ac0
Showing
11 changed files
with
437 additions
and
236 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,15 @@ | ||
from .base import WindowResult | ||
from .definitions import ( | ||
HoppingWindowDefinition, | ||
SlidingWindowDefinition, | ||
TumblingWindowDefinition, | ||
CountTumblingWindowDefinition, | ||
FixedTimeHoppingWindowDefinition, | ||
FixedTimeSlidingWindowDefinition, | ||
FixedTimeTumblingWindowDefinition, | ||
) | ||
|
||
__all__ = [ | ||
"HoppingWindowDefinition", | ||
"SlidingWindowDefinition", | ||
"TumblingWindowDefinition", | ||
"FixedTimeHoppingWindowDefinition", | ||
"FixedTimeSlidingWindowDefinition", | ||
"FixedTimeTumblingWindowDefinition", | ||
"CountTumblingWindowDefinition", | ||
"WindowResult", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import logging | ||
from typing import ( | ||
TYPE_CHECKING, | ||
Any, | ||
Iterable, | ||
Optional, | ||
) | ||
|
||
from quixstreams.state import WindowedState | ||
|
||
from .base import ( | ||
Window, | ||
WindowAggregateFunc, | ||
WindowMergeFunc, | ||
WindowResult, | ||
default_merge_func, | ||
) | ||
|
||
if TYPE_CHECKING: | ||
from quixstreams.dataframe.dataframe import StreamingDataFrame | ||
|
||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class FixedCountWindow(Window): | ||
def __init__( | ||
self, | ||
name: str, | ||
count: int, | ||
aggregate_func: WindowAggregateFunc, | ||
aggregate_default: Any, | ||
dataframe: "StreamingDataFrame", | ||
merge_func: Optional[WindowMergeFunc] = None, | ||
): | ||
super().__init__(name, dataframe) | ||
|
||
self._max_count = count | ||
self._aggregate_func = aggregate_func | ||
self._aggregate_default = aggregate_default | ||
self._merge_func = merge_func or default_merge_func | ||
|
||
def process_window( | ||
self, | ||
value: Any, | ||
timestamp_ms: int, | ||
state: WindowedState, | ||
) -> tuple[Iterable[WindowResult], Iterable[WindowResult]]: | ||
data = state.get(key="window") | ||
if data is None: | ||
metadata = {"count": 0, "start": timestamp_ms} | ||
previous_value = self._aggregate_default | ||
else: | ||
metadata, previous_value = data | ||
|
||
aggregated = self._aggregate_func(previous_value, value) | ||
|
||
metadata["count"] += 1 | ||
windows = [ | ||
WindowResult( | ||
start=metadata["start"], | ||
end=timestamp_ms, | ||
value=self._merge_func(aggregated), | ||
) | ||
] | ||
|
||
if metadata["count"] >= self._max_count: | ||
state.delete(key="window") | ||
return windows, windows | ||
|
||
state.set(key="window", value=(metadata, aggregated)) | ||
return windows, [] |
Oops, something went wrong.