Skip to content

Commit

Permalink
survey updated with new survey id (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
saravanpa-aot authored Nov 10, 2023
1 parent 2e50cec commit 4f42ae4
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions met-etl/src/etl_project/services/ops/survey_etl_service.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
from dagster import Out, Output, op
from sqlalchemy import func
from datetime import datetime

from analytics_api.models.etlruncycle import EtlRunCycle as EtlRunCycleModel
from analytics_api.models.request_type_option import RequestTypeOption as EtlRequestTypeOption
from analytics_api.models.response_type_option import ResponseTypeOption as EtlResponseTypeOptionModel
from analytics_api.models.survey import Survey as EtlSurveyModel
from met_api.models.survey import Survey as MetSurveyModel
from analytics_api.utils.util import FormIoComponentType
from dagster import Out, Output, op
from datetime import datetime
from met_api.models.survey import Survey as MetSurveyModel
from sqlalchemy import func


# get the last run cycle id for survey etl
Expand Down Expand Up @@ -248,16 +247,22 @@ def survey_end_run_cycle(context, survey_new_runcycleid):


def _update_survey_responses_with_active_survey_id(session, survey):
etl_active_survey_id = session.query(EtlSurveyModel.id).filter(EtlSurveyModel.source_survey_id == survey.id,
EtlSurveyModel.is_active == True)
etl_active_survey_id = session.query(EtlSurveyModel.id).filter(
EtlSurveyModel.source_survey_id == survey.id, EtlSurveyModel.is_active == True).first()

subquery = (
session.query(EtlSurveyModel.id)
.filter(EtlSurveyModel.source_survey_id == survey.id)
.subquery()
)

session.query(EtlResponseTypeOptionModel).filter(EtlResponseTypeOptionModel.survey_id.in_(subquery)).update(
{'survey_id': etl_active_survey_id}, synchronize_session=False)
# Fetch response records
response_records = session.query(EtlResponseTypeOptionModel).filter(
EtlResponseTypeOptionModel.survey_id.in_(subquery)).all()

# Update each response record individually
for record in response_records:
session.query(EtlResponseTypeOptionModel).filter(EtlResponseTypeOptionModel.id == record.id).update(
{'survey_id': etl_active_survey_id}, synchronize_session='fetch')

session.commit()

0 comments on commit 4f42ae4

Please sign in to comment.