Skip to content

Commit

Permalink
EPMRPP-89147 || Add debug logs for RestClient and time execution
Browse files Browse the repository at this point in the history
  • Loading branch information
AliakseiLiasnitski committed Jan 23, 2024
1 parent 0328524 commit f8fba68
Show file tree
Hide file tree
Showing 2 changed files with 149 additions and 20 deletions.
126 changes: 106 additions & 20 deletions lib/report-portal-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class RPClient {
baseURL: this.baseURL,
headers: this.headers,
restClientConfig: this.config.restClientConfig,
debug: this.debug,
});
this.statistics = new Statistics(EVENT_NAME, agentParams);
this.launchUuid = '';
Expand Down Expand Up @@ -164,6 +165,7 @@ class RPClient {
*/
startLaunch(launchDataRQ) {
const tempId = this.getUniqId();
const startTime = this.helpers.now();

if (launchDataRQ.id) {
this.map[tempId] = this.getNewItemObj((resolve) => resolve(launchDataRQ));
Expand Down Expand Up @@ -196,11 +198,19 @@ class RPClient {
helpers.saveLaunchIdToFile(response.id);
}

this.logDebug(`Success start launch with tempId ${tempId}`, response);
this.logDebug(
`Success start launch with tempId ${tempId} [time=${
this.helpers.now() - startTime
}ms]`,
response,
);
resolve(response);
},
(error) => {
this.logDebug(`Error start launch with tempId ${tempId}`, error);
this.logDebug(
`Error start launch with tempId ${tempId} [time=${this.helpers.now() - startTime}ms]`,
error,
);
console.dir(error);
reject(error);
},
Expand All @@ -226,6 +236,7 @@ class RPClient {
* @Returns {Object} - an object which contains a tempID and a promise
*/
finishLaunch(launchTempId, finishExecutionRQ) {
const startTime = this.helpers.now();
const launchObj = this.map[launchTempId];
if (!launchObj) {
return this.getRejectAnswer(
Expand All @@ -245,12 +256,22 @@ class RPClient {
const url = ['launch', launchObj.realId, 'finish'].join('/');
this.restClient.update(url, finishExecutionData, { headers: this.headers }).then(
(response) => {
this.logDebug(`Success finish launch with tempId ${launchTempId}`, response);
this.logDebug(
`Success finish launch with tempId ${launchTempId} [time=${
this.helpers.now() - startTime
}ms]`,
response,
);
console.log(`\nReportPortal Launch Link: ${response.link}`);
launchObj.resolveFinish(response);
},
(error) => {
this.logDebug(`Error finish launch with tempId ${launchTempId}`, error);
this.logDebug(
`Error finish launch with tempId ${launchTempId} [time=${
this.helpers.now() - startTime
}ms]`,
error,
);
console.dir(error);
launchObj.rejectFinish(error);
},
Expand Down Expand Up @@ -310,6 +331,7 @@ class RPClient {
*/
mergeLaunches(mergeOptions = {}) {
if (this.isLaunchMergeRequired) {
const startTime = this.helpers.now();
const launchUUIds = helpers.readLaunchesFromFile();
const params = new URLSearchParams({
'filter.in.uuid': launchUUIds,
Expand Down Expand Up @@ -337,10 +359,19 @@ class RPClient {
return this.restClient.create(mergeURL, request, { headers: this.headers });
})
.then(() => {
this.logDebug(`Launches with UUIDs: ${launchUUIds} were successfully merged!`);
this.logDebug(
`Launches with UUIDs: ${launchUUIds} were successfully merged! [time=${
this.helpers.now() - startTime
}ms]`,
);
})
.catch((error) => {
this.logDebug(`Error merging launches with UUIDs: ${launchUUIds}`, error);
this.logDebug(
`Error merging launches with UUIDs: ${launchUUIds} [time=${
this.helpers.now() - startTime
}ms]`,
error,
);
console.dir(error);
});
}
Expand Down Expand Up @@ -382,6 +413,7 @@ class RPClient {
* @Returns {Object} - an object which contains a tempId and a promise
*/
updateLaunch(launchTempId, launchData) {
const startTime = this.helpers.now();
const launchObj = this.map[launchTempId];
if (!launchObj) {
return this.getRejectAnswer(
Expand All @@ -402,11 +434,21 @@ class RPClient {
this.logDebug(`Update launch with tempId ${launchTempId}`, launchData);
this.restClient.update(url, launchData, { headers: this.headers }).then(
(response) => {
this.logDebug(`Launch with tempId ${launchTempId} were successfully updated`, response);
this.logDebug(
`Launch with tempId ${launchTempId} were successfully updated [time=${
this.helpers.now() - startTime
}ms]`,
response,
);
resolvePromise(response);
},
(error) => {
this.logDebug(`Error when updating launch with tempId ${launchTempId}`, error);
this.logDebug(
`Error when updating launch with tempId ${launchTempId} [time=${
this.helpers.now() - startTime
}ms]`,
error,
);
console.dir(error);
rejectPromise(error);
},
Expand Down Expand Up @@ -450,6 +492,7 @@ class RPClient {
* @Returns {Object} - an object which contains a tempId and a promise
*/
startTestItem(testItemDataRQ, launchTempId, parentTempId) {
const startTime = this.helpers.now();
let parentMapId = launchTempId;
const launchObj = this.map[launchTempId];
if (!launchObj) {
Expand Down Expand Up @@ -513,13 +556,21 @@ class RPClient {
this.logDebug(`Start test item with tempId ${tempId}`, testItemData);
this.restClient.create(url, testItemData, { headers: this.headers }).then(
(response) => {
this.logDebug(`Success start item with tempId ${tempId}`, response);
this.logDebug(
`Success start item with tempId ${tempId} [time=${
this.helpers.now() - startTime
}ms]`,
response,
);
this.map[tempId].realId = response.id;
this.nonRetriedItemMap.delete(itemKey);
resolve(response);
},
(error) => {
this.logDebug(`Error start item with tempId ${tempId}`, error);
this.logDebug(
`Error start item with tempId ${tempId} [time=${this.helpers.now() - startTime}ms]`,
error,
);
console.dir(error);
reject(error);
},
Expand Down Expand Up @@ -567,6 +618,7 @@ class RPClient {
* @Returns {Object} - an object which contains a tempId and a promise
*/
finishTestItem(itemTempId, finishTestItemRQ) {
const startTime = this.helpers.now();
const itemObj = this.map[itemTempId];
if (!itemObj) {
return this.getRejectAnswer(
Expand All @@ -592,28 +644,35 @@ class RPClient {
if (result.status === 'fulfilled') {
this.logDebug(
`Successfully finish child with tempId ${itemObj.children[index]}
of test item with tempId ${itemTempId}`,
of test item with tempId ${itemTempId} [time=${this.helpers.now() - startTime}ms]`,
);
} else {
this.logDebug(
`Failed to finish child with tempId ${itemObj.children[index]}
of test item with tempId ${itemTempId}`,
of test item with tempId ${itemTempId} [time=${this.helpers.now() - startTime}ms]`,
);
}
});
}

this.cleanMap(itemObj.children);

this.logDebug(`Finish test item with tempId ${itemTempId}`, finishTestItemRQ);
this.logDebug(
`Finish test item with tempId ${itemTempId} [time=${this.helpers.now() - startTime}ms]`,
finishTestItemRQ,
);
this.finishTestItemPromiseStart(
itemObj,
itemTempId,
Object.assign(finishTestItemData, { launchUuid: this.launchUuid }),
);
})
.catch(() => {
this.logDebug(`Error finish children of test item with tempId ${itemTempId}`);
this.logDebug(
`Error finish children of test item with tempId ${itemTempId} [time=${
this.helpers.now() - startTime
}ms]`,
);
});

return {
Expand All @@ -623,18 +682,27 @@ class RPClient {
}

saveLog(itemObj, requestPromiseFunc) {
const startTime = this.helpers.now();
const tempId = this.getUniqId();
this.map[tempId] = this.getNewItemObj((resolve, reject) => {
itemObj.promiseStart.then(
() => {
this.logDebug(`Save log with tempId ${tempId}`, itemObj);
requestPromiseFunc(itemObj.realId, this.launchUuid).then(
(response) => {
this.logDebug(`Successfully save log with tempId ${tempId}`, response);
this.logDebug(
`Successfully save log with tempId ${tempId} [time=${
this.helpers.now() - startTime
}ms]`,
response,
);
resolve(response);
},
(error) => {
this.logDebug(`Error save log with tempId ${tempId}`, error);
this.logDebug(
`Error save log with tempId ${tempId} [time=${this.helpers.now() - startTime}ms]`,
error,
);
console.dir(error);
reject(error);
},
Expand Down Expand Up @@ -750,6 +818,7 @@ class RPClient {
}

getRequestLogWithFile(saveLogRQ, fileObj) {
const startTime = this.helpers.now();
const url = 'log';
// eslint-disable-next-line no-param-reassign
saveLogRQ.file = { name: fileObj.name };
Expand All @@ -762,11 +831,17 @@ class RPClient {
},
})
.then((response) => {
this.logDebug(`Success save log with file: ${fileObj.name}`, response);
this.logDebug(
`Success save log with file: ${fileObj.name} [time=${this.helpers.now() - startTime}ms]`,
response,
);
return response;
})
.catch((error) => {
this.logDebug(`Error save log with file: ${fileObj.name}`, error);
this.logDebug(
`Error save log with file: ${fileObj.name} [time=${this.helpers.now() - startTime}ms]`,
error,
);
console.dir(error);
});
}
Expand Down Expand Up @@ -815,6 +890,7 @@ class RPClient {
}

finishTestItemPromiseStart(itemObj, itemTempId, finishTestItemData) {
const startTime = this.helpers.now();
itemObj.promiseStart.then(
() => {
const url = ['item', itemObj.realId].join('/');
Expand All @@ -825,11 +901,21 @@ class RPClient {
})
.then(
(response) => {
this.logDebug(`Success finish item with tempId ${itemTempId}`, response);
this.logDebug(
`Success finish item with tempId ${itemTempId} [time=${
this.helpers.now() - startTime
}ms]`,
response,
);
itemObj.resolveFinish(response);
},
(error) => {
this.logDebug(`Error finish test item with tempId ${itemTempId}`, error);
this.logDebug(
`Error finish test item with tempId ${itemTempId} [time=${
this.helpers.now() - startTime
}ms]`,
error,
);
console.dir(error);
itemObj.rejectFinish(error);
},
Expand Down
43 changes: 43 additions & 0 deletions lib/rest.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ class RestClient {
this.baseURL = options.baseURL;
this.headers = options.headers;
this.restClientConfig = options.restClientConfig;

addLogger(options.debug);
}

buildPath(path) {
Expand Down Expand Up @@ -112,4 +114,45 @@ method: ${method}`,
}
}

const addLogger = (debug) => {
if (debug) {
axios.interceptors.request.use((config) => {
const startDate = new Date();
config.startTime = startDate.valueOf();

console.log(`Request method=${config.method} url=${config.url} [${startDate.toISOString()}]`);

return config;
});

axios.interceptors.response.use(
(response) => {
const date = new Date();
const { status, config } = response;

console.log(
`Response status=${status} url=${config.url} time=${
date.valueOf() - config.startTime
}ms [${date.toISOString()}]`,
);

return response;
},
(error) => {
const date = new Date();
const { response, config } = error;
const status = response ? response.status : null;

console.log(
`Response ${status ? 'status=' + status : "message='" + error.message + "'"} url=${
config.url
} time=${date.valueOf() - config.startTime}ms [${date.toISOString()}]`,
);

return Promise.reject(error);
},
);
}
};

module.exports = RestClient;

0 comments on commit f8fba68

Please sign in to comment.