From 4137f333fb1dc601b17a6657d7584b91bc4fb419 Mon Sep 17 00:00:00 2001 From: Robbie Coomber Date: Thu, 20 Jun 2024 17:43:18 +0100 Subject: [PATCH] Fix trends tests --- posthog/api/test/test_session.py | 6 +- .../database/schema/test/test_sessions_v2.py | 2 +- .../insights/trends/test/test_trends.py | 159 +++++++++++------- posthog/models/utils.py | 19 ++- 4 files changed, 115 insertions(+), 71 deletions(-) diff --git a/posthog/api/test/test_session.py b/posthog/api/test/test_session.py index 89968cbc107f2b..ed7ab59d1479ae 100644 --- a/posthog/api/test/test_session.py +++ b/posthog/api/test/test_session.py @@ -3,25 +3,27 @@ from rest_framework import status from posthog.models.event.util import create_event +from posthog.models.utils import uuid7 from posthog.test.base import APIBaseTest class TestSessionsAPI(APIBaseTest): def setUp(self) -> None: super().setUp() + s1 = str(uuid7()) create_event( team=self.team, event="$pageview", distinct_id="d1", - properties={"$session_id": "s1", "utm_source": "google"}, + properties={"$session_id": s1, "utm_source": "google"}, event_uuid=(uuid.uuid4()), ) create_event( team=self.team, event="$pageview", distinct_id="d1", - properties={"$session_id": "s1", "utm_source": "youtube"}, + properties={"$session_id": s1, "utm_source": "youtube"}, event_uuid=(uuid.uuid4()), ) diff --git a/posthog/hogql/database/schema/test/test_sessions_v2.py b/posthog/hogql/database/schema/test/test_sessions_v2.py index 3f91710de98f62..62b647ec47a7b1 100644 --- a/posthog/hogql/database/schema/test/test_sessions_v2.py +++ b/posthog/hogql/database/schema/test/test_sessions_v2.py @@ -290,7 +290,7 @@ def test_bounce_rate(self): class TestGetLazySessionProperties(ClickhouseTestMixin, APIBaseTest): def test_all(self): results = get_lazy_session_table_properties_v2(None) - self.assertEqual(len(results), 21) + self.assertEqual(len(results), 20) self.assertEqual( results[0], { diff --git a/posthog/hogql_queries/insights/trends/test/test_trends.py b/posthog/hogql_queries/insights/trends/test/test_trends.py index fa288cea532c2f..7d6ec6ae07296f 100644 --- a/posthog/hogql_queries/insights/trends/test/test_trends.py +++ b/posthog/hogql_queries/insights/trends/test/test_trends.py @@ -43,6 +43,7 @@ from posthog.models.person.util import create_person_distinct_id from posthog.models.property_definition import PropertyDefinition from posthog.models.team.team import Team +from posthog.models.utils import uuid7 from posthog.schema import ( ActionsNode, BreakdownFilter, @@ -903,6 +904,10 @@ def test_trends_single_aggregate_math(self): @snapshot_clickhouse_queries def test_trends_with_session_property_single_aggregate_math(self): + s1 = str(uuid7("2020-01-01", 1)) + s2 = str(uuid7("2020-01-01", 2)) + s3 = str(uuid7("2020-01-01", 3)) + s4 = str(uuid7("2020-01-01", 4)) self._create_person( team_id=self.team.pk, distinct_ids=["blabla", "anonymous_id"], @@ -918,21 +923,21 @@ def test_trends_with_session_property_single_aggregate_math(self): team=self.team, event="sign up before", distinct_id="blabla", - properties={"$session_id": 1}, + properties={"$session_id": s1}, timestamp="2020-01-01 00:06:30", ) self._create_event( team=self.team, event="sign up", distinct_id="blabla", - properties={"$session_id": 1}, + properties={"$session_id": s1}, timestamp="2020-01-01 00:06:34", ) self._create_event( team=self.team, event="sign up later", distinct_id="blabla", - properties={"$session_id": 1}, + properties={"$session_id": s1}, timestamp="2020-01-01 00:06:35", ) # First session lasted 5 seconds @@ -940,14 +945,14 @@ def test_trends_with_session_property_single_aggregate_math(self): team=self.team, event="sign up", distinct_id="blabla2", - properties={"$session_id": 2}, + properties={"$session_id": s2}, timestamp="2020-01-01 00:06:35", ) self._create_event( team=self.team, event="sign up", distinct_id="blabla2", - properties={"$session_id": 2}, + properties={"$session_id": s2}, timestamp="2020-01-01 00:06:45", ) # Second session lasted 10 seconds @@ -956,7 +961,7 @@ def test_trends_with_session_property_single_aggregate_math(self): team=self.team, event="sign up", distinct_id="blabla", - properties={"$session_id": 3}, + properties={"$session_id": s3}, timestamp="2020-01-01 00:06:45", ) # Third session lasted 0 seconds @@ -965,14 +970,14 @@ def test_trends_with_session_property_single_aggregate_math(self): team=self.team, event="sign up", distinct_id="blabla", - properties={"$session_id": 4}, + properties={"$session_id": s4}, timestamp="2020-01-02 00:06:30", ) self._create_event( team=self.team, event="sign up", distinct_id="blabla", - properties={"$session_id": 4}, + properties={"$session_id": s4}, timestamp="2020-01-02 00:06:45", ) # Fourth session lasted 15 seconds @@ -1022,6 +1027,10 @@ def test_trends_with_session_property_single_aggregate_math(self): ) def test_unique_session_with_session_breakdown(self): + s1 = str(uuid7("2020-01-01", 1)) + s2 = str(uuid7("2020-01-01", 2)) + s3 = str(uuid7("2020-01-01", 3)) + s4 = str(uuid7("2020-01-01", 4)) self._create_person( team_id=self.team.pk, distinct_ids=["blabla", "anonymous_id"], @@ -1037,21 +1046,21 @@ def test_unique_session_with_session_breakdown(self): team=self.team, event="sign up before", distinct_id="blabla", - properties={"$session_id": 1}, + properties={"$session_id": s1}, timestamp="2020-01-01 00:06:30", ) self._create_event( team=self.team, event="sign up", distinct_id="blabla", - properties={"$session_id": 1}, + properties={"$session_id": s1}, timestamp="2020-01-01 00:06:34", ) self._create_event( team=self.team, event="sign up later", distinct_id="blabla", - properties={"$session_id": 1}, + properties={"$session_id": s1}, timestamp="2020-01-01 00:06:35", ) # First session lasted 5 seconds @@ -1059,14 +1068,14 @@ def test_unique_session_with_session_breakdown(self): team=self.team, event="sign up", distinct_id="blabla2", - properties={"$session_id": 2}, + properties={"$session_id": s2}, timestamp="2020-01-01 00:06:35", ) self._create_event( team=self.team, event="sign up", distinct_id="blabla2", - properties={"$session_id": 2}, + properties={"$session_id": s2}, timestamp="2020-01-01 00:06:45", ) # Second session lasted 10 seconds @@ -1075,7 +1084,7 @@ def test_unique_session_with_session_breakdown(self): team=self.team, event="sign up", distinct_id="blabla", - properties={"$session_id": 3}, + properties={"$session_id": s3}, timestamp="2020-01-01 00:06:45", ) # Third session lasted 0 seconds @@ -1084,14 +1093,14 @@ def test_unique_session_with_session_breakdown(self): team=self.team, event="sign up", distinct_id="blabla", - properties={"$session_id": 4}, + properties={"$session_id": s4}, timestamp="2020-01-02 00:06:30", ) self._create_event( team=self.team, event="sign up", distinct_id="blabla", - properties={"$session_id": 4}, + properties={"$session_id": s4}, timestamp="2020-01-02 00:06:45", ) # Fourth session lasted 15 seconds @@ -1491,6 +1500,10 @@ def test_trends_breakdown_single_aggregate_math(self): @snapshot_clickhouse_queries def test_trends_breakdown_with_session_property_single_aggregate_math_and_breakdown(self): + s1 = str(uuid7("2020-01-01", 1)) + s2 = str(uuid7("2020-01-01", 2)) + s3 = str(uuid7("2020-01-01", 3)) + s4 = str(uuid7("2020-01-01", 4)) self._create_person( team_id=self.team.pk, distinct_ids=["blabla", "anonymous_id"], @@ -1506,21 +1519,21 @@ def test_trends_breakdown_with_session_property_single_aggregate_math_and_breakd team=self.team, event="sign up before", distinct_id="blabla", - properties={"$session_id": 1, "$some_property": "value1"}, + properties={"$session_id": s1, "$some_property": "value1"}, timestamp="2020-01-01 00:06:30", ) self._create_event( team=self.team, event="sign up", distinct_id="blabla", - properties={"$session_id": 1, "$some_property": "value1"}, + properties={"$session_id": s1, "$some_property": "value1"}, timestamp="2020-01-01 00:06:34", ) self._create_event( team=self.team, event="sign up later", distinct_id="blabla", - properties={"$session_id": 1, "$some_property": "value doesnt matter"}, + properties={"$session_id": s1, "$some_property": "value doesnt matter"}, timestamp="2020-01-01 00:06:35", ) # First session lasted 5 seconds @@ -1528,14 +1541,14 @@ def test_trends_breakdown_with_session_property_single_aggregate_math_and_breakd team=self.team, event="sign up", distinct_id="blabla2", - properties={"$session_id": 2, "$some_property": "value2"}, + properties={"$session_id": s2, "$some_property": "value2"}, timestamp="2020-01-01 00:06:35", ) self._create_event( team=self.team, event="sign up", distinct_id="blabla2", - properties={"$session_id": 2, "$some_property": "value1"}, + properties={"$session_id": s2, "$some_property": "value1"}, timestamp="2020-01-01 00:06:45", ) # Second session lasted 10 seconds @@ -1544,14 +1557,14 @@ def test_trends_breakdown_with_session_property_single_aggregate_math_and_breakd team=self.team, event="sign up", distinct_id="blabla", - properties={"$session_id": 3}, + properties={"$session_id": s3}, timestamp="2020-01-01 00:06:45", ) self._create_event( team=self.team, event="sign up", distinct_id="blabla", - properties={"$session_id": 3}, + properties={"$session_id": s3}, timestamp="2020-01-01 00:06:46", ) # Third session lasted 1 seconds @@ -1560,21 +1573,21 @@ def test_trends_breakdown_with_session_property_single_aggregate_math_and_breakd team=self.team, event="sign up", distinct_id="blabla", - properties={"$session_id": 4, "$some_property": "value2"}, + properties={"$session_id": s4, "$some_property": "value2"}, timestamp="2020-01-02 00:06:30", ) self._create_event( team=self.team, event="sign up", distinct_id="blabla", - properties={"$session_id": 4, "$some_property": "value2"}, + properties={"$session_id": s4, "$some_property": "value2"}, timestamp="2020-01-02 00:06:35", ) self._create_event( team=self.team, event="sign up", distinct_id="blabla", - properties={"$session_id": 4, "$some_property": "value1"}, + properties={"$session_id": s4, "$some_property": "value1"}, timestamp="2020-01-02 00:06:45", ) # Fourth session lasted 15 seconds @@ -1639,6 +1652,10 @@ def test_trends_breakdown_with_session_property_single_aggregate_math_and_breakd @snapshot_clickhouse_queries def test_trends_person_breakdown_with_session_property_single_aggregate_math_and_breakdown(self): + s1 = str(uuid7("2020-01-01", 1)) + s2 = str(uuid7("2020-01-01", 2)) + s3 = str(uuid7("2020-01-01", 3)) + s4 = str(uuid7("2020-01-01", 4)) self._create_person( team_id=self.team.pk, distinct_ids=["blabla", "anonymous_id"], @@ -1654,21 +1671,21 @@ def test_trends_person_breakdown_with_session_property_single_aggregate_math_and team=self.team, event="sign up before", distinct_id="blabla", - properties={"$session_id": 1, "$some_property": "value1"}, + properties={"$session_id": s1, "$some_property": "value1"}, timestamp="2020-01-01 00:06:30", ) self._create_event( team=self.team, event="sign up", distinct_id="blabla", - properties={"$session_id": 1, "$some_property": "value1"}, + properties={"$session_id": s1, "$some_property": "value1"}, timestamp="2020-01-01 00:06:34", ) self._create_event( team=self.team, event="sign up later", - distinct_id="blabla", - properties={"$session_id": 1, "$some_property": "value doesnt matter"}, + distinct_id="blasbla", + properties={"$session_id": s1, "$some_property": "value doesnt matter"}, timestamp="2020-01-01 00:06:35", ) # First session lasted 5 seconds @@ -1676,14 +1693,14 @@ def test_trends_person_breakdown_with_session_property_single_aggregate_math_and team=self.team, event="sign up", distinct_id="blabla2", - properties={"$session_id": 2, "$some_property": "value2"}, + properties={"$session_id": s2, "$some_property": "value2"}, timestamp="2020-01-01 00:06:35", ) self._create_event( team=self.team, event="sign up", distinct_id="blabla2", - properties={"$session_id": 2, "$some_property": "value1"}, + properties={"$session_id": s2, "$some_property": "value1"}, timestamp="2020-01-01 00:06:45", ) # Second session lasted 10 seconds @@ -1692,14 +1709,14 @@ def test_trends_person_breakdown_with_session_property_single_aggregate_math_and team=self.team, event="sign up", distinct_id="blabla", - properties={"$session_id": 3}, + properties={"$session_id": s3}, timestamp="2020-01-01 00:06:45", ) self._create_event( team=self.team, event="sign up", distinct_id="blabla", - properties={"$session_id": 3}, + properties={"$session_id": s3}, timestamp="2020-01-01 00:06:46", ) # Third session lasted 1 seconds @@ -1708,21 +1725,21 @@ def test_trends_person_breakdown_with_session_property_single_aggregate_math_and team=self.team, event="sign up", distinct_id="blabla", - properties={"$session_id": 4, "$some_property": "value2"}, + properties={"$session_id": s4, "$some_property": "value2"}, timestamp="2020-01-02 00:06:30", ) self._create_event( team=self.team, event="sign up", distinct_id="blabla", - properties={"$session_id": 4, "$some_property": "value2"}, + properties={"$session_id": s4, "$some_property": "value2"}, timestamp="2020-01-02 00:06:35", ) self._create_event( team=self.team, event="sign up", distinct_id="blabla", - properties={"$session_id": 4, "$some_property": "value1"}, + properties={"$session_id": s4, "$some_property": "value1"}, timestamp="2020-01-02 00:06:45", ) # Fourth session lasted 15 seconds @@ -2806,6 +2823,8 @@ def test_property_filtering(self): @snapshot_clickhouse_queries def test_trends_with_hogql_math(self): + s1 = str(uuid7("2020-01-01", 1)) + s5 = str(uuid7("2020-01-01", 5)) self._create_person( team_id=self.team.pk, distinct_ids=["blabla", "anonymous_id"], @@ -2815,14 +2834,14 @@ def test_trends_with_hogql_math(self): team=self.team, event="sign up", distinct_id="blabla", - properties={"$session_id": 1}, + properties={"$session_id": s1}, timestamp="2020-01-01 00:06:30", ) self._create_event( team=self.team, event="sign up", distinct_id="blabla", - properties={"$session_id": 5}, + properties={"$session_id": s5}, timestamp="2020-01-02 00:06:45", ) @@ -2848,6 +2867,11 @@ def test_trends_with_hogql_math(self): @snapshot_clickhouse_queries def test_trends_with_session_property_total_volume_math(self): + s1 = str(uuid7("2020-01-01", 1)) + s2 = str(uuid7("2020-01-01", 2)) + s3 = str(uuid7("2020-01-01", 3)) + s4 = str(uuid7("2020-01-01", 4)) + s5 = str(uuid7("2020-01-01", 5)) self._create_person( team_id=self.team.pk, distinct_ids=["blabla", "anonymous_id"], @@ -2863,21 +2887,21 @@ def test_trends_with_session_property_total_volume_math(self): team=self.team, event="sign up before", distinct_id="blabla", - properties={"$session_id": 1}, + properties={"$session_id": s1}, timestamp="2020-01-01 00:06:30", ) self._create_event( team=self.team, event="sign up", distinct_id="blabla", - properties={"$session_id": 1}, + properties={"$session_id": s1}, timestamp="2020-01-01 00:06:34", ) self._create_event( team=self.team, event="sign up later", distinct_id="blabla", - properties={"$session_id": 1}, + properties={"$session_id": s1}, timestamp="2020-01-01 00:06:35", ) # First session lasted 5 seconds @@ -2885,14 +2909,14 @@ def test_trends_with_session_property_total_volume_math(self): team=self.team, event="sign up", distinct_id="blabla2", - properties={"$session_id": 2}, + properties={"$session_id": s2}, timestamp="2020-01-01 00:06:35", ) self._create_event( team=self.team, event="sign up", distinct_id="blabla2", - properties={"$session_id": 2}, + properties={"$session_id": s2}, timestamp="2020-01-01 00:06:45", ) # Second session lasted 10 seconds @@ -2901,7 +2925,7 @@ def test_trends_with_session_property_total_volume_math(self): team=self.team, event="sign up", distinct_id="blabla", - properties={"$session_id": 3}, + properties={"$session_id": s3}, timestamp="2020-01-01 00:06:45", ) # Third session lasted 0 seconds @@ -2910,14 +2934,14 @@ def test_trends_with_session_property_total_volume_math(self): team=self.team, event="sign up", distinct_id="blabla", - properties={"$session_id": 4}, + properties={"$session_id": s4}, timestamp="2020-01-02 00:06:30", ) self._create_event( team=self.team, event="sign up", distinct_id="blabla", - properties={"$session_id": 4}, + properties={"$session_id": s4}, timestamp="2020-01-02 00:06:45", ) # Fourth session lasted 15 seconds @@ -2926,14 +2950,14 @@ def test_trends_with_session_property_total_volume_math(self): team=self.team, event="sign up", distinct_id="blabla", - properties={"$session_id": 5}, + properties={"$session_id": s5}, timestamp="2020-01-02 00:06:40", ) self._create_event( team=self.team, event="sign up", distinct_id="blabla", - properties={"$session_id": 5}, + properties={"$session_id": s5}, timestamp="2020-01-02 00:06:45", ) # Fifth session lasted 5 seconds @@ -2994,6 +3018,11 @@ def test_trends_with_session_property_total_volume_math(self): @snapshot_clickhouse_queries def test_trends_with_session_property_total_volume_math_with_breakdowns(self): + s1 = str(uuid7("2020-01-01", 1)) + s2 = str(uuid7("2020-01-01", 2)) + s3 = str(uuid7("2020-01-01", 3)) + s4 = str(uuid7("2020-01-01", 4)) + s5 = str(uuid7("2020-01-01", 5)) self._create_person( team_id=self.team.pk, distinct_ids=["blabla", "anonymous_id"], @@ -3009,21 +3038,21 @@ def test_trends_with_session_property_total_volume_math_with_breakdowns(self): team=self.team, event="sign up before", distinct_id="blabla", - properties={"$session_id": 1, "$some_property": "value1"}, + properties={"$session_id": s1, "$some_property": "value1"}, timestamp="2020-01-01 00:06:30", ) self._create_event( team=self.team, event="sign up", distinct_id="blabla", - properties={"$session_id": 1, "$some_property": "value2"}, + properties={"$session_id": s1, "$some_property": "value2"}, timestamp="2020-01-01 00:06:34", ) self._create_event( team=self.team, event="sign up", distinct_id="blabla", - properties={"$session_id": 1, "$some_property": "value2"}, + properties={"$session_id": s1, "$some_property": "value2"}, timestamp="2020-01-01 00:06:35", ) # First session lasted 5 seconds @@ -3031,14 +3060,14 @@ def test_trends_with_session_property_total_volume_math_with_breakdowns(self): team=self.team, event="sign up", distinct_id="blabla2", - properties={"$session_id": 2, "$some_property": "value2"}, + properties={"$session_id": s2, "$some_property": "value2"}, timestamp="2020-01-01 00:06:35", ) self._create_event( team=self.team, event="sign up", distinct_id="blabla2", - properties={"$session_id": 2, "$some_property": "value1"}, + properties={"$session_id": s2, "$some_property": "value1"}, timestamp="2020-01-01 00:06:45", ) # Second session lasted 10 seconds @@ -3047,7 +3076,7 @@ def test_trends_with_session_property_total_volume_math_with_breakdowns(self): team=self.team, event="sign up", distinct_id="blabla", - properties={"$session_id": 3, "$some_property": "value1"}, + properties={"$session_id": s3, "$some_property": "value1"}, timestamp="2020-01-01 00:06:45", ) # Third session lasted 0 seconds @@ -3056,14 +3085,14 @@ def test_trends_with_session_property_total_volume_math_with_breakdowns(self): team=self.team, event="sign up", distinct_id="blabla", - properties={"$session_id": 4, "$some_property": "value2"}, + properties={"$session_id": s4, "$some_property": "value2"}, timestamp="2020-01-02 00:06:30", ) self._create_event( team=self.team, event="sign up", distinct_id="blabla", - properties={"$session_id": 4, "$some_property": "value2"}, + properties={"$session_id": s4, "$some_property": "value2"}, timestamp="2020-01-02 00:06:45", ) # Fourth session lasted 15 seconds @@ -3072,14 +3101,14 @@ def test_trends_with_session_property_total_volume_math_with_breakdowns(self): team=self.team, event="sign up", distinct_id="blabla", - properties={"$session_id": 5, "$some_property": "value1"}, + properties={"$session_id": s5, "$some_property": "value1"}, timestamp="2020-01-02 00:06:40", ) self._create_event( team=self.team, event="sign up", distinct_id="blabla", - properties={"$session_id": 5, "$some_property": "value1"}, + properties={"$session_id": s5, "$some_property": "value1"}, timestamp="2020-01-02 00:06:45", ) # Fifth session lasted 5 seconds @@ -3147,6 +3176,8 @@ def test_trends_with_session_property_total_volume_math_with_breakdowns(self): self.assertCountEqual(weekly_response[1]["data"], [0, 0, 0, 0, 5, 5, 0, 0]) def test_trends_with_session_property_total_volume_math_with_sessions_spanning_multiple_intervals(self): + s1 = str(uuid7("2020-01-01", 1)) + s2 = str(uuid7("2020-01-01", 2)) self._create_person( team_id=self.team.pk, distinct_ids=["blabla", "anonymous_id"], @@ -3162,21 +3193,21 @@ def test_trends_with_session_property_total_volume_math_with_sessions_spanning_m team=self.team, event="sign up", distinct_id="blabla", - properties={"$session_id": 1}, + properties={"$session_id": s1}, timestamp="2020-01-01 00:06:30", ) self._create_event( team=self.team, event="sign up", distinct_id="blabla", - properties={"$session_id": 1}, + properties={"$session_id": s1}, timestamp="2020-01-02 00:06:34", ) self._create_event( team=self.team, event="sign up", distinct_id="blabla", - properties={"$session_id": 1}, + properties={"$session_id": s1}, timestamp="2020-01-03 00:06:30", ) # First Session lasted 48 hours = a lot of seconds @@ -3184,14 +3215,14 @@ def test_trends_with_session_property_total_volume_math_with_sessions_spanning_m team=self.team, event="sign up", distinct_id="blabla2", - properties={"$session_id": 2}, + properties={"$session_id": s2}, timestamp="2020-01-01 00:06:35", ) self._create_event( team=self.team, event="sign up", distinct_id="blabla2", - properties={"$session_id": 2}, + properties={"$session_id": s2}, timestamp="2020-01-05 00:06:35", ) # Second session lasted 96 hours = a lot of seconds diff --git a/posthog/models/utils.py b/posthog/models/utils.py index a7809d0d49acb9..9572214dfc8c8c 100644 --- a/posthog/models/utils.py +++ b/posthog/models/utils.py @@ -96,22 +96,33 @@ def is_valid_uuid(cls, candidate: Any) -> bool: # Delete this when we can use the version from the stdlib directly, see https://github.com/python/cpython/issues/102461 -def uuid7(unix_ms_time: Optional[Union[int, str]] = None, seeded_random: Optional["Random"] = None) -> uuid.UUID: +def uuid7( + unix_ms_time: Optional[Union[int, str]] = None, random_component: Optional[Union["Random", int]] = None +) -> uuid.UUID: # timestamp part unix_ms_time_int: int if isinstance(unix_ms_time, str): + # parse the ISO format string, use the timestamp from that date = datetime.datetime.fromisoformat(unix_ms_time) unix_ms_time_int = int(date.timestamp() * 1000) elif unix_ms_time is None: + # use the current system time unix_ms_time_int = time_ns() // (10**6) else: + # use the provided timestamp directly unix_ms_time_int = unix_ms_time # random part - if seeded_random is not None: - rand_a = seeded_random.getrandbits(12) - rand_b = seeded_random.getrandbits(56) + if isinstance(random_component, int): + # use the integer directly as the random component + rand_a = random_component & 0x0FFF + rand_b = random_component >> 12 & 0x03FFFFFFFFFFFFFFF + elif random_component is not None: + # use the provided random generator + rand_a = random_component.getrandbits(12) + rand_b = random_component.getrandbits(56) else: + # use the system random generator rand_bytes = int.from_bytes(secrets.token_bytes(10), byteorder="little") rand_a = rand_bytes & 0x0FFF rand_b = (rand_bytes >> 12) & 0x03FFFFFFFFFFFFFFF