예제 #1
0
    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],
            ],
        )
예제 #2
0
    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],],
        )
예제 #3
0
    def retention(self, request: Request, *args: Any,
                  **kwargs: Any) -> Response:

        team = request.user.team
        filter = Filter(request=request)
        result = ClickhouseRetention().run(filter, team)
        return Response({"data": result})
예제 #4
0
 def calculate_retention(self, request: Request) -> Dict[str, Any]:
     team = self.team
     data = {}
     if not request.GET.get("date_from"):
         data.update({"date_from": "-11d"})
     filter = RetentionFilter(data=data, request=request, team=self.team)
     result = ClickhouseRetention().run(filter, team)
     return {"result": result}
예제 #5
0
    def retention(self, request: Request, *args: Any,
                  **kwargs: Any) -> Response:

        if not endpoint_enabled(CH_RETENTION_ENDPOINT,
                                request.user.distinct_id):
            result = super().calculate_retention(request)
            return Response({"data": result})

        team = request.user.team
        filter = Filter(request=request)
        result = ClickhouseRetention().run(filter, team)
        return Response({"data": result})
예제 #6
0
    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],
            ],
        )
예제 #7
0
    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),
            ],
        )
예제 #8
0
    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, "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"),
            [
                "Sun. 7 June",
                "Sun. 14 June",
                "Sun. 21 June",
                "Sun. 28 June",
                "Sun. 5 July",
                "Sun. 12 July",
                "Sun. 19 July",
            ],
        )