61 lines
No EOL
1.9 KiB
JavaScript
61 lines
No EOL
1.9 KiB
JavaScript
/**
|
|
* Data layer module for retrieving retry statistics from the backend API.
|
|
* Provides an async function to fetch retry stats relevant to Gate-V1 analysis.
|
|
*
|
|
* Expected API: /api/retry-stats
|
|
* Supported parameters: stratum, policy_hash
|
|
*
|
|
* @module api
|
|
*/
|
|
|
|
/**
|
|
* Constructs a query string from an object of parameters.
|
|
* Empty or undefined values are omitted.
|
|
* @param {Object} [params={}] - Key-value pairs for query parameters
|
|
* @returns {string} Query string beginning with '?', or empty string if no params.
|
|
*/
|
|
const buildQueryString = (params = {}) => {
|
|
const query = Object.entries(params)
|
|
.filter(([, value]) => value !== undefined && value !== null && value !== '')
|
|
.map(([key, value]) => `${encodeURIComponent(key)}=${encodeURIComponent(value)}`)
|
|
.join('&');
|
|
return query ? `?${query}` : '';
|
|
};
|
|
|
|
/**
|
|
* Fetches retry statistics from the API endpoint.
|
|
* @param {Object} [options] - Optional parameters for the query (e.g. stratum, policy_hash)
|
|
* @returns {Promise<Object>} Promise resolving to the JSON response containing retry analysis report.
|
|
* @throws Will throw an error if the network request fails or response is invalid.
|
|
*/
|
|
export async function fetchRetryStats(options = {}) {
|
|
const query = buildQueryString(options);
|
|
const endpoint = `/api/retry-stats${query}`;
|
|
|
|
try {
|
|
const response = await fetch(endpoint, {
|
|
method: 'GET',
|
|
headers: {
|
|
'Accept': 'application/json'
|
|
}
|
|
});
|
|
|
|
if (!response.ok) {
|
|
const text = await response.text();
|
|
throw new Error(`API request failed with status ${response.status}: ${text}`);
|
|
}
|
|
|
|
const data = await response.json();
|
|
|
|
if (!data || typeof data !== 'object') {
|
|
throw new Error('Invalid response: Expected JSON object');
|
|
}
|
|
|
|
return data;
|
|
} catch (error) {
|
|
console.error('Error during fetchRetryStats:', error);
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
export default { fetchRetryStats }; |