From e5b24813453612eb42e409cb178419d58e89a077 Mon Sep 17 00:00:00 2001 From: cuom1999 Date: Wed, 1 Nov 2023 20:54:09 -0500 Subject: [PATCH] Make chat faster --- .../migrations/0015_room_last_msg_time.py | 33 ++++++ chat_box/models.py | 29 +++-- chat_box/views.py | 36 ++----- locale/vi/LC_MESSAGES/django.po | 102 +++++++++--------- .../html}/loading-page.html | 0 templates/base.html | 7 +- templates/chat/chat.html | 2 +- templates/chat/chat_js.html | 6 -- templates/chat/message.html | 6 +- templates/three-column-content.html | 16 ++- 10 files changed, 135 insertions(+), 102 deletions(-) create mode 100644 chat_box/migrations/0015_room_last_msg_time.py rename {templates => resources/html}/loading-page.html (100%) diff --git a/chat_box/migrations/0015_room_last_msg_time.py b/chat_box/migrations/0015_room_last_msg_time.py new file mode 100644 index 0000000..a32f21b --- /dev/null +++ b/chat_box/migrations/0015_room_last_msg_time.py @@ -0,0 +1,33 @@ +# Generated by Django 3.2.18 on 2023-11-02 01:41 + +from django.db import migrations, models + + +def migrate(apps, schema_editor): + Room = apps.get_model("chat_box", "Room") + Message = apps.get_model("chat_box", "Message") + + for room in Room.objects.all(): + messages = room.message_set + last_msg = messages.first() + if last_msg: + room.last_msg_time = last_msg.time + room.save() + + +class Migration(migrations.Migration): + + dependencies = [ + ("chat_box", "0014_userroom_unread_count"), + ] + + operations = [ + migrations.AddField( + model_name="room", + name="last_msg_time", + field=models.DateTimeField( + db_index=True, null=True, verbose_name="last seen" + ), + ), + migrations.RunPython(migrate, migrations.RunPython.noop, atomic=True), + ] diff --git a/chat_box/models.py b/chat_box/models.py index fb6de76..d9837e7 100644 --- a/chat_box/models.py +++ b/chat_box/models.py @@ -1,6 +1,7 @@ from django.db import models from django.db.models import CASCADE, Q from django.utils.translation import gettext_lazy as _ +from django.utils.functional import cached_property from judge.models.profile import Profile @@ -17,25 +18,39 @@ class Room(models.Model): user_two = models.ForeignKey( Profile, related_name="user_two", verbose_name="user 2", on_delete=CASCADE ) + last_msg_time = models.DateTimeField( + verbose_name=_("last seen"), null=True, db_index=True + ) class Meta: app_label = "chat_box" - @cache_wrapper(prefix="Rc") - def contain(self, profile): - return self.user_one == profile or self.user_two == profile + @cache_wrapper(prefix="Rinfo") + def _info(self): + return { + "user_ids": [self.user_one.id, self.user_two.id], + "last_message": self.message_set.first().body, + } + + @cached_property + def _cached_info(self): + return self._info() + + def contain(self, profile): + return profile.id in self._cached_info["user_ids"] - @cache_wrapper(prefix="Rou") def other_user(self, profile): return self.user_one if profile == self.user_two else self.user_two - @cache_wrapper(prefix="Rus") + def other_user_id(self, profile): + user_ids = self._cached_info["user_ids"] + return sum(user_ids) - profile.id + def users(self): return [self.user_one, self.user_two] - @cache_wrapper(prefix="Rlmb") def last_message_body(self): - return self.message_set.first().body + return self._cached_info["last_message"] class Message(models.Model): diff --git a/chat_box/views.py b/chat_box/views.py index c4e871f..b01994a 100644 --- a/chat_box/views.py +++ b/chat_box/views.py @@ -84,8 +84,8 @@ class ChatView(ListView): self.room_id = request_room self.messages = ( Message.objects.filter(hidden=False, room=self.room_id, id__lt=last_id) - .select_related("author", "author__user") - .defer("author__about", "author__user_script")[:page_size] + .select_related("author") + .only("body", "time", "author__rating", "author__display_rank")[:page_size] ) if not only_messages: return super().get(request, *args, **kwargs) @@ -204,7 +204,9 @@ def post_message(request): }, ) else: - Room.last_message_body.dirty(room) + Room._info.dirty(room) + room.last_msg_time = new_message.time + room.save() for user in room.users(): event.post( @@ -351,11 +353,11 @@ def get_online_status(profile, other_profile_ids, rooms=None): room = Room.objects.get(id=i["room"]) other_profile = room.other_user(profile) count[other_profile.id] = i["unread_count"] + rooms = Room.objects.filter(id__in=rooms) for room in rooms: - room = Room.objects.get(id=room) - other_profile = room.other_user(profile) - last_msg[other_profile.id] = room.last_message_body() - room_of_user[other_profile.id] = room.id + other_profile_id = room.other_user_id(profile) + last_msg[other_profile_id] = room.last_message_body() + room_of_user[other_profile_id] = room.id for other_profile in other_profiles: is_online = False @@ -389,9 +391,6 @@ def get_status_context(profile, include_ignored=False): recent_profile = ( Room.objects.filter(Q(user_one=profile) | Q(user_two=profile)) .annotate( - last_msg_time=Subquery( - Message.objects.filter(room=OuterRef("pk")).values("time")[:1] - ), other_user=Case( When(user_one=profile, then="user_two"), default="user_one", @@ -412,28 +411,15 @@ def get_status_context(profile, include_ignored=False): .values_list("id", flat=True) ) - all_user_status = ( - queryset.filter(last_access__gte=last_5_minutes) - .annotate(is_online=Case(default=True, output_field=BooleanField())) - .order_by("-rating") - .exclude(id__in=admin_list) - .exclude(id__in=recent_profile_ids) - .values_list("id", flat=True)[:30] - ) - return [ { - "title": "Recent", + "title": _("Recent"), "user_list": get_online_status(profile, recent_profile_ids, recent_rooms), }, { - "title": "Admin", + "title": _("Admin"), "user_list": get_online_status(profile, admin_list), }, - { - "title": "Other", - "user_list": get_online_status(profile, all_user_status), - }, ] diff --git a/locale/vi/LC_MESSAGES/django.po b/locale/vi/LC_MESSAGES/django.po index 4b9e6d3..338f920 100644 --- a/locale/vi/LC_MESSAGES/django.po +++ b/locale/vi/LC_MESSAGES/django.po @@ -2,7 +2,7 @@ msgid "" msgstr "" "Project-Id-Version: lqdoj2\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2023-11-01 13:59+0700\n" +"POT-Creation-Date: 2023-11-02 08:11+0700\n" "PO-Revision-Date: 2021-07-20 03:44\n" "Last-Translator: Icyene\n" "Language-Team: Vietnamese\n" @@ -18,23 +18,23 @@ msgstr "" "X-Crowdin-Project-ID: 466004\n" "X-Crowdin-File-ID: 5\n" -#: chat_box/models.py:42 chat_box/models.py:68 chat_box/models.py:84 +#: chat_box/models.py:54 chat_box/models.py:80 chat_box/models.py:96 #: judge/admin/interface.py:150 judge/models/contest.py:647 #: judge/models/contest.py:853 judge/models/course.py:115 #: judge/models/profile.py:433 judge/models/profile.py:511 msgid "user" msgstr "người dùng" -#: chat_box/models.py:44 judge/models/comment.py:44 +#: chat_box/models.py:56 judge/models/comment.py:44 #: judge/models/notification.py:17 msgid "posted time" msgstr "thời gian đăng" -#: chat_box/models.py:46 judge/models/comment.py:49 +#: chat_box/models.py:58 judge/models/comment.py:49 msgid "body of comment" msgstr "nội dung bình luận" -#: chat_box/models.py:72 +#: chat_box/models.py:84 msgid "last seen" msgstr "xem lần cuối" @@ -42,6 +42,22 @@ msgstr "xem lần cuối" msgid "LQDOJ Chat" msgstr "" +#: chat_box/views.py:417 +msgid "Recent" +msgstr "Gần đây" + +#: chat_box/views.py:421 templates/base.html:187 +#: templates/comments/content-list.html:78 +#: templates/contest/contest-list-tabs.html:4 +#: templates/contest/ranking-table.html:47 templates/internal/problem.html:62 +#: templates/organization/org-left-sidebar.html:12 +#: templates/problem/left-sidebar.html:6 +#: templates/problem/problem-list-tabs.html:6 +#: templates/submission/info-base.html:12 templates/submission/list.html:395 +#: templates/submission/submission-list-tabs.html:15 +msgid "Admin" +msgstr "Admin" + #: dmoj/settings.py:364 msgid "Vietnamese" msgstr "Tiếng Việt" @@ -70,7 +86,7 @@ msgstr "Đăng ký không thành công" msgid "Login" msgstr "Đăng nhập" -#: dmoj/urls.py:225 templates/base.html:217 +#: dmoj/urls.py:225 templates/base.html:111 #: templates/organization/org-left-sidebar.html:2 msgid "Home" msgstr "Trang chủ" @@ -179,7 +195,7 @@ msgstr "Tính toán lại kết quả" msgid "username" msgstr "tên đăng nhập" -#: judge/admin/contest.py:503 templates/base.html:345 +#: judge/admin/contest.py:503 templates/base.html:239 msgid "virtual" msgstr "ảo" @@ -242,7 +258,7 @@ msgid "Limits" msgstr "Giới hạn" #: judge/admin/problem.py:219 judge/admin/submission.py:351 -#: templates/base.html:261 templates/stats/tab.html:4 +#: templates/base.html:155 templates/stats/tab.html:4 #: templates/submission/list.html:347 msgid "Language" msgstr "Ngôn ngữ" @@ -3553,7 +3569,7 @@ msgid "Updated on site" msgstr "Được cập nhật trên web" #: judge/views/user.py:431 templates/admin/auth/user/change_form.html:14 -#: templates/admin/auth/user/change_form.html:17 templates/base.html:305 +#: templates/admin/auth/user/change_form.html:17 templates/base.html:199 #: templates/user/user-tabs.html:11 msgid "Edit profile" msgstr "Chỉnh sửa thông tin" @@ -3648,68 +3664,56 @@ msgstr "Chỉnh sửa thông tin" msgid "Rejudge" msgstr "Chấm lại" -#: templates/base.html:243 +#: templates/base.html:137 msgid "Chat" msgstr "Chat" -#: templates/base.html:253 +#: templates/base.html:147 msgid "Notification" msgstr "Thông báo" -#: templates/base.html:279 +#: templates/base.html:173 msgid "Dark Mode" msgstr "" -#: templates/base.html:289 +#: templates/base.html:183 msgid "Profile" msgstr "Trang cá nhân" -#: templates/base.html:293 templates/chat/chat_js.html:5 -#: templates/comments/content-list.html:78 -#: templates/contest/contest-list-tabs.html:4 -#: templates/contest/ranking-table.html:47 templates/internal/problem.html:62 -#: templates/organization/org-left-sidebar.html:12 -#: templates/problem/left-sidebar.html:6 -#: templates/problem/problem-list-tabs.html:6 -#: templates/submission/info-base.html:12 templates/submission/list.html:395 -#: templates/submission/submission-list-tabs.html:15 -msgid "Admin" -msgstr "Admin" - -#: templates/base.html:298 +#: templates/base.html:192 msgid "Internal" msgstr "Nội bộ" -#: templates/base.html:301 +#: templates/base.html:195 msgid "Stats" msgstr "Thống kê" -#: templates/base.html:314 +#: templates/base.html:208 msgid "Log out" msgstr "Đăng xuất" -#: templates/base.html:324 +#: templates/base.html:218 #: templates/registration/password_reset_complete.html:4 msgid "Log in" msgstr "Đăng nhập" -#: templates/base.html:325 +#: templates/base.html:219 msgid "Sign up" msgstr "Đăng ký" -#: templates/base.html:339 +#: templates/base.html:233 msgid "spectating" msgstr "đang theo dõi" -#: templates/base.html:351 +#: templates/base.html:245 msgid "Compete" msgstr "Thi" -#: templates/base.html:353 +#: templates/base.html:247 msgid "General" msgstr "Chung" -#: templates/base.html:360 +#: templates/base.html:254 msgid "This site works best with JavaScript enabled." msgstr "" @@ -3772,41 +3776,29 @@ msgstr "Thêm mới" msgid "No clarifications have been made at this time." msgstr "Không có thông báo nào." -#: templates/chat/chat.html:5 templates/chat/chat_js.html:545 +#: templates/chat/chat.html:5 templates/chat/chat_js.html:539 msgid "Chat Box" msgstr "Chat Box" -#: templates/chat/chat.html:69 templates/chat/chat_js.html:507 +#: templates/chat/chat.html:72 templates/chat/chat_js.html:501 #: templates/user/base-users-js.html:10 #: templates/user/base-users-two-col.html:19 msgid "Search by handle..." msgstr "Tìm kiếm theo tên..." -#: templates/chat/chat.html:88 +#: templates/chat/chat.html:91 msgid "Enter your message" msgstr "Nhập tin nhắn" -#: templates/chat/chat.html:89 +#: templates/chat/chat.html:92 msgid "Emoji" msgstr "" -#: templates/chat/chat_js.html:3 -msgid "Recent" -msgstr "Gần đây" - -#: templates/chat/chat_js.html:4 -msgid "Following" -msgstr "Bạn bè" - -#: templates/chat/chat_js.html:6 -msgid "Other" -msgstr "Thành viên khác" - -#: templates/chat/chat_js.html:124 +#: templates/chat/chat_js.html:118 msgid "New message(s)" msgstr "Tin nhắn mới" -#: templates/chat/chat_js.html:418 +#: templates/chat/chat_js.html:412 msgid "Mute this user and delete all messages?" msgstr "Mute người dùng này và xóa tất cả tin nhắn chung?" @@ -5969,6 +5961,12 @@ msgstr "Thông tin" msgid "Check all" msgstr "Chọn tất cả" +#~ msgid "Following" +#~ msgstr "Bạn bè" + +#~ msgid "Other" +#~ msgstr "Thành viên khác" + #~ msgid "Online Users" #~ msgstr "Trực tuyến" diff --git a/templates/loading-page.html b/resources/html/loading-page.html similarity index 100% rename from templates/loading-page.html rename to resources/html/loading-page.html diff --git a/templates/base.html b/templates/base.html index be290a7..c82368d 100644 --- a/templates/base.html +++ b/templates/base.html @@ -295,7 +295,6 @@ $(function () { $('img.unveil').unveil(200); }); - const loading_page = `{% include "loading-page.html" %}`; {% endcompress %} @@ -379,10 +378,10 @@ {% endif %} -
- {% block bodyend %}{% endblock %} +
+ {% block extra_js %}{% endblock %}
- + {% block bodyend %}{% endblock %} {% block footer %}
diff --git a/templates/chat/chat.html b/templates/chat/chat.html index c6963cb..8ad9e16 100644 --- a/templates/chat/chat.html +++ b/templates/chat/chat.html @@ -82,7 +82,7 @@ {% include 'chat/user_online_status.html' %}
- + diff --git a/templates/chat/chat_js.html b/templates/chat/chat_js.html index 9d728a4..78418c5 100644 --- a/templates/chat/chat_js.html +++ b/templates/chat/chat_js.html @@ -1,10 +1,4 @@ {% endblock %} @@ -141,11 +146,14 @@ {% block after_posts %}{% endblock %} {% endblock %} +{% block extra_js %} + {% block three_col_js %}{% endblock %} +{% endblock %} + {% block bodyend %} {{ super() }} {% if REQUIRE_JAX %} {% include "mathjax-load.html" %} {% endif %} {% include "comments/math.html" %} - {% block three_col_js %}{% endblock %} {% endblock %} \ No newline at end of file