mirror of
https://github.com/sasjs/adapter.git
synced 2025-12-11 01:14:36 +00:00
Compare commits
23 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
33a202fa1c | ||
|
|
ff5463a84c | ||
|
|
aa7c3ae4a9 | ||
|
|
2e66bfde4b | ||
|
|
16e21adb20 | ||
|
|
01c5682c3d | ||
|
|
cfc8ff2837 | ||
|
|
edf25b471a | ||
|
|
bb894e6107 | ||
|
|
7bf53858f0 | ||
|
|
02780d0bcd | ||
|
|
6356aed06b | ||
|
|
69fd7b2cb5 | ||
|
|
5d1eed1494 | ||
|
|
e2e2824f37 | ||
|
|
d461135980 | ||
|
|
65fbae7610 | ||
|
|
761428502a | ||
|
|
6eb2ceaf53 | ||
|
|
66813b9824 | ||
|
|
140d8e4eac | ||
|
|
0d730e0576 | ||
|
|
9037160362 |
2
.env.example
Normal file
2
.env.example
Normal file
@@ -0,0 +1,2 @@
|
||||
SERVER_URL=https://server.com
|
||||
DEFAULT_COMPUTE_CONTEXT=SAS Job Execution compute context
|
||||
9
.github/reviewer-lottery.yml
vendored
Normal file
9
.github/reviewer-lottery.yml
vendored
Normal file
@@ -0,0 +1,9 @@
|
||||
groups:
|
||||
- name: SASjs Devs # name of the group
|
||||
reviewers: 1 # how many reviewers do you want to assign?
|
||||
usernames: # github usernames of the reviewers
|
||||
- krishna-acondy
|
||||
- YuryShkoda
|
||||
- saadjutt01
|
||||
- medjedovicm
|
||||
- allanbowe
|
||||
13
.github/workflows/assign-reviewer.yml
vendored
Normal file
13
.github/workflows/assign-reviewer.yml
vendored
Normal file
@@ -0,0 +1,13 @@
|
||||
name: 'Assign Reviewer'
|
||||
on:
|
||||
pull_request:
|
||||
types: [opened]
|
||||
|
||||
jobs:
|
||||
test:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- uses: uesteibar/reviewer-lottery@v1
|
||||
with:
|
||||
repo-token: ${{ secrets.GH_TOKEN }}
|
||||
4
.gitignore
vendored
4
.gitignore
vendored
@@ -1,2 +1,4 @@
|
||||
node_modules
|
||||
build
|
||||
build
|
||||
|
||||
.env
|
||||
File diff suppressed because one or more lines are too long
231
docs/classes/reflection-804.reflection-219.fileuploader.html
Normal file
231
docs/classes/reflection-804.reflection-219.fileuploader.html
Normal file
File diff suppressed because one or more lines are too long
312
docs/classes/reflection-804.reflection-219.sas9apiclient.html
Normal file
312
docs/classes/reflection-804.reflection-219.sas9apiclient.html
Normal file
File diff suppressed because one or more lines are too long
1670
docs/classes/reflection-804.reflection-219.sasjs.html
Normal file
1670
docs/classes/reflection-804.reflection-219.sasjs.html
Normal file
File diff suppressed because one or more lines are too long
1471
docs/classes/reflection-804.reflection-219.sasviyaapiclient.html
Normal file
1471
docs/classes/reflection-804.reflection-219.sasviyaapiclient.html
Normal file
File diff suppressed because one or more lines are too long
323
docs/classes/reflection-804.reflection-219.sessionmanager.html
Normal file
323
docs/classes/reflection-804.reflection-219.sessionmanager.html
Normal file
File diff suppressed because one or more lines are too long
228
docs/classes/root.fileuploader.html
Normal file
228
docs/classes/root.fileuploader.html
Normal file
File diff suppressed because one or more lines are too long
309
docs/classes/root.sas9apiclient.html
Normal file
309
docs/classes/root.sas9apiclient.html
Normal file
File diff suppressed because one or more lines are too long
1673
docs/classes/root.sasjs.html
Normal file
1673
docs/classes/root.sasjs.html
Normal file
File diff suppressed because one or more lines are too long
1480
docs/classes/root.sasviyaapiclient.html
Normal file
1480
docs/classes/root.sasviyaapiclient.html
Normal file
File diff suppressed because one or more lines are too long
357
docs/classes/root.sessionmanager.html
Normal file
357
docs/classes/root.sessionmanager.html
Normal file
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
215
docs/interfaces/types.polloptions.html
Normal file
215
docs/interfaces/types.polloptions.html
Normal file
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
197
docs/interfaces/types.sessionvariable.html
Normal file
197
docs/interfaces/types.sessionvariable.html
Normal file
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
106
docs/modules/reflection-804.html
Normal file
106
docs/modules/reflection-804.html
Normal file
File diff suppressed because one or more lines are too long
128
docs/modules/reflection-804.reflection-219.html
Normal file
128
docs/modules/reflection-804.reflection-219.html
Normal file
File diff suppressed because one or more lines are too long
125
docs/modules/root.html
Normal file
125
docs/modules/root.html
Normal file
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
4610
package-lock.json
generated
4610
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@@ -39,6 +39,7 @@
|
||||
"@types/isomorphic-fetch": "0.0.35",
|
||||
"@types/jest": "^26.0.15",
|
||||
"cp": "^0.2.0",
|
||||
"dotenv": "^8.2.0",
|
||||
"jest": "^25.5.4",
|
||||
"path": "^0.12.7",
|
||||
"rimraf": "^3.0.2",
|
||||
@@ -57,6 +58,7 @@
|
||||
},
|
||||
"main": "index.js",
|
||||
"dependencies": {
|
||||
"@sasjs/utils": "^1.5.0",
|
||||
"es6-promise": "^4.2.8",
|
||||
"form-data": "^3.0.0",
|
||||
"isomorphic-fetch": "^2.2.1"
|
||||
|
||||
@@ -25,17 +25,71 @@ export const computeTests = (adapter: SASjs): TestSuite => ({
|
||||
},
|
||||
assertion: (res: any) => {
|
||||
const expectedProperties = ["id", "state", "creationTimeStamp", "jobConditionCode"]
|
||||
return validate(expectedProperties, res);
|
||||
return validate(expectedProperties, res.result);
|
||||
}
|
||||
},
|
||||
{
|
||||
title: "Execute Script Viya - complete job",
|
||||
description: "Should execute sas file and return log",
|
||||
test: () => {
|
||||
const fileLines = [
|
||||
`data;`,
|
||||
`do x=1 to 100;`,
|
||||
`output;`,
|
||||
`end;`,
|
||||
`run;`
|
||||
]
|
||||
|
||||
return adapter.executeScriptSASViya(
|
||||
'sasCode.sas',
|
||||
fileLines,
|
||||
'SAS Studio compute context',
|
||||
undefined,
|
||||
true
|
||||
)
|
||||
},
|
||||
assertion: (res: any) => {
|
||||
const expectedLogContent = `1 data;\\n2 do x=1 to 100;\\n3 output;\\n4 end;\\n5 run;\\n\\n`
|
||||
|
||||
return validateLog(expectedLogContent, res.log);
|
||||
}
|
||||
},
|
||||
{
|
||||
title: "Execute Script Viya - failed job",
|
||||
description: "Should execute sas file and return log",
|
||||
test: () => {
|
||||
const fileLines = [
|
||||
`%abort;`
|
||||
]
|
||||
|
||||
return adapter.executeScriptSASViya(
|
||||
'sasCode.sas',
|
||||
fileLines,
|
||||
'SAS Studio compute context',
|
||||
undefined,
|
||||
true
|
||||
).catch((err: any) => err )
|
||||
},
|
||||
assertion: (res: any) => {
|
||||
const expectedLogContent = `1 %abort;\\nERROR: The %ABORT statement is not valid in open code.\\n`
|
||||
|
||||
return validateLog(expectedLogContent, res.log);
|
||||
}
|
||||
}
|
||||
]
|
||||
});
|
||||
|
||||
const validateLog = (text: string, log: string): boolean => {
|
||||
const isValid = JSON.stringify(log).includes(text)
|
||||
|
||||
return isValid
|
||||
}
|
||||
|
||||
const validate = (expectedProperties: string[], data: any): boolean => {
|
||||
const actualProperties = Object.keys(data);
|
||||
|
||||
const isValid = expectedProperties.every(
|
||||
(property) => actualProperties.includes(property)
|
||||
);
|
||||
return isValid
|
||||
const isValid = expectedProperties.every(
|
||||
(property) => actualProperties.includes(property)
|
||||
);
|
||||
return isValid
|
||||
}
|
||||
@@ -16,12 +16,13 @@ import {
|
||||
Folder,
|
||||
CsrfToken,
|
||||
EditContextInput,
|
||||
ErrorResponse,
|
||||
JobDefinition,
|
||||
PollOptions
|
||||
} from './types'
|
||||
import { formatDataForRequest } from './utils/formatDataForRequest'
|
||||
import { SessionManager } from './SessionManager'
|
||||
import { timestampToYYYYMMDDHHMMSS } from '@sasjs/utils/time'
|
||||
import { Logger, LogLevel } from '@sasjs/utils/logger'
|
||||
|
||||
/**
|
||||
* A client for interfacing with the SAS Viya REST API.
|
||||
@@ -430,6 +431,7 @@ export class SASViyaApiClient {
|
||||
* @param expectWebout - when set to true, the automatic _webout fileref will be checked for content, and that content returned. This fileref is used when the Job contains a SASjs web request (as opposed to executing arbitrary SAS code).
|
||||
* @param waitForResult - when set to true, function will return the session
|
||||
* @param pollOptions - an object that represents poll interval(milliseconds) and maximum amount of attempts. Object example: { MAX_POLL_COUNT: 24 * 60 * 60, POLL_INTERVAL: 1000 }.
|
||||
* @param printPid - a boolean that indicates whether the function should print (PID) of the started job.
|
||||
*/
|
||||
public async executeScript(
|
||||
jobPath: string,
|
||||
@@ -440,7 +442,8 @@ export class SASViyaApiClient {
|
||||
debug: boolean = false,
|
||||
expectWebout = false,
|
||||
waitForResult = true,
|
||||
pollOptions?: PollOptions
|
||||
pollOptions?: PollOptions,
|
||||
printPid = false
|
||||
): Promise<any> {
|
||||
try {
|
||||
const headers: any = {
|
||||
@@ -460,6 +463,28 @@ export class SASViyaApiClient {
|
||||
|
||||
executionSessionId = session!.id
|
||||
|
||||
if (printPid) {
|
||||
const { result: jobIdVariable } = await this.sessionManager.getVariable(
|
||||
executionSessionId,
|
||||
'SYSJOBID',
|
||||
accessToken
|
||||
)
|
||||
|
||||
if (jobIdVariable && jobIdVariable.value) {
|
||||
const relativeJobPath = this.rootFolderName
|
||||
? jobPath.split(this.rootFolderName).join('').replace(/^\//, '')
|
||||
: jobPath
|
||||
|
||||
const logger = new Logger(debug ? LogLevel.Debug : LogLevel.Info)
|
||||
|
||||
logger.info(
|
||||
`Triggered '${relativeJobPath}' with PID ${
|
||||
jobIdVariable.value
|
||||
} at ${timestampToYYYYMMDDHHMMSS()}`
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
const jobArguments: { [key: string]: any } = {
|
||||
_contextName: contextName,
|
||||
_OMITJSONLISTING: true,
|
||||
@@ -589,7 +614,7 @@ export class SASViyaApiClient {
|
||||
if (expectWebout) {
|
||||
resultLink = `/compute/sessions/${executionSessionId}/filerefs/_webout/content`
|
||||
} else {
|
||||
return currentJob
|
||||
return { job: currentJob, log }
|
||||
}
|
||||
|
||||
if (resultLink) {
|
||||
@@ -958,6 +983,7 @@ export class SASViyaApiClient {
|
||||
* @param waitForResult - a boolean indicating if the function should wait for a result.
|
||||
* @param expectWebout - a boolean indicating whether to expect a _webout response.
|
||||
* @param pollOptions - an object that represents poll interval(milliseconds) and maximum amount of attempts. Object example: { MAX_POLL_COUNT: 24 * 60 * 60, POLL_INTERVAL: 1000 }.
|
||||
* @param printPid - a boolean that indicates whether the function should print (PID) of the started job.
|
||||
*/
|
||||
public async executeComputeJob(
|
||||
sasJob: string,
|
||||
@@ -967,7 +993,8 @@ export class SASViyaApiClient {
|
||||
accessToken?: string,
|
||||
waitForResult = true,
|
||||
expectWebout = false,
|
||||
pollOptions?: PollOptions
|
||||
pollOptions?: PollOptions,
|
||||
printPid = false
|
||||
) {
|
||||
if (isRelativePath(sasJob) && !this.rootFolderName) {
|
||||
throw new Error(
|
||||
@@ -1053,7 +1080,8 @@ export class SASViyaApiClient {
|
||||
debug,
|
||||
expectWebout,
|
||||
waitForResult,
|
||||
pollOptions
|
||||
pollOptions,
|
||||
printPid
|
||||
)
|
||||
}
|
||||
|
||||
@@ -1238,7 +1266,7 @@ export class SASViyaApiClient {
|
||||
throw new Error(`The path ${path} does not exist on ${this.serverUrl}`)
|
||||
}
|
||||
const { result: members } = await this.request<{ items: any[] }>(
|
||||
`${this.serverUrl}/folders/folders/${folder.id}/members`,
|
||||
`${this.serverUrl}/folders/folders/${folder.id}/members?limit=${folder.memberCount}`,
|
||||
requestInfo
|
||||
)
|
||||
|
||||
|
||||
21
src/SASjs.ts
21
src/SASjs.ts
@@ -206,11 +206,20 @@ export default class SASjs {
|
||||
return await this.sasViyaApiClient!.createSession(contextName, accessToken)
|
||||
}
|
||||
|
||||
/**
|
||||
* Executes the sas code against given sas server
|
||||
* @param fileName - name of the file to run. It will be converted to path to the file being submitted for execution.
|
||||
* @param linesOfCode - lines of sas code from the file to run.
|
||||
* @param contextName - context name on which code will be run on the server.
|
||||
* @param accessToken - (optional) the access token for authorizing the request.
|
||||
* @param debug - (optional) if true, global debug config will be overriden
|
||||
*/
|
||||
public async executeScriptSASViya(
|
||||
fileName: string,
|
||||
linesOfCode: string[],
|
||||
contextName: string,
|
||||
accessToken?: string
|
||||
accessToken?: string,
|
||||
debug?: boolean
|
||||
) {
|
||||
this.isMethodSupported('executeScriptSASViya', ServerType.SASViya)
|
||||
|
||||
@@ -219,7 +228,8 @@ export default class SASjs {
|
||||
linesOfCode,
|
||||
contextName,
|
||||
accessToken,
|
||||
null
|
||||
null,
|
||||
debug ? debug : this.sasjsConfig.debug
|
||||
)
|
||||
}
|
||||
|
||||
@@ -713,6 +723,7 @@ export default class SASjs {
|
||||
* The access token is not required when the user is authenticated via the browser.
|
||||
* @param waitForResult - a boolean that indicates whether the function needs to wait for execution to complete.
|
||||
* @param pollOptions - an object that represents poll interval(milliseconds) and maximum amount of attempts. Object example: { MAX_POLL_COUNT: 24 * 60 * 60, POLL_INTERVAL: 1000 }.
|
||||
* @param printPid - a boolean that indicates whether the function should print (PID) of the started job.
|
||||
*/
|
||||
public async startComputeJob(
|
||||
sasJob: string,
|
||||
@@ -720,7 +731,8 @@ export default class SASjs {
|
||||
config: any = {},
|
||||
accessToken?: string,
|
||||
waitForResult?: boolean,
|
||||
pollOptions?: PollOptions
|
||||
pollOptions?: PollOptions,
|
||||
printPid = false
|
||||
) {
|
||||
config = {
|
||||
...this.sasjsConfig,
|
||||
@@ -742,7 +754,8 @@ export default class SASjs {
|
||||
accessToken,
|
||||
!!waitForResult,
|
||||
false,
|
||||
pollOptions
|
||||
pollOptions,
|
||||
printPid
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { Session, Context, CsrfToken } from './types'
|
||||
import { Session, Context, CsrfToken, SessionVariable } from './types'
|
||||
import { asyncForEach, makeRequest, isUrl } from './utils'
|
||||
import { prefixMessage } from '@sasjs/utils/error'
|
||||
|
||||
const MAX_SESSION_COUNT = 1
|
||||
const RETRY_LIMIT: number = 3
|
||||
@@ -168,7 +169,11 @@ export class SessionManager {
|
||||
const stateLink = session.links.find((l: any) => l.rel === 'state')
|
||||
|
||||
return new Promise(async (resolve, _) => {
|
||||
if (sessionState === 'pending') {
|
||||
if (
|
||||
sessionState === 'pending' ||
|
||||
sessionState === 'running' ||
|
||||
sessionState === ''
|
||||
) {
|
||||
if (stateLink) {
|
||||
if (this.debug) {
|
||||
console.log('Polling session status... \n') // ?
|
||||
@@ -261,4 +266,21 @@ export class SessionManager {
|
||||
throw err
|
||||
})
|
||||
}
|
||||
|
||||
async getVariable(sessionId: string, variable: string, accessToken?: string) {
|
||||
const getSessionVariable = {
|
||||
method: 'GET',
|
||||
headers: this.getHeaders(accessToken)
|
||||
}
|
||||
|
||||
return await this.request<SessionVariable>(
|
||||
`${this.serverUrl}/compute/sessions/${sessionId}/variables/${variable}`,
|
||||
getSessionVariable
|
||||
).catch((err) => {
|
||||
throw prefixMessage(
|
||||
err,
|
||||
`Error while fetching session variable '${variable}'.`
|
||||
)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
53
src/test/SessionManager.spec.ts
Normal file
53
src/test/SessionManager.spec.ts
Normal file
@@ -0,0 +1,53 @@
|
||||
import { SessionManager } from '../SessionManager'
|
||||
import * as dotenv from 'dotenv'
|
||||
|
||||
describe('SessionManager', () => {
|
||||
dotenv.config()
|
||||
|
||||
let originalFetch: any
|
||||
|
||||
const sessionManager = new SessionManager(
|
||||
process.env.SERVER_URL as string,
|
||||
process.env.DEFAULT_COMPUTE_CONTEXT as string,
|
||||
() => {}
|
||||
)
|
||||
|
||||
beforeAll(() => {
|
||||
originalFetch = (global as any).fetch
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
;(global as any).fetch = originalFetch
|
||||
})
|
||||
|
||||
describe('getVariable', () => {
|
||||
it('should fetch session variable', async () => {
|
||||
const sampleResponse = {
|
||||
ok: true,
|
||||
links: [],
|
||||
name: 'SYSJOBID',
|
||||
scope: 'GLOBAL',
|
||||
value: '25218',
|
||||
version: 1
|
||||
}
|
||||
|
||||
;(global as any).fetch = jest.fn().mockImplementation(() =>
|
||||
Promise.resolve({
|
||||
ok: true,
|
||||
headers: { get: () => '' },
|
||||
json: () => Promise.resolve(sampleResponse)
|
||||
})
|
||||
)
|
||||
|
||||
const expectedResponse = { etag: '', result: sampleResponse }
|
||||
|
||||
await expect(
|
||||
sessionManager.getVariable(
|
||||
'fakeSessionId',
|
||||
'SYSJOBID',
|
||||
'fakeAccessToken'
|
||||
)
|
||||
).resolves.toEqual(expectedResponse)
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -4,4 +4,5 @@ export interface Folder {
|
||||
id: string
|
||||
uri: string
|
||||
links: Link[]
|
||||
memberCount: number
|
||||
}
|
||||
|
||||
@@ -9,3 +9,7 @@ export interface Session {
|
||||
}
|
||||
creationTimeStamp: string
|
||||
}
|
||||
|
||||
export interface SessionVariable {
|
||||
value: string
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user