Skip to content

Commit

Permalink
[OGUI-1418] Add number of active clients to status API (#2149)
Browse files Browse the repository at this point in the history
* adds the number of active clients as per the WebSocket server
* updates tests accordingly
  • Loading branch information
graduta authored Oct 5, 2023
1 parent 29923b9 commit 0025181
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 20 deletions.
5 changes: 3 additions & 2 deletions QualityControl/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* or submit itself to any jurisdiction.
*/

import { Log, HttpServer } from '@aliceo2/web-ui';
import { Log, HttpServer, WebSocket } from '@aliceo2/web-ui';
const log = new Log(`${process.env.npm_config_log_label ?? 'qcg'}/index`);
import path from 'path';
import { setup } from './lib/api.js';
Expand Down Expand Up @@ -50,4 +50,5 @@ const require = createRequire(import.meta.url);
const pathName = require.resolve('jsroot');
http.addStaticPath(path.join(pathName, '../..'), 'jsroot');

setup(http);
const ws = new WebSocket(http);
setup(http, ws);
2 changes: 1 addition & 1 deletion QualityControl/lib/QCModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const jsonDb = new JsonFileService(config.dbFile || `${__dirname}/../db.json`);
export const userService = new UserService(jsonDb);
export const layoutService = new LayoutController(jsonDb);

const statusService = new StatusService({ version: packageJSON?.version ?? '-' }, { qc: config.qc ?? {} });
export const statusService = new StatusService({ version: packageJSON?.version ?? '-' }, { qc: config.qc ?? {} });
export const statusController = new StatusController(statusService);

export let consulService = undefined;
Expand Down
12 changes: 6 additions & 6 deletions QualityControl/lib/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,18 @@
* or submit itself to any jurisdiction.
*/

import { WebSocket } from '@aliceo2/web-ui';

import { consulService, objectController, layoutService, statusController, userService } from './QCModel.js';
import {
consulService, objectController, layoutService, statusController, userService, statusService,
} from './QCModel.js';

/**
* Adds paths and binds websocket to instance of HttpServer passed
* @param {HttpServer} http - web-ui based server implementation
* @param {WebSocket} ws - web-ui websocket server implementation
* @returns {void}
*/
export const setup = (http) => {
export const setup = (http, ws) => {
statusService.ws = ws;
http.get('/object/:id', objectController.getObjectById.bind(objectController));
http.get('/object', objectController.getObjectContent.bind(objectController));
http.get('/objects', objectController.getObjects.bind(objectController), { public: true });
Expand All @@ -42,8 +44,6 @@ export const setup = (http) => {
http.get('/status/framework', statusController.getFrameworkInfo.bind(statusController), { public: true });

http.get('/checkUser', userService.addUser.bind(userService));

new WebSocket(http);
};

/**
Expand Down
17 changes: 17 additions & 0 deletions QualityControl/lib/services/Status.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ export class StatusService {
*/
this._onlineService = undefined;

/**
* @type {WebSocket}
*/
this._ws = undefined;

this._packageInfo = packageInfo;
this._config = config;
}
Expand All @@ -54,6 +59,7 @@ export class StatusService {
return {
status: { ok: true },
version: this._packageInfo?.version ?? '-',
clients: this._ws?.server?.clients?.size ?? -1,
};
}

Expand Down Expand Up @@ -128,6 +134,7 @@ export class StatusService {
/**
* Set service to be used for querying status of data layer (CCDB)
* @param {CcdbService} dataService - service used for retrieving QC objects
* @return {void}
*/
set dataService(dataService) {
this._dataService = dataService;
Expand All @@ -136,8 +143,18 @@ export class StatusService {
/**
* Set service to be used for querying status of online mode provider (Consul)
* @param {ConsulService} onlineService - service used for retrieving list of objects currently being produced
* @return {void}
*/
set onlineService(onlineService) {
this._onlineService = onlineService;
}

/**
* Set instance of websocket server
* @param {WebSocket} ws - instance of the WS server
* @return {void}
*/
set ws(ws) {
this._ws = ws;
}
}
16 changes: 5 additions & 11 deletions QualityControl/test/lib/services/status-service.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export const statusServiceTestSuite = async () => {

const response = await statusService.retrieveFrameworkInfo();
const result = {
qcg: { version: '-', status: { ok: true } },
qcg: { version: '-', status: { ok: true }, clients: -1 },
qc: { status: { ok: true }, version: 'Not part of an FLP deployment' },
data_service_ccdb: { status: { ok: true }, version: '0.0.1-beta' },
online_service_consul: { status: { ok: false, message: 'Online mode failed to retrieve' } },
Expand All @@ -96,29 +96,23 @@ export const statusServiceTestSuite = async () => {
describe('`retrieveOwnStatus()` tests', () => {
it('should successfully return an object with status and version of itself', async () => {
const statusService = new StatusService({ version: '0.0.1' });
const res = {
status: stub().returnsThis(),
json: stub(),
};
const result = statusService.retrieveOwnStatus({}, res);
const result = statusService.retrieveOwnStatus();

assert.deepStrictEqual(result, {
status: { ok: true },
version: '0.0.1',
clients: -1,
});
});

it('should successfully return an object with status and no version of itself', async () => {
const statusService = new StatusService();
const res = {
status: stub().returnsThis(),
json: stub(),
};
const result = statusService.retrieveOwnStatus({}, res);
const result = statusService.retrieveOwnStatus();

assert.deepStrictEqual(result, {
status: { ok: true },
version: '-',
clients: -1,
});
});
});
Expand Down

0 comments on commit 0025181

Please sign in to comment.