From e647ab471e7f4c349c8c0b111d4ffda1b591638d Mon Sep 17 00:00:00 2001 From: Jan Prochazka Date: Thu, 29 Apr 2021 11:17:17 +0200 Subject: [PATCH] ability to disable background model updates --- .../src/controllers/databaseConnections.js | 2 ++ .../api/src/controllers/serverConnections.js | 3 +- .../api/src/proc/databaseConnectionProcess.js | 12 +++++-- .../api/src/proc/serverConnectionProcess.js | 6 +++- packages/tools/src/index.ts | 1 + packages/tools/src/settingsExtractors.ts | 20 +++++++++++ .../web/src/settings/SettingsModal.svelte | 36 +++++++++++++------ 7 files changed, 66 insertions(+), 14 deletions(-) create mode 100644 packages/tools/src/settingsExtractors.ts diff --git a/packages/api/src/controllers/databaseConnections.js b/packages/api/src/controllers/databaseConnections.js index 02576a5a..e5601053 100644 --- a/packages/api/src/controllers/databaseConnections.js +++ b/packages/api/src/controllers/databaseConnections.js @@ -4,6 +4,7 @@ const socket = require('../utility/socket'); const { fork } = require('child_process'); const { DatabaseAnalyser } = require('dbgate-tools'); const { handleProcessCommunication } = require('../utility/processComm'); +const config = require('./config'); module.exports = { /** @type {import('dbgate-types').OpenedDatabaseConnection[]} */ @@ -79,6 +80,7 @@ module.exports = { msgtype: 'connect', connection: { ...connection, database }, structure: lastClosed ? lastClosed.structure : null, + globalSettings: await config.getSettings() }); return newOpened; }, diff --git a/packages/api/src/controllers/serverConnections.js b/packages/api/src/controllers/serverConnections.js index 288ff371..97548d5f 100644 --- a/packages/api/src/controllers/serverConnections.js +++ b/packages/api/src/controllers/serverConnections.js @@ -5,6 +5,7 @@ const _ = require('lodash'); const AsyncLock = require('async-lock'); const { handleProcessCommunication } = require('../utility/processComm'); const lock = new AsyncLock(); +const config = require('./config'); module.exports = { opened: [], @@ -65,7 +66,7 @@ module.exports = { if (newOpened.disconnected) return; this.close(conid, false); }); - subprocess.send({ msgtype: 'connect', ...connection }); + subprocess.send({ msgtype: 'connect', ...connection, globalSettings: await config.getSettings() }); return newOpened; }); return res; diff --git a/packages/api/src/proc/databaseConnectionProcess.js b/packages/api/src/proc/databaseConnectionProcess.js index 273da714..e491d3af 100644 --- a/packages/api/src/proc/databaseConnectionProcess.js +++ b/packages/api/src/proc/databaseConnectionProcess.js @@ -1,5 +1,6 @@ const stableStringify = require('json-stable-stringify'); const childProcessChecker = require('../utility/childProcessChecker'); +const { extractBoolSettingsValue, extractIntSettingsValue } = require('dbgate-tools'); const requireEngineDriver = require('../utility/requireEngineDriver'); const connectUtility = require('../utility/connectUtility'); const { handleProcessCommunication } = require('../utility/processComm'); @@ -64,7 +65,7 @@ async function readVersion() { process.send({ msgtype: 'version', version }); } -async function handleConnect({ connection, structure }) { +async function handleConnect({ connection, structure, globalSettings }) { storedConnection = connection; lastPing = new Date().getTime(); @@ -78,7 +79,14 @@ async function handleConnect({ connection, structure }) { } else { handleFullRefresh(); } - setInterval(handleIncrementalRefresh, 30 * 1000); + + if (extractBoolSettingsValue(globalSettings, 'connection.autoRefresh', true)) { + setInterval( + handleIncrementalRefresh, + extractIntSettingsValue(globalSettings, 'connection.autoRefreshInterval', 30, 3, 3600) * 1000 + ); + } + for (const [resolve] of afterConnectCallbacks) { resolve(); } diff --git a/packages/api/src/proc/serverConnectionProcess.js b/packages/api/src/proc/serverConnectionProcess.js index 4adbe3fa..e4737157 100644 --- a/packages/api/src/proc/serverConnectionProcess.js +++ b/packages/api/src/proc/serverConnectionProcess.js @@ -1,4 +1,5 @@ const stableStringify = require('json-stable-stringify'); +const { extractBoolSettingsValue, extractIntSettingsValue } = require('dbgate-tools'); const childProcessChecker = require('../utility/childProcessChecker'); const requireEngineDriver = require('../utility/requireEngineDriver'); const { decryptConnection } = require('../utility/crypting'); @@ -51,6 +52,7 @@ function setStatusName(name) { async function handleConnect(connection) { storedConnection = connection; + const { globalSettings } = storedConnection; setStatusName('pending'); lastPing = new Date().getTime(); @@ -59,7 +61,9 @@ async function handleConnect(connection) { systemConnection = await connectUtility(driver, storedConnection); readVersion(); handleRefresh(); - setInterval(handleRefresh, 30 * 1000); + if (extractBoolSettingsValue(globalSettings, 'connection.autoRefresh', true)) { + setInterval(handleRefresh, extractIntSettingsValue(globalSettings, 'connection.autoRefreshInterval', 30, 5, 3600) * 1000); + } } catch (err) { setStatus({ name: 'error', diff --git a/packages/tools/src/index.ts b/packages/tools/src/index.ts index 7ab818b5..c3b1c611 100644 --- a/packages/tools/src/index.ts +++ b/packages/tools/src/index.ts @@ -9,3 +9,4 @@ export * from './SqlDumper'; export * from './testPermission'; export * from './SqlGenerator'; export * from './structureTools'; +export * from './settingsExtractors'; diff --git a/packages/tools/src/settingsExtractors.ts b/packages/tools/src/settingsExtractors.ts new file mode 100644 index 00000000..843a4442 --- /dev/null +++ b/packages/tools/src/settingsExtractors.ts @@ -0,0 +1,20 @@ +import _ from 'lodash'; + +export function extractIntSettingsValue(settings, name, defaultValue, min = null, max = null) { + const parsed = parseInt(settings[name]); + if (_.isNaN(parsed)) { + return defaultValue; + } + if (_.isNumber(parsed)) { + if (min != null && parsed < min) return min; + if (max != null && parsed > max) return max; + return parsed; + } + return defaultValue; +} + +export function extractBoolSettingsValue(settings, name, defaultValue) { + const res = settings[name]; + if (res == null) return defaultValue; + return !!res; +} diff --git a/packages/web/src/settings/SettingsModal.svelte b/packages/web/src/settings/SettingsModal.svelte index e841341f..20cc98c7 100644 --- a/packages/web/src/settings/SettingsModal.svelte +++ b/packages/web/src/settings/SettingsModal.svelte @@ -7,6 +7,7 @@ import FormProvider from '../forms/FormProvider.svelte'; import FormSubmit from '../forms/FormSubmit.svelte'; import FormTextField from '../forms/FormTextField.svelte'; + import FormValues from '../forms/FormValues.svelte'; import ModalBase from '../modals/ModalBase.svelte'; import { closeCurrentModal } from '../modals/modalTools'; @@ -32,17 +33,32 @@
Settings
-
Appearance
- + +
Appearance
+ -
Data grid
- - - +
Data grid
+ + + + +
Connection
+ + +