Skip to content

Commit

Permalink
manual revert charging surveys targeting flags
Browse files Browse the repository at this point in the history
  • Loading branch information
liyiy committed Nov 15, 2023
1 parent 422e8f8 commit 06acb72
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
11 changes: 7 additions & 4 deletions posthog/api/decide.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

from posthog.api.geoip import get_geoip_properties
from posthog.api.utils import get_project_id, get_token
from posthog.api.survey import SURVEY_TARGETING_FLAG_PREFIX
from posthog.database_healthcheck import DATABASE_FOR_FLAG_MATCHING
from posthog.exceptions import RequestParsingError, generate_exception_response
from posthog.logging.timing import timed
Expand Down Expand Up @@ -268,10 +269,12 @@ def get_decide(request: HttpRequest):

if feature_flags:
# Billing analytics for decide requests with feature flags
# Sample no. of decide requests with feature flags
if settings.DECIDE_BILLING_SAMPLING_RATE and random() < settings.DECIDE_BILLING_SAMPLING_RATE:
count = int(1 / settings.DECIDE_BILLING_SAMPLING_RATE)
increment_request_count(team.pk, count)
# Don't count if all requests are for survey targeting flags only.
if not all(flag.startswith(SURVEY_TARGETING_FLAG_PREFIX) for flag in feature_flags.keys()):
# Sample no. of decide requests with feature flags
if settings.DECIDE_BILLING_SAMPLING_RATE and random() < settings.DECIDE_BILLING_SAMPLING_RATE:
count = int(1 / settings.DECIDE_BILLING_SAMPLING_RATE)
increment_request_count(team.pk, count)

else:
# no auth provided
Expand Down
9 changes: 3 additions & 6 deletions posthog/api/test/test_decide.py
Original file line number Diff line number Diff line change
Expand Up @@ -2843,7 +2843,7 @@ def test_decide_analytics_samples_dont_break_with_zero_sampling(self, *args):
self.assertEqual(client.hgetall(f"posthog:decide_requests:{self.team.pk}"), {})

@patch("posthog.models.feature_flag.flag_analytics.CACHE_BUCKET_SIZE", 10)
def test_decide_analytics_fires_with_survey_linked_and_targeting_flags(self, *args):
def test_decide_analytics_only_fires_with_non_survey_targeting_flags(self, *args):
ff = FeatureFlag.objects.create(
team=self.team,
rollout_percentage=50,
Expand Down Expand Up @@ -2905,7 +2905,7 @@ def test_decide_analytics_fires_with_survey_linked_and_targeting_flags(self, *ar
)

@patch("posthog.models.feature_flag.flag_analytics.CACHE_BUCKET_SIZE", 10)
def test_decide_analytics_fire_for_survey_targeting_flags(self, *args):
def test_decide_analytics_does_not_fire_for_survey_targeting_flags(self, *args):
FeatureFlag.objects.create(
team=self.team,
rollout_percentage=50,
Expand Down Expand Up @@ -2960,10 +2960,7 @@ def test_decide_analytics_fire_for_survey_targeting_flags(self, *args):

client = redis.get_client()
# check that single increment made it to redis
self.assertEqual(
client.hgetall(f"posthog:decide_requests:{self.team.pk}"),
{b"165192618": b"1"},
)
self.assertEqual(client.hgetall(f"posthog:decide_requests:{self.team.pk}"), {})

@patch("posthog.models.feature_flag.flag_analytics.CACHE_BUCKET_SIZE", 10)
def test_decide_new_capture_activation(self, *args):
Expand Down

0 comments on commit 06acb72

Please sign in to comment.