-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of https://github.com/edx/learning-assistant into…
… bilalqamar95/python-upgrade
- Loading branch information
Showing
16 changed files
with
738 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
learning_assistant/management/commands/retire_user_messages.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
"""" | ||
Django management command to remove LearningAssistantMessage objects | ||
if they have reached their expiration date. | ||
""" | ||
import logging | ||
import time | ||
from datetime import datetime, timedelta | ||
|
||
from django.conf import settings | ||
from django.core.management.base import BaseCommand | ||
|
||
from learning_assistant.models import LearningAssistantMessage | ||
|
||
log = logging.getLogger(__name__) | ||
|
||
|
||
class Command(BaseCommand): | ||
""" | ||
Django Management command to remove expired messages. | ||
""" | ||
|
||
def add_arguments(self, parser): | ||
parser.add_argument( | ||
'--batch_size', | ||
action='store', | ||
dest='batch_size', | ||
type=int, | ||
default=300, | ||
help='Maximum number of messages to remove. ' | ||
'This helps avoid overloading the database while updating large amount of data.' | ||
) | ||
parser.add_argument( | ||
'--sleep_time', | ||
action='store', | ||
dest='sleep_time', | ||
type=int, | ||
default=10, | ||
help='Sleep time in seconds between update of batches' | ||
) | ||
|
||
def handle(self, *args, **options): | ||
""" | ||
Management command entry point. | ||
""" | ||
batch_size = options['batch_size'] | ||
sleep_time = options['sleep_time'] | ||
|
||
expiry_date = datetime.now() - timedelta(days=getattr(settings, 'LEARNING_ASSISTANT_MESSAGES_EXPIRY', 30)) | ||
|
||
total_deleted = 0 | ||
deleted_count = None | ||
|
||
while deleted_count != 0: | ||
ids_to_delete = LearningAssistantMessage.objects.filter( | ||
created__lte=expiry_date | ||
).values_list('id', flat=True)[:batch_size] | ||
|
||
ids_to_delete = list(ids_to_delete) | ||
delete_queryset = LearningAssistantMessage.objects.filter( | ||
id__in=ids_to_delete | ||
) | ||
deleted_count, _ = delete_queryset.delete() | ||
|
||
total_deleted += deleted_count | ||
log.info(f'{deleted_count} messages deleted.') | ||
time.sleep(sleep_time) | ||
|
||
log.info(f'Job completed. {total_deleted} messages deleted.') |
68 changes: 68 additions & 0 deletions
68
learning_assistant/management/commands/tests/test_retire_user_messages.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
""" | ||
Tests for the retire_user_messages management command | ||
""" | ||
from datetime import datetime, timedelta | ||
|
||
from django.contrib.auth import get_user_model | ||
from django.core.management import call_command | ||
from django.test import TestCase | ||
|
||
from learning_assistant.models import LearningAssistantMessage | ||
|
||
User = get_user_model() | ||
|
||
|
||
class RetireUserMessagesTests(TestCase): | ||
""" | ||
Tests for the retire_user_messages command. | ||
""" | ||
|
||
def setUp(self): | ||
""" | ||
Build up test data | ||
""" | ||
super().setUp() | ||
self.user = User(username='tester', email='[email protected]') | ||
self.user.save() | ||
|
||
self.course_id = 'course-v1:edx+test+23' | ||
|
||
LearningAssistantMessage.objects.create( | ||
user=self.user, | ||
course_id=self.course_id, | ||
role='user', | ||
content='Hello', | ||
created=datetime.now() - timedelta(days=60) | ||
) | ||
|
||
LearningAssistantMessage.objects.create( | ||
user=self.user, | ||
course_id=self.course_id, | ||
role='user', | ||
content='Hello', | ||
created=datetime.now() - timedelta(days=2) | ||
) | ||
|
||
LearningAssistantMessage.objects.create( | ||
user=self.user, | ||
course_id=self.course_id, | ||
role='user', | ||
content='Hello', | ||
created=datetime.now() - timedelta(days=4) | ||
) | ||
|
||
def test_run_command(self): | ||
""" | ||
Run the management command | ||
""" | ||
current_messages = LearningAssistantMessage.objects.filter() | ||
self.assertEqual(len(current_messages), 3) | ||
|
||
call_command( | ||
'retire_user_messages', | ||
batch_size=2, | ||
sleep_time=0, | ||
) | ||
|
||
current_messages = LearningAssistantMessage.objects.filter() | ||
self.assertEqual(len(current_messages), 2) |
18 changes: 18 additions & 0 deletions
18
learning_assistant/migrations/0008_alter_learningassistantmessage_role.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# Generated by Django 4.2.14 on 2024-11-04 08:52 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('learning_assistant', '0007_learningassistantmessage'), | ||
] | ||
|
||
operations = [ | ||
migrations.AlterField( | ||
model_name='learningassistantmessage', | ||
name='role', | ||
field=models.CharField(choices=[('user', 'user'), ('assistant', 'assistant')], max_length=64), | ||
), | ||
] |
31 changes: 31 additions & 0 deletions
31
learning_assistant/migrations/0009_learningassistantaudittrial.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# Generated by Django 4.2.16 on 2024-11-14 13:55 | ||
|
||
from django.conf import settings | ||
from django.db import migrations, models | ||
import django.db.models.deletion | ||
import django.utils.timezone | ||
import model_utils.fields | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
migrations.swappable_dependency(settings.AUTH_USER_MODEL), | ||
('learning_assistant', '0008_alter_learningassistantmessage_role'), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name='LearningAssistantAuditTrial', | ||
fields=[ | ||
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), | ||
('created', model_utils.fields.AutoCreatedField(default=django.utils.timezone.now, editable=False, verbose_name='created')), | ||
('modified', model_utils.fields.AutoLastModifiedField(default=django.utils.timezone.now, editable=False, verbose_name='modified')), | ||
('start_date', models.DateTimeField()), | ||
('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL, unique=True)), | ||
], | ||
options={ | ||
'abstract': False, | ||
}, | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.