-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(insights): invalidate obviously stale cache results (#17103)
- Loading branch information
1 parent
7f24c42
commit 3fb024b
Showing
4 changed files
with
279 additions
and
14 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,22 @@ | ||
from datetime import datetime, timedelta | ||
|
||
|
||
def end_of_day(reference_date: datetime): | ||
return datetime( | ||
year=reference_date.year, month=reference_date.month, day=reference_date.day, tzinfo=reference_date.tzinfo | ||
) + timedelta(days=1, microseconds=-1) | ||
def start_of_hour(dt: datetime) -> datetime: | ||
return datetime(year=dt.year, month=dt.month, day=dt.day, hour=dt.hour, tzinfo=dt.tzinfo) | ||
|
||
|
||
def start_of_day(reference_date: datetime): | ||
return datetime( | ||
year=reference_date.year, month=reference_date.month, day=reference_date.day, tzinfo=reference_date.tzinfo | ||
) | ||
def start_of_day(dt: datetime): | ||
return datetime(year=dt.year, month=dt.month, day=dt.day, tzinfo=dt.tzinfo) | ||
|
||
|
||
def end_of_day(dt: datetime): | ||
return datetime(year=dt.year, month=dt.month, day=dt.day, tzinfo=dt.tzinfo) + timedelta(days=1, microseconds=-1) | ||
|
||
|
||
def start_of_week(dt: datetime) -> datetime: | ||
# weeks start on sunday | ||
return datetime(year=dt.year, month=dt.month, day=dt.day, tzinfo=dt.tzinfo) - timedelta(days=(dt.weekday() + 1) % 7) | ||
|
||
|
||
def start_of_month(dt: datetime) -> datetime: | ||
return datetime(year=dt.year, month=dt.month, day=1, tzinfo=dt.tzinfo) |
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,33 @@ | ||
from datetime import datetime, timezone | ||
|
||
from posthog.datetime import start_of_hour, start_of_day, end_of_day, start_of_week, start_of_month | ||
|
||
|
||
def test_start_of_hour(): | ||
assert start_of_hour(datetime.fromisoformat("2023-02-08T12:05:23+00:00")) == datetime.fromisoformat( | ||
"2023-02-08T12:00:00+00:00" | ||
) | ||
|
||
|
||
def test_start_of_day(): | ||
assert start_of_day(datetime.fromisoformat("2023-02-08T12:05:23+00:00")) == datetime.fromisoformat( | ||
"2023-02-08T00:00:00+00:00" | ||
) | ||
|
||
|
||
def test_end_of_day(): | ||
assert end_of_day(datetime.fromisoformat("2023-02-08T12:05:23+00:00")) == datetime( | ||
2023, 2, 8, 23, 59, 59, 999999, tzinfo=timezone.utc | ||
) | ||
|
||
|
||
def test_start_of_week(): | ||
assert start_of_week(datetime.fromisoformat("2023-02-08T12:05:23+00:00")) == datetime.fromisoformat( | ||
"2023-02-05T00:00:00+00:00" | ||
) | ||
|
||
|
||
def test_start_of_month(): | ||
assert start_of_month(datetime.fromisoformat("2023-02-08T12:05:23+00:00")) == datetime.fromisoformat( | ||
"2023-02-01T00:00:00+00:00" | ||
) |
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