1
0
mirror of https://github.com/sasjs/server.git synced 2025-12-11 03:34:35 +00:00

Merge pull request #42 from sasjs/run-sas-code-route

Run sas code route
This commit is contained in:
Muhammad Saad
2022-01-06 16:23:07 +04:00
committed by GitHub
11 changed files with 278 additions and 166 deletions

View File

@@ -92,6 +92,16 @@ components:
- clientSecret - clientSecret
type: object type: object
additionalProperties: false additionalProperties: false
ExecuteSASCodePayload:
properties:
code:
type: string
description: 'Code of SAS program'
example: '* SAS Code HERE;'
required:
- code
type: object
additionalProperties: false
MemberType.folder: MemberType.folder:
enum: enum:
- folder - folder
@@ -358,16 +368,6 @@ components:
- description - description
type: object type: object
additionalProperties: false additionalProperties: false
RunSASPayload:
properties:
code:
type: string
description: 'Code of SAS program'
example: '* SAS Code HERE;'
required:
- code
type: object
additionalProperties: false
ExecuteReturnJsonResponse: ExecuteReturnJsonResponse:
properties: properties:
status: status:
@@ -511,6 +511,30 @@ paths:
application/json: application/json:
schema: schema:
$ref: '#/components/schemas/ClientPayload' $ref: '#/components/schemas/ClientPayload'
/SASjsApi/code/execute:
post:
operationId: ExecuteSASCode
responses:
'200':
description: Ok
content:
application/json:
schema:
type: string
description: 'Execute SAS code.'
summary: 'Run SAS Code and returns log'
tags:
- CODE
security:
-
bearerAuth: []
parameters: []
requestBody:
required: true
content:
application/json:
schema:
$ref: '#/components/schemas/ExecuteSASCodePayload'
/SASjsApi/drive/deploy: /SASjsApi/drive/deploy:
post: post:
operationId: Deploy operationId: Deploy
@@ -982,6 +1006,26 @@ paths:
format: double format: double
type: number type: number
example: '6789' example: '6789'
/SASjsApi/session:
get:
operationId: Session
responses:
'200':
description: Ok
content:
application/json:
schema:
$ref: '#/components/schemas/UserResponse'
examples:
'Example 1':
value: {id: 123, username: johnusername, displayName: John}
summary: 'Get session info (username).'
tags:
- Session
security:
-
bearerAuth: []
parameters: []
/SASjsApi/stp/execute: /SASjsApi/stp/execute:
get: get:
operationId: ExecuteReturnRaw operationId: ExecuteReturnRaw
@@ -1037,50 +1081,6 @@ paths:
application/json: application/json:
schema: schema:
$ref: '#/components/schemas/ExecuteReturnJsonPayload' $ref: '#/components/schemas/ExecuteReturnJsonPayload'
/SASjsApi/stp/run:
post:
operationId: RunSAS
responses:
'200':
description: Ok
content:
application/json:
schema:
type: string
description: 'Trigger a SAS program.'
summary: 'Run SAS Program, return raw content'
tags:
- STP
security:
-
bearerAuth: []
parameters: []
requestBody:
required: true
content:
application/json:
schema:
$ref: '#/components/schemas/RunSASPayload'
/SASjsApi/session:
get:
operationId: Session
responses:
'200':
description: Ok
content:
application/json:
schema:
$ref: '#/components/schemas/UserResponse'
examples:
'Example 1':
value: {id: 123, username: johnusername, displayName: John}
summary: 'Get session info (username).'
tags:
- Session
security:
-
bearerAuth: []
parameters: []
servers: servers:
- -
url: / url: /
@@ -1106,3 +1106,6 @@ tags:
- -
name: STP name: STP
description: 'Operations about STP' description: 'Operations about STP'
-
name: CODE
description: 'Operations on SAS code'

View File

