diff --git a/admin.php b/admin.php index 4cc0138..768ed89 100644 --- a/admin.php +++ b/admin.php @@ -61,6 +61,11 @@ $requests_today = $stmt->fetchColumn(); GENERAR REPORTE SEMANAL +
+ + Reporte automático programado: Todos los lunes 08:00 AM + +
diff --git a/api/chat.php b/api/chat.php index 7f9ebbc..ec1826c 100644 --- a/api/chat.php +++ b/api/chat.php @@ -20,8 +20,8 @@ if ($method === 'GET') { } db()->query("DELETE FROM messages WHERE type = 'image' AND created_at < DATE_SUB(NOW(), INTERVAL 5 SECOND)"); - // Limpiar otros mensajes y archivos de más de 6 horas - $oldImages = db()->prepare("SELECT message FROM messages WHERE type = 'image' AND created_at < DATE_SUB(NOW(), INTERVAL 6 HOUR)"); + // Limpiar otros mensajes y archivos de más de 7 días + $oldImages = db()->prepare("SELECT message FROM messages WHERE type = 'image' AND created_at < DATE_SUB(NOW(), INTERVAL 7 DAY)"); $oldImages->execute(); $filesToDelete = $oldImages->fetchAll(PDO::FETCH_ASSOC); @@ -32,7 +32,7 @@ if ($method === 'GET') { } } - db()->query("DELETE FROM messages WHERE created_at < DATE_SUB(NOW(), INTERVAL 6 HOUR)"); + db()->query("DELETE FROM messages WHERE created_at < DATE_SUB(NOW(), INTERVAL 7 DAY)"); // Obtener el Oyente de la Semana (Top 1) $topStmt = db()->query(" diff --git a/api/cron_weekly_report.php b/api/cron_weekly_report.php new file mode 100644 index 0000000..0799a91 --- /dev/null +++ b/api/cron_weekly_report.php @@ -0,0 +1,187 @@ +prepare("SELECT last_run FROM automation_logs WHERE task_name = ? AND DATE(last_run) = ?"); +$stmt->execute([$task_name, $today]); +if ($stmt->fetch()) { + exit("Report already sent today ($today). Skip.\n"); +} + +echo "Generating weekly report for $today...\n"; + +// Reuse logic from weekly_report.php (simplified for automation) +// 1. Get stats for the last 7 days +$stats_query = " + SELECT + COUNT(*) as total_requests, + SUM(CASE WHEN source = 'whatsapp' THEN 1 ELSE 0 END) as whatsapp_requests, + SUM(CASE WHEN source != 'whatsapp' OR source IS NULL THEN 1 ELSE 0 END) as web_requests + FROM song_requests + WHERE created_at > DATE_SUB(NOW(), INTERVAL 7 DAY) +"; +$totals = $db->query($stats_query)->fetch(); + +// 2. Top 10 Requesters +$top_requesters = $db->query(" + SELECT requester, COUNT(*) as count + FROM song_requests + WHERE created_at > DATE_SUB(NOW(), INTERVAL 7 DAY) + AND requester IS NOT NULL AND requester != '' AND requester != 'Anónimo' + GROUP BY requester + ORDER BY count DESC + LIMIT 10 +")->fetchAll(); + +// 3. Top 10 Songs (by requests) +$top_songs_requested = $db->query(" + SELECT artist, song, COUNT(*) as count + FROM song_requests + WHERE created_at > DATE_SUB(NOW(), INTERVAL 7 DAY) + GROUP BY artist, song + ORDER BY count DESC + LIMIT 10 +")->fetchAll(); + +// 4. Top 10 Songs (by likes) +$top_songs_liked = $db->query(" + SELECT song_title, likes_count + FROM song_likes + WHERE last_liked_at >= DATE_SUB(NOW(), INTERVAL 7 DAY) + ORDER BY likes_count DESC + LIMIT 10 +")->fetchAll(); + +// 5. Top 10 Chat Interactors +$top_chatters = $db->query(" + SELECT username, COUNT(*) as count + FROM messages + WHERE created_at > DATE_SUB(NOW(), INTERVAL 7 DAY) + AND username IS NOT NULL AND username != '' AND username != 'Anónimo' + GROUP BY username + ORDER BY count DESC + LIMIT 10 +")->fetchAll(); + +$report_date = date('d/m/Y'); +$week_start = date('d/m/Y', strtotime('-7 days')); + +$html_content = " + + + + + + +
+
+

