From 16b7aa6abbbc97deb0989a86b6e7f2933ddec8e2 Mon Sep 17 00:00:00 2001 From: Sabir Hassan Date: Fri, 9 Sep 2022 00:49:26 +0500 Subject: [PATCH] chore: merge js, py and r session controller classes to base session controller class --- api/src/controllers/internal/Session.ts | 187 +++++------------------- api/src/routes/api/spec/stp.spec.ts | 11 +- api/src/types/system/process.d.ts | 5 +- 3 files changed, 41 insertions(+), 162 deletions(-) diff --git a/api/src/controllers/internal/Session.ts b/api/src/controllers/internal/Session.ts index a62a019..56e7df8 100644 --- a/api/src/controllers/internal/Session.ts +++ b/api/src/controllers/internal/Session.ts @@ -19,13 +19,41 @@ import { const execFilePromise = promisify(execFile) -abstract class SessionController { +export class SessionController { protected sessions: Session[] = [] protected getReadySessions = (): Session[] => this.sessions.filter((sess: Session) => sess.ready && !sess.consumed) - protected abstract createSession(): Promise + protected async createSession(): Promise { + const sessionId = generateUniqueFileName(generateTimestamp()) + const sessionFolder = path.join(getSessionsFolder(), sessionId) + + const creationTimeStamp = sessionId.split('-').pop() as string + // death time of session is 15 mins from creation + const deathTimeStamp = ( + parseInt(creationTimeStamp) + + 15 * 60 * 1000 - + 1000 + ).toString() + + const session: Session = { + id: sessionId, + ready: true, + inUse: true, + consumed: false, + completed: false, + creationTimeStamp, + deathTimeStamp, + path: sessionFolder + } + + const headersPath = path.join(session.path, 'stpsrv_header.txt') + await createFile(headersPath, 'Content-type: text/plain') + + this.sessions.push(session) + return session + } public async getSession() { const readySessions = this.getReadySessions() @@ -167,158 +195,17 @@ ${autoExecContent}` } } -export class JSSessionController extends SessionController { - protected async createSession(): Promise { - const sessionId = generateUniqueFileName(generateTimestamp()) - const sessionFolder = path.join(getSessionsFolder(), sessionId) - - const creationTimeStamp = sessionId.split('-').pop() as string - // death time of session is 15 mins from creation - const deathTimeStamp = ( - parseInt(creationTimeStamp) + - 15 * 60 * 1000 - - 1000 - ).toString() - - const session: Session = { - id: sessionId, - ready: true, - inUse: true, - consumed: false, - completed: false, - creationTimeStamp, - deathTimeStamp, - path: sessionFolder - } - - const headersPath = path.join(session.path, 'stpsrv_header.txt') - await createFile(headersPath, 'Content-type: text/plain') - - this.sessions.push(session) - return session - } -} - -export class PythonSessionController extends SessionController { - protected async createSession(): Promise { - const sessionId = generateUniqueFileName(generateTimestamp()) - const sessionFolder = path.join(getSessionsFolder(), sessionId) - - const creationTimeStamp = sessionId.split('-').pop() as string - // death time of session is 15 mins from creation - const deathTimeStamp = ( - parseInt(creationTimeStamp) + - 15 * 60 * 1000 - - 1000 - ).toString() - - const session: Session = { - id: sessionId, - ready: true, - inUse: true, - consumed: false, - completed: false, - creationTimeStamp, - deathTimeStamp, - path: sessionFolder - } - - const headersPath = path.join(session.path, 'stpsrv_header.txt') - await createFile(headersPath, 'Content-type: text/plain') - - this.sessions.push(session) - return session - } -} - -export class RSessionController extends SessionController { - protected async createSession(): Promise { - const sessionId = generateUniqueFileName(generateTimestamp()) - const sessionFolder = path.join(getSessionsFolder(), sessionId) - - const creationTimeStamp = sessionId.split('-').pop() as string - // death time of session is 15 mins from creation - const deathTimeStamp = ( - parseInt(creationTimeStamp) + - 15 * 60 * 1000 - - 1000 - ).toString() - - const session: Session = { - id: sessionId, - ready: true, - inUse: true, - consumed: false, - completed: false, - creationTimeStamp, - deathTimeStamp, - path: sessionFolder - } - - const headersPath = path.join(session.path, 'stpsrv_header.txt') - await createFile(headersPath, 'Content-type: text/plain') - - this.sessions.push(session) - return session - } -} - export const getSessionController = ( runTime: RunTimeType -): - | SASSessionController - | JSSessionController - | PythonSessionController - | RSessionController => { - if (runTime === RunTimeType.SAS) { - return getSASSessionController() - } +): SessionController => { + if (process.sessionController) return process.sessionController - if (runTime === RunTimeType.JS) { - return getJSSessionController() - } + process.sessionController = + runTime === RunTimeType.SAS + ? new SASSessionController() + : new SessionController() - if (runTime === RunTimeType.PY) { - return getPythonSessionController() - } - - if (runTime === RunTimeType.R) { - return getRSessionController() - } - - throw new Error('No Runtime is configured') -} - -const getSASSessionController = (): SASSessionController => { - if (process.sasSessionController) return process.sasSessionController - - process.sasSessionController = new SASSessionController() - - return process.sasSessionController -} - -const getJSSessionController = (): JSSessionController => { - if (process.jsSessionController) return process.jsSessionController - - process.jsSessionController = new JSSessionController() - - return process.jsSessionController -} - -const getPythonSessionController = (): PythonSessionController => { - if (process.pythonSessionController) return process.pythonSessionController - - process.pythonSessionController = new PythonSessionController() - - return process.pythonSessionController -} - -const getRSessionController = (): RSessionController => { - if (process.rSessionController) return process.rSessionController - - process.rSessionController = new RSessionController() - - return process.rSessionController + return process.sessionController } const autoExecContent = ` diff --git a/api/src/routes/api/spec/stp.spec.ts b/api/src/routes/api/spec/stp.spec.ts index 7304a4f..1512378 100644 --- a/api/src/routes/api/spec/stp.spec.ts +++ b/api/src/routes/api/spec/stp.spec.ts @@ -21,9 +21,8 @@ import { } from '../../../utils' import { createFile, generateTimestamp, deleteFolder } from '@sasjs/utils' import { - SASSessionController, - JSSessionController, - PythonSessionController + SessionController, + SASSessionController } from '../../../controllers/internal' import * as ProcessProgramModule from '../../../controllers/internal/processProgram' import { Session } from '../../../types' @@ -472,11 +471,7 @@ const setupMocks = async () => { .mockImplementation(mockedGetSession) jest - .spyOn(JSSessionController.prototype, 'getSession') - .mockImplementation(mockedGetSession) - - jest - .spyOn(PythonSessionController.prototype, 'getSession') + .spyOn(SASSessionController.prototype, 'getSession') .mockImplementation(mockedGetSession) jest diff --git a/api/src/types/system/process.d.ts b/api/src/types/system/process.d.ts index 4be0801..2090755 100644 --- a/api/src/types/system/process.d.ts +++ b/api/src/types/system/process.d.ts @@ -7,10 +7,7 @@ declare namespace NodeJS { driveLoc: string logsLoc: string logsUUID: string - sasSessionController?: import('../../controllers/internal').SASSessionController - jsSessionController?: import('../../controllers/internal').JSSessionController - pythonSessionController?: import('../../controllers/internal').PythonSessionController - rSessionController?: import('../../controllers/internal').RSessionController + sessionController?: import('../../controllers/internal').SessionController appStreamConfig: import('../').AppStreamConfig logger: import('@sasjs/utils/logger').Logger runTimes: import('../../utils').RunTimeType[]