Skip to content

Commit

Permalink
Merge pull request #1691 from dcdenu4/plugins/update-port-retrieval
Browse files Browse the repository at this point in the history
Plugins: update how investUsageLogger gets port information
  • Loading branch information
dcdenu4 authored Nov 21, 2024
2 parents b7c7a14 + 5cc7ffe commit f98f272
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 11 deletions.
4 changes: 2 additions & 2 deletions workbench/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
"start": "yarn build-main && yarn build:preload && concurrently --kill-others \"yarn serve\" \"electron .\"",
"serve": "cross-env MODE=development node scripts/watch.js",
"lint": "eslint --cache --color --ext .jsx,.js src",
"test": "cross-env PORT=56788 jest --runInBand --testPathIgnorePatterns /tests/binary_tests/ /tests/sampledata_linkcheck/",
"test": "jest --runInBand --testPathIgnorePatterns /tests/binary_tests/ /tests/sampledata_linkcheck/",
"test-main": "jest --runInBand --testMatch **/tests/main/*.test.js",
"test-renderer": "jest --runInBand --testMatch **/tests/renderer/*.test.js",
"test-flask": "cross-env PORT=56788 jest --runInBand --testMatch **/tests/invest/*.test.js",
"test-flask": "jest --runInBand --testMatch **/tests/invest/*.test.js",
"test-electron-app": "jest --runInBand --testMatch **/tests/binary_tests/*.test.js",
"test-sampledata-registry": "jest --runInBand --testMatch **/tests/sampledata_linkcheck/*.test.js",
"postinstall": "electron-builder install-app-deps",
Expand Down
8 changes: 4 additions & 4 deletions workbench/src/main/investUsageLogger.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ const PREFIX = 'api';
export default function investUsageLogger() {
const sessionId = crypto.randomUUID();

function start(modelPyName, args) {
function start(modelPyName, args, port) {
logger.debug('logging model start');
fetch(`${HOSTNAME}:${process.env.PORT}/${PREFIX}/log_model_start`, {
fetch(`${HOSTNAME}:${port}/${PREFIX}/log_model_start`, {
method: 'post',
body: JSON.stringify({
model_pyname: modelPyName,
Expand All @@ -31,9 +31,9 @@ export default function investUsageLogger() {
.catch((error) => logger.error(error));
}

function exit(status) {
function exit(status, port) {
logger.debug('logging model exit');
fetch(`${HOSTNAME}:${process.env.PORT}/${PREFIX}/log_model_exit`, {
fetch(`${HOSTNAME}:${port}/${PREFIX}/log_model_exit`, {
method: 'post',
body: JSON.stringify({
session_id: sessionId,
Expand Down
7 changes: 5 additions & 2 deletions workbench/src/main/setupInvestHandlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ export function setupInvestRunHandlers() {
await writeInvestParameters(payload);
let cmd;
let cmdArgs;
let port;
const plugins = settingsStore.get('plugins');
if (plugins && Object.keys(plugins).includes(modelRunName)) {
cmd = settingsStore.get('mamba');
Expand All @@ -103,6 +104,7 @@ export function setupInvestRunHandlers() {
modelRunName,
`-d "${datastackPath}"`,
];
port = settingsStore.get(`plugins.${modelRunName}.port`);
} else {
cmd = settingsStore.get('investExe');
cmdArgs = [
Expand All @@ -112,6 +114,7 @@ export function setupInvestRunHandlers() {
'run',
modelRunName,
`-d "${datastackPath}"`];
port = settingsStore.get('core.port');
}

logger.debug(`about to run model with command: ${cmd} ${cmdArgs}`);
Expand Down Expand Up @@ -141,7 +144,7 @@ export function setupInvestRunHandlers() {
);
event.reply(`invest-logging-${tabID}`, path.resolve(investLogfile));
if (!ELECTRON_DEV_MODE && !process.env.PUPPETEER) {
usageLogger.start(pyModuleName, args);
usageLogger.start(pyModuleName, args, port);
}
}
}
Expand Down Expand Up @@ -176,7 +179,7 @@ export function setupInvestRunHandlers() {
});
});
if (!ELECTRON_DEV_MODE && !process.env.PUPPETEER) {
usageLogger.exit(investStdErr);
usageLogger.exit(investStdErr, port);
}
});
});
Expand Down
8 changes: 5 additions & 3 deletions workbench/tests/main/main.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,9 @@ describe('createWindow', () => {
});

describe('investUsageLogger', () => {
const expectedURL = `http://127.0.0.1:${process.env.PORT}/api/log_model_start`;
// Set default PORT for URL, but it's not used by the test.
const PORT = 3000;
const expectedURL = `http://127.0.0.1:${PORT}/api/log_model_start`;
beforeEach(() => {
// the expected response
const response = {
Expand All @@ -244,12 +246,12 @@ describe('investUsageLogger', () => {
const investStdErr = '';
const usageLogger = investUsageLogger();

usageLogger.start(modelPyname, args);
usageLogger.start(modelPyname, args, PORT);
expect(fetch.mock.calls).toHaveLength(1);
expect(fetch.mock.calls[0][0]).toBe(expectedURL);
const startPayload = JSON.parse(fetch.mock.calls[0][1].body);

usageLogger.exit(investStdErr);
usageLogger.exit(investStdErr, PORT);
expect(fetch.mock.calls).toHaveLength(2);
const exitPayload = JSON.parse(fetch.mock.calls[1][1].body);

Expand Down

0 comments on commit f98f272

Please sign in to comment.