diff --git a/trace_visualization/js/api.js b/trace_visualization/js/api.js new file mode 100644 index 0000000..c3ae620 --- /dev/null +++ b/trace_visualization/js/api.js @@ -0,0 +1,43 @@ +'use strict'; + +/** + * Sendet GET-Anfrage an /api/get-analysis und gibt geparste JSON-Daten zurück. + * @param {Object} [options] - Optionales Filterobjekt { filter: 'idle' | 'load' } + * @returns {Promise} - Promise, das das Analyse-Datenobjekt liefert + */ +export async function fetchAnalysisData(options = {}) { + const endpoint = '/api/get-analysis'; + const params = new URLSearchParams(); + + if (options.filter && (options.filter === 'idle' || options.filter === 'load')) { + params.append('filter', options.filter); + } + + const url = params.toString() ? `${endpoint}?${params.toString()}` : endpoint; + + try { + const response = await fetch(url, { + method: 'GET', + headers: { + 'Accept': 'application/json' + } + }); + + if (!response.ok) { + console.error(`API-Fehler: ${response.status} ${response.statusText}`); + throw new Error(`Fehler beim Abrufen der Analyse: ${response.status}`); + } + + const data = await response.json(); + + if (!data || typeof data !== 'object' || !Array.isArray(data.runs)) { + console.error('Unerwartetes Datenformat von /api/get-analysis:', data); + throw new Error('Ungültige Datenstruktur von der API'); + } + + return data; + } catch (error) { + console.error('Fehler beim Abruf der Analysedaten:', error); + throw error; + } +}