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

Conversation

rijuma
Copy link
Member

@rijuma rijuma commented Nov 8, 2024

We want the endpoint to return the chat history in ascending order by timestamp (creation datetime).

Copy link

github-actions bot commented Nov 8, 2024

Coverage report

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  learning_assistant
  __init__.py
  api.py
Project Total  

This report was generated by python-coverage-comment-action

@rijuma rijuma force-pushed the rijuma/fix-message-history-order branch 2 times, most recently from da175b6 to a430ccb Compare November 8, 2024 16:06
@rijuma rijuma force-pushed the rijuma/fix-message-history-order branch from a430ccb to cc51613 Compare November 8, 2024 16:11
@rijuma rijuma marked this pull request as ready for review November 8, 2024 16:12
@@ -236,6 +236,6 @@ def get(self, request, course_run_id):
user = request.user

message_count = int(request.GET.get('message_count', 50))
message_history = get_message_history(courserun_key, user, message_count)
message_history = reversed(list(get_message_history(courserun_key, user, message_count))) # Reversing order
Copy link
Member

Choose a reason for hiding this comment

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

instead of reversing it here, can we just change the ordering here?

course_id=courserun_key, user=user).order_by('-created')[:message_count]

Copy link
Member Author

Choose a reason for hiding this comment

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

We can. It was one or the other. I'll make the change.

Copy link
Member Author

Choose a reason for hiding this comment

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

Moved the reverse logic to the API call and updated corresponding tests.

@rijuma rijuma force-pushed the rijuma/fix-message-history-order branch 2 times, most recently from 2d310cd to bc5ac84 Compare November 8, 2024 21:09
@rijuma rijuma force-pushed the rijuma/fix-message-history-order branch from bc5ac84 to 2f93c7e Compare November 8, 2024 21:13
message_history = LearningAssistantMessage.objects.filter(
course_id=courserun_key, user=user).order_by('-created')[:message_count]
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.

@rijuma rijuma force-pushed the rijuma/fix-message-history-order branch 2 times, most recently from ecec467 to 20b1a74 Compare November 12, 2024 14:29
# 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.
# The alternative of getting the last message_count ordering the query in ascending created order is not possible
# since negative indexing is not supported.
Copy link
Member

Choose a reason for hiding this comment

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

Sorry to nit pick, but I still don't think this explains why we are limiting the number of messages prior to re-ordering them. It would be helpful to explain that the indexing is equivalent to the sql limit, which is more efficient than retrieving all messages and then processing that query set.

Copy link
Member Author

Choose a reason for hiding this comment

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

Just updated it. Let me know what you think.

@rijuma rijuma force-pushed the rijuma/fix-message-history-order branch from 20b1a74 to 4e1ac79 Compare November 12, 2024 15:09
@@ -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.

@rijuma rijuma merged commit 08fdad2 into main Nov 12, 2024
4 checks passed
@rijuma rijuma deleted the rijuma/fix-message-history-order branch November 12, 2024 16:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants