Skip to content

Commit

Permalink
fix(surveys): Limit all queries to date range (#17445)
Browse files Browse the repository at this point in the history
  • Loading branch information
neilkakkar authored Sep 15, 2023
1 parent 5b5d0d4 commit 98160ef
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 17 deletions.
2 changes: 1 addition & 1 deletion frontend/src/scenes/surveys/SurveyView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ function SurveyNPSResults({ survey }: { survey: Survey }): JSX.Element {
date_from: dayjs(survey.created_at).format('YYYY-MM-DD'),
date_to: survey.end_date
? dayjs(survey.end_date).format('YYYY-MM-DD')
: dayjs().format('YYYY-MM-DD'),
: dayjs().add(1, 'day').format('YYYY-MM-DD'),
},
series: [
{
Expand Down
35 changes: 19 additions & 16 deletions frontend/src/scenes/surveys/surveyLogic.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -254,17 +254,19 @@ export const surveyLogic = kea<surveyLogicType>([
if (surveyId === 'new') {
return null
}
const createdAt = (survey as Survey).created_at
const startDate = dayjs((survey as Survey).created_at).format('YYYY-MM-DD')
const endDate = survey.end_date
? dayjs(survey.end_date).format('YYYY-MM-DD')
: dayjs().add(1, 'day').format('YYYY-MM-DD')

const surveysShownHogqlQuery = `select count(distinct person.id) as 'survey shown' from events where event == 'survey shown' and properties.$survey_id == '${surveyId}'`
const surveysDismissedHogqlQuery = `select count(distinct person.id) as 'survey dismissed' from events where event == 'survey dismissed' and properties.$survey_id == '${surveyId}'`
const surveysShownHogqlQuery = `select count(distinct person.id) as 'survey shown' from events where event == 'survey shown' and properties.$survey_id == '${surveyId}' and timestamp >= '${startDate}' and timestamp <= '${endDate}' `
const surveysDismissedHogqlQuery = `select count(distinct person.id) as 'survey dismissed' from events where event == 'survey dismissed' and properties.$survey_id == '${surveyId}' and timestamp >= '${startDate}' and timestamp <= '${endDate}'`
return {
surveysShown: {
kind: NodeKind.DataTableNode,
source: {
kind: NodeKind.HogQLQuery,
query: surveysShownHogqlQuery,
filters: { dateRange: { date_from: dayjs(createdAt).format('YYYY-MM-DD') } },
},
showTimings: false,
},
Expand All @@ -273,7 +275,6 @@ export const surveyLogic = kea<surveyLogicType>([
source: {
kind: NodeKind.HogQLQuery,
query: surveysDismissedHogqlQuery,
filters: { dateRange: { date_from: dayjs(createdAt).format('YYYY-MM-DD') } },
},
showTimings: false,
},
Expand All @@ -286,15 +287,18 @@ export const surveyLogic = kea<surveyLogicType>([
if (survey.id === 'new') {
return null
}
const createdAt = (survey as Survey).created_at
const startDate = dayjs((survey as Survey).created_at).format('YYYY-MM-DD')
const endDate = survey.end_date
? dayjs(survey.end_date).format('YYYY-MM-DD')
: dayjs().add(1, 'day').format('YYYY-MM-DD')

return {
kind: NodeKind.InsightVizNode,
source: {
kind: NodeKind.TrendsQuery,
dateRange: {
date_from: dayjs(createdAt).format('YYYY-MM-DD'),
date_to: dayjs().format('YYYY-MM-DD'),
date_from: startDate,
date_to: endDate,
},
properties: [
{
Expand All @@ -318,10 +322,14 @@ export const surveyLogic = kea<surveyLogicType>([
if (survey.id === 'new') {
return null
}
const createdAt = (survey as Survey).created_at

const singleChoiceQuery = `select count(), properties.$survey_response as choice from events where event == 'survey sent' and properties.$survey_id == '${survey.id}' group by choice order by count() desc`
const multipleChoiceQuery = `select count(), arrayJoin(JSONExtractArrayRaw(properties, '$survey_response')) as choice from events where event == 'survey sent' and properties.$survey_id == '${survey.id}' group by choice order by count() desc`
const startDate = dayjs((survey as Survey).created_at).format('YYYY-MM-DD')
const endDate = survey.end_date
? dayjs(survey.end_date).format('YYYY-MM-DD')
: dayjs().add(1, 'day').format('YYYY-MM-DD')

const singleChoiceQuery = `select count(), properties.$survey_response as choice from events where event == 'survey sent' and properties.$survey_id == '${survey.id}' and timestamp >= '${startDate}' and timestamp <= '${endDate}' group by choice order by count() desc`
const multipleChoiceQuery = `select count(), arrayJoin(JSONExtractArrayRaw(properties, '$survey_response')) as choice from events where event == 'survey sent' and properties.$survey_id == '${survey.id}' and timestamp >= '${startDate}' and timestamp <= '${endDate}' group by choice order by count() desc`
return {
kind: NodeKind.DataTableNode,
source: {
Expand All @@ -330,11 +338,6 @@ export const surveyLogic = kea<surveyLogicType>([
survey.questions[0].type === SurveyQuestionType.SingleChoice
? singleChoiceQuery
: multipleChoiceQuery,
filters: {
dateRange: {
date_from: dayjs(createdAt).format('YYYY-MM-DD'),
},
},
},
showTimings: false,
}
Expand Down

0 comments on commit 98160ef

Please sign in to comment.