35274-vm/analytics.php
Flatlogic Bot d2f005da56 nu norm
2025-10-27 13:55:31 +00:00

130 lines
5.1 KiB
PHP

<?php
require_once 'db/config.php';
require_once 'layout_header.php';
$pdo = db();
// --- Fetch Key Metrics ---
$stats = [
'total' => 0,
'published' => 0,
'failed' => 0,
'pending' => 0
];
try {
$stmt = $pdo->query("SELECT status, COUNT(*) as count FROM scheduled_posts GROUP BY status");
$results = $stmt->fetchAll(PDO::FETCH_KEY_PAIR);
$stats['published'] = $results['published'] ?? 0;
$stats['failed'] = $results['failed'] ?? 0;
$stats['pending'] = $results['pending'] ?? 0;
$stats['total'] = array_sum($stats);
} catch (PDOException $e) {
// Handle error, maybe show a message
}
// --- Fetch Recent Webhook Events ---
$recent_events = [];
try {
$event_stmt = $pdo->query("SELECT * FROM webhook_events ORDER BY received_at DESC LIMIT 15");
$recent_events = $event_stmt->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
// Handle error
}
?>
<div class="container-fluid p-4">
<h1 class="h3 mb-4 text-white">Analytics</h1>
<p class="text-muted mb-4">This page provides an overview of your post scheduling and publishing activity, based on status updates received from your n8n workflows.</p>
<!-- Key Metrics -->
<div class="row mb-4">
<div class="col-xl-3 col-md-6 mb-4">
<div class="card bg-dark-surface h-100">
<div class="card-body">
<h6 class="card-title text-muted text-uppercase">Total Scheduled</h6>
<h2 class="h1 fw-bold mb-0"><?php echo $stats['total']; ?></h2>
</div>
</div>
</div>
<div class="col-xl-3 col-md-6 mb-4">
<div class="card bg-dark-surface h-100">
<div class="card-body">
<h6 class="card-title text-muted text-uppercase">Successfully Published</h6>
<h2 class="h1 fw-bold mb-0 text-success"><?php echo $stats['published']; ?></h2>
</div>
</div>
</div>
<div class="col-xl-3 col-md-6 mb-4">
<div class="card bg-dark-surface h-100">
<div class="card-body">
<h6 class="card-title text-muted text-uppercase">Failed to Publish</h6>
<h2 class="h1 fw-bold mb-0 text-danger"><?php echo $stats['failed']; ?></h2>
</div>
</div>
</div>
<div class="col-xl-3 col-md-6 mb-4">
<div class="card bg-dark-surface h-100">
<div class="card-body">
<h6 class="card-title text-muted text-uppercase">Pending / In Progress</h6>
<h2 class="h1 fw-bold mb-0 text-warning"><?php echo $stats['pending']; ?></h2>
</div>
</div>
</div>
</div>
<!-- Recent Activity Log -->
<div class="card bg-dark-surface">
<div class="card-header">
<h5 class="card-title mb-0">Recent Activity (from Webhooks)</h5>
</div>
<div class="card-body">
<div class="table-responsive">
<table class="table table-dark">
<thead>
<tr>
<th>Post ID</th>
<th>Status</th>
<th>Message</th>
<th>Received At</th>
</tr>
</thead>
<tbody>
<?php if (empty($recent_events)): ?>
<tr>
<td colspan="4" class="text-center text-muted">No recent webhook events found.</td>
</tr>
<?php else: ?>
<?php foreach ($recent_events as $event): ?>
<tr>
<td>#<?php echo htmlspecialchars($event['post_id']); ?></td>
<td>
<?php
$status_class = 'bg-secondary';
if (in_array($event['status'], ['success', 'published'])) {
$status_class = 'bg-success';
} elseif (in_array($event['status'], ['error', 'failed'])) {
$status_class = 'bg-danger';
}
?>
<span class="badge <?php echo $status_class; ?>"><?php echo htmlspecialchars(ucfirst($event['status'])); ?></span>
</td>
<td><?php echo htmlspecialchars($event['message']); ?></td>
<td><?php echo date("F j, Y, g:i a", strtotime($event['received_at'])); ?></td>
</tr>
<?php endforeach; ?>
<?php endif; ?>
</tbody>
</table>
</div>
</div>
</div>
</div>
<?php
require_once 'layout_footer.php';
?>