From f9a65e26f2ad435fe1d6d36eceab8612f51cd59a Mon Sep 17 00:00:00 2001 From: Mika Date: Sat, 24 Jan 2026 12:03:27 +0000 Subject: [PATCH] Add visualization_tool/js/api.js --- visualization_tool/js/api.js | 64 ++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 visualization_tool/js/api.js diff --git a/visualization_tool/js/api.js b/visualization_tool/js/api.js new file mode 100644 index 0000000..448a509 --- /dev/null +++ b/visualization_tool/js/api.js @@ -0,0 +1,64 @@ +/** + * Data Layer for Frozen Runs Visualization Tool + * Handles API communication and data transformation. + * © 2026 Donau2Space.de + */ + +/** + * Fetches metric data from the /metrics API endpoint. + * @async + * @param {string} [filter] - Optional filter parameter ('pinned' or 'unpinned'). + * @returns {Promise} Parsed metric data ready for visualization. + */ +export async function fetchMetrics(filter) { + try { + const queryParam = filter ? `?filter=${encodeURIComponent(filter)}` : ''; + const response = await fetch(`/metrics${queryParam}`, { + method: 'GET', + headers: { + 'Accept': 'application/json' + } + }); + + if (!response.ok) { + throw new Error(`API request failed with status ${response.status}`); + } + + const rawData = await response.json(); + return parseMetricsData(rawData); + } catch (error) { + console.error('Error fetching metrics:', error); + return []; + } +} + +/** + * Parses and structures raw metric data from API. + * @param {Array} data - Raw metric data from /metrics. + * @returns {Array} Structured metric objects for visualization. + */ +export function parseMetricsData(data) { + if (!Array.isArray(data)) return []; + + return data.map(entry => { + const { + metricName = 'unknown', + runId = 'N/A', + pinned_flag = false, + effect_size = null, + ci_lower = null, + ci_upper = null + } = entry ?? {}; + + return { + name: metricName, + runId, + pinned: Boolean(pinned_flag), + effectSize: typeof effect_size === 'number' ? effect_size : null, + confidenceInterval: { + lower: typeof ci_lower === 'number' ? ci_lower : null, + upper: typeof ci_upper === 'number' ? ci_upper : null + } + }; + }); +}