Skip to content

feat(flags): add support for matching static cohort membership #25942

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 60 commits into from
Nov 16, 2024

Conversation

dmarticus
Copy link
Contributor

Problem

This is a fast-follow to #25776. It adds support for matching flags that point to static cohorts.

The only scary thing here is that this method adds new DB access patterns because cohort matching isn't a flag match – just looks for distinct_ids that are in the necessary static cohort tables. As a result, this may be able to be surfaced in our matching order or priority... gonna think about it a bit.

Changes

  • new method to evaluate static cohorts (evaluate_static_cohort_filter)
  • new test methods to seed databases with the necessary data needed to evaluate static cohorts
  • tests for condition matching of static cohorts

Does this work well for both Cloud and self-hosted?

No impact yet, this isn't live

How did you test this code?

New tests

@dmarticus dmarticus changed the base branch from master to feat/dynamic-cohorts-rust October 31, 2024 22:34
Comment on lines +220 to 232
/// Evaluates all feature flags for the current matcher context.
///
/// ## Arguments
///
/// * `feature_flags` - The list of feature flags to evaluate.
/// * `person_property_overrides` - Any overrides for person properties.
/// * `group_property_overrides` - Any overrides for group properties.
/// * `hash_key_override` - Optional hash key overrides for experience continuity.
///
/// ## Returns
///
/// * `FlagsResponse` - The result containing flag evaluations and any errors.
pub async fn evaluate_all_feature_flags(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is an entrypoint method into this struct so added a bit more commentary on it.

Comment on lines 1142 to 1164
let query = r#"
WITH person AS (
SELECT posthog_person.id AS person_id
FROM posthog_person
JOIN posthog_persondistinctid
ON posthog_persondistinctid.person_id = posthog_person.id
WHERE
posthog_person.team_id = $1
AND posthog_persondistinctid.team_id = $1
AND posthog_persondistinctid.distinct_id = $2
LIMIT 1
),
cohort_membership AS (
SELECT c.cohort_id,
CASE WHEN pc.cohort_id IS NOT NULL THEN true ELSE false END AS is_member
FROM unnest($3::integer[]) AS c(cohort_id)
LEFT JOIN posthog_cohortpeople AS pc
ON pc.person_id = (SELECT person_id FROM person)
AND pc.cohort_id = c.cohort_id
)
SELECT cohort_id, is_member
FROM cohort_membership
"#;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This query has some extra sauce since I elected to actually parse out the data into the appropriate structures using postgres instead of Rust (it's an easy tuple of Ids and bools). I still need to join on the persondistinctid table, though, since I don't have that relationship at the time I need to evaluate this condition.

Copy link
Contributor

@oliverb123 oliverb123 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:shipit:

Base automatically changed from feat/dynamic-cohorts-rust to master November 15, 2024 23:44
@dmarticus dmarticus enabled auto-merge (squash) November 15, 2024 23:51
@dmarticus dmarticus merged commit f5a567f into master Nov 16, 2024
80 checks passed
@dmarticus dmarticus deleted the feat/static-cohorts-rust-with-caching branch November 16, 2024 00:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants