From 20938fb6ce2b4f3fc1fe2adac8eb4c3053b22dc9 Mon Sep 17 00:00:00 2001 From: Jan Prochazka Date: Fri, 28 Jan 2022 15:15:58 +0100 Subject: [PATCH] apps loading optimalization --- packages/api/src/controllers/apps.js | 58 ++++++++++++++----- packages/api/src/controllers/connections.js | 10 ++-- .../web/src/appobj/ConnectionAppObject.svelte | 4 +- .../web/src/appobj/DatabaseAppObject.svelte | 12 +++- packages/web/src/utility/metadataLoaders.ts | 31 +++++++--- 5 files changed, 82 insertions(+), 33 deletions(-) diff --git a/packages/api/src/controllers/apps.js b/packages/api/src/controllers/apps.js index 5b9ea438..cc92f883 100644 --- a/packages/api/src/controllers/apps.js +++ b/packages/api/src/controllers/apps.js @@ -53,12 +53,9 @@ module.exports = { }, async emitChangedDbApp(folder) { - for (const conn of await connections.list()) { - for (const db of conn.databases || []) { - if (db[`useApp:${folder}`]) { - socket.emitChanged(`db-apps-changed-${conn._id}-${db.name}`); - } - } + const used = await this.getUsedAppFolders(); + if (used.includes(folder)) { + socket.emitChanged('used-apps-changed'); } }, @@ -112,26 +109,55 @@ module.exports = { return `${name}${index}`; }, - getAppsForDb_meta: true, - async getAppsForDb({ conid, database }) { - const connection = await connections.get({ conid }); - if (!connection) return []; - const db = (connection.databases || []).find(x => x.name == database); + getUsedAppFolders_meta: true, + async getUsedAppFolders() { + const list = await connections.list(); const apps = []; - const res = []; - if (db) { - for (const key of _.keys(db || {})) { - if (key.startsWith('useApp:') && db[key]) { - apps.push(key.substring('useApp:'.length)); + + for (const connection of list) { + for (const db of connection.databases || []) { + for (const key of _.keys(db || {})) { + if (key.startsWith('useApp:') && db[key]) { + apps.push(key.substring('useApp:'.length)); + } } } } + + return _.uniq(apps); + }, + + getUsedApps_meta: true, + async getUsedApps() { + const apps = await this.getUsedAppFolders(); + const res = []; + for (const folder of apps) { res.push(await this.loadApp({ folder })); } return res; }, + // getAppsForDb_meta: true, + // async getAppsForDb({ conid, database }) { + // const connection = await connections.get({ conid }); + // if (!connection) return []; + // const db = (connection.databases || []).find(x => x.name == database); + // const apps = []; + // const res = []; + // if (db) { + // for (const key of _.keys(db || {})) { + // if (key.startsWith('useApp:') && db[key]) { + // apps.push(key.substring('useApp:'.length)); + // } + // } + // } + // for (const folder of apps) { + // res.push(await this.loadApp({ folder })); + // } + // return res; + // }, + loadApp_meta: true, async loadApp({ folder }) { const res = { diff --git a/packages/api/src/controllers/connections.js b/packages/api/src/controllers/connections.js index f5588db5..8c3da64b 100644 --- a/packages/api/src/controllers/connections.js +++ b/packages/api/src/controllers/connections.js @@ -178,9 +178,10 @@ module.exports = { res = await this.datastore.insert(encrypted); } socket.emitChanged('connection-list-changed'); - for (const db of connection.databases || []) { - socket.emitChanged(`db-apps-changed-${connection._id}-${db.name}`); - } + socket.emitChanged('used-apps-changed'); + // for (const db of connection.databases || []) { + // socket.emitChanged(`db-apps-changed-${connection._id}-${db.name}`); + // } return res; }, @@ -204,7 +205,8 @@ module.exports = { } const res = await this.datastore.update({ _id: conid }, { $set: { databases } }); socket.emitChanged('connection-list-changed'); - socket.emitChanged(`db-apps-changed-${conid}-${database}`); + socket.emitChanged('used-apps-changed'); + // socket.emitChanged(`db-apps-changed-${conid}-${database}`); return res; }, diff --git a/packages/web/src/appobj/ConnectionAppObject.svelte b/packages/web/src/appobj/ConnectionAppObject.svelte index 53e9d5b1..d7577220 100644 --- a/packages/web/src/appobj/ConnectionAppObject.svelte +++ b/packages/web/src/appobj/ConnectionAppObject.svelte @@ -26,7 +26,7 @@ import { getDatabaseMenuItems } from './DatabaseAppObject.svelte'; import getElectron from '../utility/getElectron'; import getConnectionLabel from '../utility/getConnectionLabel'; - import { getDatabaseList, useDbApps } from '../utility/metadataLoaders'; + import { getDatabaseList, useUsedApps } from '../utility/metadataLoaders'; import { getLocalStorage } from '../utility/storageCache'; import { apiCall } from '../utility/api'; @@ -187,7 +187,7 @@ } } - $: apps = useDbApps({ conid: data?._id, database: data.defaultDatabase }); + $: apps = useUsedApps(); export const extractKey = props => props.name; + export function filterAppsForDatabase(connection, database, $apps) { + const db = (connection?.databases || []).find(x => x.name == database); + return $apps.filter(app => db && db[`useApp:${app.name}`]); + } + export function getDatabaseMenuItems(connection, name, $extensions, $currentDatabase, $apps) { + const apps = filterAppsForDatabase(connection, name, $apps); const handleNewQuery = () => { const tooltip = `${getConnectionLabel(connection)}\n${name}`; openNewTab({ @@ -169,7 +175,7 @@ const driver = findEngineDriver(connection, getExtensions()); - const commands = _.flatten(($apps || []).map(x => x.commands || [])); + const commands = _.flatten((apps || []).map(x => x.commands || [])); return [ { onClick: handleNewQuery, text: 'New query', isNewQuery: true }, @@ -233,7 +239,7 @@ import { showSnackbarSuccess } from '../utility/snackbar'; import { findEngineDriver } from 'dbgate-tools'; import InputTextModal from '../modals/InputTextModal.svelte'; - import { getDatabaseInfo, useDbApps } from '../utility/metadataLoaders'; + import { getDatabaseInfo, useUsedApps } from '../utility/metadataLoaders'; import { openJsonDocument } from '../tabs/JsonTab.svelte'; import { apiCall } from '../utility/api'; import ErrorMessageModal from '../modals/ErrorMessageModal.svelte'; @@ -247,7 +253,7 @@ } $: isPinned = !!$pinnedDatabases.find(x => x.name == data.name && x.connection?._id == data.connection?._id); - $: apps = useDbApps({ conid: data?.connection?._id, database: data?.name }); + $: apps = useUsedApps(); ({ reloadTrigger: `app-files-changed-${folder}`, }); -const dbAppsLoader = ({ conid, database }) => ({ - url: 'apps/get-apps-for-db', - params: { conid, database }, - reloadTrigger: `db-apps-changed-${conid}-${database}`, +// const dbAppsLoader = ({ conid, database }) => ({ +// url: 'apps/get-apps-for-db', +// params: { conid, database }, +// reloadTrigger: `db-apps-changed-${conid}-${database}`, +// }); + +const usedAppsLoader = ({ conid, database }) => ({ + url: 'apps/get-used-apps', + params: { }, + reloadTrigger: `used-apps-changed`, }); const serverStatusLoader = () => ({ @@ -433,13 +439,22 @@ export function useAppFolders(args = {}) { return useCore(appFoldersLoader, args); } -export function getDbApps(args = {}) { - return getCore(dbAppsLoader, args); + + +export function getUsedApps(args = {}) { + return getCore(usedAppsLoader, args); } -export function useDbApps(args = {}) { - return useCore(dbAppsLoader, args); +export function useUsedApps(args = {}) { + return useCore(usedAppsLoader, args); } +// export function getDbApps(args = {}) { +// return getCore(dbAppsLoader, args); +// } +// export function useDbApps(args = {}) { +// return useCore(dbAppsLoader, args); +// } + export function getInstalledPlugins(args = {}) { return getCore(installedPluginsLoader, args) || []; }