feat: Enhance event logging for plot sessions and improve zoom reset functionality with data reload
This commit is contained in:
parent
73f743ce7c
commit
438ebc1462
|
@ -4306,8 +4306,59 @@
|
|||
"trigger_variable": null,
|
||||
"auto_started": true
|
||||
}
|
||||
},
|
||||
{
|
||||
"timestamp": "2025-08-15T00:51:48.111341",
|
||||
"level": "info",
|
||||
"event_type": "plot_session_created",
|
||||
"message": "Plot session 'UR29' created and started",
|
||||
"details": {
|
||||
"session_id": "plot_1",
|
||||
"variables": [
|
||||
"UR29_Brix",
|
||||
"UR29_ma",
|
||||
"AUX Blink_1.0S"
|
||||
],
|
||||
"time_window": 3600,
|
||||
"trigger_variable": null,
|
||||
"auto_started": true
|
||||
}
|
||||
},
|
||||
{
|
||||
"timestamp": "2025-08-15T00:51:59.105937",
|
||||
"level": "info",
|
||||
"event_type": "plot_session_created",
|
||||
"message": "Plot session 'UR29' created and started",
|
||||
"details": {
|
||||
"session_id": "plot_1",
|
||||
"variables": [
|
||||
"UR29_Brix",
|
||||
"UR29_ma",
|
||||
"AUX Blink_1.0S"
|
||||
],
|
||||
"time_window": 360,
|
||||
"trigger_variable": null,
|
||||
"auto_started": true
|
||||
}
|
||||
},
|
||||
{
|
||||
"timestamp": "2025-08-15T00:53:37.664354",
|
||||
"level": "info",
|
||||
"event_type": "plot_session_created",
|
||||
"message": "Plot session 'UR29' created and started",
|
||||
"details": {
|
||||
"session_id": "plot_1",
|
||||
"variables": [
|
||||
"UR29_Brix",
|
||||
"UR29_ma",
|
||||
"AUX Blink_1.0S"
|
||||
],
|
||||
"time_window": 360,
|
||||
"trigger_variable": null,
|
||||
"auto_started": true
|
||||
}
|
||||
}
|
||||
],
|
||||
"last_updated": "2025-08-15T00:50:02.569962",
|
||||
"total_entries": 387
|
||||
"last_updated": "2025-08-15T00:53:37.664354",
|
||||
"total_entries": 390
|
||||
}
|
|
@ -7,7 +7,7 @@
|
|||
"point_hover_radius": 4,
|
||||
"point_radius": 0,
|
||||
"stepped": true,
|
||||
"time_window": 3600,
|
||||
"time_window": 360,
|
||||
"trigger_enabled": false,
|
||||
"trigger_on_true": true,
|
||||
"trigger_variable": null,
|
||||
|
|
|
@ -1164,86 +1164,66 @@ const ChartjsPlot = ({ session, height = '400px' }) => {
|
|||
setDataPointsCount(0);
|
||||
}, []);
|
||||
|
||||
const resetZoom = useCallback(() => {
|
||||
const resetZoom = useCallback(async () => {
|
||||
if (!chartRef.current) return;
|
||||
|
||||
try {
|
||||
console.log('🔄 Resetting zoom...');
|
||||
|
||||
// Backup data before zoom reset
|
||||
const backup = chartRef.current.data.datasets.map(dataset => ({
|
||||
label: dataset.label,
|
||||
data: [...(dataset.data || [])],
|
||||
timestamp: Date.now()
|
||||
}));
|
||||
|
||||
dataBackupRef.current.set('current', backup);
|
||||
console.log(`📦 Data backed up: ${backup.reduce((total, ds) => total + ds.data.length, 0)} points`);
|
||||
console.log('🔄 Resetting zoom with full data reload...');
|
||||
|
||||
// Get current configuration for variable information
|
||||
const cfg = resolvedConfigRef.current || session?.config;
|
||||
if (!cfg || !cfg.variables) {
|
||||
console.warn('⚠️ No configuration available for data reload');
|
||||
return;
|
||||
}
|
||||
|
||||
// Store current streaming state
|
||||
const realtimeOptions = chartRef.current.options?.scales?.x?.realtime;
|
||||
const wasPaused = realtimeOptions?.pause || false;
|
||||
|
||||
// Try to reset zoom using the zoom plugin
|
||||
if (chartRef.current.resetZoom) {
|
||||
chartRef.current.resetZoom('none'); // Use 'none' animation mode for faster reset
|
||||
} else if (window.Chart?.helpers?.getRelativePosition) {
|
||||
// Fallback: manually reset zoom by updating scale options
|
||||
const chart = chartRef.current;
|
||||
if (chart.options?.scales?.x?.realtime) {
|
||||
// For realtime charts, reset the scale manually
|
||||
const now = Date.now();
|
||||
chart.options.scales.x.realtime.duration = chart.options.scales.x.realtime.duration || 60000;
|
||||
chart.options.scales.x.min = now - chart.options.scales.x.realtime.duration;
|
||||
chart.options.scales.x.max = now;
|
||||
chart.update('none');
|
||||
}
|
||||
// 1. Clear all chart data
|
||||
if (chartRef.current) {
|
||||
chartRef.current.data.datasets.forEach(dataset => {
|
||||
if (dataset.data) {
|
||||
dataset.data.length = 0;
|
||||
}
|
||||
});
|
||||
chartRef.current.update('quiet');
|
||||
setDataPointsCount(0);
|
||||
console.log('✅ Chart data cleared');
|
||||
}
|
||||
|
||||
// Restore data if it was lost during zoom reset
|
||||
setTimeout(() => {
|
||||
if (chartRef.current && dataBackupRef.current.has('current')) {
|
||||
const backupData = dataBackupRef.current.get('current');
|
||||
const now = Date.now();
|
||||
|
||||
// Only restore if backup is recent (within 30 seconds)
|
||||
if (now - backupData[0]?.timestamp <= 30000) {
|
||||
let restored = false;
|
||||
chartRef.current.data.datasets.forEach((dataset, index) => {
|
||||
const backupDataset = backupData[index];
|
||||
if (backupDataset && dataset.label === backupDataset.label) {
|
||||
// Only restore if current data is significantly smaller
|
||||
if (!dataset.data || dataset.data.length < backupDataset.data.length * 0.3) {
|
||||
dataset.data = [...backupDataset.data];
|
||||
restored = true;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
if (restored) {
|
||||
console.log('🔄 Chart data restored from backup');
|
||||
chartRef.current.update('none');
|
||||
|
||||
const totalPoints = chartRef.current.data.datasets.reduce((total, dataset) =>
|
||||
total + (dataset.data?.length || 0), 0
|
||||
);
|
||||
setDataPointsCount(totalPoints);
|
||||
}
|
||||
}
|
||||
|
||||
// Restore streaming state
|
||||
if (realtimeOptions && !wasPaused) {
|
||||
realtimeOptions.pause = false;
|
||||
}
|
||||
|
||||
console.log('✅ Zoom reset with data preservation complete');
|
||||
}
|
||||
}, 100); // Small delay to ensure zoom reset is complete
|
||||
// 2. Reset zoom using the zoom plugin
|
||||
if (chartRef.current.resetZoom) {
|
||||
chartRef.current.resetZoom('none');
|
||||
console.log('✅ Zoom reset applied');
|
||||
}
|
||||
|
||||
// 3. Reload historical data
|
||||
const enabledVariables = getEnabledVariables(cfg.variables);
|
||||
if (enabledVariables.length > 0) {
|
||||
console.log(`📊 Reloading data for ${enabledVariables.length} variables...`);
|
||||
|
||||
// Get current time window (use same as initial load)
|
||||
const timeWindow = cfg.time_window || 3600; // Default 1 hour
|
||||
|
||||
// Load fresh historical data
|
||||
await loadHistoricalData(enabledVariables, timeWindow);
|
||||
console.log('✅ Historical data reloaded');
|
||||
}
|
||||
|
||||
// 4. Restore streaming state if it wasn't paused
|
||||
if (realtimeOptions && !wasPaused) {
|
||||
realtimeOptions.pause = false;
|
||||
console.log('✅ Streaming state restored');
|
||||
}
|
||||
|
||||
console.log('✅ Reset zoom with full data reload complete');
|
||||
|
||||
} catch (error) {
|
||||
console.warn('Failed to reset zoom:', error);
|
||||
console.error('❌ Error during zoom reset with data reload:', error);
|
||||
}
|
||||
}, []);
|
||||
}, [session, getEnabledVariables, loadHistoricalData]);
|
||||
|
||||
// Update configuration directly (for real-time style changes)
|
||||
const updateConfig = useCallback(async (newConfig) => {
|
||||
|
|
Loading…
Reference in New Issue