From a46416e86a3413a9f839075933fbc407b89a7ef0 Mon Sep 17 00:00:00 2001 From: Ghalya AL-Eshaq Date: Mon, 2 Sep 2024 23:16:16 +0300 Subject: [PATCH 01/10] refactoring codeof SocketRooms.getAll function to reduce its complexity by seperating it into helper functions in src/socket.io/admins/rooms.js --- src/socket.io/admin/rooms.js | 124 +++++++++++++++++++---------------- 1 file changed, 69 insertions(+), 55 deletions(-) diff --git a/src/socket.io/admin/rooms.js b/src/socket.io/admin/rooms.js index a8107edaa7..1919413fc1 100644 --- a/src/socket.io/admin/rooms.js +++ b/src/socket.io/admin/rooms.js @@ -16,63 +16,77 @@ SocketRooms.getTotalGuestCount = async function () { }; SocketRooms.getAll = async function () { - const sockets = await io.server.fetchSockets(); - - totals.onlineGuestCount = 0; - totals.onlineRegisteredCount = 0; - totals.socketCount = sockets.length; - totals.topTenTopics = []; - totals.users = { - categories: 0, - recent: 0, - unread: 0, - topics: 0, - category: 0, - }; - const userRooms = {}; - const topicData = {}; - for (const s of sockets) { - for (const key of s.rooms) { - if (key === 'online_guests') { - totals.onlineGuestCount += 1; - } else if (key === 'categories') { - totals.users.categories += 1; - } else if (key === 'recent_topics') { - totals.users.recent += 1; - } else if (key === 'unread_topics') { - totals.users.unread += 1; - } else if (key.startsWith('uid_')) { - userRooms[key] = 1; - } else if (key.startsWith('category_')) { - totals.users.category += 1; - } else { - const tid = key.match(/^topic_(\d+)/); - if (tid) { - totals.users.topics += 1; - topicData[tid[1]] = topicData[tid[1]] || { count: 0 }; - topicData[tid[1]].count += 1; - } - } - } - } - totals.onlineRegisteredCount = Object.keys(userRooms).length; - - let topTenTopics = []; - Object.keys(topicData).forEach((tid) => { - topTenTopics.push({ tid: tid, count: topicData[tid].count }); - }); - topTenTopics = topTenTopics.sort((a, b) => b.count - a.count).slice(0, 10); - const topTenTids = topTenTopics.map(topic => topic.tid); - - const titles = await topics.getTopicsFields(topTenTids, ['title']); - totals.topTenTopics = topTenTopics.map((topic, index) => { - topic.title = titles[index].title; - return topic; - }); - - return totals; + const sockets = await io.server.fetchSockets(); + + totals.onlineGuestCount = 0; + totals.onlineRegisteredCount = 0; + totals.socketCount = sockets.length; + totals.topTenTopics = []; + totals.users = { + categories: 0, + recent: 0, + unread: 0, + topics: 0, + category: 0, + }; + const userRooms = {}; + const topicData = {}; + + for (const s of sockets) { + processSocket(s, totals, userRooms, topicData); + } + + totals.onlineRegisteredCount = Object.keys(userRooms).length; + + let topTenTopics = getTopTenTopics(topicData); + const topTenTids = topTenTopics.map(topic => topic.tid); + + const titles = await topics.getTopicsFields(topTenTids, ['title']); + totals.topTenTopics = topTenTopics.map((topic, index) => { + topic.title = titles[index].title; + return topic; + }); + + return totals; }; +function processSocket(s, totals, userRooms, topicData) { + for (const key of s.rooms) { + if (key === 'online_guests') { + totals.onlineGuestCount += 1; + } else if (key === 'categories') { + totals.users.categories += 1; + } else if (key === 'recent_topics') { + totals.users.recent += 1; + } else if (key === 'unread_topics') { + totals.users.unread += 1; + } else if (key.startsWith('uid_')) { + userRooms[key] = 1; + } else if (key.startsWith('category_')) { + totals.users.category += 1; + } else { + processTopicKey(key, totals, topicData); + } + } +} + +function processTopicKey(key, totals, topicData) { + const tid = key.match(/^topic_(\d+)/); + if (tid) { + totals.users.topics += 1; + topicData[tid[1]] = topicData[tid[1]] || { count: 0 }; + topicData[tid[1]].count += 1; + } +} + +function getTopTenTopics(topicData) { + let topTenTopics = []; + Object.keys(topicData).forEach((tid) => { + topTenTopics.push({ tid: tid, count: topicData[tid].count }); + }); + return topTenTopics.sort((a, b) => b.count - a.count).slice(0, 10); +} + SocketRooms.getOnlineUserCount = function (io) { let count = 0; From 180e90927c40a94a61fb5d78f233456b6bfcaa1a Mon Sep 17 00:00:00 2001 From: Ghalya AL-Eshaq Date: Mon, 2 Sep 2024 23:28:02 +0300 Subject: [PATCH 02/10] inserting prent statment to manually test the refactored code in sr/sockets.io/admin/rooms.js --- src/socket.io/admin/rooms.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/socket.io/admin/rooms.js b/src/socket.io/admin/rooms.js index 1919413fc1..e5b39e0879 100644 --- a/src/socket.io/admin/rooms.js +++ b/src/socket.io/admin/rooms.js @@ -16,6 +16,7 @@ SocketRooms.getTotalGuestCount = async function () { }; SocketRooms.getAll = async function () { + console.log(GhalyaRefactoredCode); const sockets = await io.server.fetchSockets(); totals.onlineGuestCount = 0; From 5e9869c25d54b5bc390b442ed33adae3f53c8827 Mon Sep 17 00:00:00 2001 From: Ghalya AL-Eshaq Date: Mon, 2 Sep 2024 23:32:55 +0300 Subject: [PATCH 03/10] adding more print statmeents in src/socket.io/admin/rooms.js file --- src/socket.io/admin/rooms.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/socket.io/admin/rooms.js b/src/socket.io/admin/rooms.js index e5b39e0879..57c998c7e3 100644 --- a/src/socket.io/admin/rooms.js +++ b/src/socket.io/admin/rooms.js @@ -52,6 +52,7 @@ SocketRooms.getAll = async function () { }; function processSocket(s, totals, userRooms, topicData) { + console.log(GhalyaRefactoredCode1); for (const key of s.rooms) { if (key === 'online_guests') { totals.onlineGuestCount += 1; @@ -72,6 +73,7 @@ function processSocket(s, totals, userRooms, topicData) { } function processTopicKey(key, totals, topicData) { + console.log(GhalyaRefactoredCode2); const tid = key.match(/^topic_(\d+)/); if (tid) { totals.users.topics += 1; @@ -81,6 +83,7 @@ function processTopicKey(key, totals, topicData) { } function getTopTenTopics(topicData) { + console.log(GhalyaRefactoredCode3); let topTenTopics = []; Object.keys(topicData).forEach((tid) => { topTenTopics.push({ tid: tid, count: topicData[tid].count }); From cf2965f836aab80f0a86af529d1421edcbc6580a Mon Sep 17 00:00:00 2001 From: Ghalya AL-Eshaq Date: Mon, 2 Sep 2024 23:43:59 +0300 Subject: [PATCH 04/10] fxing indentation --- src/socket.io/admin/rooms.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/socket.io/admin/rooms.js b/src/socket.io/admin/rooms.js index 57c998c7e3..1a559d8e7f 100644 --- a/src/socket.io/admin/rooms.js +++ b/src/socket.io/admin/rooms.js @@ -16,7 +16,7 @@ SocketRooms.getTotalGuestCount = async function () { }; SocketRooms.getAll = async function () { - console.log(GhalyaRefactoredCode); + console.log('GhalyaRefactoredCode'); const sockets = await io.server.fetchSockets(); totals.onlineGuestCount = 0; @@ -52,7 +52,7 @@ SocketRooms.getAll = async function () { }; function processSocket(s, totals, userRooms, topicData) { - console.log(GhalyaRefactoredCode1); + console.log('GhalyaRefactoredCode1'); for (const key of s.rooms) { if (key === 'online_guests') { totals.onlineGuestCount += 1; @@ -73,7 +73,7 @@ function processSocket(s, totals, userRooms, topicData) { } function processTopicKey(key, totals, topicData) { - console.log(GhalyaRefactoredCode2); + console.log('GhalyaRefactoredCode2'); const tid = key.match(/^topic_(\d+)/); if (tid) { totals.users.topics += 1; @@ -83,7 +83,7 @@ function processTopicKey(key, totals, topicData) { } function getTopTenTopics(topicData) { - console.log(GhalyaRefactoredCode3); + console.log('GhalyaRefactoredCode3'); let topTenTopics = []; Object.keys(topicData).forEach((tid) => { topTenTopics.push({ tid: tid, count: topicData[tid].count }); From 4add1209b89a4b6fbc3f74e9c65a85083e36f55c Mon Sep 17 00:00:00 2001 From: Ghalya AL-Eshaq Date: Mon, 2 Sep 2024 23:48:29 +0300 Subject: [PATCH 05/10] fxing indentationagain --- src/socket.io/admin/rooms.js | 128 +++++++++++++++++------------------ 1 file changed, 64 insertions(+), 64 deletions(-) diff --git a/src/socket.io/admin/rooms.js b/src/socket.io/admin/rooms.js index 1a559d8e7f..182aa18ada 100644 --- a/src/socket.io/admin/rooms.js +++ b/src/socket.io/admin/rooms.js @@ -16,79 +16,79 @@ SocketRooms.getTotalGuestCount = async function () { }; SocketRooms.getAll = async function () { - console.log('GhalyaRefactoredCode'); - const sockets = await io.server.fetchSockets(); - - totals.onlineGuestCount = 0; - totals.onlineRegisteredCount = 0; - totals.socketCount = sockets.length; - totals.topTenTopics = []; - totals.users = { - categories: 0, - recent: 0, - unread: 0, - topics: 0, - category: 0, - }; - const userRooms = {}; - const topicData = {}; - - for (const s of sockets) { - processSocket(s, totals, userRooms, topicData); - } - - totals.onlineRegisteredCount = Object.keys(userRooms).length; - - let topTenTopics = getTopTenTopics(topicData); - const topTenTids = topTenTopics.map(topic => topic.tid); - - const titles = await topics.getTopicsFields(topTenTids, ['title']); - totals.topTenTopics = topTenTopics.map((topic, index) => { - topic.title = titles[index].title; - return topic; - }); - - return totals; + console.log('GhalyaRefactoredCode'); + const sockets = await io.server.fetchSockets(); + + totals.onlineGuestCount = 0; + totals.onlineRegisteredCount = 0; + totals.socketCount = sockets.length; + totals.topTenTopics = []; + totals.users = { + categories: 0, + recent: 0, + unread: 0, + topics: 0, + category: 0, + }; + const userRooms = {}; + const topicData = {}; + + for (const s of sockets) { + processSocket(s, totals, userRooms, topicData); + } + + totals.onlineRegisteredCount = Object.keys(userRooms).length; + + const topTenTopics = getTopTenTopics(topicData); + const topTenTids = topTenTopics.map(topic => topic.tid); + + const titles = await topics.getTopicsFields(topTenTids, ['title']); + totals.topTenTopics = topTenTopics.map((topic, index) => { + topic.title = titles[index].title; + return topic; + }); + + return totals; }; function processSocket(s, totals, userRooms, topicData) { - console.log('GhalyaRefactoredCode1'); - for (const key of s.rooms) { - if (key === 'online_guests') { - totals.onlineGuestCount += 1; - } else if (key === 'categories') { - totals.users.categories += 1; - } else if (key === 'recent_topics') { - totals.users.recent += 1; - } else if (key === 'unread_topics') { - totals.users.unread += 1; - } else if (key.startsWith('uid_')) { - userRooms[key] = 1; - } else if (key.startsWith('category_')) { - totals.users.category += 1; - } else { - processTopicKey(key, totals, topicData); - } - } + console.log('GhalyaRefactoredCode1'); + for (const key of s.rooms) { + if (key === 'online_guests') { + totals.onlineGuestCount += 1; + } else if (key === 'categories') { + totals.users.categories += 1; + } else if (key === 'recent_topics') { + totals.users.recent += 1; + } else if (key === 'unread_topics') { + totals.users.unread += 1; + } else if (key.startsWith('uid_')) { + userRooms[key] = 1; + } else if (key.startsWith('category_')) { + totals.users.category += 1; + } else { + processTopicKey(key, totals, topicData); + } + } } function processTopicKey(key, totals, topicData) { - console.log('GhalyaRefactoredCode2'); - const tid = key.match(/^topic_(\d+)/); - if (tid) { - totals.users.topics += 1; - topicData[tid[1]] = topicData[tid[1]] || { count: 0 }; - topicData[tid[1]].count += 1; - } + console.log('GhalyaRefactoredCode2'); + const tid = key.match(/^topic_(\d+)/); + if (tid) { + totals.users.topics += 1; + topicData[tid[1]] = topicData[tid[1]] || { count: 0 }; + topicData[tid[1]].count += 1; + } } function getTopTenTopics(topicData) { - console.log('GhalyaRefactoredCode3'); - let topTenTopics = []; - Object.keys(topicData).forEach((tid) => { - topTenTopics.push({ tid: tid, count: topicData[tid].count }); - }); - return topTenTopics.sort((a, b) => b.count - a.count).slice(0, 10); + console.log('GhalyaRefactoredCode3'); + const topTenTopics = []; + Object.keys(topicData).forEach((tid) => { + topTenTopics.push({ tid: tid, count: topicData[tid].count }); + }); + return topTenTopics.sort((a, b) => b.count - a.count).slice(0, 10); } SocketRooms.getOnlineUserCount = function (io) { From 4e70b9b02a0697e19727061ddc743d5b9b3f4438 Mon Sep 17 00:00:00 2001 From: Ghalya AL-Eshaq Date: Tue, 3 Sep 2024 00:07:45 +0300 Subject: [PATCH 06/10] fixing errors --- src/socket.io/admin/rooms.js | 72 ++++++++++++++---------------------- 1 file changed, 27 insertions(+), 45 deletions(-) diff --git a/src/socket.io/admin/rooms.js b/src/socket.io/admin/rooms.js index 182aa18ada..a8107edaa7 100644 --- a/src/socket.io/admin/rooms.js +++ b/src/socket.io/admin/rooms.js @@ -16,7 +16,6 @@ SocketRooms.getTotalGuestCount = async function () { }; SocketRooms.getAll = async function () { - console.log('GhalyaRefactoredCode'); const sockets = await io.server.fetchSockets(); totals.onlineGuestCount = 0; @@ -32,14 +31,37 @@ SocketRooms.getAll = async function () { }; const userRooms = {}; const topicData = {}; - for (const s of sockets) { - processSocket(s, totals, userRooms, topicData); + for (const key of s.rooms) { + if (key === 'online_guests') { + totals.onlineGuestCount += 1; + } else if (key === 'categories') { + totals.users.categories += 1; + } else if (key === 'recent_topics') { + totals.users.recent += 1; + } else if (key === 'unread_topics') { + totals.users.unread += 1; + } else if (key.startsWith('uid_')) { + userRooms[key] = 1; + } else if (key.startsWith('category_')) { + totals.users.category += 1; + } else { + const tid = key.match(/^topic_(\d+)/); + if (tid) { + totals.users.topics += 1; + topicData[tid[1]] = topicData[tid[1]] || { count: 0 }; + topicData[tid[1]].count += 1; + } + } + } } - totals.onlineRegisteredCount = Object.keys(userRooms).length; - const topTenTopics = getTopTenTopics(topicData); + let topTenTopics = []; + Object.keys(topicData).forEach((tid) => { + topTenTopics.push({ tid: tid, count: topicData[tid].count }); + }); + topTenTopics = topTenTopics.sort((a, b) => b.count - a.count).slice(0, 10); const topTenTids = topTenTopics.map(topic => topic.tid); const titles = await topics.getTopicsFields(topTenTids, ['title']); @@ -51,46 +73,6 @@ SocketRooms.getAll = async function () { return totals; }; -function processSocket(s, totals, userRooms, topicData) { - console.log('GhalyaRefactoredCode1'); - for (const key of s.rooms) { - if (key === 'online_guests') { - totals.onlineGuestCount += 1; - } else if (key === 'categories') { - totals.users.categories += 1; - } else if (key === 'recent_topics') { - totals.users.recent += 1; - } else if (key === 'unread_topics') { - totals.users.unread += 1; - } else if (key.startsWith('uid_')) { - userRooms[key] = 1; - } else if (key.startsWith('category_')) { - totals.users.category += 1; - } else { - processTopicKey(key, totals, topicData); - } - } -} - -function processTopicKey(key, totals, topicData) { - console.log('GhalyaRefactoredCode2'); - const tid = key.match(/^topic_(\d+)/); - if (tid) { - totals.users.topics += 1; - topicData[tid[1]] = topicData[tid[1]] || { count: 0 }; - topicData[tid[1]].count += 1; - } -} - -function getTopTenTopics(topicData) { - console.log('GhalyaRefactoredCode3'); - const topTenTopics = []; - Object.keys(topicData).forEach((tid) => { - topTenTopics.push({ tid: tid, count: topicData[tid].count }); - }); - return topTenTopics.sort((a, b) => b.count - a.count).slice(0, 10); -} - SocketRooms.getOnlineUserCount = function (io) { let count = 0; From 7450c55e44f61ad232a1aa1965326fdbf172aa4d Mon Sep 17 00:00:00 2001 From: Ghalya AL-Eshaq Date: Tue, 3 Sep 2024 00:17:01 +0300 Subject: [PATCH 07/10] refactoring code in src/socket.io/admin/rooms.js --- src/socket.io/admin/rooms.js | 73 +++++++++++++++++++++++------------- 1 file changed, 46 insertions(+), 27 deletions(-) diff --git a/src/socket.io/admin/rooms.js b/src/socket.io/admin/rooms.js index a8107edaa7..db95a79c2b 100644 --- a/src/socket.io/admin/rooms.js +++ b/src/socket.io/admin/rooms.js @@ -16,6 +16,7 @@ SocketRooms.getTotalGuestCount = async function () { }; SocketRooms.getAll = async function () { + console.log('GhalyaRefactoredCode'); const sockets = await io.server.fetchSockets(); totals.onlineGuestCount = 0; @@ -31,37 +32,14 @@ SocketRooms.getAll = async function () { }; const userRooms = {}; const topicData = {}; + for (const s of sockets) { - for (const key of s.rooms) { - if (key === 'online_guests') { - totals.onlineGuestCount += 1; - } else if (key === 'categories') { - totals.users.categories += 1; - } else if (key === 'recent_topics') { - totals.users.recent += 1; - } else if (key === 'unread_topics') { - totals.users.unread += 1; - } else if (key.startsWith('uid_')) { - userRooms[key] = 1; - } else if (key.startsWith('category_')) { - totals.users.category += 1; - } else { - const tid = key.match(/^topic_(\d+)/); - if (tid) { - totals.users.topics += 1; - topicData[tid[1]] = topicData[tid[1]] || { count: 0 }; - topicData[tid[1]].count += 1; - } - } - } + processSocket(s, totals, userRooms, topicData); } + totals.onlineRegisteredCount = Object.keys(userRooms).length; - let topTenTopics = []; - Object.keys(topicData).forEach((tid) => { - topTenTopics.push({ tid: tid, count: topicData[tid].count }); - }); - topTenTopics = topTenTopics.sort((a, b) => b.count - a.count).slice(0, 10); + const topTenTopics = getTopTenTopics(topicData); const topTenTids = topTenTopics.map(topic => topic.tid); const titles = await topics.getTopicsFields(topTenTids, ['title']); @@ -73,6 +51,47 @@ SocketRooms.getAll = async function () { return totals; }; +function processSocket(s, totals, userRooms, topicData) { + console.log('GhalyaRefactoredCode1'); + for (const key of s.rooms) { + if (key === 'online_guests') { + totals.onlineGuestCount += 1; + } else if (key === 'categories') { + totals.users.categories += 1; + } else if (key === 'recent_topics') { + totals.users.recent += 1; + } else if (key === 'unread_topics') { + totals.users.unread += 1; + } else if (key.startsWith('uid_')) { + userRooms[key] = 1; + } else if (key.startsWith('category_')) { + totals.users.category += 1; + } else { + processTopicKey(key, totals, topicData); + } + } +} + +function processTopicKey(key, totals, topicData) { + console.log('GhalyaRefactoredCode2'); + const tid = key.match(/^topic_(\d+)/); + if (tid) { + totals.users.topics += 1; + topicData[tid[1]] = topicData[tid[1]] || { count: 0 }; + topicData[tid[1]].count += 1; + } +} + +function getTopTenTopics(topicData) { + console.log('GhalyaRefactoredCode3'); + const topTenTopics = []; + Object.keys(topicData).forEach((tid) => { + topTenTopics.push({ tid: tid, count: topicData[tid].count }); + }); + return topTenTopics.sort((a, b) => b.count - a.count).slice(0, 10); +} + + SocketRooms.getOnlineUserCount = function (io) { let count = 0; From 5924872c8ffef66fa3302f0cec427cab59783529 Mon Sep 17 00:00:00 2001 From: Ghalya AL-Eshaq Date: Thu, 5 Sep 2024 01:25:57 +0300 Subject: [PATCH 08/10] editing getTopTenTopics function to fix the maintainabillity of the code --- src/socket.io/admin/rooms.js | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/socket.io/admin/rooms.js b/src/socket.io/admin/rooms.js index db95a79c2b..49505b0d57 100644 --- a/src/socket.io/admin/rooms.js +++ b/src/socket.io/admin/rooms.js @@ -83,12 +83,13 @@ function processTopicKey(key, totals, topicData) { } function getTopTenTopics(topicData) { - console.log('GhalyaRefactoredCode3'); - const topTenTopics = []; - Object.keys(topicData).forEach((tid) => { - topTenTopics.push({ tid: tid, count: topicData[tid].count }); - }); - return topTenTopics.sort((a, b) => b.count - a.count).slice(0, 10); + console.log('GhalyaRefactoredCode3'); + const topTenTopics = []; + Object.keys(topicData).forEach((tid) => { + topTenTopics.push({ tid: tid, count: topicData[tid].count }); + }); + topTenTopics.sort((a, b) => b.count - a.count); + return topTenTopics.slice(0, 10); } From 1e66f2f68bf0a53cb489e7ff6e1eb35256384ea5 Mon Sep 17 00:00:00 2001 From: Ghalya AL-Eshaq Date: Thu, 5 Sep 2024 01:40:20 +0300 Subject: [PATCH 09/10] saving the file cuz i forgot to save the file and pushed the wrong one --- src/socket.io/admin/rooms.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/socket.io/admin/rooms.js b/src/socket.io/admin/rooms.js index 49505b0d57..b1afec8a3f 100644 --- a/src/socket.io/admin/rooms.js +++ b/src/socket.io/admin/rooms.js @@ -88,8 +88,8 @@ function getTopTenTopics(topicData) { Object.keys(topicData).forEach((tid) => { topTenTopics.push({ tid: tid, count: topicData[tid].count }); }); - topTenTopics.sort((a, b) => b.count - a.count); - return topTenTopics.slice(0, 10); + const sortedTopTenTopics = [...topTenTopics].sort((a, b) => b.count - a.count); + return sortedTopTenTopics.slice(0, 10); } From 04efb4213fa27ca5b43a8c8ca97ff89f6cdeb99b Mon Sep 17 00:00:00 2001 From: Ghalya AL-Eshaq Date: Thu, 5 Sep 2024 01:42:01 +0300 Subject: [PATCH 10/10] fixing indentation in the function getTopTenTopics of rooms.js --- src/socket.io/admin/rooms.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/socket.io/admin/rooms.js b/src/socket.io/admin/rooms.js index b1afec8a3f..ae67ed9551 100644 --- a/src/socket.io/admin/rooms.js +++ b/src/socket.io/admin/rooms.js @@ -83,13 +83,13 @@ function processTopicKey(key, totals, topicData) { } function getTopTenTopics(topicData) { - console.log('GhalyaRefactoredCode3'); - const topTenTopics = []; - Object.keys(topicData).forEach((tid) => { - topTenTopics.push({ tid: tid, count: topicData[tid].count }); - }); - const sortedTopTenTopics = [...topTenTopics].sort((a, b) => b.count - a.count); - return sortedTopTenTopics.slice(0, 10); + console.log('GhalyaRefactoredCode3'); + const topTenTopics = []; + Object.keys(topicData).forEach((tid) => { + topTenTopics.push({ tid: tid, count: topicData[tid].count }); + }); + const sortedTopTenTopics = [...topTenTopics].sort((a, b) => b.count - a.count); + return sortedTopTenTopics.slice(0, 10); }