46 lines
1.3 KiB
JavaScript
46 lines
1.3 KiB
JavaScript
/**
|
|
* @file js/api.js
|
|
* @description Beinhaltet Funktionen für die Kommunikation mit der API und das Parsen von ResultData.
|
|
* @module data_layer
|
|
*/
|
|
|
|
/**
|
|
* Stellt die Verbindung zur API her und ruft Analyseergebnisse ab.
|
|
*
|
|
* @async
|
|
* @function fetchResults
|
|
* @returns {Promise<Array<{run_id: string, step_stability_score: number, correlation_coefficient: number}>>} Array von ResultData.
|
|
* @throws {Error} Wird ausgelöst, wenn der Fetch-Vorgang fehlschlägt oder die Response ungültig ist.
|
|
*/
|
|
export async function fetchResults() {
|
|
const endpoint = '/api/results';
|
|
try {
|
|
const response = await fetch(endpoint, {
|
|
method: 'GET',
|
|
headers: {
|
|
'Accept': 'application/json'
|
|
}
|
|
});
|
|
|
|
if (!response.ok) {
|
|
throw new Error(`API-Fehler: ${response.status} ${response.statusText}`);
|
|
}
|
|
|
|
const data = await response.json();
|
|
|
|
if (!Array.isArray(data)) {
|
|
throw new Error('Ungültige API-Antwort: Daten sind nicht vom Typ Array');
|
|
}
|
|
|
|
const parsedResults = data.map(item => ({
|
|
run_id: String(item.run_id ?? ''),
|
|
step_stability_score: Number(item.step_stability_score ?? 0),
|
|
correlation_coefficient: Number(item.correlation_coefficient ?? 0)
|
|
}));
|
|
|
|
return parsedResults;
|
|
} catch (error) {
|
|
console.error('Fehler beim Abrufen der Ergebnisse:', error);
|
|
throw error;
|
|
}
|
|
}
|