Example #1
0
    def retrieve(self,
                 request: Request,
                 pk: Optional[int] = None,
                 *args: Any,
                 **kwargs: Any) -> Response:

        if not endpoint_enabled(CH_EVENT_ENDPOINT, request.user.distinct_id):
            return super().retrieve(request, pk)

        # TODO: implement getting elements
        team = request.user.team_set.get()
        query_result = sync_execute(
            SELECT_ONE_EVENT_SQL,
            {
                "team_id": team.pk,
                "event_id": pk
            },
        )
        result = ClickhouseEventSerializer(query_result[0], many=False).data

        if result["elements_hash"]:
            result["elements"] = get_elements_by_elements_hash(
                result["elements_hash"], team.pk)

        return Response(result)
Example #2
0
    def test_capture_new_person(self) -> None:
        user = self._create_user("tim")
        team_id = self.team.pk

        # TODO: with self.assertNumQueries(7):

        process_event(
            2,
            "",
            "",
            {
                "event": "$autocapture",
                "properties": {
                    "distinct_id": 2,
                    "token": self.team.api_token,
                    "$elements": [
                        {"tag_name": "a", "nth_child": 1, "nth_of_type": 2, "attr__class": "btn btn-sm",},
                        {"tag_name": "div", "nth_child": 1, "nth_of_type": 2, "$el_text": "💻",},
                    ],
                },
            },
            team_id,
            now().isoformat(),
            now().isoformat(),
        )

        process_event_ee(
            2,
            "",
            "",
            {
                "event": "$autocapture",
                "properties": {
                    "distinct_id": 2,
                    "token": self.team.api_token,
                    "$elements": [
                        {"tag_name": "a", "nth_child": 1, "nth_of_type": 2, "attr__class": "btn btn-sm",},
                        {"tag_name": "div", "nth_child": 1, "nth_of_type": 2, "$el_text": "💻",},
                    ],
                },
            },
            team_id,
            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, ["2"])
        events = get_events()

        self.assertEqual(events[0]["event"], "$autocapture")
        elements = get_elements_by_elements_hash(elements_hash=events[0]["elements_hash"], team_id=team_id)
        self.assertEqual(elements[0]["tag_name"], "a")
        self.assertEqual(elements[0]["attr_class"], ["btn", "btn-sm"])
        self.assertEqual(elements[1]["order"], 1)
        self.assertEqual(elements[1]["text"], "💻")
        self.assertEqual(events[0]["person"], "2")
Example #3
0
    def test_create_elements(self) -> None:
        elements_hash_1 = create_elements(
            event_uuid=uuid.uuid4(),
            team=self.team,
            elements=[
                Element(tag_name="a",
                        href="/a-url",
                        nth_child=1,
                        nth_of_type=0),
                Element(tag_name="button", nth_child=0, nth_of_type=0),
                Element(tag_name="div", nth_child=0, nth_of_type=0),
                Element(
                    tag_name="div",
                    nth_child=0,
                    nth_of_type=0,
                    attr_id="nested",
                ),
            ],
            use_cache=False,
        )

        self.assertEqual(len(get_all_elements()), 4)

        elements_hash_2 = create_elements(
            event_uuid=uuid.uuid4(),
            team=self.team,
            elements=[
                Element(tag_name="a",
                        href="/a-url",
                        nth_child=1,
                        nth_of_type=0),
                Element(tag_name="button", nth_child=0, nth_of_type=0),
                Element(tag_name="div", nth_child=0, nth_of_type=0),
                Element(
                    tag_name="div",
                    nth_child=0,
                    nth_of_type=0,
                    attr_id="nested",
                ),
            ],
            use_cache=False,
        )

        self.assertEqual(elements_hash_1, elements_hash_2)

        self.assertGreater(len(get_all_elements(final=False)), 4)
        self.assertEqual(len(get_all_elements(final=True)), 4)

        elements = get_elements_by_elements_hash(elements_hash=elements_hash_1,
                                                 team_id=self.team.pk)
        self.assertEqual(len(elements), 4)

        self.assertEqual(elements[0]["tag_name"], "a")
        self.assertEqual(elements[1]["tag_name"], "button")
        self.assertEqual(elements[2]["tag_name"], "div")
        self.assertEqual(elements[3]["tag_name"], "div")

        self.assertEqual(elements[0]["order"], 0)
        self.assertEqual(elements[1]["order"], 1)
        self.assertEqual(elements[2]["order"], 2)
        self.assertEqual(elements[3]["order"], 3)

        self.assertGreater(len(get_all_elements()), 4)
        sync_execute("OPTIMIZE TABLE elements FINAL")
        self.assertEqual(len(get_all_elements()), 4)