Skip to content

Commit

Permalink
[OGUI-1552] Use LogLevel from WebUI (#2596)
Browse files Browse the repository at this point in the history
* updates the codebase to use the enum `LogLevel` from the common framework rather than the local declared one
* removes the local enum file
  • Loading branch information
graduta authored Sep 14, 2024
1 parent fc57bfb commit a607369
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 97 deletions.
25 changes: 0 additions & 25 deletions Control/lib/common/logLevel.enum.js

This file was deleted.

29 changes: 12 additions & 17 deletions Control/lib/control-core/ApricotService.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,13 @@
*/

const assert = require('assert');
const logger = (require('@aliceo2/web-ui').LogManager)
.getLogger(`${process.env.npm_config_log_label ?? 'cog'}/apricotservice`);
const {LogManager, LogLevel} = require('@aliceo2/web-ui');
const {errorHandler, errorLogger} = require('./../utils.js');
const CoreEnvConfig = require('../dtos/CoreEnvConfig.js');
const CoreUtils = require('./CoreUtils.js');
const COMPONENT = 'COG-v1';
const {User} = require('./../dtos/User.js');
const {APRICOT_COMMANDS: {ListRuntimeEntries, GetRuntimeEntry}} = require('./ApricotCommands.js');
const {LOG_LEVEL} = require('../common/logLevel.enum.js');
const LOG_FACILITY = 'cog/apricotservice';

/**
Expand All @@ -35,6 +33,7 @@ class ApricotService {
constructor(apricotProxy) {
assert(apricotProxy, 'Missing GrpcProxy dependency for Apricot');
this.apricotProxy = apricotProxy;
this._logger = LogManager.getLogger(`${process.env.npm_config_log_label ?? 'cog'}/apricotservice`);

this.detectors = [];
this.hostsByDetector = new Map();
Expand All @@ -46,7 +45,7 @@ class ApricotService {
async init() {
try {
this.detectors = (await this.apricotProxy['ListDetectors']()).detectors;
logger.infoMessage(`Initial data retrieved from AliECS/Apricot: ${this.detectors} detectors`, {
this._logger.infoMessage(`Initial data retrieved from AliECS/Apricot: ${this.detectors} detectors`, {
level: 99,
system: 'GUI',
facility: 'cog/api'
Expand All @@ -57,12 +56,12 @@ class ApricotService {
const {hosts} = await this.apricotProxy['GetHostInventory']({detector});
this.hostsByDetector.set(detector, hosts);
} catch (error) {
logger.error(`Unable to retrieve list of hosts for detector: ${detector}`);
this._logger.error(`Unable to retrieve list of hosts for detector: ${detector}`);
}
})
);
} catch (error) {
logger.error('Unable to list detectors');
this._logger.error('Unable to list detectors');
}
}

Expand Down Expand Up @@ -125,8 +124,8 @@ class ApricotService {
const {hosts} = await this.apricotProxy['GetHostInventory']({detector});
this.hostsByDetector.set(detector, hosts);
} catch (error) {
logger.error(`Unable to retrieve list of hosts for detector: ${detector}`);
logger.error(error);
this._logger.error(`Unable to retrieve list of hosts for detector: ${detector}`);
this._logger.error(error);
}
})
);
Expand All @@ -151,7 +150,7 @@ class ApricotService {
throw new Error('Unable to check status of Apricot')
}
} catch (error) {
logger.error(error);
this._logger.error(error);
throw error;
}
}
Expand All @@ -164,10 +163,6 @@ class ApricotService {
executeCommand(req, res) {
const method = CoreUtils.parseMethodNameString(req.path);
if (this.apricotProxy?.isConnectionReady && method) {
if (!method.startsWith('Get')) {
const type = req.body.type ? ` (${req.body.type})` : '';
logger.info(`${req.session.personid} => ${method} ${type}`, 6);
}
this.apricotProxy[method](req.body)
.then((response) => res.json(response))
.catch((error) => errorHandler(error, res, 504, 'apricotservice'));
Expand Down Expand Up @@ -199,9 +194,9 @@ class ApricotService {
errorHandler(`A configuration with name '${envConf.id}' already exists. `
+ 'Please load existing configuration and use \'Update\'', res, 409, 'apricotservice');
} else {
logger.infoMessage(
this._logger.infoMessage(
`${req.session.username} request to save new core environment configuration "${envConf.id}"`,
{level: LOG_LEVEL.OPERATIONS, system: 'GUI', facility: LOG_FACILITY}
{level: LogLevel.OPERATIONS, system: 'GUI', facility: LOG_FACILITY}
);
await this.apricotProxy['SetRuntimeEntry']({component: COMPONENT, key: envConf.id, value: envConf.toString()});
res.status(201).json({message: `Configuration successfully saved as ${envConf.id}`});
Expand Down Expand Up @@ -229,9 +224,9 @@ class ApricotService {
const envConf = CoreEnvConfig.fromJSON(data);

const envConfigToSave = await this._getUpdatedConfigIfExists(envConf, user);
logger.infoMessage(
this._logger.infoMessage(
`${req.session.username} requested to update new core environment configuration "${envConf.id}"`,
{level: LOG_LEVEL.OPERATIONS, system: 'GUI', facility: LOG_FACILITY}
{level: LogLevel.OPERATIONS, system: 'GUI', facility: LOG_FACILITY}
);
await this.apricotProxy['SetRuntimeEntry']({
component: COMPONENT,
Expand Down
15 changes: 7 additions & 8 deletions Control/lib/control-core/ControlService.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,9 @@

const assert = require('assert');
const path = require('path');
const {WebSocketMessage, LogManager} = require('@aliceo2/web-ui');
const logger = LogManager.getLogger(`${process.env.npm_config_log_label ?? 'cog'}/controlservice`);
const {WebSocketMessage, LogManager, LogLevel} = require('@aliceo2/web-ui');
const {errorHandler, errorLogger} = require('./../utils.js');
const CoreUtils = require('./CoreUtils.js');
const {LOG_LEVEL} = require('../common/logLevel.enum.js');

/**
* Gateway for all AliECS - Core calls
Expand All @@ -37,6 +35,7 @@ class ControlService {
this.consulController = consulController;
this.coreConfig = coreConfig;
this.O2_CONTROL_PROTO_PATH = O2_CONTROL_PROTO_PATH;
this._logger = LogManager.getLogger(`${process.env.npm_config_log_label ?? 'cog'}/controlservice`);

this.intervalHeartBeat = this.initiateHeartBeat();
}
Expand All @@ -60,7 +59,7 @@ class ControlService {
await this.ctrlProx['GetEnvironments']({}, {deadline: Date.now() + 9000});
} catch (err) {
const stateCode = this.ctrlProx.client.getChannel().getConnectivityState();
logger.errorMessage(`Unable to reach AliECS (state: ${stateCode}), attempting reconnection`, {
this._logger.errorMessage(`Unable to reach AliECS (state: ${stateCode}), attempting reconnection`, {
level: 20,
system: 'GUI',
facility: 'cog/controlservice'
Expand Down Expand Up @@ -126,8 +125,8 @@ class ControlService {
vars,
workflowTemplate: path.join(repositoryName, `workflows/${operation}@${defaultRevision}`),
};
logger.infoMessage(`Request of user: ${req.session.username} to "${operation}" for ${channelId}`, {
level: LOG_LEVEL.OPERATIONS, system: 'GUI', facility: 'cog/controlservice'
this._logger.infoMessage(`Request of user: ${req.session.username} to "${operation}" for ${channelId}`, {
level: LogLevel.OPERATIONS, system: 'GUI', facility: 'cog/controlservice'
});
await this.ctrlProx[method](coreConf);
res.status(200).json({
Expand Down Expand Up @@ -221,7 +220,7 @@ class ControlService {
const personid = req?.session?.personid ?? '';
if (method.startsWith('New') || method.startsWith('CleanupTasks')) {
const operation = req.body.operation ? ` (${req.body.operation})` : '';
logger.infoMessage(`${username}(${personid}) => ${method} ${operation}`, {
this._logger.infoMessage(`${username}(${personid}) => ${method} ${operation}`, {
level: 1, facility: 'cog/controlservice'
});
} else if (method.startsWith('Control') || method.startsWith('Destroy')) {
Expand All @@ -230,7 +229,7 @@ class ControlService {
const run = req.body.runNumber;
delete req.body.runNumber;

logger.infoMessage(`${username}(${personid}) => ${method} ${type}`, {
this._logger.infoMessage(`${username}(${personid}) => ${method} ${type}`, {
level: 1, facility: 'cog/controlservice', partition, run
});
}
Expand Down
13 changes: 6 additions & 7 deletions Control/lib/control-core/RequestHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,14 @@
* granted to it by virtue of its status as an Intergovernmental Organization
* or submit itself to any jurisdiction.
*/
const {WebSocketMessage, LogManager} = require('@aliceo2/web-ui');
const {WebSocketMessage, LogManager, LogLevel} = require('@aliceo2/web-ui');
const {errorLogger} = require('./../utils.js');
const CoreUtils = require('./CoreUtils.js');
const {
RUNTIME_COMPONENT: {COG},
RUNTIME_KEY: {RUN_TYPE_TO_HOST_MAPPING}
} = require('../common/kvStore/runtime.enum.js');
const { User } = require('../dtos/User.js');
const {LOG_LEVEL} = require('../common/logLevel.enum.js');
const LOG_FACILITY = 'cog/controlrequests';

/**
Expand Down Expand Up @@ -68,7 +67,7 @@ class RequestHandler {
logMessage += `and detectors: ${req.body.detectors}`;
}

this._logger.infoMessage(logMessage, {level: LOG_LEVEL.OPERATIONS, system: 'GUI', facility: LOG_FACILITY});
this._logger.infoMessage(logMessage, {level: LogLevel.OPERATIONS, system: 'GUI', facility: LOG_FACILITY});

this.requestList[index] = {
id: index,
Expand Down Expand Up @@ -120,7 +119,7 @@ class RequestHandler {
} catch (error) {
if (error.envId) {
this._logger.errorMessage(`Creation of environment failed with: ${error.details}.`, {
level: LOG_LEVEL.ERROR, system: 'GUI', facility: LOG_FACILITY, partition: error.envId
level: LogLevel.ERROR, system: 'GUI', facility: LOG_FACILITY, partition: error.envId
});
let logMessage = `Environment was requested by user: ${username} with`;
if (req.body.workflowTemplate) {
Expand All @@ -130,7 +129,7 @@ class RequestHandler {
logMessage += `and detectors: ${req.body.detectors}.`;
}
this._logger.errorMessage(logMessage, {
level: LOG_LEVEL.ERROR, system: 'GUI', facility: LOG_FACILITY, partition: error.envId
level: LogLevel.ERROR, system: 'GUI', facility: LOG_FACILITY, partition: error.envId
});
} else {
let logMessage = `Creation of environment failed with: ${error.details}. `;
Expand All @@ -142,7 +141,7 @@ class RequestHandler {
logMessage += `and detectors: ${req.body.detectors}.`;
}
this._logger.errorMessage(logMessage, {
level: LOG_LEVEL.ERROR, system: 'GUI', facility: LOG_FACILITY,
level: LogLevel.ERROR, system: 'GUI', facility: LOG_FACILITY,
});
}

Expand All @@ -167,7 +166,7 @@ class RequestHandler {
remove(req, res) {
const index = req.params.id;
this._logger.infoMessage(`User ${req.session.username} acknowledged and removed failed request`, {
level: LOG_LEVEL.OPERATIONS, system: 'GUI', facility: LOG_FACILITY
level: LogLevel.OPERATIONS, system: 'GUI', facility: LOG_FACILITY
});
delete this.requestList[index];
return this.getAll(req, res);
Expand Down
26 changes: 13 additions & 13 deletions Control/lib/controllers/Consul.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,9 @@
* or submit itself to any jurisdiction.
*/

const logger = (require('@aliceo2/web-ui').LogManager)
.getLogger(`${process.env.npm_config_log_label ?? 'cog'}/consul`);
const { LogManager, LogLevel } = require('@aliceo2/web-ui');
const {errorHandler, errorLogger} = require('../utils.js');
const {getConsulConfig} = require('../config/publicConfigProvider.js');
const {LOG_LEVEL} = require('../common/logLevel.enum.js');

/**
* Gateway for all Consul Consumer calls
Expand All @@ -37,6 +35,8 @@ class ConsulController {
this.qcPath = this.config.qcPath;
this.readoutPath = this.config.readoutPath;
this.kVPrefix = this.config.kVPrefix;

this._logger = LogManager.getLogger(`${process.env.npm_config_log_label ?? 'cog'}/consul`);
}

/**
Expand All @@ -60,8 +60,8 @@ class ConsulController {
*/
async testConsulStatus() {
this.consulService.getConsulLeaderStatus()
.then((data) => logger.info(`Service is up and running on: ${data}`))
.catch((error) => logger.error(`Connection failed due to ${error}`));
.then((data) => this._logger.info(`Service is up and running on: ${data}`))
.catch((error) => this._logger.error(`Connection failed due to ${error}`));
}

/**
Expand All @@ -86,8 +86,8 @@ class ConsulController {
res.json(crusByHost);
}).catch((error) => {
if (error.message.includes('404')) {
logger.trace(error);
logger.error(`Could not find any Readout Cards by key ${this.flpHardwarePath}`);
this._logger.trace(error);
this._logger.error(`Could not find any Readout Cards by key ${this.flpHardwarePath}`);
errorHandler(`Could not find any Readout Cards by key ${this.flpHardwarePath}`, res, 404);
} else {
errorHandler(error, res, 502);
Expand All @@ -112,8 +112,8 @@ class ConsulController {
})
.catch((error) => {
if (error.message.includes('404')) {
logger.trace(error);
logger.error(`Could not find any FLPs by key ${this.flpHardwarePath}`);
this._logger.trace(error);
this._logger.error(`Could not find any FLPs by key ${this.flpHardwarePath}`);
errorHandler(`Could not find any FLPs by key ${this.flpHardwarePath}`, res, 404);
} else {
errorHandler(error, res, 502);
Expand Down Expand Up @@ -211,8 +211,8 @@ class ConsulController {
const crusByHost = req.body;
const keyValues = this._mapToKVPairs(crusByHost, latestCrusWithConfigByHost);
try {
logger.infoMessage(`Request of user: ${req.session.username} to update CRU configuration`, {
level: LOG_LEVEL.OPERATIONS, system: 'GUI', facility: 'cog/consul'
this._logger.infoMessage(`Request of user: ${req.session.username} to update CRU configuration`, {
level: LogLevel.OPERATIONS, system: 'GUI', facility: 'cog/consul'
});
await this.consulService.putListOfKeyValues(keyValues);
res.status(200).json({info: {message: 'CRUs Configuration saved'}});
Expand All @@ -235,8 +235,8 @@ class ConsulController {
return [...new Set(flpList)];
} catch (error) {
if (error.message.includes('404')) {
logger.trace(error);
logger.error(`Could not find any FLPs by key ${this.flpHardwarePath}`);
this._logger.trace(error);
this._logger.error(`Could not find any FLPs by key ${this.flpHardwarePath}`);
throw new Error(`Could not find any FLPs by key ${this.flpHardwarePath}`);
}
}
Expand Down
15 changes: 7 additions & 8 deletions Control/lib/controllers/Environment.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,14 @@
* granted to it by virtue of its status as an Intergovernmental Organization
* or submit itself to any jurisdiction.
*/
const {LogManager} = require('@aliceo2/web-ui');
const {LogManager, LogLevel} = require('@aliceo2/web-ui');
const LOG_FACILITY = 'cog/env-ctrl';
const {EnvironmentTransitionType} = require('./../common/environmentTransitionType.enum.js');
const {grpcErrorToNativeError} = require('./../errors/grpcErrorToNativeError.js');
const {InvalidInputError} = require('./../errors/InvalidInputError.js');
const {UnauthorizedAccessError} = require('./../errors/UnauthorizedAccessError.js');
const {updateExpressResponseFromNativeError} = require('./../errors/updateExpressResponseFromNativeError.js');
const {User} = require('./../dtos/User.js');
const {LOG_LEVEL} = require('../common/logLevel.enum.js');

/**
* Controller for dealing with all API requests on environments from AliECS:
Expand Down Expand Up @@ -96,15 +95,15 @@ class EnvironmentController {
const transitionRequestedAt = Date.now();
let response = null;
this._logger.infoMessage(`Request to transition environment by ${req.session.username} to ${transitionType}`,
{level: LOG_LEVEL.OPERATIONS, system: 'GUI', facility: LOG_FACILITY, partition: id, run: runNumber}
{level: LogLevel.OPERATIONS, system: 'GUI', facility: LOG_FACILITY, partition: id, run: runNumber}
);
try {
response = await this._envService.transitionEnvironment(id, transitionType, user);
res.status(200).json(response);
} catch (error) {
this._logger.errorMessage(
`Request to transition environment by ${req.session.username} to ${transitionType} failed due to ${error}`,
{level: LOG_LEVEL.OPERATIONS, system: 'GUI', facility: LOG_FACILITY, partition: id, run: runNumber}
{level: LogLevel.OPERATIONS, system: 'GUI', facility: LOG_FACILITY, partition: id, run: runNumber}
);
updateExpressResponseFromNativeError(res, error);
}
Expand All @@ -130,15 +129,15 @@ class EnvironmentController {
} else {
const destroyRequestedAt = Date.now();
this._logger.infoMessage(`Request to destroy environment by ${req.session.username} by force: ${force}`,
{level: LOG_LEVEL.OPERATIONS, system: 'GUI', facility: LOG_FACILITY, partition: id, run: runNumber}
{level: LogLevel.OPERATIONS, system: 'GUI', facility: LOG_FACILITY, partition: id, run: runNumber}
);
try {
const response = await this._envService.destroyEnvironment(id, {keepTasks, allowInRunningState, force}, user);
res.status(200).json(response);
} catch (error) {
this._logger.errorMessage(
`Request to destroy environment by ${req.session.username} failed due to ${error}`,
{level: LOG_LEVEL.OPERATIONS, system: 'GUI', facility: LOG_FACILITY, partition: id, run: runNumber}
{level: LogLevel.OPERATIONS, system: 'GUI', facility: LOG_FACILITY, partition: id, run: runNumber}
);
updateExpressResponseFromNativeError(res, error);
}
Expand Down Expand Up @@ -206,7 +205,7 @@ class EnvironmentController {
// Attempt to deploy environment
try {
this._logger.infoMessage(`Request by username(${username}) to deploy configuration ${configurationName}`,
{level: LOG_LEVEL.OPERATIONS, system: 'GUI', facility: LOG_FACILITY}
{level: LogLevel.OPERATIONS, system: 'GUI', facility: LOG_FACILITY}
);
const environment = await this._envService.newAutoEnvironment(
workflowTemplatePath, variables, detector, runType, user
Expand All @@ -215,7 +214,7 @@ class EnvironmentController {
} catch (error) {
this._logger.errorMessage(
`Unable to deploy request by username(${username}) for ${configurationName} due to error`,
{level: LOG_LEVEL.OPERATIONS, system: 'GUI', facility: LOG_FACILITY}
{level: LogLevel.OPERATIONS, system: 'GUI', facility: LOG_FACILITY}
);
updateExpressResponseFromNativeError(res, error);
}
Expand Down
Loading

0 comments on commit a607369

Please sign in to comment.