Skip to content

Commit

Permalink
Merge branch 'feature/SYSTEST-9487' into feature/SYSTEST-9491
Browse files Browse the repository at this point in the history
  • Loading branch information
ksentak committed Aug 8, 2023
2 parents 8e12587 + fbe3b0b commit f28a4be
Show file tree
Hide file tree
Showing 4 changed files with 214 additions and 175 deletions.
2 changes: 1 addition & 1 deletion cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ Valid output options are:
- log (default): method call request and response objects ordered by the timestamp of the objects.
- raw: list of method call objects containing both request and response in one object.
- mock-overrides: a directory of method calls extrapolated from the raw format. This option converts the raw formats into json files or yaml files. A json file would be the response from a method call that took place when mock-firebolt processed it. Yaml files contain a function that returns a json response depending on input params. Yaml files are only generated if there were multiple of the same method call with different params.
- live: This format operates similarly to the 'log' format with an added real-time feature. As each message is received, it gets immediately written to the specified output file. In addition to accepting regular file paths, the 'live' option also supports WebSocket (WS/WSS) URLs. If a WS/WSS URL is designated as the outputPath, a WebSocket connection is established with the specified URL, and the new messages are dispatched to that connection. Please note that specifying an outputPath is essential for the 'live' option. This path is necessary whether you're sending the live log to a WebSocket URL or saving a live copy of the log file to a local directory.
- live: This format operates similarly to the 'raw' format with an added real-time feature. As each message is received, it gets immediately written to the specified output file. In addition to accepting regular file paths, the 'live' option also supports WebSocket (WS/WSS) URLs. If a WS/WSS URL is designated as the outputPath, a WebSocket connection is established with the specified URL, and the new messages are dispatched to that connection. Please note that specifying an outputPath is essential for the 'live' option. This path is necessary whether you're sending the live log to a WebSocket URL or saving a live copy of the log file to a local directory.

To change the output directory of the session recording, use the --sessionOutputPath option with start, stop, or call. This can be done at any time between starting and stopping the recording. For example, --sessionOutputPath ./output/examples will save the recording to the directory specified. The default paths for log and raw formats are ./output/sessions, and for mock-overrides is ./output/mocks.

