Skip to content

Commit

Permalink
Fix for report setting on analytics (#1836)
Browse files Browse the repository at this point in the history
* updates on engagement publish

* update email template

* adding action drop down

* updated changes for User Management

* access user details page for users without a role

* updating variable name

* updating the schema

* updating as per review comments

* updating schema

* ETL and analytics changes for report setting

* adding changes for clone and delete

* fixing linting

* front end and etl changes for postion column

* update

* fix for report setting on analytics

* fixing lint
  • Loading branch information
VineetBala-AOT authored Jul 13, 2023
1 parent 57de8d6 commit 2a6f339
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 27 deletions.
17 changes: 9 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 @@ -2,7 +2,7 @@
Manages the option type questions (radio/checkbox) on a survey
"""
from sqlalchemy import func
from sqlalchemy import and_, func, or_
from sqlalchemy.sql.expression import true
from analytics_api.models.survey import Survey as SurveyModel
from analytics_api.models.response_type_option import ResponseTypeOption as ResponseTypeOptionModel
Expand All @@ -23,26 +23,27 @@ def get_survey_result(
):
"""Get the analytics survey id for an engagement id."""
analytics_survey_id = (db.session.query(SurveyModel.id)
.filter(SurveyModel.engagement_id == engagement_id)
.filter(SurveyModel.is_active == true())
.filter(and_(SurveyModel.engagement_id == engagement_id,
SurveyModel.is_active == true()))
.subquery())

# Get all the survey questions specific to a survey id which are in active status.
survey_question = (db.session.query(RequestTypeOption.position.label('position'),
RequestTypeOption.label.label('label'),
RequestTypeOption.request_id)
.filter(RequestTypeOption.survey_id.in_(analytics_survey_id))
.filter(RequestTypeOption.is_active == true())
.filter(RequestTypeOption.display == true())
.filter(and_(RequestTypeOption.survey_id.in_(analytics_survey_id),
RequestTypeOption.is_active == true(),
or_(RequestTypeOption.display == true(),
RequestTypeOption.display is None)))
.order_by(RequestTypeOption.position)
.subquery())

# Get all the survey responses with the counts for each response specific to a survey id which
# are in active status.
survey_response = (db.session.query(ResponseTypeOptionModel.request_id, ResponseTypeOptionModel.value,
func.count(ResponseTypeOptionModel.request_id).label('response'))
.filter(ResponseTypeOptionModel.survey_id.in_(analytics_survey_id))
.filter(ResponseTypeOptionModel.is_active == true())
.filter(and_(ResponseTypeOptionModel.survey_id.in_(analytics_survey_id),
ResponseTypeOptionModel.is_active == true()))
.group_by(ResponseTypeOptionModel.request_id, ResponseTypeOptionModel.value)
.subquery())

Expand Down
17 changes: 13 additions & 4 deletions met-api/src/met_api/models/report_setting.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,19 @@ def add_all_report_settings(cls, survey_id, report_settings: list, session=None)
return new_report_setting

@classmethod
def delete_report_settings(cls, survey_id, question_key):
def delete_report_settings(cls, survey_id, question_keys: list) -> ReportSetting:
"""Delete report setting by survey id and question key."""
db.session.query(ReportSetting).filter(ReportSetting.survey_id == survey_id,
ReportSetting.question_key == question_key).delete()
db.session\
.query(ReportSetting)\
.filter(ReportSetting.survey_id == survey_id,
ReportSetting.question_key.in_(question_keys))\
.delete(synchronize_session='fetch')
db.session.commit()
return survey_id, question_keys

return survey_id, question_key
@classmethod
def update_report_settings_bulk(cls, report_settings: list) -> list[ReportSetting]:
"""Save report settings."""
db.session.bulk_update_mappings(ReportSetting, report_settings)
db.session.commit()
return report_settings
30 changes: 15 additions & 15 deletions met-api/src/met_api/services/report_setting_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,25 +132,25 @@ def _delete_questions_removed_from_form(cls, survey_id, survey_question_keys):
# Loop through the data from report setting and delete any record which does not exist on
# survey form. This will happen if a existing survey question is deleted from the survey
report_settings = cls.get_report_setting(survey_id)
for report_setting in report_settings:
if report_setting['question_key'] not in survey_question_keys:
ReportSettingModel.delete_report_settings(survey_id, report_setting['question_key'])

report_setting_keys_to_delete = [report_setting['question_key'] for report_setting in report_settings
if report_setting['question_key'] not in survey_question_keys]
if len(report_setting_keys_to_delete) > 0:
ReportSettingModel.delete_report_settings(survey_id, report_setting_keys_to_delete)

return report_setting_keys_to_delete

@classmethod
def update_report_setting(cls, survey_id, new_report_settings):
"""Update report setting."""
survey = SurveyModel.find_by_id(survey_id)
if not survey:
raise KeyError(f'No survey found for {survey_id}')
for setting in new_report_settings:
report_setting_id = setting.get('id', None)
report_setting = ReportSettingModel.find_by_id(report_setting_id)
if not report_setting:
raise ValueError(f'No report setting found for {report_setting_id}')
if report_setting.survey_id != survey_id:
raise KeyError(f'Report setting {report_setting.id} does not belong to survey {survey_id}')

report_setting.display = setting.get('display', None)
report_setting.save()

return new_report_settings

report_settings_update_mapping = [{
'id': setting.get('id', None),
'display': setting.get('display', None)
} for setting in new_report_settings]

updated_report_settings = ReportSettingModel.update_report_settings_bulk(report_settings_update_mapping)
return updated_report_settings

0 comments on commit 2a6f339

Please sign in to comment.