Skip to content

Commit

Permalink
testing usageLogger PORT patch
Browse files Browse the repository at this point in the history
  • Loading branch information
dcdenu4 committed Nov 20, 2024
1 parent 033b98f commit 747cda5
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
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 @@ -109,6 +109,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 @@ -124,6 +125,7 @@ export function setupInvestRunHandlers() {
modelRunName,
`-d "${datastackPath}"`,
];
port = settingsStore.get(`plugins.${modelRunName}.port`);
} else {
cmd = settingsStore.get('investExe');
cmdArgs = [
Expand All @@ -133,6 +135,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 @@ -162,7 +165,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 @@ -197,7 +200,7 @@ export function setupInvestRunHandlers() {
});
});
if (!ELECTRON_DEV_MODE && !process.env.PUPPETEER) {
usageLogger.exit(investStdErr);
usageLogger.exit(investStdErr, port);
}
});
});
Expand Down
7 changes: 4 additions & 3 deletions workbench/tests/main/main.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,8 @@ describe('createWindow', () => {
});

describe('investUsageLogger', () => {
const expectedURL = `http://127.0.0.1:${process.env.PORT}/api/log_model_start`;
const PORT = 3000;
const expectedURL = `http://127.0.0.1:${PORT}/api/log_model_start`;
beforeEach(() => {
// the expected response
const response = {
Expand All @@ -246,12 +247,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 747cda5

Please sign in to comment.