test-runs-get-test-run-recent.js•2.92 kB
import { getAuthToken } from './auth-get-token.js';
/**
* Function to get recent test runs license usage details from LoadRunner Cloud for the last month.
*
* @param {Object} args - Arguments for the license usage request.
* @param {string} [args.projectIds] - Optional project IDs to filter the usage.
* @returns {Promise<Object>} - The recent test runs license usage details for the last month.
*/
const executeFunction = async ({ projectIds = '' } = {}) => {
const baseUrl = process.env.LRC_BASE_URL;
const tenantId = process.env.LRC_TENANT_ID;
const token = await getAuthToken();
// Calculate startTime and endTime for the last 1 month
const endTime = Date.now();
const startTime = endTime - 30 * 24 * 60 * 60 * 1000; // 30 days in ms
try {
// Construct the URL with query parameters
const url = new URL(`${baseUrl}/license/usage`);
url.searchParams.append('TENANTID', tenantId);
if (projectIds) url.searchParams.append('projectIds', projectIds);
url.searchParams.append('startTime', startTime);
url.searchParams.append('endTime', endTime);
// Set up headers for the request
const headers = {
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`
};
// Perform the fetch request
const response = await fetch(url.toString(), {
method: 'GET',
headers
});
// Check if the response was successful
if (!response.ok) {
const text = await response.text();
try {
const errorData = JSON.parse(text);
throw new Error(JSON.stringify(errorData));
} catch (jsonErr) {
// Not JSON, log the raw text
console.error('Non-JSON error response:', text);
throw new Error(text);
}
}
// Parse and return the response data
const text = await response.text();
try {
const data = JSON.parse(text);
return data;
} catch (jsonErr) {
// Not JSON, log the raw text
console.error('Non-JSON success response:', text);
return { error: 'Received non-JSON response from API', raw: text };
}
} catch (error) {
console.error('Error retrieving license usage:', error);
return { error: 'An error occurred while retrieving license usage.' };
}
};
/**
* Tool configuration for retrieving recent test runs details for the last month.
* @type {Object}
*/
const apiTool = {
function: executeFunction,
definition: {
type: 'function',
function: {
name: 'test_runs_getRecentTestRuns',
description: 'Get recent test runs details for the last month from LoadRunner Cloud. Optionally filter by projectIds.',
parameters: {
type: 'object',
properties: {
projectIds: {
type: 'string',
description: 'Optional project IDs to filter the usage.'
}
},
required: []
}
}
}
};
export { apiTool };