Ejemplo n.º 1
0
    def insert_users_by_list(self, items: List[str]) -> None:
        """
        Items can be distinct_id or email
        """
        batchsize = 1000
        use_clickhouse = is_ee_enabled()
        if use_clickhouse:
            from ee.clickhouse.models.cohort import insert_static_cohort
        try:
            cursor = connection.cursor()
            for i in range(0, len(items), batchsize):
                batch = items[i : i + batchsize]
                persons_query = (
                    Person.objects.filter(team_id=self.team_id)
                    .filter(Q(persondistinctid__distinct_id__in=batch) | Q(properties__email__in=batch))
                    .exclude(cohort__id=self.id)
                )
                if use_clickhouse:
                    insert_static_cohort([p for p in persons_query.values_list("uuid", flat=True)], self.pk, self.team)
                sql, params = persons_query.distinct("pk").only("pk").query.sql_with_params()
                query = UPDATE_QUERY.format(
                    cohort_id=self.pk,
                    values_query=sql.replace('FROM "posthog_person"', ', {} FROM "posthog_person"'.format(self.pk), 1,),
                )
                cursor.execute(query, params)

            self.is_calculating = False
            self.last_calculation = timezone.now()
            self.errors_calculating = 0
            self.save()
        except Exception:
            self.is_calculating = False
            self.errors_calculating = F("errors_calculating") + 1
            self.save()
            capture_exception()
Ejemplo n.º 2
0
    def insert_users_by_list(self, items: List[str]) -> None:
        """
        Items can be distinct_id or email
        Important! Does not insert into clickhouse
        """
        batchsize = 1000
        from ee.clickhouse.models.cohort import insert_static_cohort

        try:
            cursor = connection.cursor()
            for i in range(0, len(items), batchsize):
                batch = items[i:i + batchsize]
                persons_query = (Person.objects.filter(
                    team_id=self.team_id).filter(
                        Q(persondistinctid__team_id=self.team_id,
                          persondistinctid__distinct_id__in=batch)).exclude(
                              cohort__id=self.id))
                insert_static_cohort(
                    [p for p in persons_query.values_list("uuid", flat=True)],
                    self.pk, self.team)
                sql, params = persons_query.distinct("pk").only(
                    "pk").query.sql_with_params()
                query = UPDATE_QUERY.format(
                    cohort_id=self.pk,
                    values_query=sql.replace(
                        'FROM "posthog_person"',
                        f', {self.pk}, {self.version or "NULL"} FROM "posthog_person"',
                        1,
                    ),
                )
                cursor.execute(query, params)
            self.is_calculating = False
            self.last_calculation = timezone.now()
            self.errors_calculating = 0
            self.save()
        except Exception as err:
            if settings.DEBUG:
                raise err
            self.is_calculating = False
            self.errors_calculating = F("errors_calculating") + 1
            self.save()
            capture_exception(err)
Ejemplo n.º 3
0
    def test_delete_cohorts(self):
        insert_static_cohort([uuid4()], 0, self.teams[0])
        insert_static_cohort([uuid4()], 1, self.teams[1])
        insert_static_cohort([uuid4()], 2, self.teams[2])

        self._insert_cohortpeople_row(self.teams[0], uuid4(), 3)
        self._insert_cohortpeople_row(self.teams[1], uuid4(), 4)
        self._insert_cohortpeople_row(self.teams[2], uuid4(), 5)

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

        self.assertEqual(self.select_remaining("person_static_cohort", "cohort_id"), [2])
        self.assertEqual(self.select_remaining("cohortpeople", "cohort_id"), [5])