@pradyumn-el/pollycli
Version:
pollycli lets users access the functionalities of Polly over a command line interface
79 lines (74 loc) • 2.53 kB
JavaScript
import { pollyApi } from './api-client';
const axios = require('axios');
const pollyEnv = require('./env.json');
const pollyHeader = require('./pollyheaders');
const pollymsg = require('./message');
async function getAppLog(projectId, runId, logId, nextToken = null) {
try {
const logUrl = `${pollyEnv.baseV2Api}/workspaces/${projectId}/runs/${runId}/compute-app-sessions/logs/${logId}${ nextToken ? '?next_token=' + nextToken : ''}`;
let logReturn = await pollyApi.get(logUrl);
if(!logReturn.data) {
process.exit(0)
}
logReturn = logReturn.data.data.attributes;
return logReturn
} catch (error) {
pollymsg.pollyError("Not able to get the logs")
}
}
export async function getAppLogs(projectId, runId, logId, mode = 'all') {
let returnData = [];
if (mode == 'latest') {
returnData.push(await getAppLog(projectId, runId, logId, nextToken));
} else if (mode == 'all') {
let nextToken = null;
do {
const tempLog = await getAppLog(projectId, runId, logId, nextToken);
returnData.push(tempLog);
nextToken = tempLog.next_token;
} while (nextToken);
}
for (let logData of returnData) {
let tempLogData = await axios.get(logData.log_signed_url);
tempLogData = tempLogData.data
console.log(tempLogData)
}
return returnData;
}
export async function getLogsFromLogsV2Api(projectId, jobId, nextToken=null, attemptNum=null){
try{
let logUrl = `${pollyEnv.baseV2Api}/projects/${projectId}/jobs/${jobId}/logs-v2`;
let params = {};
if (nextToken){
params.nextToken = nextToken;
}
if (attemptNum !== null){
params.attemptNum = attemptNum;
}
let logData = await pollyApi.get(logUrl, {
params: params,
});
return logData.data;
}catch(error){
throw error;
}
}
export async function getJobLogs(projectId, jobId, mode = 'all', attempt_num=null) {
let returndata = [];
try {
let logData = await getLogsFromLogsV2Api(projectId, jobId, null, attempt_num)
while (logData["events"].length > 0){
for (let logEvent of logData["events"]){
console.log(logEvent["message"]);
}
logData = await getLogsFromLogsV2Api(projectId, jobId, logData["nextForwardToken"], attempt_num);
}
}catch(error){
if (error.hasOwnProperty("response") && error.response.data.hasOwnProperty("errors")){
pollymsg.pollyError(error.response.data.errors[0].detail);
}else{
pollymsg.pollyError("Not able to get the logs");
}
}
return returndata;
}