updated proc load task

This commit is contained in:
ayzen
2025-10-06 20:50:21 +03:00
parent 627bce6e86
commit c98b96dd0c

View File

@ -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,13 +301,22 @@ 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}`;
try {
// Download plot image
await downloadPlotlyImage(c.plotContainer, `${baseFilename}_plot`);
const processorData = this.prepareDownloadData(id);
// 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);
const processorData = this.prepareDownloadDataFromState(id, payload);
if (processorData) {
downloadJSON(processorData, `${baseFilename}_data.json`);
}
@ -316,6 +326,29 @@ export class ChartManager {
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"
};
}