Allow filtering by date in VisitIterationRepository

This commit is contained in:
Alejandro Celaya
2024-04-12 18:29:55 +02:00
parent 13ee71f351
commit ce0f61b66d
6 changed files with 54 additions and 16 deletions

View File

@@ -8,14 +8,14 @@ use Doctrine\ORM\EntityManagerInterface;
use Shlinkio\Shlink\Core\Exception\IpCannotBeLocatedException;
use Shlinkio\Shlink\Core\Visit\Entity\Visit;
use Shlinkio\Shlink\Core\Visit\Entity\VisitLocation;
use Shlinkio\Shlink\Core\Visit\Repository\VisitLocationRepositoryInterface;
use Shlinkio\Shlink\Core\Visit\Repository\VisitIterationRepositoryInterface;
use Shlinkio\Shlink\IpGeolocation\Model\Location;
class VisitLocator implements VisitLocatorInterface
{
public function __construct(
private readonly EntityManagerInterface $em,
private readonly VisitLocationRepositoryInterface $repo,
private readonly VisitIterationRepositoryInterface $repo,
) {
}

View File

@@ -6,9 +6,14 @@ namespace Shlinkio\Shlink\Core\Visit\Repository;
use Doctrine\ORM\QueryBuilder;
use Happyr\DoctrineSpecification\Repository\EntitySpecificationRepository;
use Shlinkio\Shlink\Common\Doctrine\Type\ChronosDateTimeType;
use Shlinkio\Shlink\Common\Util\DateRange;
use Shlinkio\Shlink\Core\Visit\Entity\Visit;
class VisitLocationRepository extends EntitySpecificationRepository implements VisitLocationRepositoryInterface
/**
* Allows iterating large amounts of visits in a memory-efficient way, to use in batch processes
*/
class VisitIterationRepository extends EntitySpecificationRepository implements VisitIterationRepositoryInterface
{
/**
* @return iterable<Visit>
@@ -42,9 +47,18 @@ class VisitLocationRepository extends EntitySpecificationRepository implements V
/**
* @return iterable<Visit>
*/
public function findAllVisits(int $blockSize = self::DEFAULT_BLOCK_SIZE): iterable
public function findAllVisits(?DateRange $dateRange = null, int $blockSize = self::DEFAULT_BLOCK_SIZE): iterable
{
$qb = $this->createQueryBuilder('v');
if ($dateRange?->startDate !== null) {
$qb->andWhere($qb->expr()->gte('v.date', ':since'));
$qb->setParameter('since', $dateRange->startDate, ChronosDateTimeType::CHRONOS_DATETIME);
}
if ($dateRange?->endDate !== null) {
$qb->andWhere($qb->expr()->lte('v.date', ':until'));
$qb->setParameter('until', $dateRange->endDate, ChronosDateTimeType::CHRONOS_DATETIME);
}
return $this->visitsIterableForQuery($qb, $blockSize);
}

View File

@@ -4,9 +4,10 @@ declare(strict_types=1);
namespace Shlinkio\Shlink\Core\Visit\Repository;
use Shlinkio\Shlink\Common\Util\DateRange;
use Shlinkio\Shlink\Core\Visit\Entity\Visit;
interface VisitLocationRepositoryInterface
interface VisitIterationRepositoryInterface
{
public const DEFAULT_BLOCK_SIZE = 10000;
@@ -23,5 +24,5 @@ interface VisitLocationRepositoryInterface
/**
* @return iterable<Visit>
*/
public function findAllVisits(int $blockSize = self::DEFAULT_BLOCK_SIZE): iterable;
public function findAllVisits(?DateRange $dateRange = null, int $blockSize = self::DEFAULT_BLOCK_SIZE): iterable;
}