Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,25 @@ Below are the existing sink names and their corresponding object names (as neede
``CatalogPathway`` model and marks the pathway deleted in ``catalog_pathway``.
- ``PathwayEnrollmentSink`` - Listens for the ``post_save`` Django signal on the openedx-core
``PathwayEnrollment`` model. Object name: ``pathway_enrollment``
- ``StudentCompetencyStatusSink`` - Listens for the ``post_save`` Django signal on the openedx-core
``StudentCompetencyStatus`` model (a learner's mastery status for one competency). Object name:
``student_competency_status``
- ``StudentCompetencyCourseStatusSink`` - Listens for the ``post_save`` Django signal on the
openedx-core ``StudentCompetencyCriteriaGroupStatus`` model, course-level groups only (a learner's
mastery status for one competency in one course run, with the competency and course key). Object name:
``student_competency_course_status``
- ``UserRetirementSink`` - Listen for the ``USER_RETIRE_LMS_MISC`` Django signal and
remove the user PII information from ClickHouse. This is a special sink with no assigned object name.

The competency sinks need an openedx-core with competency-based education (the
``CompetencyTaxonomy`` model). Without it they stay inactive and ``TaxonomySink`` writes every
taxonomy's ``taxonomy_type`` as ``tags``. Their ``EVENT_SINK_CLICKHOUSE_MODEL_CONFIG`` keys are
``student_competency_status``, ``student_competency_course_status`` and ``competency_taxonomy``.

Apply the matching tutor-contrib-aspects ClickHouse migrations before upgrading to a version that
adds sink columns, such as ``taxonomy_type`` on ``taxonomy``. Sink inserts are positional CSV and
tolerate one bad row, so a single-row dump against the old schema is dropped without an error.

Commands
========

Expand Down
12 changes: 12 additions & 0 deletions src/platform_plugin_aspects/settings/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ def plugin_settings(settings):
"module": "openedx_tagging.models.base",
"model": "Taxonomy",
},
"competency_taxonomy": {
"module": "openedx_learning.models",
"model": "CompetencyTaxonomy",
},
"object_tag": {
"module": "openedx_tagging.models.base",
"model": "ObjectTag",
Expand All @@ -111,6 +115,14 @@ def plugin_settings(settings):
"module": "openedx_catalog.models",
"model": "PathwayEnrollment",
},
"student_competency_status": {
"module": "openedx_learning.models",
"model": "StudentCompetencyStatus",
},
"student_competency_course_status": {
"module": "openedx_learning.models",
"model": "StudentCompetencyCriteriaGroupStatus",
},
}

