-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(surveys): allow to stop surveys once reached enough responses (#…
…21528) Co-authored-by: Neil Kakkar <neilkakkar@gmail.com>
- Loading branch information
1 parent
ba46cfb
commit 50360e7
Showing
31 changed files
with
410 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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
Binary file modified
BIN
+4.54 KB
(110%)
frontend/__snapshots__/scenes-app-surveys--new-survey--dark.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+4.55 KB
(110%)
frontend/__snapshots__/scenes-app-surveys--new-survey--light.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+2.73 KB
(110%)
frontend/__snapshots__/scenes-app-surveys--new-survey-appearance-section--dark.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+1.89 KB
(100%)
...tend/__snapshots__/scenes-app-surveys--new-survey-appearance-section--light.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+2.64 KB
(100%)
...nd/__snapshots__/scenes-app-surveys--new-survey-customisation-section--dark.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+2.59 KB
(100%)
...d/__snapshots__/scenes-app-surveys--new-survey-customisation-section--light.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+2.69 KB
(100%)
...end/__snapshots__/scenes-app-surveys--new-survey-presentation-section--dark.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+2.56 KB
(100%)
...nd/__snapshots__/scenes-app-surveys--new-survey-presentation-section--light.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+2.63 KB
(100%)
frontend/__snapshots__/scenes-app-surveys--new-survey-targeting-section--dark.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+2.58 KB
(100%)
frontend/__snapshots__/scenes-app-surveys--new-survey-targeting-section--light.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# Generated by Django 4.1.13 on 2024-04-14 13:56 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
("posthog", "0405_team_heatmaps_opt_in"), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name="survey", | ||
name="responses_limit", | ||
field=models.PositiveIntegerField(null=True), | ||
), | ||
] |
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
from itertools import groupby | ||
from django.db.models import Q | ||
from django.utils import timezone | ||
from uuid import UUID | ||
from datetime import datetime | ||
|
||
from posthog.clickhouse.client.connection import Workload | ||
from posthog.client import sync_execute | ||
from posthog.models import Survey | ||
|
||
|
||
def _get_surveys_response_counts( | ||
surveys_ids: list[UUID], team_id: int, earliest_survey_creation_date: datetime | ||
) -> dict[str, int]: | ||
data = sync_execute( | ||
""" | ||
SELECT JSONExtractString(properties, '$survey_id') as survey_id, count() | ||
FROM events | ||
WHERE event = 'survey sent' | ||
AND team_id = %(team_id)s | ||
AND timestamp >= %(earliest_survey_creation_date)s | ||
AND survey_id in %(surveys_ids)s | ||
GROUP BY survey_id | ||
""", | ||
{ | ||
"surveys_ids": surveys_ids, | ||
"team_id": team_id, | ||
"earliest_survey_creation_date": earliest_survey_creation_date, | ||
}, | ||
workload=Workload.OFFLINE, | ||
) | ||
|
||
counts = {} | ||
for survey_id, count in data: | ||
counts[survey_id] = count | ||
return counts | ||
|
||
|
||
def _stop_survey_if_reached_limit(survey: Survey, responses_count: int) -> None: | ||
# Since the job might take a long time, the survey configuration could've been changed by the user | ||
# after we've queried it. | ||
survey.refresh_from_db() | ||
if survey.responses_limit is None or survey.end_date is not None: | ||
return | ||
|
||
if responses_count < survey.responses_limit: | ||
return | ||
|
||
survey.end_date = timezone.now() | ||
survey.responses_limit = None | ||
survey.save(update_fields=["end_date", "responses_limit"]) | ||
|
||
|
||
def stop_surveys_reached_target() -> None: | ||
all_surveys = Survey.objects.exclude(Q(responses_limit__isnull=True) | Q(end_date__isnull=False)).only( | ||
"id", "responses_limit", "team_id", "created_at" | ||
) | ||
|
||
all_surveys_sorted = sorted(all_surveys, key=lambda survey: survey.team_id) | ||
for team_id, team_surveys in groupby(all_surveys_sorted, lambda survey: survey.team_id): | ||
team_surveys_list = list(team_surveys) | ||
surveys_ids = [survey.id for survey in team_surveys_list] | ||
earliest_survey_creation_date = min([survey.created_at for survey in team_surveys_list]) | ||
|
||
response_counts = _get_surveys_response_counts(surveys_ids, team_id, earliest_survey_creation_date) | ||
for survey in team_surveys_list: | ||
survey_id = str(survey.id) | ||
if survey_id not in response_counts: | ||
continue | ||
|
||
_stop_survey_if_reached_limit(survey, response_counts[survey_id]) |
This file contains 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
Oops, something went wrong.