Skip to content

Commit

Permalink
fix: use submit_ids for flow submission counts
Browse files Browse the repository at this point in the history
  • Loading branch information
farmerpaul committed Dec 6, 2024
1 parent 6cdd97d commit 2008eac
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 28 deletions.
31 changes: 18 additions & 13 deletions src/apps/answers/crud/answers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1005,20 +1005,25 @@ async def get_activity_and_flow_ids_by_source_subject(self, source_subject_id: u

@staticmethod
def _query_submissions_by_subject(subject_column: InstrumentedAttribute, subject_id: uuid.UUID) -> Query:
query: Query = select(
case(
query: Query = (
select(
func.count(func.distinct(AnswerSchema.submit_id)).label("submission_count"),
case(
(
AnswerSchema.flow_history_id.isnot(None),
AnswerSchema.id_from_history_id(AnswerSchema.flow_history_id),
),
else_=AnswerSchema.id_from_history_id(AnswerSchema.activity_history_id),
).label("activity_id"),
(
AnswerSchema.flow_history_id.isnot(None),
AnswerSchema.id_from_history_id(AnswerSchema.flow_history_id),
),
else_=AnswerSchema.id_from_history_id(AnswerSchema.activity_history_id),
).label("activity_id"),
(
AnswerSchema.source_subject_id
if subject_column == AnswerSchema.target_subject_id
else AnswerSchema.target_subject_id
).label("subject_id"),
).where(subject_column == subject_id)
AnswerSchema.source_subject_id
if subject_column == AnswerSchema.target_subject_id
else AnswerSchema.target_subject_id
).label("subject_id"),
)
.where(subject_column == subject_id)
.group_by("activity_id", "subject_id")
)
return query

async def get_submissions_by_target_subject(self, target_subject_id: uuid.UUID) -> list[dict]:
Expand Down
32 changes: 17 additions & 15 deletions src/apps/answers/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -1920,25 +1920,27 @@ async def get_target_subject_ids_by_respondent_and_activity_or_flow(
respondent_subject_id, activity_or_flow_id
)

async def get_activity_and_flow_ids_by_target_subject(self, target_subject_id: uuid.UUID) -> list[uuid.UUID]:
async def get_activity_and_flow_ids_by_target_subject(self, target_subject_id: uuid.UUID) -> list[dict]:
"""
Get a list of activity and flow IDs based on answers submitted for a target subject
Get a list of activity and flow IDs based on answers submitted for a target subject.
Excludes answers whose source subject was soft-deleted.
The data returned is just a combined list of activity and flow IDs, without any
distinction between the two
distinction between the two.
"""
results = await AnswersCRUD(self.answer_session).get_activity_and_flow_ids_by_target_subject(target_subject_id)
existing_subject_ids = await self._filter_out_soft_deleted_subjects(results)
activity_ids = [result["activity_id"] for result in results if result["subject_id"] in existing_subject_ids]

return activity_ids

async def get_activity_and_flow_ids_by_source_subject(self, source_subject_id: uuid.UUID) -> list[uuid.UUID]:
async def get_activity_and_flow_ids_by_source_subject(self, source_subject_id: uuid.UUID) -> list[dict]:
"""
Get a list of activity and flow IDs based on answers submitted for a source subject
Get a list of activity and flow IDs based on answers submitted for a source subject.
Excludes answers whose target subject was soft-deleted.
The data returned is just a combined list of activity and flow IDs, without any
distinction between the two
distinction between the two.
"""
results = await AnswersCRUD(self.answer_session).get_activity_and_flow_ids_by_source_subject(source_subject_id)
existing_subject_ids = await self._filter_out_soft_deleted_subjects(results)
Expand All @@ -1948,7 +1950,7 @@ async def get_activity_and_flow_ids_by_source_subject(self, source_subject_id: u

async def _filter_out_soft_deleted_subjects(self, submissions: list[dict]) -> set[uuid.UUID]:
"""
Return subject_ids contained in given submissions array that correspond to soft-deleted subjects
Filter out submissions whose subject_id column corresponds to soft-deleted subjects
"""
subject_ids = set([activityOrFlow["subject_id"] for activityOrFlow in submissions])

Expand All @@ -1970,23 +1972,23 @@ async def get_submissions_by_subject(self, subject_id: uuid.UUID) -> Submissions

submissions_activity_count = SubmissionsActivityCountBySubject(subject_id=subject_id)

for activityOrFlow in submissions_target:
for activity_submissions in submissions_target:
activity_counters = submissions_activity_count.activities.setdefault(
uuid.UUID(activityOrFlow["activity_id"]), SubmissionsSubjectCounters()
uuid.UUID(activity_submissions["activity_id"]), SubmissionsSubjectCounters()
)
respondent_subject_id = activityOrFlow["subject_id"]
respondent_subject_id = activity_submissions["subject_id"]
if respondent_subject_id in existing_subject_ids:
activity_counters.respondents.add(respondent_subject_id)
activity_counters.subject_submissions_count += 1
activity_counters.subject_submissions_count += activity_submissions["submission_count"]

for activityOrFlow in submissions_respondent:
for activity_submissions in submissions_respondent:
activity_counters = submissions_activity_count.activities.setdefault(
uuid.UUID(activityOrFlow["activity_id"]), SubmissionsSubjectCounters()
uuid.UUID(activity_submissions["activity_id"]), SubmissionsSubjectCounters()
)
target_subject_id = activityOrFlow["subject_id"]
target_subject_id = activity_submissions["subject_id"]
if target_subject_id in existing_subject_ids:
activity_counters.subjects.add(target_subject_id)
activity_counters.respondent_submissions_count += 1
activity_counters.respondent_submissions_count += activity_submissions["submission_count"]

return submissions_activity_count

Expand Down

0 comments on commit 2008eac

Please sign in to comment.