# This is on by default here, off by default in tutor-contrib-aspects until we only
Expand Down
103 changes: 102 additions & 1 deletion src/platform_plugin_aspects/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
ExternalIdSink,
ObjectTagSink,
PathwayEnrollmentSink,
StudentCompetencyCourseStatusSink,
StudentCompetencyStatusSink,
TagSink,
TaxonomySink,
UserProfileSink,
Expand Down Expand Up @@ -242,12 +244,21 @@ def on_taxonomy_saved( # pylint: disable=unused-argument # pragma: no cover
transaction.on_commit(lambda: on_taxonomy_saved(*args, **kwargs))


# Connect the ExternalId.post_save signal handler only if we have a model to attach to.
# Connect the Taxonomy.post_save signal handler only if we have a model to attach to.
# (prevents celery errors during tests)
_taxonomy = get_model("taxonomy")
if _taxonomy:
post_save.connect(on_taxonomy_saved_txn, sender=_taxonomy) # pragma: no cover

# A competency taxonomy is saved once, as openedx-core's CompetencyTaxonomy (a
# multi-table-inheritance child of Taxonomy), and Django sends that post_save with the
# child class only. This is the only signal that dumps a competency taxonomy.
_competency_taxonomy = get_model("competency_taxonomy")
if _competency_taxonomy:
post_save.connect( # pragma: no cover
on_taxonomy_saved_txn, sender=_competency_taxonomy
)


def on_object_tag_saved_txn(*args, **kwargs):
"""
Expand Down Expand Up @@ -408,3 +419,93 @@ def on_pathway_enrollment_saved( # pylint: disable=unused-argument # pragma: n
_pathway_enrollment = get_model("pathway_enrollment")
if _pathway_enrollment: # pragma: no cover
post_save.connect(on_pathway_enrollment_saved_txn, sender=_pathway_enrollment)


def on_student_competency_status_saved_txn(*args, **kwargs):
"""
Handle student_competency_status saves in the middle of a transaction.

openedx-core sends post_save for a raise before its transaction commits, so a
dump queued right away could read the old status. A rolled-back raise is never
queued. If queuing fails, the error is logged and the row needs a backfill.
"""

def on_student_competency_status_saved( # pylint: disable=unused-argument # pragma: no cover
sender, instance, **kwargs
):
"""
Receives post save signal and queues the dump job.
"""
# import here, because signal is registered at startup, but items in tasks are not yet able to be loaded
from platform_plugin_aspects.tasks import ( # pylint: disable=import-outside-toplevel
dump_data_to_clickhouse,
)

sink = StudentCompetencyStatusSink(None, None)
dump_data_to_clickhouse.delay(
sink_module=sink.__module__,
sink_name=sink.__class__.__name__,
object_id=str(instance.id),
)

transaction.on_commit(
lambda: on_student_competency_status_saved(*args, **kwargs), robust=True
)


# Connect the StudentCompetencyStatus.post_save signal handler only if we have a model to attach to.
# (prevents celery errors during tests)
_student_competency_status = get_model("student_competency_status")
if _student_competency_status: # pragma: no cover
post_save.connect(
on_student_competency_status_saved_txn, sender=_student_competency_status
)


def on_student_competency_course_status_saved_txn(*args, **kwargs):
"""
Handle student_competency_course_status saves in the middle of a transaction.

Deferred to the commit for the same reasons as student_competency_status saves.
Only rows the sink dumps (course-level groups) are queued.
"""

def on_student_competency_course_status_saved( # pylint: disable=unused-argument
sender, instance, **kwargs
):
"""
Receives post save signal and queues the dump job.
"""
# import here, because signal is registered at startup, but items in tasks are not yet able to be loaded
from platform_plugin_aspects.tasks import ( # pylint: disable=import-outside-toplevel
dump_data_to_clickhouse,
)

# The rollup saves every group level; checking the sink first spares the group
# lookup, and the broker message, on installs that don't sink these rows.
if not StudentCompetencyCourseStatusSink.is_enabled():
return
if not StudentCompetencyCourseStatusSink.is_course_level(instance):
return

sink = StudentCompetencyCourseStatusSink(None, None)
dump_data_to_clickhouse.delay(
sink_module=sink.__module__,
sink_name=sink.__class__.__name__,
object_id=str(instance.id),
)

transaction.on_commit(
lambda: on_student_competency_course_status_saved(*args, **kwargs),
robust=True,
)


# Connect the StudentCompetencyCriteriaGroupStatus (course status) post_save signal handler only if we have a
# model to attach to. (prevents celery errors during tests)
_student_competency_course_status = get_model("student_competency_course_status")
if _student_competency_course_status: # pragma: no cover
post_save.connect(
on_student_competency_course_status_saved_txn,
sender=_student_competency_course_status,
)
4 changes: 4 additions & 0 deletions src/platform_plugin_aspects/sinks/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
from .course_overview_sink import CourseOverviewSink, XBlockSink
from .external_id_sink import ExternalIdSink
from .pathway_enrollment_sink import PathwayEnrollmentSink
from .student_competency_status_sink import (
StudentCompetencyCourseStatusSink,
StudentCompetencyStatusSink,
)
from .tag_sink import ObjectTagSink, TagSink, TaxonomySink
from .user_profile_sink import UserProfileSink
from .user_retire_sink import UserRetirementSink
100 changes: 99 additions & 1 deletion src/platform_plugin_aspects/sinks/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from rest_framework import serializers

from platform_plugin_aspects.utils import (
get_competency_api,
get_model,
get_or_create_xapi_actor_id,
get_tags_for_block,
Expand Down Expand Up @@ -279,6 +280,85 @@ def get_actor_id(self, instance):
return get_or_create_xapi_actor_id(instance.user)


class StudentCompetencyStatusSerializer(
BaseSinkSerializer, serializers.ModelSerializer
):
"""
Serializer for the openedx-core StudentCompetencyStatus model.

``created`` is when the learner first got a status for the competency. openedx-core
only raises statuses today, so ``modified`` on a Demonstrated row is when they
demonstrated it. That is current behavior, not a promise: a later staff edit or
recalculation could move it.
"""

actor_id = serializers.SerializerMethodField()
taxonomy_id = serializers.IntegerField(source="tag.taxonomy_id")
status_name = serializers.CharField(source="status.status")

class Meta:
"""Meta class for the StudentCompetencyStatusSerializer"""

model = get_model("student_competency_status")
fields = [
"id",
"actor_id",
"tag_id",
"taxonomy_id",
"status_id",
"status_name",
"created",
"modified",
"dump_id",
"time_last_dumped",
]

def get_actor_id(self, instance):
"""Return the learner's xAPI actor id instead of any user identifier."""
return get_or_create_xapi_actor_id(instance.user)


class StudentCompetencyCourseStatusSerializer(
BaseSinkSerializer, serializers.ModelSerializer
):
"""
Serializer for the openedx-core StudentCompetencyCriteriaGroupStatus model.

Only course-level groups are sunk (see StudentCompetencyCourseStatusSink): a
row is the learner's status for one competency in one course run. The group's
competency and course key are denormalized so ClickHouse needs no criteria-group table.
"""

actor_id = serializers.SerializerMethodField()
tag_id = serializers.IntegerField(source="group.tag_id")
taxonomy_id = serializers.IntegerField(source="group.tag.taxonomy_id")
course_key = serializers.CharField(source="group.course.course_key")
status_name = serializers.CharField(source="status.status")

class Meta:
"""Meta class for the StudentCompetencyCourseStatusSerializer"""

model = get_model("student_competency_course_status")
fields = [
"id",
"actor_id",
"group_id",
"tag_id",
"taxonomy_id",
"course_key",
"status_id",
"status_name",
"created",
"modified",
"dump_id",
"time_last_dumped",
]

def get_actor_id(self, instance):
"""Return the learner's xAPI actor id instead of any user identifier."""
return get_or_create_xapi_actor_id(instance.user)


class TagSerializer(BaseSinkSerializer, serializers.ModelSerializer):
"""Serializer for the Tag model."""

Expand Down Expand Up @@ -329,7 +409,17 @@ def get_lineage(self, instance):


class TaxonomySerializer(BaseSinkSerializer, serializers.ModelSerializer):
"""Serializer for the Taxonomy model."""
"""
Serializer for the Taxonomy model.

``taxonomy_type`` is ``competency`` for a taxonomy backed by an openedx-core
CompetencyTaxonomy row and ``tags`` otherwise, computed the way the platform's
taxonomy REST serializer does it (openedx_tagging ADR 0013,
competency-taxonomy-detection). It is the last field
because sink inserts are positional CSV and the column was added last.
"""

taxonomy_type = serializers.SerializerMethodField()

class Meta:
"""Meta class for the TaxonomySerializer."""
Expand All @@ -340,4 +430,12 @@ class Meta:
"name",
"dump_id",
"time_last_dumped",
"taxonomy_type",
]

