feat: implement a new simulation analyzer component to track and analyze various performance metrics and material flow.

This commit is contained in:
2025-12-22 13:53:46 +05:30
parent 67e6407153
commit aadecec2c9

View File

@@ -245,15 +245,51 @@ function Analyzer() {
setAnalyzing(false); setAnalyzing(false);
}; };
// Simulation Time Tracking
const simulationTimeRef = useRef<number>(0);
const lastSpeedUpdateRef = useRef<number>(Date.now());
const prevSpeedRef = useRef<number>(speed); // Initialize with current speed
// Reset accumulated time when simulation stops/starts
useEffect(() => { useEffect(() => {
if (!isPlaying) { if (!isPlaying) {
resetAllRefs(); resetAllRefs();
simulationTimeRef.current = 0;
lastSpeedUpdateRef.current = Date.now();
} else { } else {
// Reset start time when simulation starts // Reset start time when simulation starts
startTimeRef.current = new Date().toISOString(); startTimeRef.current = new Date().toISOString();
simulationTimeRef.current = 0;
lastSpeedUpdateRef.current = Date.now();
} }
}, [isPlaying]); }, [isPlaying]);
// Track accumulated simulation time on speed change
useEffect(() => {
if (!isPlaying) {
prevSpeedRef.current = speed;
return;
}
const now = Date.now();
const deltaReal = now - lastSpeedUpdateRef.current;
// Add time segment using previous speed
simulationTimeRef.current += deltaReal * Math.max(1, prevSpeedRef.current);
lastSpeedUpdateRef.current = now;
prevSpeedRef.current = speed; // Update for next segment
}, [speed, isPlaying]);
const getSimulationDuration = () => {
if (!isPlaying) return simulationTimeRef.current;
const now = Date.now();
const deltaReal = now - lastSpeedUpdateRef.current;
// Current accumulating segment uses current speed
return simulationTimeRef.current + deltaReal * Math.max(1, speed);
};
// ============================================================================ // ============================================================================
// ENHANCED UTILITY FUNCTIONS // ENHANCED UTILITY FUNCTIONS
// ============================================================================ // ============================================================================
@@ -326,12 +362,12 @@ function Analyzer() {
const wip = materialsOnAsset; const wip = materialsOnAsset;
// Calculate throughput (items per second) // Calculate throughput (items per second)
// Ensure we don't divide by zero // Use accumulated simulation duration
const durationMs = Date.now() - new Date(startTimeRef.current).getTime(); const durationMs = getSimulationDuration();
// Normalize by simulation speed to get "simulation time" throughput // Throughput = items / simulation seconds
const currentSpeed = Math.max(1, speed); // durationMs is already in "simulation milliseconds" (real ms * speed)
const throughput = durationMs > 1000 ? ((removals.length / durationMs) * 1000) / currentSpeed : 0; const throughput = durationMs > 1000 ? (removals.length / durationMs) * 1000 : 0;
// Calculate lead times (processing times on this asset) // Calculate lead times (processing times on this asset)
const leadTimes = removals.map((m) => m.processingTime || 0).filter((t) => t > 0); const leadTimes = removals.map((m) => m.processingTime || 0).filter((t) => t > 0);
@@ -1975,7 +2011,6 @@ function Analyzer() {
}; };
}); });
// Material Flow Analysis
// Material Flow Analysis // Material Flow Analysis
const totalMaterialsInSystem = materials.filter((m) => m.isActive).length; const totalMaterialsInSystem = materials.filter((m) => m.isActive).length;