Skip to content

Commit

Permalink
perf: misc performance improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
pateljannat committed Jan 17, 2025
1 parent 58abfd0 commit 145342b
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 97 deletions.
35 changes: 25 additions & 10 deletions frontend/src/components/DiscussionReplies.vue
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
</div>

<TextEditor
v-if="renderEditor"
class="mt-5"
:content="newReply"
:mentions="mentionUsers"
Expand All @@ -94,14 +95,16 @@ import { createResource, TextEditor, Button, Dropdown } from 'frappe-ui'
import { timeAgo } from '../utils'
import UserAvatar from '@/components/UserAvatar.vue'
import { ChevronLeft, MoreHorizontal } from 'lucide-vue-next'
import { ref, inject, onMounted, computed } from 'vue'
import { ref, inject, onMounted } from 'vue'
import { createToast } from '../utils'
const showTopics = defineModel('showTopics')
const newReply = ref('')
const socket = inject('$socket')
const user = inject('$user')
const allUsers = inject('$allUsers')
const mentionUsers = ref([])
const renderEditor = ref(false)
const props = defineProps({
topic: {
Expand All @@ -124,6 +127,7 @@ onMounted(() => {
socket.on('delete_message', (data) => {
replies.reload()
})
fetchMentionUsers()
})
const replies = createResource({
Expand All @@ -150,15 +154,26 @@ const newReplyResource = createResource({
},
})
const mentionUsers = computed(() => {
let users = Object.values(allUsers.data).map((user) => {
return {
value: user.name,
label: user.full_name,
}
})
return users
})
const fetchMentionUsers = () => {
if (user.data?.is_student) {
renderEditor.value = true
} else {
allUsers.reload(
{},
{
onSuccess(data) {
mentionUsers.value = Object.values(data).map((user) => {
return {
value: user.name,
label: user.full_name,
}
})
renderEditor.value = true
},
}
)
}
}
const postReply = () => {
newReplyResource.submit(
Expand Down
12 changes: 1 addition & 11 deletions frontend/src/main.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import './index.css'

import { createApp } from 'vue'
import router from './router'
import App from './App.vue'
Expand All @@ -8,15 +7,8 @@ import dayjs from '@/utils/dayjs'
import { createDialog } from '@/utils/dialogs'
import translationPlugin from './translation'
import { usersStore } from './stores/user'
import { sessionStore } from './stores/session'
import { initSocket } from './socket'
import {
FrappeUI,
setConfig,
frappeRequest,
resourcesPlugin,
pageMetaPlugin,
} from 'frappe-ui'
import { FrappeUI, setConfig, frappeRequest, pageMetaPlugin } from 'frappe-ui'

let pinia = createPinia()
let app = createApp(App)
Expand All @@ -32,8 +24,6 @@ app.provide('$socket', initSocket())
app.mount('#app')

const { userResource, allUsers } = usersStore()
let { isLoggedIn } = sessionStore()

app.provide('$user', userResource)
app.provide('$allUsers', allUsers)
app.config.globalProperties.$user = userResource
Expand Down
11 changes: 4 additions & 7 deletions frontend/src/pages/Notifications.vue
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
class="flex items-center py-2 justify-between"
>
<div class="flex items-center">
<UserAvatar :user="allUsers.data[log.from_user]" class="mr-2" />
<Avatar :image="log.user_image" :label="log.full_name" class="mr-2" />
<div class="notification" v-html="log.subject"></div>
</div>
<div class="flex items-center space-x-2">
Expand Down Expand Up @@ -57,6 +57,7 @@
</template>
<script setup>
import {
Avatar,
createListResource,
createResource,
Breadcrumbs,
Expand All @@ -66,14 +67,12 @@ import {
Tooltip,
} from 'frappe-ui'
import { computed, inject, ref, onMounted } from 'vue'
import UserAvatar from '@/components/UserAvatar.vue'
import { useRouter } from 'vue-router'
import { X } from 'lucide-vue-next'
import { updateDocumentTitle } from '@/utils'
const user = inject('$user')
const socket = inject('$socket')
const allUsers = inject('$allUsers')
const activeTab = ref('Unread')
const router = useRouter()
Expand All @@ -93,24 +92,22 @@ const notifications = computed(() => {
const unReadNotifications = createListResource({
doctype: 'Notification Log',
fields: ['subject', 'from_user', 'link', 'read', 'name'],
url: 'lms.lms.api.get_notifications',
filters: {
for_user: user.data?.name,
read: 0,
},
orderBy: 'creation desc',
auto: true,
cache: 'Unread Notifications',
})
const readNotifications = createListResource({
doctype: 'Notification Log',
fields: ['subject', 'from_user', 'link', 'read', 'name'],
url: 'lms.lms.api.get_notifications',
filters: {
for_user: user.data?.name,
read: 1,
},
orderBy: 'creation desc',
auto: true,
cache: 'Read Notifications',
})
Expand Down
10 changes: 1 addition & 9 deletions frontend/src/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,21 +217,13 @@ let router = createRouter({
})

router.beforeEach(async (to, from, next) => {
const { userResource, allUsers } = usersStore()
const { userResource } = usersStore()
let { isLoggedIn } = sessionStore()

try {
if (isLoggedIn) {
await userResource.promise
}
if (
isLoggedIn &&
(to.name == 'Lesson' ||
to.name == 'Batch' ||
to.name == 'Notifications')
) {
await allUsers.promise
}
} catch (error) {
isLoggedIn = false
}
Expand Down
5 changes: 1 addition & 4 deletions frontend/src/stores/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import router from '@/router'
import { ref, computed } from 'vue'

export const sessionStore = defineStore('lms-session', () => {
let { userResource, allUsers } = usersStore()
let { userResource } = usersStore()

function sessionUser() {
let cookies = new URLSearchParams(document.cookie.split('; ').join('&'))
Expand All @@ -17,9 +17,6 @@ export const sessionStore = defineStore('lms-session', () => {
}

let user = ref(sessionUser())
if (user.value) {
allUsers.reload()
}
const isLoggedIn = computed(() => !!user.value)

const login = createResource({
Expand Down
19 changes: 19 additions & 0 deletions lms/lms/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,7 @@ def get_assigned_badges(member):

@frappe.whitelist()
def get_all_users():
frappe.only_for(["Moderator", "Course Creator", "Batch Evaluator"])
users = frappe.get_all(
"User",
{
Expand Down Expand Up @@ -1189,3 +1190,21 @@ def prepare_heatmap_data(start_date, number_of_days, date_count):
def get_week_difference(start_date, current_date):
diff_in_days = date_diff(current_date, start_date)
return diff_in_days // 7


@frappe.whitelist()
def get_notifications(filters):
notifications = frappe.get_all(
"Notification Log",
filters,
["subject", "from_user", "link", "read", "name"],
order_by="creation desc",
)

for notification in notifications:
from_user_details = frappe.db.get_value(
"User", notification.from_user, ["full_name", "user_image"], as_dict=1
)
notification.update(from_user_details)

return notifications
10 changes: 0 additions & 10 deletions lms/lms/doctype/course_lesson/course_lesson.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,6 @@ def get_exercises(self):
exercises = [value for name, value in macros if name == "Exercise"]
return [frappe.get_doc("LMS Exercise", name) for name in exercises]

def get_progress(self):
return frappe.db.get_value(
"LMS Course Progress", {"lesson": self.name, "owner": frappe.session.user}, "status"
)

def get_slugified_class(self):
if self.get_progress():
return ("").join([s for s in self.get_progress().lower().split()])
return


@frappe.whitelist()
def save_progress(lesson, course):
Expand Down
17 changes: 11 additions & 6 deletions lms/lms/doctype/lms_course_progress/lms_course_progress.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,30 +22,34 @@
"in_standard_filter": 1,
"label": "Course",
"options": "LMS Course",
"read_only": 1
"read_only": 1,
"search_index": 1
},
{
"fetch_from": "lesson.chapter",
"fieldname": "chapter",
"fieldtype": "Link",
"label": "Chapter",
"options": "Course Chapter",
"read_only": 1
"read_only": 1,
"search_index": 1
},
{
"fieldname": "lesson",
"fieldtype": "Link",
"in_list_view": 1,
"label": "Lesson",
"options": "Course Lesson"
"options": "Course Lesson",
"search_index": 1
},
{
"fieldname": "status",
"fieldtype": "Select",
"in_list_view": 1,
"in_standard_filter": 1,
"label": "Status",
"options": "Complete\nPartially Complete\nIncomplete"
"options": "Complete\nPartially Complete\nIncomplete",
"search_index": 1
},
{
"fieldname": "column_break_3",
Expand All @@ -55,7 +59,8 @@
"fieldname": "member",
"fieldtype": "Link",
"label": "Member",
"options": "User"
"options": "User",
"search_index": 1
},
{
"fetch_from": "member.full_name",
Expand All @@ -67,7 +72,7 @@
],
"index_web_pages_for_search": 1,
"links": [],
"modified": "2024-02-27 11:43:08.326886",
"modified": "2025-01-17 15:54:34.040621",
"modified_by": "Administrator",
"module": "LMS",
"name": "LMS Course Progress",
Expand Down
18 changes: 0 additions & 18 deletions lms/lms/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -450,24 +450,6 @@ def get_signup_optin_checks():
return (", ").join(links)


def get_popular_courses():
courses = frappe.get_all("LMS Course", {"published": 1, "upcoming": 0})
course_membership = []

for course in courses:
course_membership.append(
{
"course": course.name,
"members": cint(frappe.db.count("LMS Enrollment", {"course": course.name})),
}
)

course_membership = sorted(
course_membership, key=lambda x: x.get("members"), reverse=True
)
return course_membership[:3]


def format_amount(amount, currency):
amount_reduced = amount / 1000
if amount_reduced < 1:
Expand Down
22 changes: 0 additions & 22 deletions lms/lms/web_template/courses_enrolled/courses_enrolled.html
Original file line number Diff line number Diff line change
@@ -1,31 +1,9 @@
{% set enrolled = get_enrolled_courses().in_progress + get_enrolled_courses().completed %}


{% if enrolled | length %}
<div class="cards-parent">
{% for course in enrolled %}
{{ widgets.CourseCard(course=course) }}
{% endfor %}
</div>


{% else %}
{% set site_name = frappe.db.get_single_value("System Settings", "app_name") %}
<div class="empty-state p-5">
<div style="text-align: left; flex: 1;">
<div class="text-center">
<div class="empty-state-heading">{{ _("You haven't enrolled for any courses") }}</div>
<div class="course-meta mb-6">{{ _("Here are a few courses we recommend for you to get started with {0}").format(site_name) }}</div>
</div>
{% set recommended_courses = get_popular_courses() %}
<div class="cards-parent">
{% for course in recommended_courses %}
{% if course %}
{% set course_details = frappe.get_doc("LMS Course", course.course) %}
{{ widgets.CourseCard(course=course_details) }}
{% endif %}
{% endfor %}
</div>
</div>
</div>
{% endif %}

0 comments on commit 145342b

Please sign in to comment.