Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[OGUI-1418] Add number of active sessions to /status/gui route #2147

Merged
merged 2 commits into from
Oct 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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