Skip to content

Commit

Permalink
[OGUI-1418] Add number of active sessions to /status/gui route (#2147)
Browse files Browse the repository at this point in the history
* adds `webSocket` server instance to the status service instance so that it can query on number of active clients
* adds information on active sessions to `api/status/gui` to be used by Monitoring services.
* in case of failure, `-1` will be returned as monitoring services expect always a number
  • Loading branch information
graduta authored Oct 5, 2023
1 parent 42fcdfd commit 3b21084
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 7 deletions.
10 changes: 9 additions & 1 deletion InfoLogger/lib/StatusService.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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;
}

Expand Down
4 changes: 3 additions & 1 deletion InfoLogger/lib/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -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});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}, {}));
});
Expand All @@ -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);
});
});
Expand All @@ -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});
Expand All @@ -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}};
Expand All @@ -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'}};
Expand All @@ -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'}}
};
Expand All @@ -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));
Expand Down

0 comments on commit 3b21084

Please sign in to comment.