From 2a5073687db320e73beec29301e088eb0ed79b4f Mon Sep 17 00:00:00 2001 From: KernelDeimos Date: Fri, 20 Dec 2024 09:52:08 -0500 Subject: [PATCH] perf: reorder get-launch-apps behavior As the recommended app list is hard-coded, these apps should be fetched first as they don't depend on a database request. --- src/backend/src/routers/get-launch-apps.js | 82 +++++++++++----------- 1 file changed, 41 insertions(+), 41 deletions(-) diff --git a/src/backend/src/routers/get-launch-apps.js b/src/backend/src/routers/get-launch-apps.js index 95d4ce654..ab48519e7 100644 --- a/src/backend/src/routers/get-launch-apps.js +++ b/src/backend/src/routers/get-launch-apps.js @@ -29,47 +29,6 @@ const { DB_READ } = require('../services/database/consts.js'); router.get('/get-launch-apps', auth, express.json(), async (req, res, next)=>{ let result = {}; - // -----------------------------------------------------------------------// - // Recent apps - // -----------------------------------------------------------------------// - let apps = []; - - const db = req.services.get('database').get(DB_READ, 'apps'); - - // First try the cache to see if we have recent apps - apps = kv.get('app_opens:user:' + req.user.id); - - // If cache is empty, query the db and update the cache - if(!apps || !Array.isArray(apps) || apps.length === 0){ - apps = await db.read( - 'SELECT DISTINCT app_uid FROM app_opens WHERE user_id = ? GROUP BY app_uid ORDER BY MAX(_id) DESC LIMIT 10', - [req.user.id]); - // Update cache with the results from the db (if any results were returned) - if(apps && Array.isArray(apps) && apps.length > 0) { - kv.set('app_opens:user:' + req.user.id, apps); - } - } - - // prepare each app for returning to user by only returning the necessary fields - // and adding them to the retobj array - result.recent = []; - console.log('\x1B[36;1m -------- RECENT APPS -------- \x1B[0m', apps); - for ( const { app_uid: uid } of apps ) { - console.log('\x1B[36;1m -------- UID -------- \x1B[0m', uid); - const app = await get_app({ uid }); - if ( ! app ) continue - - result.recent.push({ - uuid: app.uid, - name: app.name, - title: app.title, - icon: app.icon, - godmode: app.godmode, - maximize_on_start: app.maximize_on_start, - index_url: app.index_url, - }); - } - // -----------------------------------------------------------------------// // Recommended apps // -----------------------------------------------------------------------// @@ -117,6 +76,47 @@ router.get('/get-launch-apps', auth, express.json(), async (req, res, next)=>{ }); } + // -----------------------------------------------------------------------// + // Recent apps + // -----------------------------------------------------------------------// + let apps = []; + + const db = req.services.get('database').get(DB_READ, 'apps'); + + // First try the cache to see if we have recent apps + apps = kv.get('app_opens:user:' + req.user.id); + + // If cache is empty, query the db and update the cache + if(!apps || !Array.isArray(apps) || apps.length === 0){ + apps = await db.read( + 'SELECT DISTINCT app_uid FROM app_opens WHERE user_id = ? GROUP BY app_uid ORDER BY MAX(_id) DESC LIMIT 10', + [req.user.id]); + // Update cache with the results from the db (if any results were returned) + if(apps && Array.isArray(apps) && apps.length > 0) { + kv.set('app_opens:user:' + req.user.id, apps); + } + } + + // prepare each app for returning to user by only returning the necessary fields + // and adding them to the retobj array + result.recent = []; + console.log('\x1B[36;1m -------- RECENT APPS -------- \x1B[0m', apps); + for ( const { app_uid: uid } of apps ) { + console.log('\x1B[36;1m -------- UID -------- \x1B[0m', uid); + const app = await get_app({ uid }); + if ( ! app ) continue + + result.recent.push({ + uuid: app.uid, + name: app.name, + title: app.title, + icon: app.icon, + godmode: app.godmode, + maximize_on_start: app.maximize_on_start, + index_url: app.index_url, + }); + } + return res.send(result); })