Update: 2026-06-22 00:31:28
This commit is contained in:
59
backend/bot/cron_kazan_adjuster.php
Normal file
59
backend/bot/cron_kazan_adjuster.php
Normal file
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
/**
|
||||
* cron_kazan_adjuster.php
|
||||
* يضبط عمولة كازان ويضيف حوافز للسائقين المتواجدين في مناطق ذروة المنافسين
|
||||
*
|
||||
* المبدأ:
|
||||
* - تقليل العمولة للمنطقة النشطة (مثال: من 15% إلى 10%)
|
||||
* - يتم تشغيله كل 10 دقائق
|
||||
*/
|
||||
|
||||
require_once __DIR__ . '/../core/bootstrap.php';
|
||||
require_once __DIR__ . '/../functions.php';
|
||||
|
||||
try {
|
||||
$con = Database::get('main');
|
||||
$redis = getRedisConnection();
|
||||
} catch (Exception $e) {
|
||||
die("Connection failed: " . $e->getMessage() . "\n");
|
||||
}
|
||||
|
||||
$countries = ['SY', 'JO', 'EG', 'IQ'];
|
||||
|
||||
foreach ($countries as $country) {
|
||||
$surgeKey = "surge:opportunities:{$country}";
|
||||
$hotspotsJson = $redis->get($surgeKey);
|
||||
$hotspots = $hotspotsJson ? json_decode($hotspotsJson, true) : [];
|
||||
|
||||
// In a real scenario, Kazan configs might be stored in a `kazan_commissions` table
|
||||
// or applied dynamically at the ride request time.
|
||||
// Here we will save the "active discounted Kazan multiplier" in Redis so the ride logic can apply it instantly.
|
||||
|
||||
$kazanDiscountConfig = [];
|
||||
|
||||
if (!empty($hotspots)) {
|
||||
foreach ($hotspots as $grid => $multiplier) {
|
||||
// If competitor surge is high enough, we lower our Kazan by a factor
|
||||
// Standard Kazan is let's say 15%.
|
||||
if ($multiplier > 1.2) {
|
||||
// Apply a 30% reduction to Kazan Commission for this grid
|
||||
$kazanDiscountConfig[$grid] = 0.70; // 70% of standard commission
|
||||
} elseif ($multiplier > 1.05) {
|
||||
// Apply a 15% reduction
|
||||
$kazanDiscountConfig[$grid] = 0.85;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$kazanKey = "surge:kazan_discounts:{$country}";
|
||||
|
||||
if (empty($kazanDiscountConfig)) {
|
||||
$redis->del($kazanKey);
|
||||
echo "[$country] No active Kazan discounts.\n";
|
||||
} else {
|
||||
$redis->setex($kazanKey, 1200, json_encode($kazanDiscountConfig));
|
||||
echo "[$country] Updated Kazan discounts for " . count($kazanDiscountConfig) . " active hotspots.\n";
|
||||
}
|
||||
}
|
||||
|
||||
echo "Done.\n";
|
||||
76
backend/bot/cron_seasonal_pricing.php
Normal file
76
backend/bot/cron_seasonal_pricing.php
Normal file
@@ -0,0 +1,76 @@
|
||||
<?php
|
||||
/**
|
||||
* cron_seasonal_pricing.php
|
||||
* يضبط الأسعار بشكل موسمي (مثال: وقت الإفطار في رمضان، الأعياد)
|
||||
* ويقارن سرعة وصول السائقين (ETA Benchmark) إذا توفرت بيانات
|
||||
*/
|
||||
|
||||
require_once __DIR__ . '/../core/bootstrap.php';
|
||||
require_once __DIR__ . '/../functions.php';
|
||||
|
||||
try {
|
||||
$con = Database::get('main');
|
||||
$redis = getRedisConnection();
|
||||
} catch (Exception $e) {
|
||||
die("Connection failed: " . $e->getMessage() . "\n");
|
||||
}
|
||||
|
||||
$countries = ['SY' => 'Asia/Damascus', 'JO' => 'Asia/Amman', 'EG' => 'Africa/Cairo', 'IQ' => 'Asia/Baghdad'];
|
||||
|
||||
// Simulated Calendar of Events
|
||||
$seasons = [
|
||||
'ramadan_iftar' => [
|
||||
'is_active' => true,
|
||||
'start_hour' => 18,
|
||||
'end_hour' => 20,
|
||||
'modifier' => 1.25 // 25% surge during Iftar
|
||||
],
|
||||
'eid' => [
|
||||
'is_active' => false,
|
||||
'start_hour' => 0,
|
||||
'end_hour' => 24,
|
||||
'modifier' => 1.15
|
||||
],
|
||||
'severe_weather' => [
|
||||
'is_active' => false, // Can be toggled manually or via Weather API
|
||||
'start_hour' => 0,
|
||||
'end_hour' => 24,
|
||||
'modifier' => 1.30
|
||||
]
|
||||
];
|
||||
|
||||
foreach ($countries as $code => $timezone) {
|
||||
date_default_timezone_set($timezone);
|
||||
$currentHour = (int)date('H');
|
||||
|
||||
$activeMultiplier = 1.0;
|
||||
$activeReason = null;
|
||||
|
||||
foreach ($seasons as $seasonName => $rules) {
|
||||
if ($rules['is_active'] && $currentHour >= $rules['start_hour'] && $currentHour < $rules['end_hour']) {
|
||||
$activeMultiplier = max($activeMultiplier, $rules['modifier']);
|
||||
$activeReason = $seasonName;
|
||||
}
|
||||
}
|
||||
|
||||
$seasonalKey = "surge:seasonal:{$code}";
|
||||
|
||||
if ($activeMultiplier > 1.0) {
|
||||
$data = [
|
||||
'multiplier' => $activeMultiplier,
|
||||
'reason' => $activeReason,
|
||||
'timestamp' => date('Y-m-d H:i:s')
|
||||
];
|
||||
$redis->setex($seasonalKey, 3600, json_encode($data)); // Expires in 1 hour
|
||||
echo "[$code] Applied Seasonal Pricing: $activeMultiplier x ($activeReason)\n";
|
||||
} else {
|
||||
$redis->del($seasonalKey);
|
||||
echo "[$code] No active seasonal pricing.\n";
|
||||
}
|
||||
|
||||
// --- Point 16: ETA Benchmarking Placeholder ---
|
||||
// If we had a cron that pulled Google Distance Matrix ETAs vs Competitor ETAs, we'd log it here.
|
||||
// echo "[$code] ETA Benchmark recorded.\n";
|
||||
}
|
||||
|
||||
echo "Done.\n";
|
||||
144
backend/bot/cron_surge_opportunity.php
Normal file
144
backend/bot/cron_surge_opportunity.php
Normal file
@@ -0,0 +1,144 @@
|
||||
<?php
|
||||
/**
|
||||
* cron_surge_opportunity.php
|
||||
* مؤشر فرصة الذروة للعمل في الخلفية كـ Cron Job
|
||||
*
|
||||
* المنطق:
|
||||
* 1. يحسب baseline و current لكل منافس في كل grid.
|
||||
* 2. يحدد المناطق التي تشهد surge شامل من المنافسين.
|
||||
* 3. يحفظ المناطق والمضاعف المقترح (multiplier) في Redis.
|
||||
*/
|
||||
|
||||
// Allow script to run indefinitely
|
||||
set_time_limit(0);
|
||||
ini_set('memory_limit', '256M');
|
||||
|
||||
// Mock request to satisfy connect.php dependencies if any
|
||||
$_SERVER['REQUEST_METHOD'] = 'POST';
|
||||
|
||||
require_once __DIR__ . '/../core/bootstrap.php';
|
||||
require_once __DIR__ . '/../functions.php';
|
||||
|
||||
try {
|
||||
$con = Database::get('main');
|
||||
} catch (Exception $e) {
|
||||
die("Database connection failed: " . $e->getMessage() . "\n");
|
||||
}
|
||||
|
||||
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,
|
||||
cp.competitor_name,
|
||||
cp.country_code,
|
||||
AVG(CASE WHEN cp.created_at < DATE_SUB(NOW(), INTERVAL 6 HOUR)
|
||||
THEN cp.price_per_km END) AS baseline_avg,
|
||||
AVG(CASE WHEN cp.created_at >= DATE_SUB(NOW(), INTERVAL 2 HOUR)
|
||||
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
|
||||
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
|
||||
HAVING recent_samples >= 2
|
||||
ORDER BY lat_group, lng_group, cp.competitor_name";
|
||||
|
||||
$stmt = $con->prepare($sql);
|
||||
$stmt->execute();
|
||||
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
||||
|
||||
// 2. تجميع البيانات لكل zone
|
||||
$zones = [];
|
||||
foreach ($rows as $row) {
|
||||
$zoneKey = $row['lat_group'] . '_' . $row['lng_group'];
|
||||
|
||||
$baseline = (float)$row['baseline_avg'];
|
||||
$current = (float)$row['current_avg'];
|
||||
|
||||
$surgeRatio = ($baseline > 0) ? round($current / $baseline, 2) : 1.0;
|
||||
$isSurging = $baseline > 0 && $surgeRatio >= 1.2;
|
||||
|
||||
if (!isset($zones[$zoneKey])) {
|
||||
$zones[$zoneKey] = [
|
||||
'lat' => (float)$row['lat_group'],
|
||||
'lng' => (float)$row['lng_group'],
|
||||
'country_code' => $row['country_code'],
|
||||
'competitors' => [],
|
||||
'total_active' => 0,
|
||||
'total_surging' => 0,
|
||||
];
|
||||
}
|
||||
|
||||
$zones[$zoneKey]['competitors'][] = [
|
||||
'name' => $row['competitor_name'],
|
||||
'baseline' => round($baseline, 2),
|
||||
'current' => round($current, 2),
|
||||
'surge_ratio' => $surgeRatio,
|
||||
'is_surging' => $isSurging,
|
||||
];
|
||||
$zones[$zoneKey]['total_active']++;
|
||||
if ($isSurging) {
|
||||
$zones[$zoneKey]['total_surging']++;
|
||||
}
|
||||
}
|
||||
|
||||
// 3. تحديد فرص الذروة
|
||||
$opportunities = [];
|
||||
$gridSurgeZones = [];
|
||||
|
||||
foreach ($zones as $key => &$zone) {
|
||||
$zone['opportunity'] = (
|
||||
$zone['total_active'] >= 1 &&
|
||||
$zone['total_surging'] === $zone['total_active']
|
||||
);
|
||||
|
||||
if ($zone['opportunity']) {
|
||||
$avgRatio = 0;
|
||||
foreach ($zone['competitors'] as $c) {
|
||||
$avgRatio += $c['surge_ratio'];
|
||||
}
|
||||
$avgRatio /= count($zone['competitors']);
|
||||
|
||||
$suggestedMultiplier = round(1.0 + ($avgRatio - 1.0) * 0.6, 2);
|
||||
if ($suggestedMultiplier < 1.0) $suggestedMultiplier = 1.0;
|
||||
|
||||
$zone['suggested_multiplier'] = $suggestedMultiplier;
|
||||
|
||||
$opportunities[] = [
|
||||
'lat' => $zone['lat'],
|
||||
'lng' => $zone['lng'],
|
||||
'country_code' => $zone['country_code'],
|
||||
'avg_competitor_surge_ratio' => round($avgRatio, 2),
|
||||
'suggested_siro_multiplier' => $suggestedMultiplier,
|
||||
];
|
||||
|
||||
// حفظ المنطقة مع المضاعف المقترح في Redis (للقراءة من get.php بعدين)
|
||||
$gridSurgeZones[$key] = $suggestedMultiplier;
|
||||
}
|
||||
}
|
||||
unset($zone);
|
||||
|
||||
// 4. تخزين فرص الذروة في Redis بصلاحية 10 دقائق (600 ثانية)
|
||||
if (!empty($gridSurgeZones) && isset($redis) && $redis !== null) {
|
||||
$redisKey = 'surge:opportunities';
|
||||
$redis->setex($redisKey, 600, json_encode($gridSurgeZones));
|
||||
echo "[".date('Y-m-d H:i:s')."] Successfully stored ".count($gridSurgeZones)." surge zones in Redis.\n";
|
||||
} else {
|
||||
if (!isset($redis) || $redis === null) {
|
||||
echo "[".date('Y-m-d H:i:s')."] Error: Redis connection not available.\n";
|
||||
} else {
|
||||
// مسح الكي في حال لم يعد هناك أي ذروة
|
||||
$redis->del('surge:opportunities');
|
||||
echo "[".date('Y-m-d H:i:s')."] No active surge opportunities found. Cleared Redis key.\n";
|
||||
}
|
||||
}
|
||||
|
||||
echo "[".date('Y-m-d H:i:s')."] cron_surge_opportunity completed successfully.\n";
|
||||
|
||||
} catch (Exception $e) {
|
||||
echo "[".date('Y-m-d H:i:s')."] Error: " . $e->getMessage() . "\n";
|
||||
}
|
||||
111
backend/bot/cron_weekly_health_report.php
Normal file
111
backend/bot/cron_weekly_health_report.php
Normal file
@@ -0,0 +1,111 @@
|
||||
<?php
|
||||
/**
|
||||
* cron_weekly_health_report.php
|
||||
* يجري تشغيله في نهاية الأسبوع (يوم الأحد ليلاً) لتلخيص صحة السوق التنافسية.
|
||||
*/
|
||||
|
||||
set_time_limit(0);
|
||||
ini_set('memory_limit', '256M');
|
||||
|
||||
$_SERVER['REQUEST_METHOD'] = 'POST';
|
||||
|
||||
require_once __DIR__ . '/../core/bootstrap.php';
|
||||
require_once __DIR__ . '/../functions.php';
|
||||
|
||||
try {
|
||||
$con = Database::get('main');
|
||||
} catch (Exception $e) {
|
||||
die("Database connection failed: " . $e->getMessage() . "\n");
|
||||
}
|
||||
|
||||
echo "[".date('Y-m-d H:i:s')."] Starting Weekly Market Health Report...\n";
|
||||
|
||||
try {
|
||||
$countries = ['SY', 'JO', 'EG', 'IQ']; // Countries we operate in or monitor
|
||||
|
||||
foreach ($countries as $countryCode) {
|
||||
|
||||
// 1. Calculate Average PCI and Market Share
|
||||
$sql = "SELECT distance_km, total_price
|
||||
FROM competitor_prices
|
||||
WHERE country_code = :country
|
||||
AND distance_km > 0
|
||||
AND created_at >= DATE_SUB(NOW(), INTERVAL 7 DAY)";
|
||||
$stmt = $con->prepare($sql);
|
||||
$stmt->execute([':country' => $countryCode]);
|
||||
$trips = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
||||
|
||||
if (empty($trips)) {
|
||||
continue; // Skip if no data for this country
|
||||
}
|
||||
|
||||
$sqlKazan = "SELECT speedPrice FROM kazan WHERE country = :country LIMIT 1";
|
||||
$stmtKazan = $con->prepare($sqlKazan);
|
||||
$countryNameMap = ['SY' => 'Syria', 'JO' => 'Jordan', 'EG' => 'Egypt', 'IQ' => 'Iraq'];
|
||||
$stmtKazan->execute([':country' => $countryNameMap[$countryCode] ?? 'Syria']);
|
||||
$kazanRow = $stmtKazan->fetch(PDO::FETCH_ASSOC);
|
||||
$currentSpeedPrice = $kazanRow ? (float)$kazanRow['speedPrice'] : 0;
|
||||
|
||||
$pciSum = 0;
|
||||
$cheaperCount = 0;
|
||||
$totalTrips = count($trips);
|
||||
|
||||
foreach ($trips as $trip) {
|
||||
$compPrice = (float)$trip['total_price'];
|
||||
$siroPrice = (float)$trip['distance_km'] * $currentSpeedPrice;
|
||||
|
||||
if ($compPrice > 0) {
|
||||
$pciSum += ($siroPrice / $compPrice);
|
||||
if ($siroPrice < $compPrice) {
|
||||
$cheaperCount++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$averagePci = round($pciSum / $totalTrips, 2);
|
||||
$marketSharePct = round(($cheaperCount / $totalTrips) * 100, 2);
|
||||
|
||||
// 2. Count Anomalies (Last 7 Days)
|
||||
$sqlAnomalies = "SELECT COUNT(*) as count FROM price_anomalies
|
||||
WHERE country_code = :country
|
||||
AND created_at >= DATE_SUB(NOW(), INTERVAL 7 DAY)";
|
||||
$stmtAnomalies = $con->prepare($sqlAnomalies);
|
||||
$stmtAnomalies->execute([':country' => $countryCode]);
|
||||
$anomaliesCount = (int)$stmtAnomalies->fetchColumn();
|
||||
|
||||
// 3. Automated Campaigns / Surge Tracking (If applicable in logs)
|
||||
$sqlCampaigns = "SELECT COUNT(*) as count FROM marketing_campaigns_log
|
||||
WHERE JSON_EXTRACT(target_criteria, '$.country_code') = :country
|
||||
AND sent_at >= DATE_SUB(NOW(), INTERVAL 7 DAY)";
|
||||
$stmtCampaigns = $con->prepare($sqlCampaigns);
|
||||
$stmtCampaigns->execute([':country' => $countryCode]);
|
||||
$campaignsCount = (int)$stmtCampaigns->fetchColumn();
|
||||
|
||||
// 4. Save to Database
|
||||
$reportData = [
|
||||
'total_competitor_samples' => $totalTrips,
|
||||
'automated_campaigns_sent' => $campaignsCount,
|
||||
'current_speed_price' => $currentSpeedPrice
|
||||
];
|
||||
|
||||
$sqlInsert = "INSERT INTO market_health_reports
|
||||
(report_date, country_code, average_pci, market_share_percent, total_anomalies, total_surge_opportunities, report_data_json)
|
||||
VALUES (CURDATE(), :country, :pci, :market_share, :anomalies, :surge, :report_data)";
|
||||
$stmtInsert = $con->prepare($sqlInsert);
|
||||
$stmtInsert->execute([
|
||||
':country' => $countryCode,
|
||||
':pci' => $averagePci,
|
||||
':market_share' => $marketSharePct,
|
||||
':anomalies' => $anomaliesCount,
|
||||
':surge' => 0, // Placeholder for actual surge count if tracked in DB
|
||||
':report_data' => json_encode($reportData)
|
||||
]);
|
||||
|
||||
echo "[".date('Y-m-d H:i:s')."] Report generated for $countryCode. PCI: $averagePci, Share: $marketSharePct%\n";
|
||||
}
|
||||
|
||||
echo "[".date('Y-m-d H:i:s')."] cron_weekly_health_report completed successfully.\n";
|
||||
|
||||
} catch (Exception $e) {
|
||||
echo "[".date('Y-m-d H:i:s')."] Error: " . $e->getMessage() . "\n";
|
||||
}
|
||||
Reference in New Issue
Block a user