Skip to content

Commit

Permalink
add responses to the table
Browse files Browse the repository at this point in the history
  • Loading branch information
jurajmajerik committed Oct 19, 2023
1 parent 39e7d7a commit 6a200d3
Showing 1 changed file with 34 additions and 14 deletions.
48 changes: 34 additions & 14 deletions frontend/src/scenes/surveys/surveyLogic.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ export interface QuestionResultsReady {
[key: string]: boolean
}

const getResponseField = (i: number): string => (i === 0 ? '$survey_response' : `$survey_response_${i}`)

export const surveyLogic = kea<surveyLogicType>([
props({} as SurveyLogicProps),
key(({ id }) => id),
Expand Down Expand Up @@ -228,13 +230,12 @@ export const surveyLogic = kea<surveyLogicType>([
? dayjs(survey.end_date).add(1, 'day').format('YYYY-MM-DD')
: dayjs().add(1, 'day').format('YYYY-MM-DD')

const surveyResponseField =
questionIndex === 0 ? '$survey_response' : `$survey_response_${questionIndex}`

const query: HogQLQuery = {
kind: NodeKind.HogQLQuery,
query: `
SELECT JSONExtractString(properties, '${surveyResponseField}') AS survey_response, COUNT(survey_response)
SELECT
JSONExtractString(properties, '${getResponseField(questionIndex)}') AS survey_response,
COUNT(survey_response)
FROM events
WHERE event = 'survey sent'
AND properties.$survey_id = '${props.id}'
Expand Down Expand Up @@ -269,13 +270,12 @@ export const surveyLogic = kea<surveyLogicType>([
? dayjs(survey.end_date).add(1, 'day').format('YYYY-MM-DD')
: dayjs().add(1, 'day').format('YYYY-MM-DD')

const surveyResponseField =
questionIndex === 0 ? '$survey_response' : `$survey_response_${questionIndex}`

const query: HogQLQuery = {
kind: NodeKind.HogQLQuery,
query: `
SELECT JSONExtractString(properties, '${surveyResponseField}') AS survey_response, COUNT(survey_response)
SELECT
JSONExtractString(properties, '${getResponseField(questionIndex)}') AS survey_response,
COUNT(survey_response)
FROM events
WHERE event = 'survey sent'
AND properties.$survey_id = '${props.id}'
Expand Down Expand Up @@ -312,13 +312,12 @@ export const surveyLogic = kea<surveyLogicType>([
? dayjs(survey.end_date).add(1, 'day').format('YYYY-MM-DD')
: dayjs().add(1, 'day').format('YYYY-MM-DD')

const surveyResponseField =
questionIndex === 0 ? '$survey_response' : `$survey_response_${questionIndex}`

const query: HogQLQuery = {
kind: NodeKind.HogQLQuery,
query: `
SELECT count(), arrayJoin(JSONExtractArrayRaw(properties, '${surveyResponseField}')) AS choice
SELECT
count(),
arrayJoin(JSONExtractArrayRaw(properties, '${getResponseField(questionIndex)}')) AS choice
FROM events
WHERE event == 'survey sent'
AND properties.$survey_id == '${survey.id}'
Expand Down Expand Up @@ -599,7 +598,7 @@ export const surveyLogic = kea<surveyLogicType>([
],
dataTableQuery: [
(s) => [s.survey, s.surveyResponseProperty],
(survey, surveyResponseProperty): DataTableNode | null => {
(survey): DataTableNode | null => {
if (survey.id === 'new') {
return null
}
Expand All @@ -608,7 +607,28 @@ export const surveyLogic = kea<surveyLogicType>([
kind: NodeKind.DataTableNode,
source: {
kind: NodeKind.EventsQuery,
select: ['*', `properties.${surveyResponseProperty}`, 'timestamp', 'person'],
select: [
'*',
...survey.questions.map((q, i) => {
if (q.type === SurveyQuestionType.MultipleChoice) {
// Join array items into a string
return `coalesce(arrayStringConcat(JSONExtractArrayRaw(properties, '${getResponseField(
i
)}'), ', ')) -- ${q.question}`
} else if (q.type === SurveyQuestionType.Rating) {
// Decrement 1 due to 0 - 10 scale
return `coalesce(JSONExtractInt(properties, '${getResponseField(i)}') - 1) -- ${
q.question
}`
}

return `coalesce(JSONExtractString(properties, '${getResponseField(i)}')) -- ${
q.question
}`
}),
'timestamp',
'person',
],
orderBy: ['timestamp DESC'],
where: [`event == 'survey sent'`],
after: createdAt,
Expand Down

0 comments on commit 6a200d3

Please sign in to comment.