Skip to content

Commit

Permalink
fix(surveys): Survey responses data table uses start_date (#24407)
Browse files Browse the repository at this point in the history
Followup to #24384 , the data table in the surveys view still uses createdAt, change this to use start_date if its available.
Also changes the responses_count API to use start_date, if available.
  • Loading branch information
Phanatic authored Aug 15, 2024
1 parent 17ae6d1 commit 41db0aa
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 8 deletions.
5 changes: 3 additions & 2 deletions frontend/src/scenes/surveys/surveyLogic.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -910,7 +910,8 @@ export const surveyLogic = kea<surveyLogicType>([
if (survey.id === 'new') {
return null
}
const createdAt = (survey as Survey).created_at
const surveyWithResults = survey as Survey
const startDate = surveyWithResults.start_date || surveyWithResults.created_at
return {
kind: NodeKind.DataTableNode,
source: {
Expand All @@ -934,7 +935,7 @@ export const surveyLogic = kea<surveyLogicType>([
],
orderBy: ['timestamp DESC'],
where: [`event == 'survey sent'`],
after: createdAt,
after: startDate,
properties: [
{
type: PropertyFilterType.Event,
Expand Down
6 changes: 3 additions & 3 deletions posthog/api/survey.py
Original file line number Diff line number Diff line change
Expand Up @@ -557,8 +557,8 @@ def destroy(self, request: Request, *args: Any, **kwargs: Any) -> Response:

@action(methods=["GET"], detail=False)
def responses_count(self, request: request.Request, **kwargs):
earliest_survey_creation_date = Survey.objects.filter(team_id=self.team_id).aggregate(Min("created_at"))[
"created_at__min"
earliest_survey_start_date = Survey.objects.filter(team_id=self.team_id).aggregate(Min("start_date"))[
"start_date__min"
]
data = sync_execute(
f"""
Expand All @@ -567,7 +567,7 @@ def responses_count(self, request: request.Request, **kwargs):
WHERE event = 'survey sent' AND team_id = %(team_id)s AND timestamp >= %(timestamp)s
GROUP BY survey_id
""",
{"team_id": self.team_id, "timestamp": earliest_survey_creation_date},
{"team_id": self.team_id, "timestamp": earliest_survey_start_date},
)

counts = {}
Expand Down
12 changes: 12 additions & 0 deletions posthog/api/test/__snapshots__/test_survey.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,18 @@
GROUP BY survey_id
'''
# ---
# name: TestResponsesCount.test_responses_count_only_after_first_survey_started
'''
/* user_id:0 request:_snapshot_ */
SELECT JSONExtractString(properties, '$survey_id') as survey_id,
count()
FROM events
WHERE event = 'survey sent'
AND team_id = 2
AND timestamp >= '2024-04-25 14:40:09'
GROUP BY survey_id
'''
# ---
# name: TestSurveysAPIList.test_list_surveys
'''
SELECT COUNT(*) AS "__count"
Expand Down
6 changes: 3 additions & 3 deletions posthog/api/test/test_survey.py
Original file line number Diff line number Diff line change
Expand Up @@ -2685,7 +2685,7 @@ def test_responses_count(self):
}

earliest_survey = Survey.objects.create(team_id=self.team.id)
earliest_survey.created_at = datetime.now() - timedelta(days=101)
earliest_survey.start_date = datetime.now() - timedelta(days=101)
earliest_survey.save()

for survey_id, count in survey_counts.items():
Expand All @@ -2706,7 +2706,7 @@ def test_responses_count(self):

@snapshot_clickhouse_queries
@freeze_time("2024-05-01 14:40:09")
def test_responses_count_only_after_first_survey_created(self):
def test_responses_count_only_after_first_survey_started(self):
survey_counts = {
"d63bb580-01af-4819-aae5-edcf7ef2044f": 3,
"fe7c4b62-8fc9-401e-b483-e4ff98fd13d5": 6,
Expand All @@ -2719,7 +2719,7 @@ def test_responses_count_only_after_first_survey_created(self):
}

earliest_survey = Survey.objects.create(team_id=self.team.id)
earliest_survey.created_at = datetime.now() - timedelta(days=6)
earliest_survey.start_date = datetime.now() - timedelta(days=6)
earliest_survey.save()

for survey_id, count in survey_counts.items():
Expand Down

0 comments on commit 41db0aa

Please sign in to comment.