From c98b96dd0cc179bc9c2e91a9fe247e3153718dc4 Mon Sep 17 00:00:00 2001 From: ayzen Date: Mon, 6 Oct 2025 20:50:21 +0300 Subject: [PATCH] updated proc load task --- vna_system/web_ui/static/js/modules/charts.js | 101 ++++++++++++++---- 1 file changed, 78 insertions(+), 23 deletions(-) diff --git a/vna_system/web_ui/static/js/modules/charts.js b/vna_system/web_ui/static/js/modules/charts.js index 696df70..fed2333 100644 --- a/vna_system/web_ui/static/js/modules/charts.js +++ b/vna_system/web_ui/static/js/modules/charts.js @@ -53,7 +53,7 @@ export class ChartManager { addResult(payload) { try { - const { processor_id, timestamp, plotly_config, metadata, data } = payload; + const { processor_id, timestamp, plotly_config, metadata } = payload; if (!processor_id) { console.warn('Invalid result - missing processor_id:', payload); return; @@ -63,10 +63,11 @@ export class ChartManager { return; } + // Note: 'data' field is no longer included in broadcasts to reduce traffic + // Use get_processor_state command to retrieve full data when needed this.chartData.set(processor_id, { timestamp: new Date((timestamp ?? Date.now()) * 1000), metadata: metadata || {}, - data: data || {}, plotly_config: plotly_config || { data: [], layout: {} } }); @@ -300,22 +301,54 @@ export class ChartManager { const c = this.charts.get(id); if (!c?.plotContainer) return; - try { - const timestamp = new Date().toISOString().replace(/[:.]/g, '-'); - const baseFilename = `${id}_${timestamp}`; + const timestamp = new Date().toISOString().replace(/[:.]/g, '-'); + const baseFilename = `${id}_${timestamp}`; + try { + // Download plot image await downloadPlotlyImage(c.plotContainer, `${baseFilename}_plot`); - const processorData = this.prepareDownloadData(id); - if (processorData) { - downloadJSON(processorData, `${baseFilename}_data.json`); - } + // Request full processor state from backend via WebSocket + const websocket = window.vnaDashboard?.websocket; + if (websocket && websocket.getProcessorState) { + // Set up one-time listener for processor_state response + const stateHandler = (payload) => { + if (payload.processor_id === id) { + websocket.off('processor_state', stateHandler); - this.notifications?.show?.({ - type: 'success', - title: 'Скачивание завершено', - message: `Скачаны график и данные ${formatProcessorName(id)}` - }); + const processorData = this.prepareDownloadDataFromState(id, payload); + if (processorData) { + downloadJSON(processorData, `${baseFilename}_data.json`); + } + + this.notifications?.show?.({ + type: 'success', + title: 'Скачивание завершено', + message: `Скачаны график и данные ${formatProcessorName(id)}` + }); + } + }; + + websocket.on('processor_state', stateHandler); + websocket.getProcessorState(id); + + // Timeout fallback in case server doesn't respond + setTimeout(() => { + websocket.off('processor_state', stateHandler); + }, 10000); + } else { + console.warn('WebSocket not available, downloading limited data'); + const processorData = this.prepareDownloadDataFallback(id); + if (processorData) { + downloadJSON(processorData, `${baseFilename}_data.json`); + } + + this.notifications?.show?.({ + type: 'warning', + title: 'Скачивание завершено', + message: `График скачан. Данные ограничены (нет подключения к серверу)` + }); + } } catch (e) { console.error('Chart download failed:', e); this.notifications?.show?.({ @@ -326,15 +359,38 @@ export class ChartManager { } } - prepareDownloadData(processorId) { - const chart = this.charts.get(processorId); - const latestData = this.chartData.get(processorId); + prepareDownloadDataFromState(processorId, statePayload) { + if (!statePayload || !statePayload.state) return null; - if (!chart || !latestData) return null; - - // Extract sweep_history from metadata if available - const sweepHistory = latestData.metadata?.sweep_history || []; + const { state, current_data } = statePayload; + return { + processor_info: { + processor_id: processorId, + processor_name: formatProcessorName(processorId), + download_timestamp: new Date().toISOString() + }, + state: { + config: state.config, + history_count: state.history_count, + max_history: state.max_history + }, + current_data: current_data ? { + data: safeClone(current_data.data), + plotly_config: safeClone(current_data.plotly_config), + timestamp: current_data.timestamp + } : null, + sweep_history: state.sweep_history || [] + }; + } + + prepareDownloadDataFallback(processorId) { + const chart = this.charts.get(processorId); + const latestData = this.chartData.get(processorId); + + if (!chart || !latestData) return null; + + // Fallback when WebSocket is not available - limited data only return { processor_info: { processor_id: processorId, @@ -342,12 +398,11 @@ export class ChartManager { download_timestamp: new Date().toISOString() }, current_data: { - data: safeClone(latestData.data), metadata: safeClone(latestData.metadata), timestamp: latestData.timestamp instanceof Date ? latestData.timestamp.toISOString() : latestData.timestamp, plotly_config: safeClone(latestData.plotly_config) }, - sweep_history: sweepHistory + note: "Limited data - sweep history not available without server connection" }; }