Expand Down
2 changes: 1 addition & 1 deletion cli/src/usage.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const lines = [
{ cmdInfo: "--broadcastEvent ../examples/device-onDeviceNameChanged1.event.json", comment: "Send BroadcastEvent (method, result keys expected)" },
{ cmdInfo: "--sequence ../examples/events1.sequence.json ", comment: "Send an event sequence (See examples/device-onDeviceNameChanged.sequence.json)" },
{ cmdInfo: "--session start/stop ", comment: "Start/Stop Firebolt session recording" },
{ cmdInfo: "--sessionOutput log|raw|mock-overrides|live ", comment: "Set the output format to; log: (paired time sequence of calls, responses)|raw: similiar to log but not paired with request|mock-overrides: a directory of mock overrides|live: log messages as they are received in real time" },
{ cmdInfo: "--sessionOutput log|raw|mock-overrides|live ", comment: "Set the output format to; log: (paired time sequence of calls, responses)|raw: similiar to log but not paired with request|mock-overrides: a directory of mock overrides|live: log messages as they are received in real time - can also be a websocket url (live only)" },
{ cmdInfo: "--sessionOutputPath ../examples/path ", comment: "Specifiy the session output path. Default for 'log' format will be ./output/sessions and ./output/mocks/<START_TIME> for 'mock-overrides'. Can also be a websocket url" },
{ cmdInfo: "--getStatus ", comment: "Shows ws connection status of the user"},
{ cmdInfo: "--downloadOverrides https://github.com/myOrg/myRepo.git", comment: "Specifies the url of a github repository to clone"},
Expand Down
119 changes: 48 additions & 71 deletions server/src/sessionManagement.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -25,56 +25,61 @@ import fs from 'fs';
import yaml from 'js-yaml';
import WebSocket from 'ws';

class SessionWebSocket {
class SessionHandler {
constructor() {
this.mode = null;
this.ws = null;
this.stream = null;
}

open(dir) {
this.ws = new WebSocket(dir);
this.ws.on('open', () => {
logger.info(`Websocket connection opened: ${dir}`);
})
}

close() {
if (this.ws) {
this.ws.close();
this.ws = null;
// Determine mode based on directory/url
_determineMode(dir) {
const wsRegex = /^(ws(s)?):\/\//i;
if (wsRegex.test(dir)) {
this.mode = 'websocket';
} else {
this.mode = 'filestream';
}
}
}

class SessionFileStream {
constructor() {
this.stream = null;
}

open(dir) {
if (!fs.existsSync(dir)) {
logger.info("Directory does not exist for: " + dir);
fs.mkdirSync(dir, { recursive: true});
this._determineMode(dir);

if (this.mode === 'websocket') {
this.ws = new WebSocket(dir);
this.ws.on('open', () => {
logger.info(`Websocket connection opened: ${dir}`);
});
} else {
if (!fs.existsSync(dir)) {
logger.info("Directory does not exist for: " + dir);
fs.mkdirSync(dir, { recursive: true });
}

this.stream = fs.createWriteStream(`${dir}/FireboltCalls_live.log`, { flags: 'a' });
}

this.stream = fs.createWriteStream(`${dir}/FireboltCalls_live.log`, { flags: 'a' });
}

write(data) {
if (this.stream) {
if (this.mode === 'websocket' && this.ws) {
this.ws.send(data);
} else if (this.stream) {
this.stream.write(`${data}\n`);
}
}

close() {
if (this.stream) {
if (this.mode === 'websocket' && this.ws) {
this.ws.close();
this.ws = null;
} else if (this.stream) {
this.stream.end();
this.stream = null;
}
}
}

let sessionWebSocket = new SessionWebSocket();
let sessionFileStream = new SessionFileStream();
let sessionHandler = new SessionHandler();

class FireboltCall {
constructor(methodCall, params) {
Expand Down Expand Up @@ -112,6 +117,10 @@ class Session {
// const sessionStartString = sessionStart.toISOString().replace(/T/, '_').replace(/\..+/, '');
// logger.info(`${sessionStart.toISOString()}`);

// Check if the output path is a WebSocket URL
const wsRegex = /^(ws(s)?):\/\//i;
this.sessionOutputPath = wsRegex.test(this.sessionOutputPath) ? "./sessions/output" : this.sessionOutputPath;

if (!fs.existsSync(this.sessionOutputPath)) {
logger.info("Directory does not exist for: " + this.sessionOutputPath)
fs.mkdirSync(this.sessionOutputPath, { recursive: true});
Expand Down Expand Up @@ -406,13 +415,9 @@ function startRecording(userId) {
function stopRecording(userId) {
if (isRecording(userId)) {
logger.info('Stopping recording');
sessionRecording[userId].recording = false;
const sessionData = sessionRecording[userId].recordedSession.exportSession();
if (sessionWebSocket.ws) {
sessionWebSocket.close();
}
if (sessionFileStream.stream) {
sessionFileStream.close();
}
sessionHandler.close();
delete sessionRecording[userId];
return sessionData;
} else {
Expand All @@ -432,11 +437,7 @@ function addCall(methodCall, params, userId) {
sessionRecording[userId].recordedSession.calls.push(call);
if (sessionRecording[userId].recordedSession.sessionOutput === "live") {
const data = JSON.stringify(call);
if (sessionWebSocket.ws) {
sessionWebSocket.ws.send(data);
} else if (sessionFileStream.stream) {
sessionFileStream.write(data);
}
sessionHandler.write(data);
}
}
}
Expand All @@ -455,22 +456,12 @@ function getOutputFormat() {
}

function setOutputDir(dir, userId) {
if (!sessionRecording[userId]) {
logger.error(`No active session found for user: ${userId}`);
return;
}
const wsRegex = /^(ws(s)?):\/\//i;

if (wsRegex.test(dir)) {
sessionWebSocket.open(dir);
} else {
sessionRecording[userId].recordedSession.sessionOutputPath = dir;
sessionRecording[userId].recordedSession.mockOutputPath = dir;
logger.info(`Setting output path for user ${userId} to:`, sessionRecording[userId].recordedSession.mockOutputPath);
if (sessionRecording[userId].recordedSession.sessionOutput === "live") {
sessionFileStream.open(dir);
}
if (sessionRecording[userId].recordedSession.sessionOutput === "live") {
sessionHandler.open(dir);
}
sessionRecording[userId].recordedSession.sessionOutputPath = dir;
sessionRecording[userId].recordedSession.mockOutputPath = dir;
logger.info("Setting output path: " + sessionRecording[userId].recordedSession.mockOutputPath);
}

function getSessionOutputDir(){
Expand All @@ -490,36 +481,22 @@ function updateCallWithResponse(method, result, key, userId) {
sessionRecording[userId].recordedSession.calls.concat(...methodCalls);
if (sessionRecording[userId].recordedSession.sessionOutput === "live") {
const data = JSON.stringify(methodCalls[i]);
if (sessionWebSocket.ws) {
sessionWebSocket.ws.send(data);
} else if (sessionFileStream.stream) {
sessionFileStream.write(data);
}
sessionHandler.write(data);
}
}
}
}
}

// Utility function for unit tests
const setTestEntity = (entityName, mockEntity) => {
switch (entityName) {
case 'websocket':
sessionWebSocket = mockEntity;
break;
case 'filestream':
sessionFileStream = mockEntity;
break;
default:
throw new Error('Unknown entity name');
}
const setTestEntity = (mockEntity) => {
sessionHandler = mockEntity
}

export const testExports = {
setTestEntity,
setOutputDir,
SessionFileStream,
SessionWebSocket
SessionHandler
}

export { Session, FireboltCall, startRecording, setOutputDir, stopRecording, addCall, isRecording, updateCallWithResponse, setOutputFormat, getOutputFormat, getSessionOutputDir, getMockOutputDir };
Loading

0 comments on commit f28a4be

Please sign in to comment.