Skip to content

Commit

Permalink
fix(retention): Properly apply Monday as the first day of the week (#…
Browse files Browse the repository at this point in the history
…18481)

* fix(retention): Properly apply Monday as the first day of the week

* Update query snapshots

---------

Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
  • Loading branch information
Twixes and github-actions[bot] authored Nov 8, 2023
1 parent b3d88ea commit f986c24
Show file tree
Hide file tree
Showing 5 changed files with 249 additions and 55 deletions.
80 changes: 40 additions & 40 deletions ee/clickhouse/queries/test/__snapshots__/test_retention.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
NULL as breakdown_values_filter,
NULL as selected_interval,
returning_event_query as
(SELECT toStartOfWeek(toTimeZone(toDateTime(e.timestamp, 'UTC'), 'UTC')) AS event_date,
(SELECT toStartOfWeek(toTimeZone(toDateTime(e.timestamp, 'UTC'), 'UTC'), 0) AS event_date,
e."$group_0" as target
FROM events e
WHERE team_id = 2
Expand All @@ -17,13 +17,13 @@
GROUP BY target,
event_date),
target_event_query as
(SELECT DISTINCT toStartOfWeek(toTimeZone(toDateTime(e.timestamp, 'UTC'), 'UTC')) AS event_date,
(SELECT DISTINCT toStartOfWeek(toTimeZone(toDateTime(e.timestamp, 'UTC'), 'UTC'), 0) AS event_date,
e."$group_0" as target,
[
dateDiff(
'Week',
toStartOfWeek(toDateTime('2020-06-07 00:00:00', 'UTC')),
toStartOfWeek(toTimeZone(toDateTime(e.timestamp, 'UTC'), 'UTC'))
toStartOfWeek(toDateTime('2020-06-07 00:00:00', 'UTC'), 0),
toStartOfWeek(toTimeZone(toDateTime(e.timestamp, 'UTC'), 'UTC'), 0)
)
] as breakdown_values
FROM events e
Expand Down Expand Up @@ -70,7 +70,7 @@
[0] as breakdown_values_filter,
NULL as selected_interval,
returning_event_query as
(SELECT toStartOfWeek(toTimeZone(toDateTime(e.timestamp, 'UTC'), 'UTC')) AS event_date,
(SELECT toStartOfWeek(toTimeZone(toDateTime(e.timestamp, 'UTC'), 'UTC'), 0) AS event_date,
e."$group_0" as target
FROM events e
WHERE team_id = 2
Expand All @@ -82,13 +82,13 @@
GROUP BY target,
event_date),
target_event_query as
(SELECT DISTINCT toStartOfWeek(toTimeZone(toDateTime(e.timestamp, 'UTC'), 'UTC')) AS event_date,
(SELECT DISTINCT toStartOfWeek(toTimeZone(toDateTime(e.timestamp, 'UTC'), 'UTC'), 0) AS event_date,
e."$group_0" as target,
[
dateDiff(
'Week',
toStartOfWeek(toDateTime('2020-06-07 00:00:00', 'UTC')),
toStartOfWeek(toTimeZone(toDateTime(e.timestamp, 'UTC'), 'UTC'))
toStartOfWeek(toDateTime('2020-06-07 00:00:00', 'UTC'), 0),
toStartOfWeek(toTimeZone(toDateTime(e.timestamp, 'UTC'), 'UTC'), 0)
)
] as breakdown_values
FROM events e
Expand Down Expand Up @@ -128,7 +128,7 @@
NULL as breakdown_values_filter,
NULL as selected_interval,
returning_event_query as
(SELECT toStartOfWeek(toTimeZone(toDateTime(e.timestamp, 'UTC'), 'UTC')) AS event_date,
(SELECT toStartOfWeek(toTimeZone(toDateTime(e.timestamp, 'UTC'), 'UTC'), 0) AS event_date,
e."$group_1" as target
FROM events e
WHERE team_id = 2
Expand All @@ -140,13 +140,13 @@
GROUP BY target,
event_date),
target_event_query as
(SELECT DISTINCT toStartOfWeek(toTimeZone(toDateTime(e.timestamp, 'UTC'), 'UTC')) AS event_date,
(SELECT DISTINCT toStartOfWeek(toTimeZone(toDateTime(e.timestamp, 'UTC'), 'UTC'), 0) AS event_date,
e."$group_1" as target,
[
dateDiff(
'Week',
toStartOfWeek(toDateTime('2020-06-07 00:00:00', 'UTC')),
toStartOfWeek(toTimeZone(toDateTime(e.timestamp, 'UTC'), 'UTC'))
toStartOfWeek(toDateTime('2020-06-07 00:00:00', 'UTC'), 0),
toStartOfWeek(toTimeZone(toDateTime(e.timestamp, 'UTC'), 'UTC'), 0)
)
] as breakdown_values
FROM events e
Expand Down Expand Up @@ -190,7 +190,7 @@
NULL as breakdown_values_filter,
NULL as selected_interval,
returning_event_query as
(SELECT toStartOfWeek(toTimeZone(toDateTime(e.timestamp, 'UTC'), 'UTC')) AS event_date,
(SELECT toStartOfWeek(toTimeZone(toDateTime(e.timestamp, 'UTC'), 'UTC'), 0) AS event_date,
e."$group_0" as target
FROM events e
WHERE team_id = 2
Expand All @@ -202,13 +202,13 @@
GROUP BY target,
event_date),
target_event_query as
(SELECT DISTINCT toStartOfWeek(toTimeZone(toDateTime(e.timestamp, 'UTC'), 'UTC')) AS event_date,
(SELECT DISTINCT toStartOfWeek(toTimeZone(toDateTime(e.timestamp, 'UTC'), 'UTC'), 0) AS event_date,
e."$group_0" as target,
[
dateDiff(
'Week',
toStartOfWeek(toDateTime('2020-06-07 00:00:00', 'UTC')),
toStartOfWeek(toTimeZone(toDateTime(e.timestamp, 'UTC'), 'UTC'))
toStartOfWeek(toDateTime('2020-06-07 00:00:00', 'UTC'), 0),
toStartOfWeek(toTimeZone(toDateTime(e.timestamp, 'UTC'), 'UTC'), 0)
)
] as breakdown_values
FROM events e
Expand Down Expand Up @@ -255,7 +255,7 @@
[0] as breakdown_values_filter,
NULL as selected_interval,
returning_event_query as
(SELECT toStartOfWeek(toTimeZone(toDateTime(e.timestamp, 'UTC'), 'UTC')) AS event_date,
(SELECT toStartOfWeek(toTimeZone(toDateTime(e.timestamp, 'UTC'), 'UTC'), 0) AS event_date,
e."$group_0" as target
FROM events e
WHERE team_id = 2
Expand All @@ -267,13 +267,13 @@
GROUP BY target,
event_date),
target_event_query as
(SELECT DISTINCT toStartOfWeek(toTimeZone(toDateTime(e.timestamp, 'UTC'), 'UTC')) AS event_date,
(SELECT DISTINCT toStartOfWeek(toTimeZone(toDateTime(e.timestamp, 'UTC'), 'UTC'), 0) AS event_date,
e."$group_0" as target,
[
dateDiff(
'Week',
toStartOfWeek(toDateTime('2020-06-07 00:00:00', 'UTC')),
toStartOfWeek(toTimeZone(toDateTime(e.timestamp, 'UTC'), 'UTC'))
toStartOfWeek(toDateTime('2020-06-07 00:00:00', 'UTC'), 0),
toStartOfWeek(toTimeZone(toDateTime(e.timestamp, 'UTC'), 'UTC'), 0)
)
] as breakdown_values
FROM events e
Expand Down Expand Up @@ -313,7 +313,7 @@
NULL as breakdown_values_filter,
NULL as selected_interval,
returning_event_query as
(SELECT toStartOfWeek(toTimeZone(toDateTime(e.timestamp, 'UTC'), 'UTC')) AS event_date,
(SELECT toStartOfWeek(toTimeZone(toDateTime(e.timestamp, 'UTC'), 'UTC'), 0) AS event_date,
e."$group_1" as target
FROM events e
WHERE team_id = 2
Expand All @@ -325,13 +325,13 @@
GROUP BY target,
event_date),
target_event_query as
(SELECT DISTINCT toStartOfWeek(toTimeZone(toDateTime(e.timestamp, 'UTC'), 'UTC')) AS event_date,
(SELECT DISTINCT toStartOfWeek(toTimeZone(toDateTime(e.timestamp, 'UTC'), 'UTC'), 0) AS event_date,
e."$group_1" as target,
[
dateDiff(
'Week',
toStartOfWeek(toDateTime('2020-06-07 00:00:00', 'UTC')),
toStartOfWeek(toTimeZone(toDateTime(e.timestamp, 'UTC'), 'UTC'))
toStartOfWeek(toDateTime('2020-06-07 00:00:00', 'UTC'), 0),
toStartOfWeek(toTimeZone(toDateTime(e.timestamp, 'UTC'), 'UTC'), 0)
)
] as breakdown_values
FROM events e
Expand Down Expand Up @@ -375,7 +375,7 @@
NULL as breakdown_values_filter,
NULL as selected_interval,
returning_event_query as
(SELECT toStartOfWeek(toTimeZone(toDateTime(e.timestamp, 'UTC'), 'UTC')) AS event_date,
(SELECT toStartOfWeek(toTimeZone(toDateTime(e.timestamp, 'UTC'), 'UTC'), 0) AS event_date,
e.person_id as target
FROM events e
LEFT JOIN
Expand All @@ -394,13 +394,13 @@
GROUP BY target,
event_date),
target_event_query as
(SELECT DISTINCT toStartOfWeek(toTimeZone(toDateTime(e.timestamp, 'UTC'), 'UTC')) AS event_date,
(SELECT DISTINCT toStartOfWeek(toTimeZone(toDateTime(e.timestamp, 'UTC'), 'UTC'), 0) AS event_date,
e.person_id as target,
[
dateDiff(
'Week',
toStartOfWeek(toDateTime('2020-06-07 00:00:00', 'UTC')),
toStartOfWeek(toTimeZone(toDateTime(e.timestamp, 'UTC'), 'UTC'))
toStartOfWeek(toDateTime('2020-06-07 00:00:00', 'UTC'), 0),
toStartOfWeek(toTimeZone(toDateTime(e.timestamp, 'UTC'), 'UTC'), 0)
)
] as breakdown_values
FROM events e
Expand Down Expand Up @@ -451,7 +451,7 @@
NULL as breakdown_values_filter,
NULL as selected_interval,
returning_event_query as
(SELECT toStartOfWeek(toTimeZone(toDateTime(e.timestamp, 'UTC'), 'UTC')) AS event_date,
(SELECT toStartOfWeek(toTimeZone(toDateTime(e.timestamp, 'UTC'), 'UTC'), 0) AS event_date,
e.person_id as target
FROM events e
LEFT JOIN
Expand All @@ -470,13 +470,13 @@
GROUP BY target,
event_date),
target_event_query as
(SELECT DISTINCT toStartOfWeek(toTimeZone(toDateTime(e.timestamp, 'UTC'), 'UTC')) AS event_date,
(SELECT DISTINCT toStartOfWeek(toTimeZone(toDateTime(e.timestamp, 'UTC'), 'UTC'), 0) AS event_date,
e.person_id as target,
[
dateDiff(
'Week',
toStartOfWeek(toDateTime('2020-06-07 00:00:00', 'UTC')),
toStartOfWeek(toTimeZone(toDateTime(e.timestamp, 'UTC'), 'UTC'))
toStartOfWeek(toDateTime('2020-06-07 00:00:00', 'UTC'), 0),
toStartOfWeek(toTimeZone(toDateTime(e.timestamp, 'UTC'), 'UTC'), 0)
)
] as breakdown_values
FROM events e
Expand Down Expand Up @@ -632,7 +632,7 @@
[0] as breakdown_values_filter,
NULL as selected_interval,
returning_event_query as
(SELECT toStartOfWeek(toTimeZone(toDateTime(e.timestamp, 'UTC'), 'UTC')) AS event_date,
(SELECT toStartOfWeek(toTimeZone(toDateTime(e.timestamp, 'UTC'), 'UTC'), 0) AS event_date,
e."$group_0" as target
FROM events e
WHERE team_id = 2
Expand All @@ -644,13 +644,13 @@
GROUP BY target,
event_date),
target_event_query as
(SELECT DISTINCT toStartOfWeek(toTimeZone(toDateTime(e.timestamp, 'UTC'), 'UTC')) AS event_date,
(SELECT DISTINCT toStartOfWeek(toTimeZone(toDateTime(e.timestamp, 'UTC'), 'UTC'), 0) AS event_date,
e."$group_0" as target,
[
dateDiff(
'Week',
toStartOfWeek(toDateTime('2020-06-07 00:00:00', 'UTC')),
toStartOfWeek(toTimeZone(toDateTime(e.timestamp, 'UTC'), 'UTC'))
toStartOfWeek(toDateTime('2020-06-07 00:00:00', 'UTC'), 0),
toStartOfWeek(toTimeZone(toDateTime(e.timestamp, 'UTC'), 'UTC'), 0)
)
] as breakdown_values
FROM events e
Expand Down Expand Up @@ -693,7 +693,7 @@
[0] as breakdown_values_filter,
NULL as selected_interval,
returning_event_query as
(SELECT toStartOfWeek(toTimeZone(toDateTime(e.timestamp, 'UTC'), 'UTC')) AS event_date,
(SELECT toStartOfWeek(toTimeZone(toDateTime(e.timestamp, 'UTC'), 'UTC'), 0) AS event_date,
e."$group_0" as target
FROM events e
WHERE team_id = 2
Expand All @@ -705,13 +705,13 @@
GROUP BY target,
event_date),
target_event_query as
(SELECT DISTINCT toStartOfWeek(toTimeZone(toDateTime(e.timestamp, 'UTC'), 'UTC')) AS event_date,
(SELECT DISTINCT toStartOfWeek(toTimeZone(toDateTime(e.timestamp, 'UTC'), 'UTC'), 0) AS event_date,
e."$group_0" as target,
[
dateDiff(
'Week',
toStartOfWeek(toDateTime('2020-06-07 00:00:00', 'UTC')),
toStartOfWeek(toTimeZone(toDateTime(e.timestamp, 'UTC'), 'UTC'))
toStartOfWeek(toDateTime('2020-06-07 00:00:00', 'UTC'), 0),
toStartOfWeek(toTimeZone(toDateTime(e.timestamp, 'UTC'), 'UTC'), 0)
)
] as breakdown_values
FROM events e
Expand Down
12 changes: 9 additions & 3 deletions posthog/models/filters/mixins/retention.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,16 @@ def date_from(self) -> datetime:
if self.period == "Hour":
return self.date_to - tdelta
elif self.period == "Week":
date_from = self.date_to - tdelta
return date_from - timedelta(days=date_from.isoweekday() % 7)
date_from: datetime = self.date_to - tdelta
week_start_alignment_days = date_from.isoweekday() % 7
if team := getattr(self, "team", None):
from posthog.models.team.team import WeekStartDay

if team.week_start_day == WeekStartDay.MONDAY:
week_start_alignment_days = date_from.weekday()
return date_from - timedelta(days=week_start_alignment_days)
else:
date_to = self.date_to.replace(hour=0, minute=0, second=0, microsecond=0)
date_to: datetime = self.date_to.replace(hour=0, minute=0, second=0, microsecond=0)
return date_to - tdelta

@cached_property
Expand Down
Loading

0 comments on commit f986c24

Please sign in to comment.