示例#1
0
    def test_alias_both_existing(self) -> None:
        create_person(distinct_ids=["old_distinct_id"], team_id=self.team.pk)
        create_person(distinct_ids=["new_distinct_id"], team_id=self.team.pk)

        process_event_ee(
            "new_distinct_id",
            "",
            "",
            {
                "event": "$create_alias",
                "properties": {
                    "distinct_id": "new_distinct_id",
                    "token": self.team.api_token,
                    "alias": "old_distinct_id",
                },
            },
            self.team.pk,
            now().isoformat(),
            now().isoformat(),
        )

        events = get_events()
        distinct_ids = [item["distinct_id"] for item in get_person_distinct_ids(team_id=self.team.pk)]

        self.assertEqual(len(events), 1)
        self.assertEqual(sorted(distinct_ids), sorted(["old_distinct_id", "new_distinct_id"]))
示例#2
0
    def test_capture_sent_at(self) -> None:
        self._create_user("tim")
        create_person(team_id=self.team.pk, distinct_ids=["asdfasdfasdf"])

        right_now = now()
        tomorrow = right_now + timedelta(days=1, hours=2)
        tomorrow_sent_at = right_now + timedelta(days=1, hours=2, minutes=10)

        # event sent_at 10 minutes after timestamp
        process_event_ee(
            "movie played",
            "",
            "",
            {
                "event": "$pageview",
                "timestamp": tomorrow.isoformat(),
                "properties": {"distinct_id": "asdfasdfasdf", "token": self.team.api_token,},
            },
            self.team.pk,
            right_now.isoformat(),
            tomorrow_sent_at.isoformat(),
        )

        events = get_events()
        returned_time = datetime.strptime(events[0]["timestamp"], "%Y-%m-%dT%H:%M:%S.%f%z")
        event_seconds_before_now = (right_now - returned_time).seconds

        # assert that the event is actually recorded 10 minutes before now
        self.assertGreater(event_seconds_before_now, 590)
        self.assertLess(event_seconds_before_now, 610)
示例#3
0
    def test_distinct_with_anonymous_id_which_was_already_created(self) -> None:
        create_person(team_id=self.team.pk, distinct_ids=["anonymous_id"])
        create_person(team_id=self.team.pk, distinct_ids=["new_distinct_id"], properties={"email": "*****@*****.**"})

        process_event_ee(
            "new_distinct_id",
            "",
            "",
            {
                "event": "$identify",
                "properties": {
                    "$anon_distinct_id": "anonymous_id",
                    "token": self.team.api_token,
                    "distinct_id": "new_distinct_id",
                },
            },
            self.team.pk,
            now().isoformat(),
            now().isoformat(),
        )

        # self.assertEqual(Event.objects.count(), 0)
        person = get_person_by_distinct_id(self.team.pk, "new_distinct_id")
        distinct_ids = [item["distinct_id"] for item in get_person_distinct_ids(team_id=self.team.pk)]
        self.assertEqual(sorted(distinct_ids), sorted(["anonymous_id", "new_distinct_id"]))
        self.assertEqual(person["properties"]["email"], "*****@*****.**")
示例#4
0
    def test_capture_no_sent_at(self) -> None:
        self._create_user("james")
        create_person(team_id=self.team.pk, distinct_ids=["asdfasdfasdf"])

        right_now = now()
        tomorrow = right_now + timedelta(days=1, hours=2)

        # event sent_at 10 minutes after timestamp
        process_event_ee(
            "movie played",
            "",
            "",
            {
                "event": "$pageview",
                "timestamp": tomorrow.isoformat(),
                "properties": {"distinct_id": "asdfasdfasdf", "token": self.team.api_token,},
            },
            self.team.pk,
            right_now.isoformat(),
            None,
        )

        events = get_events()
        returned_time = datetime.strptime(events[0]["timestamp"], "%Y-%m-%dT%H:%M:%S.%f%z")

        difference = abs((tomorrow - returned_time).seconds)

        self.assertLess(difference, 1)
示例#5
0
文件: person.py 项目: akbansa/posthog
    def split_person(self, main_distinct_id: Optional[str]):
        distinct_ids = Person.objects.get(pk=self.pk).distinct_ids
        if not main_distinct_id:
            self.properties = {}
            self.save()
            main_distinct_id = distinct_ids[0]

        for distinct_id in distinct_ids:
            if not distinct_id == main_distinct_id:
                with transaction.atomic():
                    pdi = PersonDistinctId.objects.select_for_update().get(
                        person=self, distinct_id=distinct_id)
                    person = Person.objects.create(team_id=self.team_id)
                    pdi.person_id = str(person.id)
                    pdi.version = (pdi.version or 0) + 1
                    pdi.save(update_fields=["version", "person_id"])

                from ee.clickhouse.models.person import create_person, create_person_distinct_id

                create_person_distinct_id(team_id=self.team_id,
                                          distinct_id=distinct_id,
                                          person_id=str(self.uuid),
                                          sign=-1)
                create_person_distinct_id(
                    team_id=self.team_id,
                    distinct_id=distinct_id,
                    person_id=str(person.uuid),
                    sign=1,
                    version=pdi.version,
                )
                create_person(
                    team_id=self.team_id,
                    uuid=str(person.uuid),
                )
