Update: 2026-06-21 18:58:05
This commit is contained in:
@@ -18,17 +18,19 @@ try {
|
||||
// 1. Ensure Table Exists
|
||||
$sql = "
|
||||
CREATE TABLE IF NOT EXISTS competitor_prices (
|
||||
id INT AUTO_INCREMENT PRIMARY KEY,
|
||||
app_name VARCHAR(50) NOT NULL,
|
||||
start_lat DECIMAL(10,8) NOT NULL,
|
||||
start_lng DECIMAL(11,8) NOT NULL,
|
||||
end_lat DECIMAL(10,8) NOT NULL,
|
||||
end_lng DECIMAL(11,8) NOT NULL,
|
||||
distance_km FLOAT NOT NULL,
|
||||
price FLOAT NOT NULL,
|
||||
recorded_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
INDEX idx_app_time (app_name, recorded_at)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
|
||||
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)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
|
||||
";
|
||||
$con->exec($sql);
|
||||
|
||||
|
||||
@@ -17,7 +17,12 @@ try {
|
||||
// =========================================================
|
||||
|
||||
// 1. Security & Configuration
|
||||
$SECRET_KEY = getenv('BOT_SECRET_KEY') ?: 'SIRO_BOT_SUPER_SECRET_123';
|
||||
$SECRET_KEY = getenv('BOT_SECRET_KEY');
|
||||
if (!$SECRET_KEY) {
|
||||
http_response_code(500);
|
||||
echo json_encode(['status' => 'failure', 'message' => 'Server configuration error: BOT_SECRET_KEY not set in environment']);
|
||||
exit;
|
||||
}
|
||||
$ALLOWED_DEVICES = ['SHAM_CASH_BOT_01', 'PRICE_SCRAPER_BOT_01'];
|
||||
|
||||
$method = $_SERVER['REQUEST_METHOD'];
|
||||
@@ -123,23 +128,25 @@ if ($method === 'GET') {
|
||||
$end_lat = (float)($result_data['end_lat'] ?? 0);
|
||||
$end_lng = (float)($result_data['end_lng'] ?? 0);
|
||||
|
||||
$pricePerKm = $distance_km > 0 ? ($price / $distance_km) : 0.0;
|
||||
$country_code = $result_data['country_code'] ?? 'SY';
|
||||
|
||||
// 1. Save to MySQL
|
||||
$stmt = $con->prepare("
|
||||
INSERT INTO competitor_prices
|
||||
(app_name, start_lat, start_lng, end_lat, end_lng, distance_km, price)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?)
|
||||
(competitor_name, from_latitude, from_longitude, to_latitude, to_longitude, distance_km, total_price, price_per_km, country_code)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
");
|
||||
$stmt->execute([$app_name, $start_lat, $start_lng, $end_lat, $end_lng, $distance_km, $price]);
|
||||
$stmt->execute([$app_name, (string)$start_lat, (string)$start_lng, (string)$end_lat, (string)$end_lng, $distance_km, $price, $pricePerKm, $country_code]);
|
||||
|
||||
// 2. Save to Redis (Calculate Price Per KM)
|
||||
if ($distance_km > 0 && $price > 0) {
|
||||
$pricePerKm = $price / $distance_km;
|
||||
// Store in Redis (Main) to be used by Pricing Engine
|
||||
// Store recent 50 prices for the app
|
||||
$redis->lpush("competitor:price_history:$app_name", $pricePerKm);
|
||||
$redis->ltrim("competitor:price_history:$app_name", 0, 49);
|
||||
|
||||
error_log("[Bot Worker] Price Check $app_name: Dist $distance_km, Price $price");
|
||||
error_log("[Bot Worker] Price Check $app_name: Dist $distance_km, Price $price, Country $country_code");
|
||||
}
|
||||
} else {
|
||||
// It's a payment task
|
||||
|
||||
Reference in New Issue
Block a user