def get_taxonomy_type(self, instance):
"""Return ``competency`` or ``tags``, through openedx-core's public CBE API."""
competency_api = get_competency_api()
if competency_api and competency_api.is_competency_taxonomy(instance):
return "competency"
return "tags"
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
"""Student Competency Status sinks: per competency, and per competency and course"""

from platform_plugin_aspects.sinks.base_sink import ModelBaseSink
from platform_plugin_aspects.sinks.serializers import (
StudentCompetencyCourseStatusSerializer,
StudentCompetencyStatusSerializer,
)


class StudentCompetencyStatusSink(ModelBaseSink): # pylint: disable=abstract-method
"""
Sink for the openedx-core StudentCompetencyStatus model: a learner's current mastery status for one competency
"""

model = "student_competency_status"
unique_key = "id"
clickhouse_table_name = "student_competency_status"
timestamp_field = "time_last_dumped"
name = "Student Competency Status"
serializer_class = StudentCompetencyStatusSerializer

def get_queryset(self, start_pk=None):
return super().get_queryset(start_pk).select_related("user", "tag", "status")

def get_object(self, item_id):
return self.get_queryset().get(id=item_id)


class StudentCompetencyCourseStatusSink(
ModelBaseSink
): # pylint: disable=abstract-method
"""
Sink for openedx-core StudentCompetencyCriteriaGroupStatus rows of course-level groups.

A course-level group (one with a course run) holds the learner's status for one competency
in one course, the only place AttemptedNotDemonstrated is stored per competency and course.
Root and leaf group rows are not sunk: nothing in Aspects reads them, and they would
multiply the rows.
"""

model = "student_competency_course_status"
unique_key = "id"
clickhouse_table_name = "student_competency_course_status"
timestamp_field = "time_last_dumped"
name = "Student Competency Course Status"
serializer_class = StudentCompetencyCourseStatusSerializer

@staticmethod
def is_course_level(status):
"""Return True if ``status`` belongs to a course-level group, the only rows this sink dumps."""
return status.group.course_id is not None

def get_queryset(self, start_pk=None):
return (
super()
.get_queryset(start_pk)
.filter(group__course__isnull=False)
.select_related("user", "status", "group__tag", "group__course")
)

def get_object(self, item_id):
return self.get_queryset().get(id=item_id)
Loading
Loading