Added split info about bots, non-bots and total visits to the visits stats

This commit is contained in:
Alejandro Celaya
2023-01-02 12:28:34 +01:00
parent e71f6bb528
commit 37c8328eed
8 changed files with 82 additions and 45 deletions

View File

@@ -8,15 +8,34 @@ use JsonSerializable;
final class VisitsStats implements JsonSerializable
{
public function __construct(private int $visitsCount, private int $orphanVisitsCount)
{
private readonly VisitsSummary $nonOrphanVisitsSummary;
private readonly VisitsSummary $orphanVisitsSummary;
public function __construct(
int $nonOrphanVisitsTotal,
int $orphanVisitsTotal,
?int $nonOrphanVisitsNonBots = null,
?int $orphanVisitsNonBots = null,
) {
$this->nonOrphanVisitsSummary = VisitsSummary::fromTotalAndNonBots(
$nonOrphanVisitsTotal,
$nonOrphanVisitsNonBots ?? $nonOrphanVisitsTotal,
);
$this->orphanVisitsSummary = VisitsSummary::fromTotalAndNonBots(
$orphanVisitsTotal,
$orphanVisitsNonBots ?? $orphanVisitsTotal,
);
}
public function jsonSerialize(): array
{
return [
'visitsCount' => $this->visitsCount,
'orphanVisitsCount' => $this->orphanVisitsCount,
'nonOrphanVisits' => $this->nonOrphanVisitsSummary,
'orphanVisits' => $this->orphanVisitsSummary,
// Deprecated
'visitsCount' => $this->nonOrphanVisitsSummary->total,
'orphanVisitsCount' => $this->orphanVisitsSummary->total,
];
}
}

View File

@@ -0,0 +1,28 @@
<?php
declare(strict_types=1);
namespace Shlinkio\Shlink\Core\Visit\Model;
use JsonSerializable;
final class VisitsSummary implements JsonSerializable
{
private function __construct(public readonly int $total, public readonly int $nonBots)
{
}
public static function fromTotalAndNonBots(int $total, int $nonBots): self
{
return new self($total, $nonBots);
}
public function jsonSerialize(): array
{
return [
'total' => $this->total,
'nonBots' => $this->nonBots,
'bots' => $this->total - $this->nonBots,
];
}
}