Browse Source

counters

imwald
Nuša Pukšič 4 months ago
parent
commit
954df9f885
  1. 9
      src/Controller/Administration/VisitorAnalyticsController.php
  2. 14
      src/Repository/VisitRepository.php
  3. 11
      templates/admin/analytics.html.twig

9
src/Controller/Administration/VisitorAnalyticsController.php

@ -18,8 +18,17 @@ class VisitorAnalyticsController extends AbstractController @@ -18,8 +18,17 @@ class VisitorAnalyticsController extends AbstractController
{
$visitStats = $visitRepository->getVisitCountByRoute();
// Counters for the last 24 hours and last 7 days
$last24h = new \DateTimeImmutable('-24 hours');
$last7d = new \DateTimeImmutable('-7 days');
$last24hCount = $visitRepository->countVisitsSince($last24h);
$last7dCount = $visitRepository->countVisitsSince($last7d);
return $this->render('admin/analytics.html.twig', [
'visitStats' => $visitStats,
'last24hCount' => $last24hCount,
'last7dCount' => $last7dCount,
]);
}
}

14
src/Repository/VisitRepository.php

@ -4,6 +4,7 @@ namespace App\Repository; @@ -4,6 +4,7 @@ namespace App\Repository;
use App\Entity\Visit;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\DBAL\Types\Types;
use Doctrine\Persistence\ManagerRegistry;
/**
@ -34,4 +35,17 @@ class VisitRepository extends ServiceEntityRepository @@ -34,4 +35,17 @@ class VisitRepository extends ServiceEntityRepository
->getQuery()
->getResult();
}
/**
* Returns total number of visits since the given datetime (inclusive).
*/
public function countVisitsSince(\DateTimeImmutable $since): int
{
$qb = $this->createQueryBuilder('v')
->select('COUNT(v.id)')
->where('v.visitedAt >= :since')
->setParameter('since', $since, Types::DATETIME_IMMUTABLE);
return (int) $qb->getQuery()->getSingleScalarResult();
}
}

11
templates/admin/analytics.html.twig

@ -6,6 +6,14 @@ @@ -6,6 +6,14 @@
<div class="analytics-container">
<h1>Page Visit Analytics</h1>
<div class="analytics-card">
<h2>Total Visits</h2>
<ul class="analytics-stats">
<li><strong>Last 24 hours:</strong> {{ last24hCount }}</li>
<li><strong>Last 7 days:</strong> {{ last7dCount }}</li>
</ul>
</div>
<div class="analytics-card">
<h2>Visit Count by Route</h2>
@ -14,7 +22,7 @@ @@ -14,7 +22,7 @@
<thead>
<tr>
<th>Route</th>
<th>Visit Count</th>
<th style="min-width: 100px;text-align: right;">#</th>
</tr>
</thead>
<tbody>
@ -36,4 +44,3 @@ @@ -36,4 +44,3 @@
</div>
</div>
{% endblock %}

Loading…
Cancel
Save