Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(surveys): add column with responses count #17623

Merged
merged 18 commits into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified frontend/__snapshots__/scenes-app-surveys--surveys-list.png
jurajmajerik marked this conversation as resolved.
Show resolved Hide resolved
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions frontend/src/lib/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,10 @@ class ApiRequest {
return this.projectsDetail(teamId).addPathComponent('surveys')
}

public surveysResponsesCount(teamId?: TeamType['id']): ApiRequest {
return this.projectsDetail(teamId).addPathComponent('surveys/responses_count')
}

public survey(id: Survey['id'], teamId?: TeamType['id']): ApiRequest {
return this.surveys(teamId).addPathComponent(id)
}
Expand Down Expand Up @@ -1440,6 +1444,9 @@ const api = {
async update(surveyId: Survey['id'], data: Partial<Survey>): Promise<Survey> {
return await new ApiRequest().survey(surveyId).update({ data })
},
async getResponsesCount(): Promise<{ [key: string]: number }> {
return await new ApiRequest().surveysResponsesCount().get()
},
},

dataWarehouseTables: {
Expand Down
14 changes: 6 additions & 8 deletions frontend/src/scenes/surveys/Surveys.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,12 @@ export function Surveys(): JSX.Element {
)
},
},
// TODO: add responses count later
jurajmajerik marked this conversation as resolved.
Show resolved Hide resolved
// {
// title: 'Responses',
// render: function RenderResponses() {
// // const responsesCount = getResponsesCount(survey)
// return <div>{0}</div>
// },
// },
{
title: 'Responses',
render: function RenderResponses(_, survey) {
return <div>{survey.responses_count}</div>
},
},
jurajmajerik marked this conversation as resolved.
Show resolved Hide resolved
{
dataIndex: 'type',
title: 'Type',
Expand Down
12 changes: 10 additions & 2 deletions frontend/src/scenes/surveys/surveysLogic.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ export const surveysLogic = kea<surveysLogicType>([
surveys: {
__default: [] as Survey[],
loadSurveys: async () => {
const response = await api.surveys.list()
return response.results
const responseSurveys = await api.surveys.list()
return responseSurveys.results
},
deleteSurvey: async (id) => {
await api.surveys.delete(id)
Expand All @@ -36,6 +36,13 @@ export const surveysLogic = kea<surveysLogicType>([
const updatedSurvey = await api.surveys.update(id, { ...updatePayload })
return values.surveys.map((survey) => (survey.id === id ? updatedSurvey : survey))
},
loadResponsesCount: async () => {
jurajmajerik marked this conversation as resolved.
Show resolved Hide resolved
const surveysResponsesCount = await api.surveys.getResponsesCount()
return values.surveys.map((survey) => ({
...survey,
responses_count: surveysResponsesCount[survey.id] || 0,
}))
},
},
})),
listeners(() => ({
Expand Down Expand Up @@ -72,5 +79,6 @@ export const surveysLogic = kea<surveysLogicType>([
}),
afterMount(async ({ actions }) => {
await actions.loadSurveys()
await actions.loadResponsesCount()
}),
])
1 change: 1 addition & 0 deletions frontend/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2083,6 +2083,7 @@ export interface Survey {
end_date: string | null
archived: boolean
remove_targeting_flag?: boolean
responses_count?: number
}

export enum SurveyType {
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 21 additions & 2 deletions posthog/api/survey.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@

from posthog.api.shared import UserBasicSerializer
from posthog.api.utils import get_token
from posthog.client import sync_execute
from posthog.exceptions import generate_exception_response
from posthog.models.feedback.survey import Survey
from rest_framework.response import Response
from rest_framework.decorators import action
from posthog.api.feature_flag import FeatureFlagSerializer, MinimalFeatureFlagSerializer
from posthog.api.routing import StructuredViewSetMixin
from rest_framework import serializers, viewsets
from rest_framework import serializers, viewsets, request
from rest_framework.permissions import IsAuthenticated
from rest_framework.request import Request
from rest_framework import status
Expand Down Expand Up @@ -138,7 +140,6 @@ def create(self, validated_data):
return super().create(validated_data)

def update(self, instance: Survey, validated_data):

if validated_data.get("remove_targeting_flag"):
if instance.targeting_flag:
instance.targeting_flag.delete()
Expand Down Expand Up @@ -207,6 +208,24 @@ def destroy(self, request: Request, *args: Any, **kwargs: Any) -> Response:

return super().destroy(request, *args, **kwargs)

@action(methods=["GET"], detail=False)
def responses_count(self, request: request.Request, **kwargs):
data = sync_execute(
f"""
SELECT JSONExtractString(properties, '$survey_id') as survey_id, count()
FROM events
WHERE event = 'survey sent' AND team_id = %(team_id)s
GROUP BY survey_id
""",
{"team_id": self.team_id},
)

counts = {}
for survey_id, count in data:
counts[survey_id] = count

return Response(counts)


class SurveyAPISerializer(serializers.ModelSerializer):
"""
Expand Down