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 23af7dd commit 05008b5
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 27 deletions.
33 changes: 19 additions & 14 deletions src/apps/answers/crud/answers.py
Original file line number Diff line number Diff line change
Expand Up @@ -942,7 +942,7 @@ async def delete_by_ids(self, ids: list[uuid.UUID]):

async def get_target_subject_ids_by_respondent(
self, respondent_subject_id: uuid.UUID, activity_or_flow_id: uuid.UUID
):
) -> list[tuple[uuid.UUID, int]]:
query: Query = (
select(
AnswerSchema.target_subject_id,
Expand Down Expand Up @@ -1006,20 +1006,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
28 changes: 15 additions & 13 deletions src/apps/answers/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -1941,10 +1941,11 @@ async def get_target_subject_ids_by_respondent_and_activity_or_flow(

async def get_activity_and_flow_ids_by_target_subject(self, target_subject_id: uuid.UUID) -> list[uuid.UUID]:
"""
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)
Expand All @@ -1954,10 +1955,11 @@ async def get_activity_and_flow_ids_by_target_subject(self, target_subject_id: u

async def get_activity_and_flow_ids_by_source_subject(self, source_subject_id: uuid.UUID) -> list[uuid.UUID]:
"""
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 @@ -1967,7 +1969,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 @@ -1989,23 +1991,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 05008b5

Please sign in to comment.