-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Conversation
…g into feat/dynamic-cohorts-rust
…ht idea. Next up I will implement a version that stores the dependency graph as well so that we can only cache the relevant cohorts instead of caching and iterating through cohort
/// 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( |
There was a problem hiding this comment.
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.
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 | ||
"#; |
There was a problem hiding this comment.
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 Id
s and bool
s). 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.
…g into feat/dynamic-cohorts-rust
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
…stHog/posthog into feat/static-cohorts-rust-with-caching
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
evaluate_static_cohort_filter
)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