Ejemplo n.º 1
0
class TestClickhouseRetention(ClickhouseTestMixin, retention_test_factory(ClickhouseRetention, _create_event, Person.objects.create)):  # type: ignore

    # period filtering for clickhouse only
    def test_retention_period(self):
        Person.objects.create(
            team=self.team, distinct_ids=["person1", "alias1"], properties={"email": "*****@*****.**"},
        )
        Person.objects.create(
            team=self.team, distinct_ids=["person2"], properties={"email": "*****@*****.**"},
        )

        self._create_pageviews(
            [
                ("person1", self._date(0)),
                ("person1", self._date(1)),
                ("person1", self._date(2, month=1)),
                ("person1", self._date(10, month=1)),
                ("person1", self._date(15)),
                ("person1", self._date(18)),
                ("person2", self._date(13)),
            ]
        )

        filter = Filter(data={"date_from": self._date(0, hour=0), "period": "Week"})

        result = ClickhouseRetention().run(filter, self.team, total_intervals=7)

        self.assertEqual(
            self.pluck(result, "values", "count"),
            [[1, 0, 1, 1, 0, 1, 1], [0, 0, 0, 0, 0, 0], [2, 1, 0, 1, 1], [1, 0, 1, 1], [0, 0, 0], [1, 1], [1],],
        )
Ejemplo n.º 2
0
class TestClickhouseRetention(ClickhouseTestMixin,
                              retention_test_factory(ClickhouseRetention,
                                                     _create_event,
                                                     _create_person,
                                                     _create_action)
                              ):  # type: ignore
    pass
Ejemplo n.º 3
0
class TestClickhouseRetention(ClickhouseTestMixin,
                              retention_test_factory(ClickhouseRetention,
                                                     _create_event,
                                                     _create_person,
                                                     _create_action)
                              ):  # type: ignore
    def _create_groups_and_events(self):
        GroupTypeMapping.objects.create(team=self.team,
                                        group_type="organization",
                                        group_type_index=0)
        GroupTypeMapping.objects.create(team=self.team,
                                        group_type="company",
                                        group_type_index=1)

        create_group(team_id=self.team.pk,
                     group_type_index=0,
                     group_key="org:5",
                     properties={"industry": "finance"})
        create_group(team_id=self.team.pk,
                     group_type_index=0,
                     group_key="org:6",
                     properties={"industry": "technology"})

        create_group(team_id=self.team.pk,
                     group_type_index=1,
                     group_key="company:1",
                     properties={})
        create_group(team_id=self.team.pk,
                     group_type_index=1,
                     group_key="company:2",
                     properties={})

        Person.objects.create(team=self.team,
                              distinct_ids=["person1", "alias1"])
        Person.objects.create(team=self.team, distinct_ids=["person2"])
        Person.objects.create(team=self.team, distinct_ids=["person3"])

        self._create_events([
            ("person1", self._date(0), {
                "$group_0": "org:5",
                "$group_1": "company:1"
            }),
            ("person2", self._date(0), {
                "$group_0": "org:6"
            }),
            ("person3", self._date(0)),
            ("person1", self._date(1), {
                "$group_0": "org:5"
            }),
            ("person2", self._date(1), {
                "$group_0": "org:6"
            }),
            ("person1", self._date(7), {
                "$group_0": "org:5"
            }),
            ("person2", self._date(7), {
                "$group_0": "org:6"
            }),
            ("person1", self._date(14), {
                "$group_0": "org:5"
            }),
            ("person1", self._date(month=1, day=-6), {
                "$group_0": "org:5",
                "$group_1": "company:1"
            }),
            ("person2", self._date(month=1, day=-6), {
                "$group_0": "org:6"
            }),
            ("person2", self._date(month=1, day=1), {
                "$group_0": "org:6"
            }),
            ("person1", self._date(month=1, day=1), {
                "$group_0": "org:5"
            }),
            ("person2", self._date(month=1, day=15), {
                "$group_0": "org:6",
                "$group_1": "company:1"
            }),
        ])

    @snapshot_clickhouse_queries
    def test_groups_filtering(self):
        self._create_groups_and_events()

        result = ClickhouseRetention().run(
            RetentionFilter(
                data={
                    "date_to":
                    self._date(10, month=1, hour=0),
                    "period":
                    "Week",
                    "total_intervals":
                    7,
                    "properties": [{
                        "key": "industry",
                        "value": "technology",
                        "type": "group",
                        "group_type_index": 0
                    }],
                },
                team=self.team,
            ),
            self.team,
        )

        self.assertEqual(
            self.pluck(result, "values", "count"),
            [
                [1, 1, 0, 1, 1, 0, 1],
                [1, 0, 1, 1, 0, 1],
                [0, 0, 0, 0, 0],
                [1, 1, 0, 1],
                [1, 0, 1],
                [0, 0],
                [1],
            ],
        )

        result = ClickhouseRetention().run(
            RetentionFilter(
                data={
                    "date_to":
                    self._date(10, month=1, hour=0),
                    "period":
                    "Week",
                    "total_intervals":
                    7,
                    "properties": [{
                        "key": "industry",
                        "value": "",
                        "type": "group",
                        "group_type_index": 0,
                        "operator": "is_set"
                    }],
                },
                team=self.team,
            ),
            self.team,
        )

        self.assertEqual(
            self.pluck(result, "values", "count"),
            [
                [2, 2, 1, 2, 2, 0, 1],
                [2, 1, 2, 2, 0, 1],
                [1, 1, 1, 0, 0],
                [2, 2, 0, 1],
                [2, 0, 1],
                [0, 0],
                [1],
            ],
        )

    @snapshot_clickhouse_queries
    def test_groups_aggregating(self):
        self._create_groups_and_events()

        filter = RetentionFilter(
            data={
                "date_to": self._date(10, month=1, hour=0),
                "period": "Week",
                "total_intervals": 7,
                "aggregation_group_type_index": 0,
            },
            team=self.team,
        )

        result = ClickhouseRetention().run(filter, self.team)
        self.assertEqual(
            self.pluck(result, "values", "count"),
            [
                [2, 2, 1, 2, 2, 0, 1],
                [2, 1, 2, 2, 0, 1],
                [1, 1, 1, 0, 0],
                [2, 2, 0, 1],
                [2, 0, 1],
                [0, 0],
                [1],
            ],
        )

        filter = RetentionFilter(
            data={
                "date_to": self._date(10, month=1, hour=0),
                "period": "Week",
                "total_intervals": 7,
                "aggregation_group_type_index": 1,
            },
            team=self.team,
        )

        result = ClickhouseRetention().run(filter, self.team)
        self.assertEqual(
            self.pluck(result, "values", "count"),
            [
                [1, 0, 0, 1, 0, 0, 1],
                [0, 0, 0, 0, 0, 0],
                [0, 0, 0, 0, 0],
                [1, 0, 0, 1],
                [0, 0, 0],
                [0, 0],
                [1],
            ],
        )
Ejemplo n.º 4
0
class TestClickhouseRetention(ClickhouseTestMixin,
                              retention_test_factory(ClickhouseRetention,
                                                     _create_event,
                                                     Person.objects.create,
                                                     _create_action)
                              ):  # type: ignore

    # period filtering for clickhouse only because start of week is different
    def test_retention_period_weekly(self):
        Person.objects.create(
            team=self.team,
            distinct_ids=["person1", "alias1"],
            properties={"email": "*****@*****.**"},
        )
        Person.objects.create(
            team=self.team,
            distinct_ids=["person2"],
            properties={"email": "*****@*****.**"},
        )

        self._create_pageviews([
            ("person1", self._date(0)),
            ("person2", self._date(0)),
            ("person1", self._date(1)),
            ("person2", self._date(1)),
            ("person1", self._date(7)),
            ("person2", self._date(7)),
            ("person1", self._date(14)),
            ("person1", self._date(month=1, day=-6)),
            ("person2", self._date(month=1, day=-6)),
            ("person2", self._date(month=1, day=1)),
            ("person1", self._date(month=1, day=1)),
            ("person2", self._date(month=1, day=15)),
        ])

        filter = Filter(data={
            "date_to": self._date(10, month=1, hour=0),
            "period": "Week"
        })

        result = ClickhouseRetention().run(filter,
                                           self.team,
                                           total_intervals=7)

        self.assertEqual(
            self.pluck(result, "label"),
            [
                "Week 0", "Week 1", "Week 2", "Week 3", "Week 4", "Week 5",
                "Week 6"
            ],
        )

        self.assertEqual(
            self.pluck(result, "values", "count"),
            [
                [2, 2, 1, 2, 2, 0, 1],
                [2, 1, 2, 2, 0, 1],
                [1, 1, 1, 0, 0],
                [2, 2, 0, 1],
                [2, 0, 1],
                [0, 0],
                [1],
            ],
        )

        self.assertEqual(
            self.pluck(result, "date"),
            [
                datetime(2020, 6, 7, 0, tzinfo=pytz.UTC),
                datetime(2020, 6, 14, 0, tzinfo=pytz.UTC),
                datetime(2020, 6, 21, 0, tzinfo=pytz.UTC),
                datetime(2020, 6, 28, 0, tzinfo=pytz.UTC),
                datetime(2020, 7, 5, 0, tzinfo=pytz.UTC),
                datetime(2020, 7, 12, 0, tzinfo=pytz.UTC),
                datetime(2020, 7, 19, 0, tzinfo=pytz.UTC),
            ],
        )
Ejemplo n.º 5
0
class TestClickhouseRetention(ClickhouseTestMixin,
                              retention_test_factory(ClickhouseRetention,
                                                     _create_event,
                                                     Person.objects.create)
                              ):  # type: ignore

    # period filtering for clickhouse only because start of week is different
    def test_retention_period(self):
        Person.objects.create(
            team=self.team,
            distinct_ids=["person1", "alias1"],
            properties={"email": "*****@*****.**"},
        )
        Person.objects.create(
            team=self.team,
            distinct_ids=["person2"],
            properties={"email": "*****@*****.**"},
        )

        self._create_pageviews([
            ("person1", self._date(0)),
            ("person2", self._date(0)),
            ("person1", self._date(1)),
            ("person2", self._date(1)),
            ("person1", self._date(7)),
            ("person2", self._date(7)),
            ("person1", self._date(14)),
            ("person1", self._date(month=1, day=-6)),
            ("person2", self._date(month=1, day=-6)),
            ("person2", self._date(month=1, day=1)),
            ("person1", self._date(month=1, day=1)),
            ("person2", self._date(month=1, day=15)),
        ])

        filter = Filter(data={
            "date_from": self._date(0, hour=0),
            "period": "Week"
        })

        result = ClickhouseRetention().run(filter,
                                           self.team,
                                           total_intervals=7)

        self.assertEqual(
            self.pluck(result, "values", "count"),
            [
                [2, 2, 1, 2, 2, 0, 1],
                [2, 1, 2, 2, 0, 1],
                [1, 1, 1, 0, 0],
                [2, 2, 0, 1],
                [2, 0, 1],
                [0, 0],
                [1],
            ],
        )

        self.assertEqual(
            self.pluck(result, "date"),
            [
                "Sun. 7 June",
                "Sun. 14 June",
                "Sun. 21 June",
                "Sun. 28 June",
                "Sun. 5 July",
                "Sun. 12 July",
                "Sun. 19 July",
            ],
        )