48 lines
1.3 KiB
JavaScript
48 lines
1.3 KiB
JavaScript
/**
|
|
* @module api
|
|
* Data layer module for fetching Drift-Matrix data.
|
|
*
|
|
* Provides a function to retrieve matrix data from the backend.
|
|
*/
|
|
|
|
/**
|
|
* Fetches Drift-Matrix data from the API for a given run.
|
|
*
|
|
* @async
|
|
* @function fetchMatrixData
|
|
* @param {string|number} run_id - The identifier of the run for which drift data should be retrieved.
|
|
* @returns {Promise<Array<{quadrant: string, warn_reasons: string[], counts: number[]}>>} - The matrix data as JSON array.
|
|
* @throws {Error} Throws if the fetch fails or response is invalid.
|
|
*/
|
|
export async function fetchMatrixData(run_id) {
|
|
if (!run_id) {
|
|
throw new Error('fetchMatrixData: Missing required parameter run_id');
|
|
}
|
|
|
|
const url = new URL('/drift-matrix', window.location.origin);
|
|
url.searchParams.append('run_id', run_id);
|
|
|
|
try {
|
|
const response = await fetch(url.toString(), {
|
|
method: 'GET',
|
|
headers: {
|
|
'Accept': 'application/json'
|
|
}
|
|
});
|
|
|
|
if (!response.ok) {
|
|
throw new Error(`API request failed with status ${response.status}`);
|
|
}
|
|
|
|
const data = await response.json();
|
|
|
|
if (!Array.isArray(data)) {
|
|
throw new Error('Invalid API response format: expected an array');
|
|
}
|
|
|
|
return data;
|
|
} catch (error) {
|
|
console.error('Error fetching drift matrix data:', error);
|
|
throw error;
|
|
}
|
|
}
|