From 83cb61919932dc26a0241381647e83ed365ed01c Mon Sep 17 00:00:00 2001 From: Nathan Moore Date: Fri, 15 Sep 2023 11:04:57 -0400 Subject: [PATCH 1/4] Adding participant demographic data from qualtrix response to raw completions spreadsheet --- gdrive/client.py | 32 ++++++++++++++++++++++++++++++-- gdrive/export_api.py | 13 +++++++++++++ gdrive/export_client.py | 10 +++++++++- 3 files changed, 52 insertions(+), 3 deletions(-) diff --git a/gdrive/client.py b/gdrive/client.py index 0c38710..75c3f53 100644 --- a/gdrive/client.py +++ b/gdrive/client.py @@ -166,11 +166,39 @@ def delete_file(id: str) -> None: service.files().delete(fileId=id, supportsAllDrives=True).execute() -def upload_participant(first, last, email, responseId, time, date): +def upload_participant( + first, + last, + email, + responseId, + time, + date, + ethnicity, + race, + gender, + age, + income, + skin_tone, +): """ Append participant data to spreadsheet """ - values = [[first, last, first + " " + last, email, responseId, time, date]] + values = [ + [ + first, + last, + first + " " + last, + email, + responseId, + time, + date, + ethnicity, + race, + gender, + income, + skin_tone, + ] + ] body = {"values": values} result = ( diff --git a/gdrive/export_api.py b/gdrive/export_api.py index 6367593..587b12e 100644 --- a/gdrive/export_api.py +++ b/gdrive/export_api.py @@ -71,6 +71,11 @@ async def survey_upload_response_task(request): log.info("Response found, beginning export.") + # By the time we get here, we can count on the response containing the demographic data + # as it is included in the Completed flow responses. Responses without complete status + # throws exception in get_qualtrics_response + survey_resp = response["response"] + if request.participant: participant = request.participant client.upload_participant( @@ -80,6 +85,14 @@ async def survey_upload_response_task(request): request.responseId, participant.time, participant.date, + survey_resp["ethnicity"], + ", ".join( + survey_resp["race"] + ), # Can have more than one value in a list + survey_resp["gender"], + survey_resp["age"], + survey_resp["income"], + survey_resp["skin_tone"], ) # call function that queries ES for all analytics entries (flow interactionId) with responseId diff --git a/gdrive/export_client.py b/gdrive/export_client.py index c9763bb..9874b8c 100644 --- a/gdrive/export_client.py +++ b/gdrive/export_client.py @@ -169,4 +169,12 @@ def get_qualtrics_response(surveyId: str, responseId: str): raise error.ExportError( f"No survey response found for responseId: {responseId}" ) - return r.json() + + resp = r.json() + + if resp["status"] != "Complete": + raise error.ExportError( + f"Cannot upload incomplete survery response to raw completions spreadsheet: {responseId}" + ) + + return resp From 0c5afd6b51219a806520304aac5180b1d98985ad Mon Sep 17 00:00:00 2001 From: Nathan Moore Date: Fri, 15 Sep 2023 13:22:52 -0400 Subject: [PATCH 2/4] Moving response status check out of get_qualtrics_response --- gdrive/export_api.py | 5 +++++ gdrive/export_client.py | 9 +-------- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/gdrive/export_api.py b/gdrive/export_api.py index 587b12e..f064288 100644 --- a/gdrive/export_api.py +++ b/gdrive/export_api.py @@ -71,6 +71,11 @@ async def survey_upload_response_task(request): log.info("Response found, beginning export.") + if response["status"] != "Complete": + raise error.ExportError( + f"Cannot upload incomplete survery response to raw completions spreadsheet: {request.responseId}" + ) + # By the time we get here, we can count on the response containing the demographic data # as it is included in the Completed flow responses. Responses without complete status # throws exception in get_qualtrics_response diff --git a/gdrive/export_client.py b/gdrive/export_client.py index 9874b8c..06ebd82 100644 --- a/gdrive/export_client.py +++ b/gdrive/export_client.py @@ -170,11 +170,4 @@ def get_qualtrics_response(surveyId: str, responseId: str): f"No survey response found for responseId: {responseId}" ) - resp = r.json() - - if resp["status"] != "Complete": - raise error.ExportError( - f"Cannot upload incomplete survery response to raw completions spreadsheet: {responseId}" - ) - - return resp + return r.json() From 5cf8e3acc6e1de3a32582893875c4b5d2628ef03 Mon Sep 17 00:00:00 2001 From: Nathan Moore Date: Fri, 15 Sep 2023 13:29:26 -0400 Subject: [PATCH 3/4] Missed updating survey-export endpoint documentation --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 10246a2..39a46c5 100644 --- a/README.md +++ b/README.md @@ -81,7 +81,9 @@ interactionId: #### Analytics + Survey Response Export Exports analytics in bulk that are associated with a survey response. Also uploads -participant data to google spread sheet. +participant data to google spread sheet. Data provided in the request body, as well +as demographic data collected from Qualtrix api will be uploaded. Only responses that +have a Complete status (user has completed the survey) will be uploaded to the google spead sheet. `POST /survey-export` From ac0c3b113ca1015a28363d45a2072a76e34d70ef Mon Sep 17 00:00:00 2001 From: Nathan Moore Date: Fri, 15 Sep 2023 13:48:10 -0400 Subject: [PATCH 4/4] Another comment --- gdrive/export_api.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/gdrive/export_api.py b/gdrive/export_api.py index f064288..bba0158 100644 --- a/gdrive/export_api.py +++ b/gdrive/export_api.py @@ -50,7 +50,9 @@ async def survey_upload_response( request: SurveyParticipantModel, background_tasks: BackgroundTasks ): """ - Single endpoint that kicks off qualtrics response fetching and exporting + Single endpoint that kicks off qualtrics response fetching and exporting. Requests response data + from the Qualtrix API and uploads contact and demographic data to the google drive. Does not upload + responses without a complete status. """ background_tasks.add_task(survey_upload_response_task, request)