-
Notifications
You must be signed in to change notification settings - Fork 25
feat(concurrent cursor): attempt at clamping datetime #234
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
3d08e09
attempt at clamping datetime
maxi297 7fbb37b
Auto-fix lint and format issues
314aa77
Fix day clamping initialization
maxi297 105e7a0
Fix bug in day clamping and add tests
maxi297 e9003b1
linting
maxi297 26752b2
PR feedback and questions, add interpolation on clamping, use cursor …
brianjlai 63c1593
Merge branch 'main' into maxi297/attempt-at-clamping-datetime
brianjlai 299bacf
fix imports after rebase
brianjlai File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,99 @@ | ||
from abc import ABC | ||
from datetime import datetime, timedelta | ||
from enum import Enum | ||
from typing import Callable | ||
|
||
from airbyte_cdk.sources.streams.concurrent.cursor_types import CursorValueType | ||
|
||
|
||
class ClampingStrategy(ABC): | ||
def clamp(self, value: CursorValueType) -> CursorValueType: | ||
raise NotImplementedError() | ||
|
||
|
||
class NoClamping(ClampingStrategy): | ||
def clamp(self, value: CursorValueType) -> CursorValueType: | ||
return value | ||
|
||
|
||
class ClampingEndProvider: | ||
def __init__( | ||
self, | ||
clamping_strategy: ClampingStrategy, | ||
end_provider: Callable[[], CursorValueType], | ||
granularity: timedelta, | ||
) -> None: | ||
self._clamping_strategy = clamping_strategy | ||
self._end_provider = end_provider | ||
self._granularity = granularity | ||
|
||
def __call__(self) -> CursorValueType: | ||
return self._clamping_strategy.clamp(self._end_provider()) - self._granularity | ||
|
||
|
||
class DayClampingStrategy(ClampingStrategy): | ||
def __init__(self, is_ceiling: bool = True) -> None: | ||
self._is_ceiling = is_ceiling | ||
|
||
def clamp(self, value: datetime) -> datetime: # type: ignore # datetime implements method from CursorValueType | ||
return_value = value.replace(hour=0, minute=0, second=0, microsecond=0) | ||
if self._is_ceiling: | ||
return return_value + timedelta(days=1) | ||
return return_value | ||
|
||
|
||
class MonthClampingStrategy(ClampingStrategy): | ||
def __init__(self, is_ceiling: bool = True) -> None: | ||
self._is_ceiling = is_ceiling | ||
|
||
def clamp(self, value: datetime) -> datetime: # type: ignore # datetime implements method from CursorValueType | ||
return_value = value.replace(hour=0, minute=0, second=0, microsecond=0) | ||
needs_to_round = value.day != 1 | ||
if not needs_to_round: | ||
return return_value | ||
|
||
return self._ceil(return_value) if self._is_ceiling else return_value.replace(day=1) | ||
|
||
def _ceil(self, value: datetime) -> datetime: | ||
return value.replace( | ||
year=value.year + 1 if value.month == 12 else value.year, | ||
month=(value.month % 12) + 1, | ||
day=1, | ||
hour=0, | ||
minute=0, | ||
second=0, | ||
microsecond=0, | ||
) | ||
|
||
|
||
class Weekday(Enum): | ||
""" | ||
These integer values map to the same ones used by the Datetime.date.weekday() implementation | ||
""" | ||
|
||
MONDAY = 0 | ||
brianjlai marked this conversation as resolved.
Show resolved
Hide resolved
|
||
TUESDAY = 1 | ||
WEDNESDAY = 2 | ||
THURSDAY = 3 | ||
FRIDAY = 4 | ||
SATURDAY = 5 | ||
SUNDAY = 6 | ||
|
||
|
||
class WeekClampingStrategy(ClampingStrategy): | ||
def __init__(self, day_of_week: Weekday, is_ceiling: bool = True) -> None: | ||
self._day_of_week = day_of_week.value | ||
self._is_ceiling = is_ceiling | ||
|
||
def clamp(self, value: datetime) -> datetime: # type: ignore # datetime implements method from CursorValueType | ||
days_diff_to_ceiling = ( | ||
7 - (value.weekday() - self._day_of_week) | ||
if value.weekday() > self._day_of_week | ||
else abs(value.weekday() - self._day_of_week) | ||
) | ||
delta = ( | ||
timedelta(days_diff_to_ceiling) | ||
if self._is_ceiling | ||
else timedelta(days_diff_to_ceiling - 7) | ||
) | ||
return value.replace(hour=0, minute=0, second=0, microsecond=0) + delta |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.