Update: 2026-06-30 21:12:26
This commit is contained in:
@@ -35,7 +35,7 @@ if (empty($data) || !is_array($data)) {
|
||||
}
|
||||
|
||||
$insertedCount = 0;
|
||||
$stmt = $con->prepare("INSERT INTO scraped_competitor_prices (task_id, app_name, start_location, end_location, price_amount, currency) VALUES (?, ?, ?, ?, ?, ?)");
|
||||
$stmt = $con->prepare("INSERT INTO scraped_competitor_prices (task_id, app_name, competitor_name, start_location, end_location, start_lat, start_lng, end_lat, end_lng, price_amount, price_per_km, currency, country_code) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
|
||||
|
||||
foreach ($data as $row) {
|
||||
if (isset($row['status']) && $row['status'] !== 'success') {
|
||||
@@ -83,7 +83,21 @@ foreach ($data as $row) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($stmt->execute([$taskId, $appName, $startLoc, $endLoc, $amount, $currency])) {
|
||||
$distanceKm = (float)($resultData['distance_km'] ?? 1);
|
||||
if ($distanceKm <= 0) $distanceKm = 1;
|
||||
$pricePerKm = $amount / $distanceKm;
|
||||
|
||||
$startLat = $resultData['start_lat'] ?? null;
|
||||
$startLng = $resultData['start_lng'] ?? null;
|
||||
$endLat = $resultData['end_lat'] ?? null;
|
||||
$endLng = $resultData['end_lng'] ?? null;
|
||||
$countryCode = 'JO'; // Default for now, as scraping is in Jordan
|
||||
|
||||
if ($stmt->execute([
|
||||
$taskId, $appName, $appName, $startLoc, $endLoc,
|
||||
$startLat, $startLng, $endLat, $endLng,
|
||||
$amount, $pricePerKm, $currency, $countryCode
|
||||
])) {
|
||||
$insertedCount++;
|
||||
} else {
|
||||
echo "Failed to insert task_id: $taskId. Error: " . implode(" ", $stmt->errorInfo()) . "\n";
|
||||
|
||||
@@ -30,8 +30,8 @@ echo "[".date('Y-m-d H:i:s')."] Starting cron_surge_opportunity...\n";
|
||||
try {
|
||||
// 1. حساب الـ baseline و current
|
||||
$sql = "SELECT
|
||||
ROUND(cp.from_latitude * 74, 0) / 74 AS lat_group,
|
||||
ROUND(cp.from_longitude * 74, 0) / 74 AS lng_group,
|
||||
ROUND(cp.start_lat * 74, 0) / 74 AS lat_group,
|
||||
ROUND(cp.start_lng * 74, 0) / 74 AS lng_group,
|
||||
cp.competitor_name,
|
||||
cp.country_code,
|
||||
AVG(CASE WHEN cp.created_at < DATE_SUB(NOW(), INTERVAL 6 HOUR)
|
||||
@@ -40,7 +40,7 @@ try {
|
||||
THEN cp.price_per_km END) AS current_avg,
|
||||
COUNT(*) AS total_samples,
|
||||
SUM(CASE WHEN cp.created_at >= DATE_SUB(NOW(), INTERVAL 2 HOUR) THEN 1 ELSE 0 END) AS recent_samples
|
||||
FROM competitor_prices cp
|
||||
FROM scraped_competitor_prices cp
|
||||
WHERE cp.created_at >= DATE_SUB(NOW(), INTERVAL 7 DAY)
|
||||
AND cp.price_per_km > 0
|
||||
GROUP BY lat_group, lng_group, cp.competitor_name, cp.country_code
|
||||
|
||||
@@ -26,10 +26,10 @@ try {
|
||||
foreach ($countries as $countryCode) {
|
||||
|
||||
// 1. Calculate Average PCI and Market Share
|
||||
$sql = "SELECT distance_km, total_price
|
||||
FROM competitor_prices
|
||||
$sql = "SELECT (price_amount / price_per_km) AS distance_km, price_amount AS total_price
|
||||
FROM scraped_competitor_prices
|
||||
WHERE country_code = :country
|
||||
AND distance_km > 0
|
||||
AND price_per_km > 0
|
||||
AND created_at >= DATE_SUB(NOW(), INTERVAL 7 DAY)";
|
||||
$stmt = $con->prepare($sql);
|
||||
$stmt->execute([':country' => $countryCode]);
|
||||
|
||||
@@ -17,19 +17,27 @@ try {
|
||||
|
||||
// 1. Ensure Table Exists
|
||||
$sql = "
|
||||
CREATE TABLE IF NOT EXISTS competitor_prices (
|
||||
id INT AUTO_INCREMENT PRIMARY KEY,
|
||||
competitor_name VARCHAR(50) NOT NULL,
|
||||
from_latitude VARCHAR(30) NOT NULL,
|
||||
from_longitude VARCHAR(30) NOT NULL,
|
||||
to_latitude VARCHAR(30) NOT NULL,
|
||||
to_longitude VARCHAR(30) NOT NULL,
|
||||
distance_km DECIMAL(8,2) NOT NULL,
|
||||
total_price DECIMAL(10,2) NOT NULL,
|
||||
price_per_km DECIMAL(8,2) NOT NULL,
|
||||
country_code VARCHAR(5) NOT NULL DEFAULT 'SY',
|
||||
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
INDEX idx_competitor_country (competitor_name, country_code)
|
||||
CREATE TABLE IF NOT EXISTS `scraped_competitor_prices` (
|
||||
`id` BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
|
||||
`task_id` varchar(100) DEFAULT NULL,
|
||||
`app_name` varchar(100) NOT NULL,
|
||||
`competitor_name` varchar(100) NOT NULL,
|
||||
`start_location` varchar(255) NOT NULL,
|
||||
`end_location` varchar(255) NOT NULL,
|
||||
`start_lat` decimal(10,7) DEFAULT NULL,
|
||||
`start_lng` decimal(10,7) DEFAULT NULL,
|
||||
`end_lat` decimal(10,7) DEFAULT NULL,
|
||||
`end_lng` decimal(10,7) DEFAULT NULL,
|
||||
`price_amount` decimal(8,2) NOT NULL,
|
||||
`price_per_km` decimal(8,2) NOT NULL,
|
||||
`currency` varchar(10) NOT NULL DEFAULT 'JOD',
|
||||
`country_code` varchar(10) NOT NULL DEFAULT 'JO',
|
||||
`scraped_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
KEY `idx_app_name` (`app_name`),
|
||||
KEY `idx_competitor_name` (`competitor_name`),
|
||||
KEY `idx_start_location` (`start_location`),
|
||||
KEY `idx_country_code` (`country_code`)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
|
||||
";
|
||||
$con->exec($sql);
|
||||
|
||||
@@ -133,11 +133,13 @@ if ($method === 'GET') {
|
||||
|
||||
// 1. Save to MySQL
|
||||
$stmt = $con->prepare("
|
||||
INSERT INTO competitor_prices
|
||||
(competitor_name, from_latitude, from_longitude, to_latitude, to_longitude, distance_km, total_price, price_per_km, country_code)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
INSERT INTO scraped_competitor_prices
|
||||
(task_id, app_name, competitor_name, start_location, end_location, start_lat, start_lng, end_lat, end_lng, price_amount, price_per_km, currency, country_code)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
");
|
||||
$stmt->execute([$app_name, (string)$start_lat, (string)$start_lng, (string)$end_lat, (string)$end_lng, $distance_km, $price, $pricePerKm, $country_code]);
|
||||
$start_loc = "Lat: $start_lat, Lng: $start_lng";
|
||||
$end_loc = "Lat: $end_lat, Lng: $end_lng";
|
||||
$stmt->execute([$task_id, $app_name, $app_name, $start_loc, $end_loc, (string)$start_lat, (string)$start_lng, (string)$end_lat, (string)$end_lng, $price, $pricePerKm, 'JOD', $country_code]);
|
||||
|
||||
// 2. Save to Redis (Calculate Price Per KM)
|
||||
if ($distance_km > 0 && $price > 0) {
|
||||
|
||||
Reference in New Issue
Block a user