From a7efab72328400432ad1eac47491efea3fde3a12 Mon Sep 17 00:00:00 2001 From: Mika Date: Mon, 9 Mar 2026 13:44:27 +0000 Subject: [PATCH] Add artifact.visualization/js/api.js --- artifact.visualization/js/api.js | 61 ++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 artifact.visualization/js/api.js diff --git a/artifact.visualization/js/api.js b/artifact.visualization/js/api.js new file mode 100644 index 0000000..4eb2f9e --- /dev/null +++ b/artifact.visualization/js/api.js @@ -0,0 +1,61 @@ +/** + * 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} 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 }; \ No newline at end of file