@@ -0,0 +1,63 @@
import express from 'express'
import { Request, Security, Route, Tags, Post, Body } from 'tsoa'
import { ExecutionController } from './internal'
import { PreProgramVars } from '../types'
interface ExecuteSASCodePayload {
/**
* Code of SAS program
* @example "* SAS Code HERE;"
*/
code: string
}
@Security('bearerAuth')
@Route('SASjsApi/code')
@Tags('CODE')
export class CodeController {
/**
* Execute SAS code.
* @summary Run SAS Code and returns log
*/
@Post('/execute')
public async executeSASCode(
@Request() request: express.Request,
@Body() body: ExecuteSASCodePayload
): Promise<string> {
return executeSASCode(request, body)
}
}
const executeSASCode = async (req: any, { code }: ExecuteSASCodePayload) => {
try {
const result = await new ExecutionController().executeProgram(
code,
getPreProgramVariables(req),
{ ...req.query, _debug: 131 },
undefined,
true
)
return result as string
} catch (err: any) {
throw {
code: 400,
status: 'failure',
message: 'Job execution failed.',
error: typeof err === 'object' ? err.toString() : err
}
}
}
const getPreProgramVariables = (req: any): PreProgramVars => {
const host = req.get('host')
const protocol = req.protocol + '://'
const { user, accessToken } = req
return {
username: user.username,
userId: user.userId,
displayName: user.displayName,
serverUrl: protocol + host,
accessToken
}
}

View File

@@ -1,7 +1,8 @@
export * from './auth' export * from './auth'
export * from './client' export * from './client'
export * from './code'
export * from './drive' export * from './drive'
export * from './group' export * from './group'
export * from './session'
export * from './stp' export * from './stp'
export * from './user' export * from './user'
export * from './session'

View File

