예제 #1
0
def get_person_ids_by_cohort_id(team: Team,
                                cohort_id: int,
                                limit: Optional[int] = None,
                                offset: Optional[int] = None):
    from ee.clickhouse.models.property import parse_prop_grouped_clauses

    filters = Filter(data={
        "properties": [{
            "key": "id",
            "value": cohort_id,
            "type": "cohort"
        }],
    })
    filter_query, filter_params = parse_prop_grouped_clauses(
        team_id=team.pk,
        property_group=filters.property_groups,
        table_name="pdi")

    results = sync_execute(
        GET_PERSON_IDS_BY_FILTER.format(
            distinct_query=filter_query,
            query="",
            GET_TEAM_PERSON_DISTINCT_IDS=get_team_distinct_ids_query(team.pk),
            offset="OFFSET %(offset)s" if offset else "",
            limit="ORDER BY _timestamp ASC LIMIT %(limit)s" if limit else "",
        ),
        {
            **filter_params, "team_id": team.pk,
            "offset": offset,
            "limit": limit
        },
    )

    return [str(row[0]) for row in results]
예제 #2
0
def get_person_ids_by_cohort_id(team: Team, cohort_id: int):
    from ee.clickhouse.models.property import parse_prop_clauses

    filters = Filter(data={"properties": [{"key": "id", "value": cohort_id, "type": "cohort"}],})
    filter_query, filter_params = parse_prop_clauses(filters.properties, team.pk, table_name="pid")

    results = sync_execute(GET_PERSON_IDS_BY_FILTER.format(distinct_query=filter_query, query=""), filter_params)

    return [str(row[0]) for row in results]
예제 #3
0
def recalculate_cohortpeople(cohort: Cohort) -> Optional[int]:
    cohort_filter, cohort_params = format_person_query(cohort,
                                                       0,
                                                       custom_match_field="id")

    before_count = sync_execute(GET_COHORT_SIZE_SQL, {
        "cohort_id": cohort.pk,
        "team_id": cohort.team_id
    })
    logger.info(
        "Recalculating cohortpeople starting",
        team_id=cohort.team_id,
        cohort_id=cohort.pk,
        size_before=before_count[0][0],
    )

    cohort_filter = GET_PERSON_IDS_BY_FILTER.format(
        distinct_query="AND " + cohort_filter,
        query="",
        offset="",
        limit="",
        GET_TEAM_PERSON_DISTINCT_IDS=get_team_distinct_ids_query(
            cohort.team_id),
    )

    insert_cohortpeople_sql = INSERT_PEOPLE_MATCHING_COHORT_ID_SQL.format(
        cohort_filter=cohort_filter)
    sync_execute(insert_cohortpeople_sql, {
        **cohort_params, "cohort_id": cohort.pk,
        "team_id": cohort.team_id
    })

    remove_cohortpeople_sql = REMOVE_PEOPLE_NOT_MATCHING_COHORT_ID_SQL.format(
        cohort_filter=cohort_filter)
    sync_execute(remove_cohortpeople_sql, {
        **cohort_params, "cohort_id": cohort.pk,
        "team_id": cohort.team_id
    })

    count_result = sync_execute(GET_COHORT_SIZE_SQL, {
        "cohort_id": cohort.pk,
        "team_id": cohort.team_id
    })

    if count_result and len(count_result) and len(count_result[0]):
        count = count_result[0][0]

        logger.info(
            "Recalculating cohortpeople done",
            team_id=cohort.team_id,
            cohort_id=cohort.pk,
            size_before=before_count[0][0],
            size=count,
        )
        return count

    return None
예제 #4
0
def recalculate_cohortpeople(cohort: Cohort):
    cohort_filter, cohort_params = format_person_query(cohort,
                                                       0,
                                                       custom_match_field="id")

    before_count = sync_execute(GET_COHORT_SIZE_SQL, {
        "cohort_id": cohort.pk,
        "team_id": cohort.team_id
    })
    logger.info(
        "Recalculating cohortpeople starting",
        team_id=cohort.team_id,
        cohort_id=cohort.pk,
        size_before=before_count[0][0],
    )

    cohort_filter = GET_PERSON_IDS_BY_FILTER.format(distinct_query="AND " +
                                                    cohort_filter,
                                                    query="")

    insert_cohortpeople_sql = INSERT_PEOPLE_MATCHING_COHORT_ID_SQL.format(
        cohort_filter=cohort_filter)
    sync_execute(insert_cohortpeople_sql, {
        **cohort_params, "cohort_id": cohort.pk,
        "team_id": cohort.team_id
    })

    remove_cohortpeople_sql = REMOVE_PEOPLE_NOT_MATCHING_COHORT_ID_SQL.format(
        cohort_filter=cohort_filter)
    sync_execute(remove_cohortpeople_sql, {
        **cohort_params, "cohort_id": cohort.pk,
        "team_id": cohort.team_id
    })

    count = sync_execute(GET_COHORT_SIZE_SQL, {
        "cohort_id": cohort.pk,
        "team_id": cohort.team_id
    })
    logger.info(
        "Recalculating cohortpeople done",
        team_id=cohort.team_id,
        cohort_id=cohort.pk,
        size_before=before_count[0][0],
        size=count[0][0],
    )
예제 #5
0
def recalculate_cohortpeople(cohort: Cohort):
    cohort_filter, cohort_params = format_person_query(cohort,
                                                       custom_match_field="id")

    cohort_filter = GET_PERSON_IDS_BY_FILTER.format(distinct_query="AND " +
                                                    cohort_filter,
                                                    query="")

    insert_cohortpeople_sql = INSERT_PEOPLE_MATCHING_COHORT_ID_SQL.format(
        cohort_filter=cohort_filter)
    sync_execute(insert_cohortpeople_sql, {
        **cohort_params, "cohort_id": cohort.pk,
        "team_id": cohort.team_id
    })

    remove_cohortpeople_sql = REMOVE_PEOPLE_NOT_MATCHING_COHORT_ID_SQL.format(
        cohort_filter=cohort_filter)
    sync_execute(remove_cohortpeople_sql, {
        **cohort_params, "cohort_id": cohort.pk,
        "team_id": cohort.team_id
    })