Fix: Replace fake daily hits data with real database data
This commit is contained in:
1 parent
e6221bc7b1
commit
ab6d3af44c
2 files changed
+55
-25
No files matched your search
@@ -132,13 +132,17 @@ class PublicCampaignsController extends BaseController
|
||||
|
||||
// Get stats by referral
|
||||
$byReferral = $this->getStatsByReferral($redirectId, $startDate, $endDate);
|
||||
|
||||
// Get daily hits data
|
||||
$dailyHits = $this->getDailyHitsData($redirectId, $startDate, $endDate);
|
||||
|
||||
return [
|
||||
'overview' => $overview,
|
||||
'by_country' => $byCountry,
|
||||
'by_device' => $byDevice,
|
||||
'by_browser' => $byBrowser,
|
||||
'by_referral' => $byReferral
|
||||
'by_referral' => $byReferral,
|
||||
'daily_hits' => $dailyHits
|
||||
];
|
||||
}
|
||||
|
||||
@@ -171,6 +175,34 @@ class PublicCampaignsController extends BaseController
|
||||
return $this->db->fetchAll($sql, $params) ?: [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get daily hits data for the chart
|
||||
*/
|
||||
private function getDailyHitsData(int $redirectId, string $startDate, string $endDate): array
|
||||
{
|
||||
$sql = "SELECT
|
||||
DATE(timestamp) as date,
|
||||
COUNT(*) as hits
|
||||
FROM redirect_stats
|
||||
WHERE redirect_id = ?";
|
||||
|
||||
$params = [$redirectId];
|
||||
|
||||
if ($startDate) {
|
||||
$sql .= " AND DATE(timestamp) >= ?";
|
||||
$params[] = $startDate;
|
||||
}
|
||||
|
||||
if ($endDate) {
|
||||
$sql .= " AND DATE(timestamp) <= ?";
|
||||
$params[] = $endDate;
|
||||
}
|
||||
|
||||
$sql .= " GROUP BY DATE(timestamp) ORDER BY date ASC";
|
||||
|
||||
return $this->db->fetchAll($sql, $params) ?: [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get filter options for the specific redirect
|
||||
*/
|
||||
|
||||
@@ -353,7 +353,8 @@ window.publicCampaignStatsData = {
|
||||
by_country: <?= json_encode($stats['by_country'] ?? []) ?>,
|
||||
by_device: <?= json_encode($stats['by_device'] ?? []) ?>,
|
||||
by_browser: <?= json_encode($stats['by_browser'] ?? []) ?>,
|
||||
by_referral: <?= json_encode($stats['by_referral'] ?? []) ?>
|
||||
by_referral: <?= json_encode($stats['by_referral'] ?? []) ?>,
|
||||
daily_hits: <?= json_encode($stats['daily_hits'] ?? []) ?>
|
||||
}
|
||||
};
|
||||
</script>
|
||||
@@ -427,16 +428,32 @@ function publicCampaignStats() {
|
||||
const ctx = document.getElementById('daily-hits-chart');
|
||||
if (!ctx) return;
|
||||
|
||||
// Generate sample daily data for the last 30 days
|
||||
const dailyData = this.generateDailyHitsData();
|
||||
// Use real daily hits data from database
|
||||
const dailyData = this.stats.daily_hits || [];
|
||||
|
||||
if (dailyData.length === 0) {
|
||||
// Show empty state if no data
|
||||
ctx.getContext('2d').clearRect(0, 0, ctx.width, ctx.height);
|
||||
ctx.getContext('2d').fillStyle = '#666';
|
||||
ctx.getContext('2d').font = '16px Arial';
|
||||
ctx.getContext('2d').textAlign = 'center';
|
||||
ctx.getContext('2d').fillText('No data available for the selected period', ctx.width / 2, ctx.height / 2);
|
||||
return;
|
||||
}
|
||||
|
||||
const labels = dailyData.map(d => {
|
||||
const date = new Date(d.date);
|
||||
return date.toLocaleDateString('en-US', { month: 'short', day: 'numeric' });
|
||||
});
|
||||
const values = dailyData.map(d => parseInt(d.hits));
|
||||
|
||||
new Chart(ctx, {
|
||||
type: 'line',
|
||||
data: {
|
||||
labels: dailyData.labels,
|
||||
labels: labels,
|
||||
datasets: [{
|
||||
label: 'Daily Hits',
|
||||
data: dailyData.values,
|
||||
data: values,
|
||||
borderColor: '#556ee6',
|
||||
backgroundColor: 'rgba(85, 110, 230, 0.1)',
|
||||
borderWidth: 3,
|
||||
@@ -473,25 +490,6 @@ function publicCampaignStats() {
|
||||
});
|
||||
},
|
||||
|
||||
generateDailyHitsData() {
|
||||
const days = 30;
|
||||
const labels = [];
|
||||
const values = [];
|
||||
|
||||
for (let i = days - 1; i >= 0; i--) {
|
||||
const date = new Date();
|
||||
date.setDate(date.getDate() - i);
|
||||
labels.push(date.toLocaleDateString('en-US', { month: 'short', day: 'numeric' }));
|
||||
|
||||
// Generate realistic daily hits data
|
||||
const baseHits = Math.floor(this.stats.overview.total_clicks / days);
|
||||
const variation = Math.floor(baseHits * 0.3 * (Math.random() - 0.5));
|
||||
values.push(Math.max(0, baseHits + variation));
|
||||
}
|
||||
|
||||
return { labels, values };
|
||||
},
|
||||
|
||||
updateCountryChart() {
|
||||
const ctx = document.getElementById('country-chart');
|
||||
if (!ctx) return;
|
||||
|
||||
Reference in new issue
Block a user