Skip to content

Commit

Permalink
a different tack on diffing suspend data
Browse files Browse the repository at this point in the history
Attempt records now have a 'diffed' boolean field, recording whether
they have any cmi.suspend_data elements that might need to be diffed.
It's set to 'false' when elements with that key are saved.

This makes it much quicker to find attempts that need to be diffed, so
the task should be able to get to work much quicker.

see #133
  • Loading branch information
christianp committed Apr 7, 2021
1 parent 3904583 commit 115d4ca
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 1 deletion.
18 changes: 18 additions & 0 deletions numbas_lti/migrations/0065_attempt_diffed.py
@@ -0,0 +1,18 @@
# Generated by Django 2.2.13 on 2021-04-07 08:15

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('numbas_lti', '0064_auto_20210303_1050'),
]

operations = [
migrations.AddField(
model_name='attempt',
name='diffed',
field=models.BooleanField(default=False),
),
]
3 changes: 3 additions & 0 deletions numbas_lti/models.py
Expand Up @@ -502,6 +502,7 @@ class Attempt(models.Model):

deleted = models.BooleanField(default=False)
broken = models.BooleanField(default=False)
diffed = models.BooleanField(default=False)

all_data_received = models.BooleanField(default=False)

Expand Down Expand Up @@ -1107,6 +1108,8 @@ def diff_scormelements(attempt, key='cmi.suspend_data'):
e.save()
last = e
lastvalue = value
attempt.diffed = True
attempt.save()

def resolve_dependency_order(deps):
order = list(deps.keys())
Expand Down
7 changes: 7 additions & 0 deletions numbas_lti/save_scorm_data.py
Expand Up @@ -15,6 +15,7 @@ def save_scorm_data(attempt,batches):
unsaved_elements = []
question_scores_changed = set()
with transaction.atomic():
needs_diff = False
for id,elements in batches.items():
for element in elements:
time = timezone.make_aware(datetime.datetime.fromtimestamp(element['time']))
Expand Down Expand Up @@ -44,8 +45,14 @@ def save_scorm_data(attempt,batches):
unsaved_elements.append(element)
else:
raise e
if element['key'] == 'cmi.suspend_data':
needs_diff = True
done.append(id)

if needs_diff:
attempt.diffed = False
attempts.save(update_fields=('diffed',))

This comment has been minimized.

Copy link
@georgestagg

georgestagg Apr 8, 2021

Contributor

Should this line be attempt.save(...)?

This comment has been minimized.

Copy link
@christianp

christianp Apr 12, 2021

Author Member

yep!


for number in question_scores_changed:
attempt.update_question_score_info(number)
return done,unsaved_elements
2 changes: 1 addition & 1 deletion numbas_lti/tasks.py
Expand Up @@ -27,7 +27,7 @@ def attempt_report_outcome(attempt):

@periodic_task(crontab(minute='*'))
def diff_suspend_data():
attempts = Attempt.objects.filter(scormelements__key='cmi.suspend_data',scormelements__diff=None).annotate(n=Count('scormelements')).filter(n__gt=1)
attempts = Attempt.objects.filter(diffed=False)
MAX_TIME = 10
start = datetime.now()
if attempts.exists():
Expand Down

0 comments on commit 115d4ca

Please sign in to comment.