Files
Siro/backend/Admin/geofence/get_heatmap.php
T

78 lines
2.3 KiB
PHP

<?php
/**
* get_heatmap.php
* ───────────────
* تقرأ بيانات الخريطة الحرارية المجمعة من Redis
* البيانات مقسمة حسب الدولة (عبر Bounding Boxes في الـ Cron)
*/
header('Content-Type: application/json; charset=utf-8');
require_once __DIR__ . '/../../connect.php'; // Includes functions.php which has filterRequest()
$days = (int)(filterRequest('days') ?? 7);
$source = filterRequest('source') ?? 'all';
$countryCode = strtoupper(filterRequest('country_code') ?? 'all');
try {
$redis = getRedisConnection();
$cacheJson = $redis->get('siro:cache:heatmap:data');
} catch (Exception $e) {
echo json_encode(['status' => 'error', 'message' => 'Redis connection failed']);
exit;
}
if (!$cacheJson) {
echo json_encode(['status' => 'error', 'message' => 'Cache not generated yet']);
exit;
}
$cacheData = json_decode($cacheJson, true);
if (!$cacheData || !isset($cacheData['data'])) {
echo json_encode(['status' => 'error', 'message' => 'Invalid cache data']);
exit;
}
$limitDate = date('Y-m-d', strtotime("-$days days"));
$filteredLocations = [];
$stats = ['geofence' => 0, 'app_usage' => 0, 'silent_push' => 0];
$dataByCountry = $cacheData['data'];
// تحديد الدول التي سنسحب منها
$countriesToSearch = ($countryCode === 'ALL') ? array_keys($dataByCountry) : [$countryCode];
foreach ($countriesToSearch as $cc) {
if (!isset($dataByCountry[$cc])) continue;
foreach ($dataByCountry[$cc] as $loc) {
// فلتر الأيام
if ($loc['date'] < $limitDate) continue;
// فلتر المصدر
if ($source !== 'all' && $loc['source'] !== $source) continue;
$filteredLocations[] = [
'latitude' => $loc['lat'],
'longitude' => $loc['lng'],
'source' => $loc['source']
];
if (isset($stats[$loc['source']])) {
$stats[$loc['source']]++;
}
}
}
echo json_encode([
'status' => 'success',
'data' => $filteredLocations,
'total' => count($filteredLocations),
'stats' => $stats,
'source' => 'redis_cache'
], JSON_UNESCAPED_UNICODE);
// تم إزالة دالة filterRequest من هنا لتجنب خطأ Redeclaration لأنها معرفة في functions.php
?>