@@ -110,7 +110,7 @@ export class SessionController {
// TODO: don't wait forever // TODO: don't wait forever
while ((await fileExists(codeFilePath)) && !session.crashed) {} while ((await fileExists(codeFilePath)) && !session.crashed) {}
console.log('session crashed?', !!session.crashed, session.crashed) console.log('session crashed?', !!session.crashed, session.crashed || '')
session.ready = true session.ready = true
return Promise.resolve(session) return Promise.resolve(session)

View File

@@ -5,13 +5,6 @@ import { ExecutionController } from './internal'
import { PreProgramVars } from '../types' import { PreProgramVars } from '../types'
import { getTmpFilesFolderPath, makeFilesNamesMap } from '../utils' import { getTmpFilesFolderPath, makeFilesNamesMap } from '../utils'
interface RunSASPayload {
/**
* Code of SAS program
* @example "* SAS Code HERE;"
*/
code: string
}
interface ExecuteReturnJsonPayload { interface ExecuteReturnJsonPayload {
/** /**
* Location of SAS program * Location of SAS program
@@ -48,18 +41,6 @@ export class STPController {
return executeReturnRaw(request, _program) return executeReturnRaw(request, _program)
} }
/**
* Trigger a SAS program.
* @summary Run SAS Program, return raw content
*/
@Post('/run')
public async runSAS(
@Request() request: express.Request,
@Body() body: RunSASPayload
): Promise<string> {
return runSAS(request, body)
}
/** /**
* Trigger a SAS program using it's location in the _program parameter. * Trigger a SAS program using it's location in the _program parameter.
* Enable debugging using the _debug parameter. * Enable debugging using the _debug parameter.
@@ -109,25 +90,6 @@ const executeReturnRaw = async (
} }
} }
const runSAS = async (req: any, { code }: RunSASPayload) => {
try {
const result = await new ExecutionController().executeProgram(
code,
getPreProgramVariables(req),
req.query
)
return result as string
} catch (err: any) {
throw {
code: 400,
status: 'failure',
message: 'Job execution failed.',
error: typeof err === 'object' ? err.toString() : err
}
}
}
const executeReturnJson = async ( const executeReturnJson = async (
req: any, req: any,
_program: string _program: string

View File

@@ -0,0 +1,25 @@
import express from 'express'
import { runSASValidation } from '../../utils'
import { CodeController } from '../../controllers/'
const runRouter = express.Router()
const controller = new CodeController()
runRouter.post('/execute', async (req, res) => {
const { error, value: body } = runSASValidation(req.body)
if (error) return res.status(400).send(error.details[0].message)
try {
const response = await controller.executeSASCode(req, body)
res.send(response)
} catch (err: any) {
const statusCode = err.code
delete err.code
res.status(statusCode).send(err)
}
})
export default runRouter

View File

@@ -11,6 +11,7 @@ import {
import driveRouter from './drive' import driveRouter from './drive'
import stpRouter from './stp' import stpRouter from './stp'
import codeRouter from './code'
import userRouter from './user' import userRouter from './user'
import groupRouter from './group' import groupRouter from './group'
import clientRouter from './client' import clientRouter from './client'
@@ -31,6 +32,7 @@ router.use(
router.use('/drive', authenticateAccessToken, driveRouter) router.use('/drive', authenticateAccessToken, driveRouter)
router.use('/group', desktopRestrict, groupRouter) router.use('/group', desktopRestrict, groupRouter)
router.use('/stp', authenticateAccessToken, stpRouter) router.use('/stp', authenticateAccessToken, stpRouter)
router.use('/code', authenticateAccessToken, codeRouter)
router.use('/user', desktopRestrict, userRouter) router.use('/user', desktopRestrict, userRouter)
router.use( router.use(
'/', '/',

View File

@@ -24,22 +24,6 @@ stpRouter.get('/execute', async (req, res) => {
} }
}) })
stpRouter.post('/run', async (req, res) => {
const { error, value: body } = runSASValidation(req.body)
if (error) return res.status(400).send(error.details[0].message)
try {
const response = await controller.runSAS(req, body)
res.send(response)
} catch (err: any) {
const statusCode = err.code
delete err.code
res.status(statusCode).send(err)
}
})
stpRouter.post( stpRouter.post(
'/execute', '/execute',
fileUploadController.preuploadMiddleware, fileUploadController.preuploadMiddleware,

View File

@@ -38,6 +38,10 @@
{ {
"name": "STP", "name": "STP",
"description": "Operations about STP" "description": "Operations about STP"
},
{
"name": "CODE",
"description": "Operations on SAS code"
} }
], ],
"yaml": true, "yaml": true,

View File

@@ -43,7 +43,7 @@ services:
- ./web:/usr/server/web - ./web:/usr/server/web
mongodb: mongodb:
image: mongo:latest image: mongo:5.0.4
ports: ports:
- 27017:27017 - 27017:27017
volumes: volumes:

View File

@@ -2,17 +2,37 @@ import React, { useEffect, useRef, useState } from 'react'
import axios from 'axios' import axios from 'axios'
import Box from '@mui/material/Box' import Box from '@mui/material/Box'
import { Button, Paper, Stack, Toolbar } from '@mui/material' import { Button, Paper, Stack, Tab } from '@mui/material'
import Editor from '@monaco-editor/react' import { makeStyles } from '@mui/styles'
import Editor, { OnMount } from '@monaco-editor/react'
import { useLocation } from 'react-router-dom' import { useLocation } from 'react-router-dom'
import { TabContext, TabList, TabPanel } from '@mui/lab'
const useStyles = makeStyles(() => ({
root: {
fontSize: '1rem',
color: 'gray',
'&.Mui-selected': {
color: 'black'
}
}
}))
const Studio = () => { const Studio = () => {
const location = useLocation() const location = useLocation()
const [fileContent, setFileContent] = useState('') const [fileContent, setFileContent] = useState('')
const [log, setLog] = useState('') const [log, setLog] = useState('')
const [webout, setWebout] = useState('')
const [tab, setTab] = React.useState('1')
const handleTabChange = (_e: any, newValue: string) => {
setTab(newValue)
}
const editorRef = useRef(null) const editorRef = useRef(null as any)
const handleEditorDidMount = (editor: any) => (editorRef.current = editor) const handleEditorDidMount: OnMount = (editor) => {
editor.focus()
editorRef.current = editor
}
const getSelection = () => { const getSelection = () => {
const editor = editorRef.current as any const editor = editorRef.current as any
@@ -20,25 +40,47 @@ const Studio = () => {
return selection ?? '' return selection ?? ''
} }
const handleRunSelectionBtnClick = () => runCode(getSelection()) const handleRunBtnClick = () => runCode(getSelection() || fileContent)
const handleRunBtnClick = () => runCode(fileContent)
const runCode = (code: string) => { const runCode = (code: string) => {
axios axios
.post(`/SASjsApi/stp/run`, { code }) .post(`/SASjsApi/code/execute`, { code })
.then((res: any) => { .then((res: any) => {
const data = setLog(`<div><h2>SAS Log</h2><pre>${res?.data?.log}</pre></div>`)
typeof res.data === 'string'
? res.data
: `<pre><code>${JSON.stringify(res.data, null, 4)}</code></pre>`
setLog(data) let weboutString: string
document?.getElementById('sas_log')?.scrollIntoView() try {
weboutString = res.data.webout
.split('>>weboutBEGIN<<')[1]
.split('>>weboutEND<<')[0]
} catch (_) {
weboutString = res?.data?.webout ?? ''
}
let webout: string
try {
webout = JSON.stringify(JSON.parse(weboutString), null, 4)
} catch (_) {
webout = weboutString
}
setWebout(`<pre><code>${webout}</code></pre>`)
setTab('2')
}) })
.catch((err) => console.log(err)) .catch((err) => console.log(err))
} }
useEffect(() => {
const content = localStorage.getItem('fileContent') ?? ''
setFileContent(content)
}, [])
useEffect(() => {
if (fileContent.length) {
localStorage.setItem('fileContent', fileContent)
}
}, [fileContent])
useEffect(() => { useEffect(() => {
const params = new URLSearchParams(location.search) const params = new URLSearchParams(location.search)
const programPath = params.get('_program') const programPath = params.get('_program')
@@ -50,48 +92,74 @@ const Studio = () => {
.catch((err) => console.log(err)) .catch((err) => console.log(err))
}, [location.search]) }, [location.search])
const classes = useStyles()
return ( return (
<Box component="main" sx={{ flexGrow: 1, p: 3 }}> <>
<Toolbar /> <br />
<Paper <br />
sx={{ <br />
height: '75vh', <Box sx={{ width: '100%', typography: 'body1' }}>
padding: '10px', <TabContext value={tab}>
overflow: 'auto', <Box
position: 'relative' sx={{
}} borderBottom: 1,
elevation={3} borderColor: 'divider'
> }}
<Editor style={{ position: 'fixed', background: 'white', width: '100%' }}
height="95%" >
value={fileContent} <TabList onChange={handleTabChange} centered>
onMount={handleEditorDidMount} <Tab className={classes.root} label="Code" value="1" />
onChange={(val) => { <Tab className={classes.root} label="Log" value="2" />
if (val) setFileContent(val) <Tab className={classes.root} label="Webout" value="3" />
}} </TabList>
/> </Box>
</Paper> <TabPanel value="1">
<Stack {/* <Toolbar /> */}
spacing={3} <Paper
direction="row" sx={{
sx={{ justifyContent: 'center', marginTop: '20px' }} height: '70vh',
> marginTop: '50px',
<Button variant="contained" onClick={handleRunBtnClick}> padding: '10px',
Run SAS Code overflow: 'auto',
</Button> position: 'relative'
<Button variant="contained" onClick={handleRunSelectionBtnClick}> }}
Run Selected SAS Code elevation={3}
</Button> >
</Stack> <Editor
{log && ( height="95%"
<> value={fileContent}
<br /> onMount={handleEditorDidMount}
<h2 id="sas_log">Output</h2> onChange={(val) => {
<br /> if (val) setFileContent(val)
<div dangerouslySetInnerHTML={{ __html: log }} /> }}
</> />
)} </Paper>
</Box> <Stack
spacing={3}
direction="row"
sx={{ justifyContent: 'center', marginTop: '20px' }}
>
<Button variant="contained" onClick={handleRunBtnClick}>
Run SAS Code
</Button>
</Stack>
</TabPanel>
<TabPanel value="2">
<div
id="sas_log"
style={{ marginTop: '50px' }}
dangerouslySetInnerHTML={{ __html: log }}
/>
</TabPanel>
<TabPanel value="3">
<div
style={{ marginTop: '50px' }}
dangerouslySetInnerHTML={{ __html: webout }}
/>
</TabPanel>
</TabContext>
</Box>
</>
) )
} }