-
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(web-analytics): Turn timezones into UTC offsets instead of timez…
…one names (#26409)
- Loading branch information
1 parent
90a2854
commit c0652cf
Showing
4 changed files
with
149 additions
and
26 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
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 |
---|---|---|
|
@@ -1006,50 +1006,142 @@ def test_cohort_test_filters(self): | |
assert results == [["/path1", 1, 1]] | ||
|
||
def test_timezone_filter(self): | ||
d1, s1 = "d1", str(uuid7("2024-07-30")) | ||
d2, s2 = "d2", str(uuid7("2024-07-30")) | ||
date = "2024-07-30" | ||
|
||
for idx, (distinct_id, session_id) in enumerate( | ||
[ | ||
("UTC", str(uuid7(date))), | ||
("Asia/Calcutta", str(uuid7(date))), | ||
("America/New_York", str(uuid7(date))), | ||
("America/Sao_Paulo", str(uuid7(date))), | ||
] | ||
): | ||
_create_person( | ||
team_id=self.team.pk, | ||
distinct_ids=[distinct_id], | ||
properties={"name": session_id, "email": f"{distinct_id}@example.com"}, | ||
) | ||
|
||
for i in range(idx + 1): | ||
_create_event( | ||
team=self.team, | ||
event="$pageview", | ||
distinct_id=distinct_id, | ||
timestamp=date, | ||
properties={"$session_id": session_id, "$pathname": f"/path{i}", "$timezone": distinct_id}, | ||
) | ||
|
||
results = self._run_web_stats_table_query( | ||
"all", | ||
None, | ||
breakdown_by=WebStatsBreakdown.TIMEZONE, | ||
).results | ||
|
||
# Brasilia UTC-3, New York UTC-4, Calcutta UTC+5:30, UTC | ||
assert results == [[-3.0, 1.0, 4.0], [-4.0, 1.0, 3.0], [5.5, 1.0, 2.0], [0.0, 1.0, 1.0]] | ||
|
||
def test_timezone_filter_dst_change(self): | ||
did = "id" | ||
sid = str(uuid7("2019-02-17")) | ||
|
||
_create_person( | ||
team_id=self.team.pk, | ||
distinct_ids=[d1], | ||
properties={"name": d1, "email": "[email protected]"}, | ||
distinct_ids=[did], | ||
properties={"name": sid, "email": f"[email protected]"}, | ||
) | ||
|
||
# Cross daylight savings time change in Brazil | ||
for i in range(6): | ||
_create_event( | ||
team=self.team, | ||
event="$pageview", | ||
distinct_id=did, | ||
timestamp=f"2019-02-17 0{i}:00:00", | ||
properties={"$session_id": sid, "$pathname": f"/path1", "$timezone": "America/Sao_Paulo"}, | ||
) | ||
|
||
results = self._run_web_stats_table_query( | ||
"all", | ||
None, | ||
breakdown_by=WebStatsBreakdown.TIMEZONE, | ||
).results | ||
|
||
# Change from UTC-2 to UTC-3 in the middle of the night | ||
assert results == [[-3.0, 1.0, 4.0], [-2.0, 1.0, 2.0]] | ||
|
||
def test_timezone_filter_with_invalid_timezone(self): | ||
date = "2024-07-30" | ||
|
||
for idx, (distinct_id, session_id) in enumerate( | ||
[ | ||
("UTC", str(uuid7(date))), | ||
("Timezone_not_exists", str(uuid7(date))), | ||
] | ||
): | ||
_create_person( | ||
team_id=self.team.pk, | ||
distinct_ids=[distinct_id], | ||
properties={"name": session_id, "email": f"{distinct_id}@example.com"}, | ||
) | ||
|
||
for i in range(idx + 1): | ||
_create_event( | ||
team=self.team, | ||
event="$pageview", | ||
distinct_id=distinct_id, | ||
timestamp=date, | ||
properties={"$session_id": session_id, "$pathname": f"/path{i}", "$timezone": distinct_id}, | ||
) | ||
|
||
with self.assertRaisesRegex(Exception, "Cannot load time zone"): | ||
self._run_web_stats_table_query( | ||
"all", | ||
None, | ||
breakdown_by=WebStatsBreakdown.TIMEZONE, | ||
) | ||
|
||
def test_timezone_filter_with_empty_timezone(self): | ||
did = "id" | ||
sid = str(uuid7("2019-02-17")) | ||
|
||
_create_person( | ||
team_id=self.team.pk, | ||
distinct_ids=[did], | ||
properties={"name": sid, "email": f"[email protected]"}, | ||
) | ||
|
||
# Key not exists | ||
_create_event( | ||
team=self.team, | ||
event="$pageview", | ||
distinct_id=d1, | ||
timestamp="2024-07-30", | ||
properties={"$session_id": s1, "$pathname": "/path1", "$timezone": "America/New_York"}, | ||
distinct_id=did, | ||
timestamp=f"2019-02-17 00:00:00", | ||
properties={"$session_id": sid, "$pathname": f"/path1"}, | ||
) | ||
|
||
_create_person( | ||
team_id=self.team.pk, | ||
distinct_ids=[d2], | ||
properties={"name": d2, "email": "[email protected]"}, | ||
) | ||
# Key exists, it's null | ||
_create_event( | ||
team=self.team, | ||
event="$pageview", | ||
distinct_id=d2, | ||
timestamp="2024-07-30", | ||
properties={"$session_id": s2, "$pathname": "/path2", "$timezone": "America/Brasilia"}, | ||
distinct_id=did, | ||
timestamp=f"2019-02-17 00:00:00", | ||
properties={"$session_id": sid, "$pathname": f"/path1", "$timezone": None}, | ||
) | ||
|
||
# Key exists, it's empty string | ||
_create_event( | ||
team=self.team, | ||
event="$pageview", | ||
distinct_id=d2, | ||
timestamp="2024-07-30", | ||
properties={"$session_id": s2, "$pathname": "/path3", "$timezone": "America/Brasilia"}, | ||
distinct_id=did, | ||
timestamp=f"2019-02-17 00:00:00", | ||
properties={"$session_id": sid, "$pathname": f"/path1", "$timezone": ""}, | ||
) | ||
|
||
flush_persons_and_events() | ||
|
||
results = self._run_web_stats_table_query( | ||
"all", | ||
None, | ||
breakdown_by=WebStatsBreakdown.TIMEZONE, | ||
filter_test_accounts=True, | ||
).results | ||
|
||
assert results == [["America/Brasilia", 1.0, 2.0], ["America/New_York", 1.0, 1.0]] | ||
# Don't crash, treat all of them null | ||
assert results == [] |