Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Updated Learning Assistant History payload to return in ascending order #129

Merged
merged 4 commits into from
Nov 12, 2024
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
4 changes: 4 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ Change Log
Unreleased
**********

4.4.5 - 2024-11-12
******************
* Updated Learning Assistant History payload to return in ascending order

4.4.4 - 2024-11-06
******************
* Fixed Learning Assistant History endpoint
Expand Down
2 changes: 1 addition & 1 deletion learning_assistant/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
Plugin for a learning assistant backend, intended for use within edx-platform.
"""

__version__ = '4.4.4'
__version__ = '4.4.5'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you include a changelog entry?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


default_app_config = 'learning_assistant.apps.LearningAssistantConfig' # pylint: disable=invalid-name
8 changes: 6 additions & 2 deletions learning_assistant/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,10 @@ def get_message_history(courserun_key, user, message_count):

Returns a number of messages equal to the message_count value.
"""
message_history = LearningAssistantMessage.objects.filter(
course_id=courserun_key, user=user).order_by('-created')[:message_count]
# Explanation over the double reverse: This fetches the last message_count elements ordered by creating order DESC.
# Slicing the list in the model is an equivalent of adding LIMIT on the query.
# The result is the last chat messages for that user and course but in inversed order, so in order to flip them
# its first turn into a list and then reversed.
message_history = list(LearningAssistantMessage.objects.filter(
course_id=courserun_key, user=user).order_by('-created')[:message_count])[::-1]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can get the same affect by doing order_by('created'). Right now you're still sorting twice.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Discussed offline and the alternative would be less performant. I've added a comment with a justification on this.

return message_history
4 changes: 2 additions & 2 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,8 +365,8 @@ def test_get_message_history(self):

return_value = get_message_history(self.course_key, self.user, message_count)

expected_value = LearningAssistantMessage.objects.filter(
course_id=self.course_key, user=self.user).order_by('-created')[:message_count]
expected_value = list(LearningAssistantMessage.objects.filter(
course_id=self.course_key, user=self.user).order_by('-created')[:message_count])[::-1]

# Ensure same number of entries
self.assertEqual(len(return_value), len(expected_value))
Expand Down
30 changes: 21 additions & 9 deletions tests/test_views.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
Tests for the learning assistant views.
"""
import datetime
import json
import sys
from importlib import import_module
Expand Down Expand Up @@ -347,22 +348,33 @@ def test_learning_message_history_view_get(
course_id=self.course_id,
user=self.user,
role='staff',
content='Expected content of message',
content='Older message',
created=datetime.date(2024, 10, 1)
)
message_count = 1

LearningAssistantMessage.objects.create(
course_id=self.course_id,
user=self.user,
role='staff',
content='Newer message',
created=datetime.date(2024, 10, 3)
)

db_messages = LearningAssistantMessage.objects.all().order_by('created')
db_messages_count = len(db_messages)

mock_get_course_id.return_value = self.course_id
response = self.client.get(
reverse('message-history', kwargs={'course_run_id': self.course_id})+f'?message_count={message_count}',
reverse('message-history', kwargs={'course_run_id': self.course_id})+f'?message_count={db_messages_count}',
content_type='application/json'
)
data = response.data

# Ensure same number of entries
actual_message_count = LearningAssistantMessage.objects.count()
self.assertEqual(len(data), actual_message_count)
self.assertEqual(len(data), db_messages_count)

# Ensure values are as expected
actual_message = LearningAssistantMessage.objects.get(course_id=self.course_id)
self.assertEqual(data[0]['role'], actual_message.role)
self.assertEqual(data[0]['content'], actual_message.content)
self.assertEqual(data[0]['timestamp'], actual_message.created.isoformat())
for i, message in enumerate(data):
self.assertEqual(message['role'], db_messages[i].role)
self.assertEqual(message['content'], db_messages[i].content)
self.assertEqual(message['timestamp'], db_messages[i].created.isoformat())
Loading