Skip to content

Commit

Permalink
handling no data in available response table
Browse files Browse the repository at this point in the history
  • Loading branch information
VineetBala-AOT committed Nov 30, 2023
1 parent 1be6e78 commit bd4931e
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"""updating_available_response_options
Revision ID: 812b1f67015a
Revises: e7fdf769e8ff
Create Date: 2023-11-30 09:50:55.874798
"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = '812b1f67015a'
down_revision = 'e7fdf769e8ff'
branch_labels = None
depends_on = None


def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.alter_column('available_response_option', 'request_key', type_=sa.Text())
op.alter_column('available_response_option', 'request_id', type_=sa.Text())
# ### end Alembic commands ###


def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.alter_column('available_response_option', 'request_key', type_=sa.String(length=100))
op.alter_column('available_response_option', 'request_id', type_=sa.String(length=20))
# ### end Alembic commands ###
36 changes: 28 additions & 8 deletions analytics-api/src/analytics_api/models/request_type_option.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,18 @@ def get_survey_result(
.group_by(ResponseTypeOptionModel.request_key, ResponseTypeOptionModel.value)
.subquery())

# Check if there are records in survey_response before executing the final query
if db.session.query(survey_response.c.request_key).first():
# Combine the data fetched above such that the result has a format as below
# - position: is a unique value for each question which helps to get the order of question on the survey
# - label: is the the survey question
# - value: user selected response for each question
# - count: number of time the same value is selected as a response to each question
survey_response_exists = db.session.query(survey_response.c.request_key).first()
available_response_exists = db.session.query(available_response.c.request_key).first()

# Combine the data fetched above such that the result has a format as below
# - position: is a unique value for each question which helps to get the order of question on the survey
# - label: is the the survey question
# - value: user selected response for each question
# - count: number of time the same value is selected as a response to each question

# Check if there are records in survey_response and available_response before executing the final query
# which fetches all the available responses along with the corresponding reponses.
if survey_response_exists and available_response_exists:
survey_result = (db.session.query((survey_question.c.position).label('position'),
(survey_question.c.label).label('question'),
func.json_agg(func.json_build_object(
Expand All @@ -82,9 +87,24 @@ def get_survey_result(
.outerjoin(available_response, survey_question.c.key == available_response.c.request_key)
.outerjoin(survey_response,
(available_response.c.value == survey_response.c.value) &
(available_response.c.request_key == survey_response.c.request_key))
(available_response.c.request_key == survey_response.c.request_key),
full=True)
.group_by(survey_question.c.position, survey_question.c.label))

return survey_result.all()
# Check if there are records in survey_response before executing the final query which fetches reponses
# even if the available_response table is not yet populated.
elif survey_response_exists:
survey_result = (db.session.query((survey_question.c.position).label('position'),
(survey_question.c.label).label('question'),
func.json_agg(func.json_build_object('value',
survey_response.c.value,
'count',
survey_response.c.response))
.label('result'))
.join(survey_response, survey_response.c.request_key == survey_question.c.key)
.group_by(survey_question.c.position, survey_question.c.label))

return survey_result.all()

return None # Return None indicating no records
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ const CommentTable = () => {
<>
{row.comments.map((comment) => (
<div style={{ paddingTop: '10px' }} key={comment.label}>
<MetLabel>{comment.label}:</MetLabel>
<MetLabel>{comment.label}</MetLabel>
<div style={{ paddingTop: '5px' }}>
<MetParagraph>{comment.text}</MetParagraph>
</div>
Expand Down

0 comments on commit bd4931e

Please sign in to comment.