Skip to content

Commit

Permalink
Support for filtering by multiple assignments in get_annotations endp…
Browse files Browse the repository at this point in the history
…oint

Required by the LMS dashboards
  • Loading branch information
marcospri committed Jul 5, 2024
1 parent 76b0b26 commit 67bd2e0
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 15 deletions.
15 changes: 8 additions & 7 deletions h/services/bulk_api/lms_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def _annotation_query(
self,
groups: list[str],
h_userids: list[str] | None = None,
assignment_id: str | None = None,
assignment_ids: list[str] | None = None,
):
query = (
select(
Expand Down Expand Up @@ -77,10 +77,11 @@ def _annotation_query(
Group.authority_provided_id.in_(groups),
)
)
if assignment_id:
if assignment_ids:
query = query.where(
AnnotationMetadata.data["lms"]["assignment"]["resource_link_id"].astext
== assignment_id,
AnnotationMetadata.data["lms"]["assignment"][
"resource_link_id"
].astext.in_(assignment_ids)
)

if h_userids:
Expand All @@ -106,18 +107,18 @@ def get_annotation_counts(
groups: list[str],
group_by: CountsGroupBy,
h_userids: list[str] | None = None,
assignment_id: str | None = None,
assignment_ids: list[str] | None = None,
) -> list[AnnotationCounts]:
"""
Get basic stats per user for an LMS assignment.
:param groups: List of "authority_provided_id" to filter groups by.
:param group_by: By which column to aggregate the data.
:param h_userids: List of User.userid to filter annotations by
:param assignment_id: ID of the assignment to filter annotations by
:param assignment_ids: ID of the assignment to filter annotations by
"""
annos_query = self._annotation_query(
groups, h_userids=h_userids, assignment_id=assignment_id
groups, h_userids=h_userids, assignment_ids=assignment_ids
).cte("annotations")

# Alias some columns
Expand Down
12 changes: 8 additions & 4 deletions h/views/api/bulk/annotation_counts.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,14 @@
"h_userids": {
"$ref": "#/$defs/nonEmptyStringArray"
},
"assignment_id": {
"type": [
"string",
"null"
"assignment_ids": {
"anyOf": [
{
"ref": "#/$defs/nonEmptyStringArray"
},
{
"type": "null"
}
]
}
},
Expand Down
2 changes: 1 addition & 1 deletion h/views/api/bulk/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def get_annotation_counts(request):
stats = request.find_service(BulkLMSStatsService).get_annotation_counts(
group_by=CountsGroupBy[data["group_by"].upper()],
groups=query_filter["groups"],
assignment_id=query_filter.get("assignment_id"),
assignment_ids=query_filter.get("assignment_ids"),
h_userids=query_filter.get("h_userids"),
)

Expand Down
2 changes: 1 addition & 1 deletion tests/unit/h/services/bulk_api/lms_stats_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def test_get_annotation_counts_by_user(
):
stats = svc.get_annotation_counts(
groups=[group.authority_provided_id],
assignment_id="ASSIGNMENT_ID",
assignment_ids=["ASSIGNMENT_ID"],
group_by=CountsGroupBy.USER,
)

Expand Down
4 changes: 2 additions & 2 deletions tests/unit/h/views/api/bulk/stats_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def test_get_annotation_counts(
bulk_stats_service.get_annotation_counts.assert_called_once_with(
group_by=CountsGroupBy.USER,
groups=assignment_request["filter"]["groups"],
assignment_id=assignment_request["filter"]["assignment_id"],
assignment_ids=assignment_request["filter"]["assignment_ids"],
h_userids=assignment_request["filter"]["h_userids"],
)
return_data = [
Expand All @@ -63,7 +63,7 @@ def assignment_request(self, pyramid_request):
"filter": {
"groups": ["3a022b6c146dfd9df4ea8662178eac"],
"h_userids": ["acc:user@authority"],
"assignment_id": "ASSIGNMENT_ID",
"assignment_ids": ["ASSIGNMENT_ID"],
},
}

Expand Down

0 comments on commit 67bd2e0

Please sign in to comment.