From 54a2bd39576c67097cde35b71fb958caea434460 Mon Sep 17 00:00:00 2001 From: Li Yi Yu Date: Thu, 30 Nov 2023 13:28:39 -0500 Subject: [PATCH] chore(surveys): do not double charge for survey sent events (#18698) * do not double charge for survey sent events * fix events and test * fix event count in test * fix tests --- .../test/__snapshots__/test_usage_report.ambr | 6 +++ posthog/tasks/test/test_usage_report.py | 49 +++++++++++++++++-- posthog/tasks/usage_report.py | 2 +- 3 files changed, 52 insertions(+), 5 deletions(-) diff --git a/posthog/tasks/test/__snapshots__/test_usage_report.ambr b/posthog/tasks/test/__snapshots__/test_usage_report.ambr index 9cabd193acff2..3cadd9faae952 100644 --- a/posthog/tasks/test/__snapshots__/test_usage_report.ambr +++ b/posthog/tasks/test/__snapshots__/test_usage_report.ambr @@ -15,6 +15,9 @@ FROM events WHERE timestamp between '2022-01-10 00:00:00' AND '2022-01-10 23:59:59' AND event != '$feature_flag_called' + AND event NOT IN ('survey sent', + 'survey shown', + 'survey dismissed') GROUP BY team_id ' --- @@ -196,6 +199,9 @@ FROM events WHERE timestamp between '2022-01-01 00:00:00' AND '2022-01-10 23:59:59' AND event != '$feature_flag_called' + AND event NOT IN ('survey sent', + 'survey shown', + 'survey dismissed') GROUP BY team_id ' --- diff --git a/posthog/tasks/test/test_usage_report.py b/posthog/tasks/test/test_usage_report.py index a10a16e17893a..a10d8146d4ac8 100644 --- a/posthog/tasks/test/test_usage_report.py +++ b/posthog/tasks/test/test_usage_report.py @@ -367,8 +367,8 @@ def _test_usage_report(self) -> List[dict]: "plugins_enabled": {"Installed and enabled": 1}, "instance_tag": "none", "event_count_lifetime": 55, - "event_count_in_period": 23, - "event_count_in_month": 43, + "event_count_in_period": 22, + "event_count_in_month": 42, "event_count_with_groups_in_period": 2, "recording_count_in_period": 5, "recording_count_total": 16, @@ -409,8 +409,8 @@ def _test_usage_report(self) -> List[dict]: "teams": { str(self.org_1_team_1.id): { "event_count_lifetime": 44, - "event_count_in_period": 13, - "event_count_in_month": 33, + "event_count_in_period": 12, + "event_count_in_month": 32, "event_count_with_groups_in_period": 2, "recording_count_in_period": 0, "recording_count_total": 0, @@ -984,6 +984,47 @@ def test_usage_report_survey_responses(self, billing_task_mock: MagicMock, posth assert org_2_report["teams"]["5"]["survey_responses_count_in_period"] == 1 assert org_2_report["teams"]["5"]["survey_responses_count_in_month"] == 7 + @patch("posthog.tasks.usage_report.Client") + @patch("posthog.tasks.usage_report.send_report_to_billing_service") + def test_survey_events_are_not_double_charged( + self, billing_task_mock: MagicMock, posthog_capture_mock: MagicMock + ) -> None: + self._setup_teams() + for i in range(5): + _create_event( + distinct_id="4", + event="survey sent", + properties={ + "$survey_id": "see22eep-o12-as124", + "$survey_response": "correct", + }, + timestamp=now() - relativedelta(hours=i), + team=self.org_1_team_1, + ) + _create_event( + distinct_id="4", + event="survey shown", + timestamp=now() - relativedelta(hours=i), + team=self.org_1_team_1, + ) + _create_event( + distinct_id="4", + event="survey dismissed", + timestamp=now() - relativedelta(hours=i), + team=self.org_1_team_1, + ) + flush_persons_and_events() + period = get_previous_day(at=now() + relativedelta(days=1)) + period_start, period_end = period + all_reports = _get_all_org_reports(period_start, period_end) + report = _get_full_org_usage_report_as_dict( + _get_full_org_usage_report(all_reports[str(self.org_1.id)], get_instance_metadata(period)) + ) + assert report["organization_name"] == "Org 1" + assert report["survey_responses_count_in_month"] == 5 + assert report["event_count_in_period"] == 0 + assert report["event_count_in_month"] == 0 + @freeze_time("2022-01-10T00:01:00Z") class TestExternalDataSyncUsageReport(ClickhouseDestroyTablesMixin, TestCase, ClickhouseTestMixin): diff --git a/posthog/tasks/usage_report.py b/posthog/tasks/usage_report.py index 3e8d4907d4f3f..a0f4f83ffb1a0 100644 --- a/posthog/tasks/usage_report.py +++ b/posthog/tasks/usage_report.py @@ -393,7 +393,7 @@ def get_teams_with_billable_event_count_in_period( f""" SELECT team_id, count({distinct_expression}) as count FROM events - WHERE timestamp between %(begin)s AND %(end)s AND event != '$feature_flag_called' + WHERE timestamp between %(begin)s AND %(end)s AND event != '$feature_flag_called' AND event NOT IN ('survey sent', 'survey shown', 'survey dismissed') GROUP BY team_id """, {"begin": begin, "end": end},