Add drift_matrix_visualization/js/api.js

This commit is contained in:
Mika 2026-02-23 14:48:35 +00:00
parent 7ebdb99645
commit d8d0abb4f5

View file

@ -0,0 +1,48 @@
/**
* @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;
}
}