This commit is contained in:
Jake Kasper
2025-09-02 07:59:54 -05:00
parent a37e871d1c
commit fcf5d3571c

View File

@@ -32,38 +32,45 @@ const History = () => {
const [showFilters, setShowFilters] = useState(false); const [showFilters, setShowFilters] = useState(false);
const [showProductDropdown, setShowProductDropdown] = useState(false); const [showProductDropdown, setShowProductDropdown] = useState(false);
// Haversine in meters
const haversineMeters = (lat1, lng1, lat2, lng2) => {
const R = 6371e3;
const toRad = (d) => (d * Math.PI) / 180;
const dLat = toRad(lat2 - lat1);
const dLng = toRad(lng2 - lng1);
const a = Math.sin(dLat / 2) ** 2 + Math.cos(toRad(lat1)) * Math.cos(toRad(lat2)) * Math.sin(dLng / 2) ** 2;
return 2 * R * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
};
const computeDistanceFeetFromPoints = (points = []) => {
if (!Array.isArray(points) || points.length < 2) return 0;
let meters = 0;
for (let i = 1; i < points.length; i++) {
const p1 = points[i - 1];
const p2 = points[i];
meters += haversineMeters(p1.lat, p1.lng, p2.lat, p2.lng);
}
return meters * 3.28084;
};
// Calculate coverage percentage based on GPS tracking and equipment specifications // Calculate coverage percentage based on GPS tracking and equipment specifications
const calculateCoverage = (application, log) => { const calculateCoverage = (application, log) => {
if (!log?.gpsTrack?.points || log.gpsTrack.points.length < 2) { if (!log?.gpsTrack?.points || log.gpsTrack.points.length < 2) return 0;
return 0; // No movement = no coverage
}
const totalDistance = log.gpsTrack.totalDistance || 0; const storedMeters = typeof log.gpsTrack.totalDistance === 'number' ? log.gpsTrack.totalDistance : 0;
const totalDistanceFeet = storedMeters > 0 ? storedMeters * 3.28084 : computeDistanceFeetFromPoints(log.gpsTrack.points);
const plannedArea = application.totalSectionArea || 0; const plannedArea = application.totalSectionArea || 0;
if (totalDistanceFeet === 0 || plannedArea === 0) return 0;
if (totalDistance === 0 || plannedArea === 0) {
return 0;
}
// Estimate equipment width based on type // Estimate equipment width in feet
let equipmentWidth = 4; // Default 4 feet for unknown equipment let equipmentWidth = 4;
const equipmentName = application.equipmentName?.toLowerCase() || ''; const equipmentName = application.equipmentName?.toLowerCase() || '';
if (equipmentName.includes('spreader')) { if (equipmentName.includes('spreader')) equipmentWidth = 12;
equipmentWidth = 12; // Spreaders typically 8-16 feet wide else if (equipmentName.includes('sprayer')) equipmentWidth = 20;
} else if (equipmentName.includes('sprayer')) { else if (equipmentName.includes('mower')) equipmentWidth = 6;
equipmentWidth = 20; // Sprayers typically 15-30 feet wide
} else if (equipmentName.includes('mower')) {
equipmentWidth = 6; // Mowers typically 4-8 feet wide
}
// Calculate theoretical coverage area const theoreticalCoverageArea = totalDistanceFeet * equipmentWidth;
// Distance (feet) * Width (feet) = Coverage area (sq ft)
const theoreticalCoverageArea = totalDistance * equipmentWidth;
// Calculate coverage percentage (capped at 100%)
const coveragePercentage = Math.min((theoreticalCoverageArea / plannedArea) * 100, 100); const coveragePercentage = Math.min((theoreticalCoverageArea / plannedArea) * 100, 100);
return Math.round(coveragePercentage); return Math.round(coveragePercentage);
}; };
@@ -673,4 +680,4 @@ const History = () => {
); );
}; };
export default History; export default History;