Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: refactor tracking log event emissions
Browse files Browse the repository at this point in the history
Ian2012 committed Aug 23, 2023
1 parent 2a8e44c commit ed45d2d
Showing 2 changed files with 20 additions and 15 deletions.
31 changes: 18 additions & 13 deletions completion/models.py
Original file line number Diff line number Diff line change
@@ -120,10 +120,13 @@ def submit_completion(self, user, block_key, completion):
block_key=block_key,
)
is_new = False

if not is_new and obj.completion != completion:
obj.completion = completion
obj.full_clean()
obj.save(update_fields={'completion', 'modified'})

obj.emit_tracking_log()
else:
# If the feature is not enabled, this method should not be called.
# Error out with a RuntimeError.
@@ -132,19 +135,6 @@ def submit_completion(self, user, block_key, completion):
called when the feature is disabled."
)

tracker.emit(
str(BLOCK_COMPLETION_CHANGED_EVENT_TYPE),
{
'user_id': user.id,
'course_id': str(block_key.course_key),
'context_key': str(context_key),
'block_key': str(block_key),
'block_type': block_type,
'completion': completion,
'is_new': is_new,
}
)

return obj, is_new

@transaction.atomic()
@@ -340,3 +330,18 @@ class Meta:

def __unicode__(self):
return f'BlockCompletion: {self.user.username}, {self.context_key}, {self.block_key}: {self.completion}'

def emit_tracking_log(self):
"""
Emit a tracking log when a block completion is created or updated.
"""
tracker.emit(
BLOCK_COMPLETION_CHANGED_EVENT_TYPE,
{
'user_id': self.user.id,
'course_id': str(self.context_key),
'block_id': str(self.block_key),
'block_type': self.block_type,
'completion': self.completion,
}
)
4 changes: 2 additions & 2 deletions completion/tests/test_models.py
Original file line number Diff line number Diff line change
@@ -41,7 +41,7 @@ def setUp(self):
self.set_up_completion()

def test_changed_value(self):
with self.assertNumQueries(6): # Get, update, 2 * savepoints, 2 * exists checks
with self.assertNumQueries(7): # 2 * Get, update, 2 * savepoints, 2 * exists checks
completion, isnew = models.BlockCompletion.objects.submit_completion(
user=self.user,
block_key=self.block_key,
@@ -53,7 +53,7 @@ def test_changed_value(self):
self.assertEqual(models.BlockCompletion.objects.count(), 1)

def test_unchanged_value(self):
with self.assertNumQueries(3): # Get + 2 * savepoints
with self.assertNumQueries(4): # 2 * Get + 2 * savepoints
completion, isnew = models.BlockCompletion.objects.submit_completion(
user=self.user,
block_key=self.block_key,

0 comments on commit ed45d2d

Please sign in to comment.