Reporte Semanal Automático - Lili Records Radio

+

Periodo: $week_start al $report_date

+
+ +
+
+

Total Peticiones

+
{$totals['total_requests']}
+
+
+

Vía WhatsApp

+
{$totals['whatsapp_requests']}
+
+
+

Vía Web

+
{$totals['web_requests']}
+
+
+ +

🏆 Top 10 Oyentes de la Semana

+ + + + + "; +foreach ($top_requesters as $i => $r) { + $crown = ($i === 0) ? "👑 " : ""; + $html_content .= ""; +} +$html_content .= "
#OyentePeticiones
" . ($i + 1) . "$crown" . htmlspecialchars($r['requester']) . "" . $r['count'] . "
+ +

💬 Top 10 Interactores del Chat

+ + + + + "; +foreach ($top_chatters as $i => $c) { + $emoji = ($i === 0) ? "👑 " : "💬 "; + $html_content .= ""; +} +$html_content .= "
#UsuarioMensajes
" . ($i + 1) . "$emoji" . htmlspecialchars($c['username']) . "" . $c['count'] . "
+ +

🎵 Top Canciones (Más Pedidas)

+ + + "; +foreach ($top_songs_requested as $s) { + $html_content .= ""; +} +$html_content .= "
CanciónArtistaVeces
" . htmlspecialchars($s['song']) . "" . htmlspecialchars($s['artist']) . "" . $s['count'] . "
+ +

⭐ Top Canciones (Más Likes)

+ + + "; +foreach ($top_songs_liked as $s) { + $html_content .= ""; +} +$html_content .= "
CanciónLikes
" . htmlspecialchars($s['song_title']) . " " . $s['likes_count'] . "
+ + +
+ +"; + +$subject = "📊 [AUTO] Reporte Semanal - Lili Records Radio ($report_date)"; +$res = MailService::sendMail(null, $subject, $html_content); + +if (!empty($res['success'])) { + echo "Report sent successfully.\n"; + // Log the run + $stmt = $db->prepare("INSERT INTO automation_logs (task_name, last_run) VALUES (?, NOW()) ON DUPLICATE KEY UPDATE last_run = NOW()"); + $stmt->execute([$task_name]); +} else { + echo "Failed to send report: " . ($res['error'] ?? 'Unknown error') . "\n"; +} diff --git a/weekly_report.php b/weekly_report.php index 232011d..7ba9a61 100644 --- a/weekly_report.php +++ b/weekly_report.php @@ -51,6 +51,17 @@ $top_songs_liked = $db->query(" LIMIT 10 ")->fetchAll(); +// 5. Top 10 Chat Interactors +$top_chatters = $db->query(" + SELECT username, COUNT(*) as count + FROM messages + WHERE created_at > DATE_SUB(NOW(), INTERVAL 7 DAY) + AND username IS NOT NULL AND username != '' AND username != 'Anónimo' + GROUP BY username + ORDER BY count DESC + LIMIT 10 +")->fetchAll(); + $report_date = date('d/m/Y'); $week_start = date('d/m/Y', strtotime('-7 days')); @@ -124,6 +135,29 @@ $html_content .= " +

💬 Top 10 Interactores del Chat

+ + + + + + + + + "; +foreach ($top_chatters as $i => $c) { + $emoji = ($i === 0) ? "👑 " : "💬 "; + $html_content .= " + + + + + "; +} +$html_content .= " + +
#UsuarioMensajes
" . ($i + 1) . "$emoji" . htmlspecialchars($c['username']) . "" . $c['count'] . "
+

🎵 Top Canciones (Más Pedidas)