示例#6
0
    def create_people(self):
        self.people = [self.make_person(i) for i in range(self.n_people)]
        self.distinct_ids = [str(UUIDT()) for _ in self.people]
        self.people = Person.objects.bulk_create(self.people)

        pids = [
            PersonDistinctId(team=self.team,
                             person=person,
                             distinct_id=distinct_id)
            for person, distinct_id in zip(self.people, self.distinct_ids)
        ]
        PersonDistinctId.objects.bulk_create(pids)
        from ee.clickhouse.models.person import create_person, create_person_distinct_id

        for person in self.people:
            create_person(
                uuid=str(person.uuid),
                team_id=person.team.pk,
                properties=person.properties,
                is_identified=person.is_identified,
            )
        for pid in pids:
            create_person_distinct_id(
                pid.team.pk, pid.distinct_id,
                str(pid.person.uuid))  # use dummy number for id
示例#7
0
    def test_delete_persons(self):
        uuid0 = create_person(self.teams[0].pk, properties={"x": 0})
        uuid1 = create_person(self.teams[1].pk, properties={"x": 1})
        uuid2 = create_person(self.teams[2].pk, properties={"x": 2})
        create_person_distinct_id(self.teams[0].pk, "0", uuid0)
        create_person_distinct_id(self.teams[1].pk, "1", uuid1)
        create_person_distinct_id(self.teams[2].pk, "2", uuid2)

        delete_teams_data([self.teams[0].pk, self.teams[1].pk])

        self.assertEqual(self.select_remaining("person", "properties"), ['{"x": 2}'])
        self.assertEqual(self.select_remaining("person_distinct_id", "distinct_id"), ["2"])
示例#8
0
    def test_ip_capture(self) -> None:
        user = self._create_user("tim")
        create_person(team_id=self.team.pk, distinct_ids=["asdfasdfasdf"])

        process_event_ee(
            "asdfasdfasdf",
            "11.12.13.14",
            "",
            {"event": "$pageview", "properties": {"distinct_id": "asdfasdfasdf", "token": self.team.api_token,},},
            self.team.pk,
            now().isoformat(),
            now().isoformat(),
        )

        events = get_events()
        self.assertEqual(events[0]["properties"]["$ip"], "11.12.13.14")
示例#9
0
    def test_set_is_identified(self) -> None:
        distinct_id = "777"
        create_person(team_id=self.team.pk, distinct_ids=[distinct_id])
        person_before_event = get_person_by_distinct_id(team_id=self.team.pk, distinct_id=distinct_id)

        self.assertFalse(person_before_event["is_identified"])
        process_event_ee(
            distinct_id,
            "",
            "",
            {"event": "$identify", "properties": {},},
            self.team.pk,
            now().isoformat(),
            now().isoformat(),
        )

        person_after_event = get_person_by_distinct_id(team_id=self.team.pk, distinct_id=distinct_id)
        self.assertTrue(person_after_event["is_identified"])
示例#10
0
    def test_capture_no_element(self) -> None:
        user = self._create_user("tim")
        create_person(team_id=self.team.pk, distinct_ids=["asdfasdfasdf"])

        process_event_ee(
            "asdfasdfasdf",
            "",
            "",
            {"event": "$pageview", "properties": {"distinct_id": "asdfasdfasdf", "token": self.team.api_token,},},
            self.team.pk,
            now().isoformat(),
            now().isoformat(),
        )

        distinct_ids = [item["distinct_id"] for item in get_person_distinct_ids(team_id=self.team.pk)]
        self.assertEqual(distinct_ids, ["asdfasdfasdf"])
        events = get_events()
        self.assertEqual(events[0]["event"], "$pageview")
示例#11
0
def _create_person(**kwargs) -> Person:
    if kwargs.get("uuid"):
        uuid = str(kwargs.pop("uuid"))
    else:
        uuid = str(UUIDT())
    distinct_ids = kwargs.pop("distinct_ids")
    person = create_person(uuid=uuid, **kwargs)
    for id in distinct_ids:
        create_person_distinct_id(0, kwargs["team_id"], id, str(person))
    return Person(id=person, uuid=person)
