Skip to content

Commit 5521789

Browse files
Merge pull request #240 from edx/COSMO2-853-thread-update-delete-instrumentation
feat: add forum telemetry for thread update and delete
2 parents 358748b + bb4c9e2 commit 5521789

1 file changed

Lines changed: 59 additions & 5 deletions

File tree

  • lms/djangoapps/discussion/rest_api

lms/djangoapps/discussion/rest_api/views.py

Lines changed: 59 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -806,17 +806,71 @@ def partial_update(self, request, thread_id):
806806
Implements the PATCH method for the instance endpoint as described in
807807
the class docstring.
808808
"""
809-
if request.content_type != MergePatchParser.media_type:
810-
raise UnsupportedMediaType(request.content_type)
811-
return Response(update_thread(request, thread_id, request.data))
809+
set_custom_attribute("forum.operation", "thread.update")
810+
set_custom_attribute("forum.entity_type", "thread")
811+
set_custom_attribute("forum.entity_id", thread_id)
812+
set_custom_attribute("forum.actor_id", str(getattr(request.user, "id", "")))
813+
814+
try:
815+
course_id = request.data.get("course_id") or get_course_id_from_thread_id(
816+
thread_id
817+
)
818+
set_custom_attribute("forum.course_id", str(course_id))
819+
820+
update_fields = [
821+
field for field, value in request.data.items() if value is not None
822+
]
823+
if update_fields:
824+
set_custom_attribute("forum.update_fields", ",".join(update_fields))
825+
826+
if request.data.get("type"):
827+
set_custom_attribute("forum.thread_type", request.data.get("type"))
828+
if request.data.get("topic_id"):
829+
set_custom_attribute(
830+
"forum.commentable_id", request.data.get("topic_id")
831+
)
832+
if request.data.get("group_id") is not None:
833+
set_custom_attribute(
834+
"forum.group_id", str(request.data.get("group_id"))
835+
)
836+
837+
if request.content_type != MergePatchParser.media_type:
838+
raise UnsupportedMediaType(request.content_type)
839+
840+
response = Response(update_thread(request, thread_id, request.data))
841+
set_custom_attribute("forum.result", "success")
842+
set_custom_attribute("forum.http_status", str(response.status_code))
843+
return response
844+
except Exception as exc:
845+
set_custom_attribute("forum.result", "error")
846+
set_custom_attribute("forum.http_status", str(status.HTTP_400_BAD_REQUEST))
847+
set_custom_attribute("forum.error_type", _discussion_error_type(exc))
848+
raise
812849

813850
def destroy(self, request, thread_id):
814851
"""
815852
Implements the DELETE method for the instance endpoint as described in
816853
the class docstring
817854
"""
818-
delete_thread(request, thread_id)
819-
return Response(status=204)
855+
set_custom_attribute("forum.operation", "thread.delete")
856+
set_custom_attribute("forum.entity_type", "thread")
857+
set_custom_attribute("forum.entity_id", thread_id)
858+
set_custom_attribute("forum.actor_id", str(getattr(request.user, "id", "")))
859+
860+
try:
861+
course_id = get_course_id_from_thread_id(thread_id)
862+
set_custom_attribute("forum.course_id", str(course_id))
863+
set_custom_attribute("forum.delete_mode", "soft")
864+
865+
delete_thread(request, thread_id)
866+
set_custom_attribute("forum.result", "success")
867+
set_custom_attribute("forum.http_status", str(status.HTTP_204_NO_CONTENT))
868+
return Response(status=204)
869+
except Exception as exc:
870+
set_custom_attribute("forum.result", "error")
871+
set_custom_attribute("forum.http_status", str(status.HTTP_400_BAD_REQUEST))
872+
set_custom_attribute("forum.error_type", _discussion_error_type(exc))
873+
raise
820874

821875

822876
class LearnerThreadView(APIView):

0 commit comments

Comments
 (0)