From 66257cbd2062c2b58f6b558bf6b346aa9e3450a3 Mon Sep 17 00:00:00 2001 From: George Raduta Date: Thu, 5 Oct 2023 13:34:35 +0200 Subject: [PATCH 1/2] Add number of active sessions to status API --- InfoLogger/lib/StatusService.js | 10 +++++++++- InfoLogger/lib/api.js | 4 +++- InfoLogger/test/lib/mocha-status-service.js | 16 +++++++++++----- 3 files changed, 23 insertions(+), 7 deletions(-) diff --git a/InfoLogger/lib/StatusService.js b/InfoLogger/lib/StatusService.js index 740c7ddba..908d13953 100644 --- a/InfoLogger/lib/StatusService.js +++ b/InfoLogger/lib/StatusService.js @@ -23,13 +23,19 @@ class StatusService { * Setup StatusService * @param {JSON} config - of the framework * @param {JSON} projPackage - package json file + * @param {WebSocket} webSocketServer - instance of the web socket server used by the application */ - constructor(config, projPackage) { + constructor(config, projPackage, webSocketServer) { if (!config) { throw new Error('Empty Framework configuration'); } this.config = config; this.projPackage = projPackage; + + /** + * @type {WebSocket} + */ + this._ws = webSocketServer; } /** @@ -62,6 +68,7 @@ class StatusService { const ilg = {status: {ok: true}}; result = Object.assign(result, ilg); } + result.clients = this._ws?.server?.clients?.size ?? -1; res.status(200).json(result); } @@ -98,6 +105,7 @@ class StatusService { const ilg = {hostname: http.hostname, port: http.port, status: {ok: true}, name: http.name ?? '' }; info = Object.assign(info, ilg); } + info.clients = this._ws?.server?.clients?.size ?? -1; return info; } diff --git a/InfoLogger/lib/api.js b/InfoLogger/lib/api.js index c8ec3dd73..45b718aa8 100644 --- a/InfoLogger/lib/api.js +++ b/InfoLogger/lib/api.js @@ -28,13 +28,15 @@ let liveSource = null; const jsonDb = new JsonFileConnector(config.dbFile || __dirname + '/../db.json'); const profileService = new ProfileService(jsonDb); -const statusService = new StatusService(config, projPackage); module.exports.attachTo = async (http, ws) => { const { QueryController } = await import('./controller/QueryController.mjs'); const queryController = new QueryController(); + const statusService = new StatusService(config, projPackage, ws); + + http.post('/query', query); http.get('/query/stats', queryController.getQueryStats.bind(queryController), {public: true}); diff --git a/InfoLogger/test/lib/mocha-status-service.js b/InfoLogger/test/lib/mocha-status-service.js index 6077d42ec..f518a1cd7 100644 --- a/InfoLogger/test/lib/mocha-status-service.js +++ b/InfoLogger/test/lib/mocha-status-service.js @@ -31,6 +31,7 @@ describe('Status Service test suite', () => { assert.throws(() => new StatusService(null), new Error('Empty Framework configuration')); assert.throws(() => new StatusService(undefined), new Error('Empty Framework configuration')); }); + it('should successfully initialize StatusService', () => { assert.doesNotThrow(() => new StatusService({hostname: 'localhost', port: 8080}, {})); }); @@ -39,17 +40,19 @@ describe('Status Service test suite', () => { describe('`getProjectInfo()` tests', () => { it('should successfully return ilg info even if version is missing', () => { const statusService = new StatusService(config, undefined); - const info = {hostname: 'localhost', port: 8080, status: {ok: true}, name: 'TST'}; + const info = {hostname: 'localhost', port: 8080, status: {ok: true}, name: 'TST', clients: -1}; assert.deepStrictEqual(statusService.getProjectInfo(), info); }); + it('should successfully return ilg version even if http configuration is missing', () => { const statusService = new StatusService({}, {version: '1.9.2'}); - const info = {version: '1.9.2'}; + const info = {version: '1.9.2', clients: -1}; assert.deepStrictEqual(statusService.getProjectInfo(), info); }); + it('should successfully add project version if package.json was provided', () => { const statusService = new StatusService(config, {version: '1.9.2'}); - const info = {hostname: 'localhost', port: 8080, status: {ok: true}, version: '1.9.2', name: 'TST'}; + const info = {hostname: 'localhost', port: 8080, status: {ok: true}, version: '1.9.2', name: 'TST', clients: -1}; assert.deepStrictEqual(statusService.getProjectInfo(), info); }); }); @@ -60,6 +63,7 @@ describe('Status Service test suite', () => { const info = {host: 'localhost', port: 6102, status: {ok: false, message: 'Unable to connect to InfoLogger Server'}}; assert.deepStrictEqual(statusService.getLiveSourceStatus(config.infoLoggerServer), info); }); + it('should successfully return InfoLogger Server info with status ok when live source is present', () => { const statusService = new StatusService(config, undefined); statusService.setLiveSource({isConnected: true, onconnect: () => true}); @@ -76,6 +80,7 @@ describe('Status Service test suite', () => { const mysql = await statusService.getDataSourceStatus(config.mysql); assert.deepStrictEqual(mysql, info); }); + it('should successfully return mysql info with status ok true when data source is present and connected', async () => { const statusService = new StatusService(config, undefined); const info = {host: 'localhost', port: 6103, database: 'INFOLOGGER', status: {ok: true}}; @@ -87,6 +92,7 @@ describe('Status Service test suite', () => { const mysql = await statusService.getDataSourceStatus(config.mysql); assert.deepStrictEqual(mysql, info); }); + it('should successfully return mysql info with status ok false when data source is present but it is not connected', async () => { const statusService = new StatusService(config, undefined); const info = {host: 'localhost', port: 6103, database: 'INFOLOGGER', status: {ok: false, message: 'Could not connect'}}; @@ -110,7 +116,7 @@ describe('Status Service test suite', () => { await statusService.frameworkInfo(undefined, res); const info = { - 'infoLogger-gui': {hostname: 'localhost', port: 8080, status: {ok: true}, name: 'TST'}, + 'infoLogger-gui': {hostname: 'localhost', port: 8080, status: {ok: true}, name: 'TST', clients: -1}, mysql: {host: 'localhost', port: 6103, database: 'INFOLOGGER', status: {ok: false, message: 'There was no data source set up'}}, infoLoggerServer: {host: 'localhost', port: 6102, status: {ok: false, message: 'Unable to connect to InfoLogger Server'}} }; @@ -129,7 +135,7 @@ describe('Status Service test suite', () => { } await statusService.getILGStatus(undefined, res); - const info = {status: {ok: true}}; + const info = {status: {ok: true}, clients: -1}; assert.ok(res.status.calledWith(200)); assert.ok(res.json.calledWith(info)); From a6f66b31fdf43df73fee46e720fc1f41ae12e7ce Mon Sep 17 00:00:00 2001 From: George Raduta Date: Thu, 5 Oct 2023 13:35:07 +0200 Subject: [PATCH 2/2] Rename test file as per convention --- .../lib/{mocha-status-service.js => mocha-status-service.test.js} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename InfoLogger/test/lib/{mocha-status-service.js => mocha-status-service.test.js} (100%) diff --git a/InfoLogger/test/lib/mocha-status-service.js b/InfoLogger/test/lib/mocha-status-service.test.js similarity index 100% rename from InfoLogger/test/lib/mocha-status-service.js rename to InfoLogger/test/lib/mocha-status-service.test.js