示例#12
0
    def test_anonymized_ip_capture(self) -> None:
        self.team.anonymize_ips = True
        self.team.save()

        user = self._create_user("tim")
        create_person(team_id=self.team.pk, distinct_ids=["asdfasdfasdf"])

        process_event_ee(
            "asdfasdfasdf",
            "11.12.13.14",
            "",
            {"event": "$pageview", "properties": {"distinct_id": "asdfasdfasdf", "token": self.team.api_token,},},
            self.team.pk,
            now().isoformat(),
            now().isoformat(),
        )

        events = get_events()
        self.assertNotIn("$ip", events[0]["properties"].keys())
示例#13
0
    def test_distinct_with_anonymous_id(self) -> None:
        create_person(team_id=self.team.pk, distinct_ids=["anonymous_id"])

        process_event_ee(
            "new_distinct_id",
            "",
            "",
            {
                "event": "$identify",
                "properties": {
                    "$anon_distinct_id": "anonymous_id",
                    "token": self.team.api_token,
                    "distinct_id": "new_distinct_id",
                },
            },
            self.team.pk,
            now().isoformat(),
            now().isoformat(),
        )

        events = get_events()
        distinct_ids = [item["distinct_id"] for item in get_person_distinct_ids(team_id=self.team.pk)]
        self.assertEqual(len(events), 1)
        self.assertEqual(sorted(distinct_ids), sorted(["anonymous_id", "new_distinct_id"]))

        # check no errors as this call can happen multiple times
        process_event_ee(
            "new_distinct_id",
            "",
            "",
            {
                "event": "$identify",
                "properties": {
                    "$anon_distinct_id": "anonymous_id",
                    "token": self.team.api_token,
                    "distinct_id": "new_distinct_id",
                },
            },
            self.team.pk,
            now().isoformat(),
            now().isoformat(),
        )
示例#14
0
    def test_distinct_team_leakage(self) -> None:
        team2 = Team.objects.create()
        create_person(team_id=team2.pk, distinct_ids=["2"], properties={"email": "*****@*****.**"})
        create_person(team_id=self.team.pk, distinct_ids=["1", "2"])

        try:
            process_event_ee(
                "2",
                "",
                "",
                {
                    "event": "$identify",
                    "properties": {"$anon_distinct_id": "1", "token": self.team.api_token, "distinct_id": "2",},
                },
                self.team.pk,
                now().isoformat(),
                now().isoformat(),
            )
        except:
            pass

        ids = {self.team.pk: [], team2.pk: []}

        for pid in get_person_distinct_ids(team_id=self.team.pk):
            ids[pid["team_id"]].append(pid["distinct_id"])

        for pid in get_person_distinct_ids(team_id=team2.pk):
            ids[pid["team_id"]].append(pid["distinct_id"])

        self.assertEqual(sorted(ids[self.team.pk]), sorted(["1", "2"]))
        self.assertEqual(ids[team2.pk], ["2"])

        people1 = get_persons(team_id=self.team.pk)
        people2 = get_persons(team_id=team2.pk)

        self.assertEqual(len(people1), 1)
        self.assertEqual(len(people2), 1)
        self.assertEqual(people1[0]["team_id"], self.team.pk)
        self.assertEqual(people1[0]["properties"], {})
        self.assertEqual(people2[0]["team_id"], team2.pk)
        self.assertEqual(people2[0]["properties"], {"email": "*****@*****.**"})
示例#15
0
    def test_alias_merge_properties(self) -> None:
        create_person(
            distinct_ids=["old_distinct_id"],
            team_id=self.team.pk,
            properties={"key_on_both": "old value both", "key_on_old": "old value"},
        )

        create_person(
            distinct_ids=["new_distinct_id"],
            team_id=self.team.pk,
            properties={"key_on_both": "new value both", "key_on_new": "new value"},
        )

        process_event_ee(
            "new_distinct_id",
            "",
            "",
            {
                "event": "$create_alias",
                "properties": {
                    "distinct_id": "new_distinct_id",
                    "token": self.team.api_token,
                    "alias": "old_distinct_id",
                },
            },
            self.team.pk,
            now().isoformat(),
            now().isoformat(),
        )

        events = get_events()
        self.assertEqual(len(events), 1)

        distinct_ids = [item["distinct_id"] for item in get_person_distinct_ids(team_id=self.team.pk)]
        self.assertEqual(sorted(distinct_ids), sorted(["old_distinct_id", "new_distinct_id"]))

        persons = get_persons(team_id=self.team.pk)
        self.assertEqual(
            persons[0]["properties"],
            {"key_on_both": "new value both", "key_on_new": "new value", "key_on_old": "old value",},
        )