From 61cb615ae7d605d07d59bed9b9a69b6c5096b63a Mon Sep 17 00:00:00 2001 From: Hamza-Ayed Date: Mon, 6 Jul 2026 04:33:26 +0300 Subject: [PATCH] Update: 2026-07-06 04:33:26 --- backend/bot/recalculate_all_distances.php | 81 +++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 backend/bot/recalculate_all_distances.php diff --git a/backend/bot/recalculate_all_distances.php b/backend/bot/recalculate_all_distances.php new file mode 100644 index 00000000..d806e888 --- /dev/null +++ b/backend/bot/recalculate_all_distances.php @@ -0,0 +1,81 @@ +getMessage() . "\n"); +} + +echo "Fetching ALL rows to recalculate distance and duration...\n"; + +// Get all valid rows +$stmt = $con->prepare(" + SELECT id, start_lat, start_lng, end_lat, end_lng, country_code, price_amount + FROM scraped_competitor_prices + WHERE start_lat IS NOT NULL + AND start_lng IS NOT NULL +"); +$stmt->execute(); +$rows = $stmt->fetchAll(PDO::FETCH_ASSOC); + +if (!$rows || count($rows) === 0) { + echo "No rows found.\n"; + exit(0); +} + +echo "Found " . count($rows) . " rows to update. Processing...\n"; + +$updateStmt = $con->prepare(" + UPDATE scraped_competitor_prices + SET duration_min = :duration_min, + distance_km = :distance_km, + price_per_km = :price_per_km + WHERE id = :id +"); + +$successCount = 0; +$failCount = 0; + +foreach ($rows as $index => $row) { + $id = $row['id']; + $countryCode = $row['country_code'] ?? 'JO'; + $price = (float)$row['price_amount']; + + // Call Intaleq Maps (OSRM API) + $routeInfo = getOsrmRouteDetails($row['start_lat'], $row['start_lng'], $row['end_lat'], $row['end_lng'], $countryCode); + + if ($routeInfo && isset($routeInfo['duration_min']) && isset($routeInfo['distance_km'])) { + $dist = (float)$routeInfo['distance_km']; + $dur = (int)round($routeInfo['duration_min']); + $pricePerKm = $dist > 0 ? ($price / $dist) : 0; + + $updateStmt->execute([ + ':duration_min' => $dur, + ':distance_km' => $dist, + ':price_per_km' => $pricePerKm, + ':id' => $id + ]); + $successCount++; + echo "Row $id: Distance {$dist}km, Duration {$dur}min, Price/Km {$pricePerKm} [SUCCESS]\n"; + } else { + $failCount++; + echo "Row $id: Failed to fetch route details.\n"; + } + + // Small sleep to avoid hitting API rate limits too hard (100ms) + usleep(100000); +} + +echo "======================================\n"; +echo "Completed processing " . count($rows) . " rows.\n"; +echo "Success: $successCount\n"; +echo "Failed: $failCount\n";