feat(flags): add support for matching static cohort membership#25942
Merged
feat(flags): add support for matching static cohort membership#25942
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
dmarticus
commented
Nov 4, 2024
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( |
Contributor
Author
There was a problem hiding this comment.
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 | ||
| "#; |
Contributor
Author
There was a problem hiding this comment.
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.
…g into feat/dynamic-cohorts-rust
…stHog/posthog into feat/static-cohorts-rust-with-caching
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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