Refactor point value object and add observability

This commit is contained in:
Bernard Ngandu
2025-10-10 14:55:36 +02:00
parent 8a43d3967c
commit 68eb54995f
46 changed files with 3691 additions and 229 deletions
+25 -84
View File
@@ -4,14 +4,13 @@ declare(strict_types=1);
namespace App\Controller;
use App\Dto\SignalPayload;
use App\Entity\Signal;
use App\Repository\SignalRepository;
use App\Service\SignalSnapshotBuilder;
use DateTimeImmutable;
use DateTimeZone;
use App\Payload\SignalPayload;
use App\Service\ClientKeyProvider;
use App\Service\SignalSnapshotService;
use App\Service\SignalSubmissionService;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Attribute\MapQueryParameter;
use Symfony\Component\HttpKernel\Attribute\MapRequestPayload;
use Symfony\Component\Routing\Annotation\Route;
@@ -20,104 +19,46 @@ use Symfony\Component\Routing\Annotation\Route;
class SignalController
{
public function __construct(
private readonly SignalRepository $signals,
private readonly SignalSnapshotBuilder $snapshotBuilder,
private readonly SignalSnapshotService $snapshotService,
private readonly SignalSubmissionService $submissionService,
private readonly ClientKeyProvider $clientKeyProvider,
) {
}
#[Route(path: '', name: 'api_signals_index', methods: ['GET'])]
public function index(Request $request, #[MapQueryParameter('limit', flags: FILTER_NULL_ON_FAILURE)] ?int $limit = null): JsonResponse
{
$limit = $this->normalizeLimit($limit);
$clientKey = $this->clientKeyProvider->fromRequest($request);
$snapshot = $this->snapshotService->buildSnapshot($limit, $clientKey);
$clientKey = $this->hashIp($this->extractClientIp($request));
$signals = $this->signals->findRecent($limit);
$snapshot = $this->snapshotBuilder->build($signals);
$payload = [
'clientKey' => $clientKey,
'points' => $snapshot['points'],
'density' => $snapshot['density'],
'latestByUser' => $snapshot['latestByUser'],
'totals' => $snapshot['totals'],
'updatedAt' => new DateTimeImmutable('now', new DateTimeZone('UTC'))->format(DATE_ATOM),
];
return new JsonResponse($payload);
return new JsonResponse($snapshot);
}
#[Route(path: '', name: 'api_signals_store', methods: ['POST'])]
public function store(#[MapRequestPayload(validationFailedStatusCode: JsonResponse::HTTP_UNPROCESSABLE_ENTITY)] SignalPayload $payload, Request $request): JsonResponse
public function store(Request $request, #[MapRequestPayload] SignalPayload $payload): JsonResponse
{
$lat = $payload->lat;
$lng = $payload->lng;
$clientKey = $this->clientKeyProvider->fromRequest($request);
$signal = $this->submissionService->submit($clientKey, $payload);
if (! is_finite($lat) || ! is_finite($lng)) {
return $this->errorResponse('invalid_coordinates', 'Latitude and longitude must be numbers.', JsonResponse::HTTP_UNPROCESSABLE_ENTITY);
}
if ($lat < -90 || $lat > 90 || $lng < -180 || $lng > 180) {
return $this->errorResponse('out_of_bounds', 'Latitude or longitude out of range.', JsonResponse::HTTP_UNPROCESSABLE_ENTITY);
}
$clientIp = $this->extractClientIp($request);
$clientKey = $this->hashIp($clientIp);
$signal = new Signal()
->setUserKey($clientKey)
->setLat($lat)
->setLng($lng)
->setCreatedAt(new DateTimeImmutable('now', new DateTimeZone('UTC')));
$this->signals->save($signal);
$signalLocation = $signal->getSignalLocation();
$userLocation = $signal->getUserLocation();
return new JsonResponse([
'status' => 'stored',
'clientKey' => $clientKey,
'point' => [
'id' => $signal->getId(),
'lat' => $signal->getLat(),
'lng' => $signal->getLng(),
'signalLocation' => [
'lat' => $signalLocation->getLat(),
'lng' => $signalLocation->getLng(),
],
'userLocation' => [
'lat' => $userLocation->getLat(),
'lng' => $userLocation->getLng(),
],
'createdAt' => $signal->getCreatedAt()->format(DATE_ATOM),
'userKey' => $signal->getUserKey(),
],
], JsonResponse::HTTP_CREATED);
}
private function errorResponse(string $error, string $message, int $statusCode): JsonResponse
{
return new JsonResponse([
'error' => $error,
'message' => $message,
], $statusCode);
}
private function normalizeLimit(?int $limit): int
{
$limit ??= 750;
if ($limit < 1 || $limit > 5000) {
return 750;
}
return $limit;
}
private function extractClientIp(Request $request): string
{
$forwarded = $request->headers->get('X-Forwarded-For');
if ($forwarded !== null && $forwarded !== '') {
$parts = array_filter(array_map('trim', explode(',', $forwarded)));
if ($parts !== []) {
return $parts[0];
}
}
return $request->getClientIp() ?? '0.0.0.0';
}
private function hashIp(string $ip): string
{
return substr(hash('sha256', $ip), 0, 12);
], Response::HTTP_CREATED);
}
}
+5 -2
View File
@@ -4,6 +4,7 @@ declare(strict_types=1);
namespace App\DataFixtures;
use App\ValueObject\Point;
use App\Entity\Signal;
use DateInterval;
use DateTimeImmutable;
@@ -38,10 +39,12 @@ class SignalFixtures extends Fixture
];
foreach ($coordinates as $config) {
$signalLocation = Point::fromLatLng($config['lat'], $config['lng']);
$signal = new Signal()
->setUserKey($config['user'])
->setLat($config['lat'])
->setLng($config['lng'])
->setUserLocation(Point::fromLatLng($config['lat'], $config['lng']))
->setSignalLocation($signalLocation)
->setCreatedAt($baseTime->add(new DateInterval(sprintf('PT%dM', $config['offset']))));
$manager->persist($signal);
-14
View File
@@ -1,14 +0,0 @@
<?php
declare(strict_types=1);
namespace App\Dto;
final readonly class SignalPayload
{
public function __construct(
public float $lat,
public float $lng,
) {
}
}
+13 -12
View File
@@ -5,6 +5,7 @@ declare(strict_types=1);
namespace App\Entity;
use App\Repository\SignalRepository;
use App\ValueObject\Point;
use DateTimeImmutable;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
@@ -23,11 +24,11 @@ class Signal
#[ORM\Column(type: Types::STRING, length: 64)]
private string $userKey;
#[ORM\Column(type: Types::FLOAT)]
private float $lat;
#[ORM\Embedded(class: Point::class, columnPrefix: 'user_')]
private Point $userLocation;
#[ORM\Column(type: Types::FLOAT)]
private float $lng;
#[ORM\Embedded(class: Point::class, columnPrefix: 'signal_')]
private Point $signalLocation;
#[ORM\Column(type: Types::DATETIME_IMMUTABLE, name: 'created_at')]
private DateTimeImmutable $createdAt;
@@ -49,26 +50,26 @@ class Signal
return $this;
}
public function getLat(): float
public function getUserLocation(): Point
{
return $this->lat;
return $this->userLocation;
}
public function setLat(float $lat): self
public function setUserLocation(Point $userLocation): self
{
$this->lat = $lat;
$this->userLocation = $userLocation;
return $this;
}
public function getLng(): float
public function getSignalLocation(): Point
{
return $this->lng;
return $this->signalLocation;
}
public function setLng(float $lng): self
public function setSignalLocation(Point $signalLocation): self
{
$this->lng = $lng;
$this->signalLocation = $signalLocation;
return $this;
}
@@ -0,0 +1,38 @@
<?php
declare(strict_types=1);
namespace App\Exception;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Attribute\WithHttpStatus;
use function sprintf;
#[WithHttpStatus(Response::HTTP_UNPROCESSABLE_ENTITY, headers: ['x-error-code' => 'invalid_coordinates'])]
final class InvalidPointException extends \RuntimeException
{
private function __construct(string $message)
{
parent::__construct($message);
}
public static function invalidNumber(): self
{
return new self('Latitude and longitude must be finite numbers.');
}
public static function latitudeOutOfRange(float $lat): self
{
return new self(sprintf('Latitude %.6f is out of bounds.', $lat));
}
public static function longitudeOutOfRange(float $lng): self
{
return new self(sprintf('Longitude %.6f is out of bounds.', $lng));
}
public static function missingCoordinates(): self
{
return new self('Missing latitude or longitude.');
}
}
@@ -0,0 +1,17 @@
<?php
declare(strict_types=1);
namespace App\Exception;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Attribute\WithHttpStatus;
#[WithHttpStatus(Response::HTTP_BAD_REQUEST, headers: ['x-error-code' => 'missing_client_key'])]
final class MissingClientKeyException extends \RuntimeException
{
public function __construct()
{
parent::__construct('Unable to determine your network address.');
}
}
@@ -0,0 +1,18 @@
<?php
declare(strict_types=1);
namespace App\Exception;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Attribute\WithHttpStatus;
use function sprintf;
#[WithHttpStatus(Response::HTTP_UNPROCESSABLE_ENTITY, headers: ['x-error-code' => 'point_too_far'])]
final class PointTooFarException extends \RuntimeException
{
public function __construct(float $maximumDistanceKm)
{
parent::__construct(sprintf('The submitted point must be within %.2fkm of your location.', $maximumDistanceKm));
}
}
@@ -0,0 +1,24 @@
<?php
declare(strict_types=1);
namespace App\Exception;
use DateTimeInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Attribute\WithHttpStatus;
use function sprintf;
#[WithHttpStatus(Response::HTTP_TOO_MANY_REQUESTS, headers: ['x-error-code' => 'submission_rate_limited'])]
final class SubmissionRateLimitedException extends \RuntimeException
{
public function __construct(?DateTimeInterface $retryAfter)
{
$message = 'Too many submissions from your network. Please wait before trying again.';
if ($retryAfter !== null) {
$message .= sprintf(' You can retry after %s.', $retryAfter->format(DATE_ATOM));
}
parent::__construct($message);
}
}
@@ -0,0 +1,12 @@
<?php
declare(strict_types=1);
namespace App\Message;
final class SignalCreatedMessage
{
public function __construct(public readonly int $signalId)
{
}
}
@@ -0,0 +1,67 @@
<?php
declare(strict_types=1);
namespace App\MessageHandler;
use App\Message\SignalCreatedMessage;
use App\Repository\SignalRepository;
use App\Service\SignalSnapshotBuilder;
use DateTimeImmutable;
use DateTimeZone;
use Symfony\Component\Mercure\HubInterface;
use Symfony\Component\Mercure\Update;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
use function json_encode;
use const JSON_THROW_ON_ERROR;
use Throwable;
#[AsMessageHandler]
final class SignalCreatedMessageHandler
{
public function __construct(
private readonly SignalRepository $signals,
private readonly SignalSnapshotBuilder $snapshotBuilder,
private readonly HubInterface $hub,
#[Autowire('%app.signal_stream_topic%')]
private readonly string $topic,
#[Autowire('%app.signal_snapshot_limit%')]
private readonly int $snapshotLimit,
private readonly ?LoggerInterface $logger = null,
) {
}
public function __invoke(SignalCreatedMessage $message): void
{
$recentSignals = $this->signals->findRecent($this->snapshotLimit);
$snapshot = $this->snapshotBuilder->build($recentSignals);
$payload = [
'type' => 'snapshot',
'payload' => [
'points' => $snapshot['points'],
'density' => $snapshot['density'],
'latestByUser' => $snapshot['latestByUser'],
'totals' => $snapshot['totals'],
'updatedAt' => (new DateTimeImmutable('now', new DateTimeZone('UTC')))->format(DATE_ATOM),
],
];
$data = json_encode($payload, JSON_THROW_ON_ERROR);
$update = new Update(
topics: $this->topic,
data: $data,
);
try {
$this->hub->publish($update);
} catch (Throwable $exception) {
$this->logger?->error('Failed to publish signal update to Mercure.', [
'exception' => $exception,
]);
}
}
}
+16
View File
@@ -0,0 +1,16 @@
<?php
declare(strict_types=1);
namespace App\Payload;
use App\ValueObject\Point;
final readonly class SignalPayload
{
public function __construct(
public Point $signalLocation,
public Point $userLocation,
) {
}
}
+59
View File
@@ -0,0 +1,59 @@
<?php
declare(strict_types=1);
namespace App\Service;
use App\Exception\MissingClientKeyException;
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpFoundation\Request;
final class ClientKeyProvider
{
public function __construct(
private readonly LoggerInterface $logger,
) {
}
public function fromRequest(Request $request): string
{
$ip = $this->extractClientIp($request);
$clientKey = $this->hashIp($ip);
$this->logger->debug('Generated client key for request.', [
'client_key' => $clientKey,
]);
return $clientKey;
}
private function extractClientIp(Request $request): string
{
$forwarded = $request->headers->get('X-Forwarded-For');
if ($forwarded !== null && $forwarded !== '') {
$parts = array_filter(array_map('trim', explode(',', $forwarded)));
if ($parts !== []) {
$this->logger->info('Resolved client address from forwarded header.', [
'resolution' => 'forwarded',
]);
return $parts[0];
}
}
$ip = $request->getClientIp();
if (is_string($ip) && $ip !== '') {
$this->logger->info('Resolved client address from remote address.', [
'resolution' => 'remote',
]);
return $ip;
}
$this->logger->critical('Failed to resolve client address for key generation.');
throw new MissingClientKeyException();
}
private function hashIp(string $ip): string
{
return substr(hash('sha256', $ip), 0, 12);
}
}
@@ -0,0 +1,52 @@
<?php
declare(strict_types=1);
namespace App\Service;
use App\ValueObject\Point;
use App\Exception\PointTooFarException;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
final class PointProximityValidator
{
public function __construct(
#[Autowire('%app.max_signal_distance_km%')]
private readonly float $maximumDistanceKm,
#[Autowire(service: 'monolog.logger.signals')]
private readonly LoggerInterface $logger,
) {
}
public function assertWithinRange(Point $userLocation, Point $signalLocation): void
{
$distance = $this->distanceInKm($userLocation, $signalLocation);
$this->logger->debug('Calculated proximity between user and signal.', [
'distance_km' => $distance,
'max_distance_km' => $this->maximumDistanceKm,
]);
if ($distance > $this->maximumDistanceKm) {
$this->logger->warning('Rejected signal because it is beyond the allowed proximity.', [
'distance_km' => $distance,
'max_distance_km' => $this->maximumDistanceKm,
]);
throw new PointTooFarException($this->maximumDistanceKm);
}
}
private function distanceInKm(Point $a, Point $b): float
{
$lat1 = deg2rad($a->getLat());
$lat2 = deg2rad($b->getLat());
$deltaLat = deg2rad($b->getLat() - $a->getLat());
$deltaLng = deg2rad($b->getLng() - $a->getLng());
$haversine = sin($deltaLat / 2) ** 2 + cos($lat1) * cos($lat2) * sin($deltaLng / 2) ** 2;
$c = 2 * atan2(sqrt($haversine), sqrt(1 - $haversine));
return 6371 * $c;
}
}
+27 -6
View File
@@ -12,9 +12,21 @@ class SignalSnapshotBuilder
* @param list<Signal> $signals
*
* @return array{
* points: list<array{id: int, lat: float, lng: float, createdAt: string, userKey: string}>,
* points: list<array{
* id: int,
* signalLocation: array{lat: float, lng: float},
* userLocation: array{lat: float, lng: float},
* createdAt: string,
* userKey: string,
* }>,
* density: list<array{lat: float, lng: float, intensity: int}>,
* latestByUser: list<array{id: int, lat: float, lng: float, createdAt: string, userKey: string}>,
* latestByUser: list<array{
* id: int,
* signalLocation: array{lat: float, lng: float},
* userLocation: array{lat: float, lng: float},
* createdAt: string,
* userKey: string,
* }>,
* totals: array{points: int, contributors: int}
* }
*/
@@ -25,18 +37,27 @@ class SignalSnapshotBuilder
$latestByUser = [];
foreach ($signals as $signal) {
$signalLocation = $signal->getSignalLocation();
$userLocation = $signal->getUserLocation();
$point = [
'id' => (int) $signal->getId(),
'lat' => $signal->getLat(),
'lng' => $signal->getLng(),
'signalLocation' => [
'lat' => $signalLocation->getLat(),
'lng' => $signalLocation->getLng(),
],
'userLocation' => [
'lat' => $userLocation->getLat(),
'lng' => $userLocation->getLng(),
],
'createdAt' => $signal->getCreatedAt()->format(DATE_ATOM),
'userKey' => $signal->getUserKey(),
];
$points[] = $point;
$bucketLat = round($signal->getLat(), 3);
$bucketLng = round($signal->getLng(), 3);
$bucketLat = round($signalLocation->getLat(), 3);
$bucketLng = round($signalLocation->getLng(), 3);
$bucketKey = $bucketLat . ':' . $bucketLng;
if (! isset($densityBuckets[$bucketKey])) {
@@ -0,0 +1,60 @@
<?php
declare(strict_types=1);
namespace App\Service;
use App\Repository\SignalRepository;
use DateTimeImmutable;
use DateTimeZone;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
final class SignalSnapshotService
{
public function __construct(
private readonly SignalRepository $signals,
private readonly SignalSnapshotBuilder $snapshotBuilder,
#[Autowire('%app.signal_snapshot_limit%')]
private readonly int $snapshotLimit,
#[Autowire(service: 'monolog.logger.signals')]
private readonly LoggerInterface $logger,
) {
}
public function buildSnapshot(?int $limit, string $clientKey): array
{
$resolvedLimit = $this->normalizeLimit($limit);
$signals = $this->signals->findRecent($resolvedLimit);
$snapshot = $this->snapshotBuilder->build($signals);
$this->logger->info('Built signal snapshot for client.', [
'client_key' => $clientKey,
'requested_limit' => $limit,
'applied_limit' => $resolvedLimit,
'point_count' => count($snapshot['points']),
]);
return [
'clientKey' => $clientKey,
'points' => $snapshot['points'],
'density' => $snapshot['density'],
'latestByUser' => $snapshot['latestByUser'],
'totals' => $snapshot['totals'],
'updatedAt' => new DateTimeImmutable('now', new DateTimeZone('UTC'))->format(DATE_ATOM),
];
}
private function normalizeLimit(?int $limit): int
{
if ($limit === null) {
return $this->snapshotLimit;
}
if ($limit < 1) {
return $this->snapshotLimit;
}
return min($limit, $this->snapshotLimit);
}
}
@@ -0,0 +1,95 @@
<?php
declare(strict_types=1);
namespace App\Service;
use App\Payload\SignalPayload;
use App\Entity\Signal;
use App\Exception\SubmissionRateLimitedException;
use App\Message\SignalCreatedMessage;
use App\Repository\SignalRepository;
use DateTimeImmutable;
use DateTimeZone;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\RateLimiter\RateLimiterFactory;
final class SignalSubmissionService
{
public function __construct(
private readonly SignalRepository $signals,
#[Autowire(service: 'limiter.signal_submission')]
private readonly RateLimiterFactory $submissionLimiter,
private readonly PointProximityValidator $proximityValidator,
private readonly MessageBusInterface $bus,
#[Autowire(service: 'monolog.logger.signals')]
private readonly LoggerInterface $logger,
) {
}
public function submit(string $clientKey, SignalPayload $payload): Signal
{
$this->enforceRateLimit($clientKey);
$signalLocation = $payload->signalLocation;
$userLocation = $payload->userLocation;
$this->proximityValidator->assertWithinRange($userLocation, $signalLocation);
$this->logger->info('Storing new signal submission.', [
'client_key' => $clientKey,
'signal_location' => [
'lat' => round($signalLocation->getLat(), 5),
'lng' => round($signalLocation->getLng(), 5),
],
'user_location' => [
'lat' => round($userLocation->getLat(), 5),
'lng' => round($userLocation->getLng(), 5),
],
]);
$signal = (new Signal())
->setUserKey($clientKey)
->setUserLocation($userLocation)
->setSignalLocation($signalLocation)
->setCreatedAt(new DateTimeImmutable('now', new DateTimeZone('UTC')));
$this->signals->save($signal);
$signalId = $signal->getId();
if ($signalId !== null) {
$this->logger->debug('Dispatching signal created message.', [
'signal_id' => $signalId,
]);
$this->bus->dispatch(new SignalCreatedMessage($signalId));
$this->logger->info('Signal stored successfully.', [
'signal_id' => $signalId,
'client_key' => $clientKey,
]);
}
return $signal;
}
private function enforceRateLimit(string $clientKey): void
{
$rateLimiter = $this->submissionLimiter->create($clientKey);
$limit = $rateLimiter->consume(1);
if (! $limit->isAccepted()) {
$retryAfter = $limit->getRetryAfter();
$this->logger->warning('Signal submission rejected due to rate limiting.', [
'client_key' => $clientKey,
'retry_after' => $retryAfter?->format(DATE_ATOM),
]);
throw new SubmissionRateLimitedException($limit->getRetryAfter());
}
$this->logger->debug('Signal submission passed rate limiting.', [
'client_key' => $clientKey,
'remaining_tokens' => $limit->getRemainingTokens(),
]);
}
}
+66
View File
@@ -0,0 +1,66 @@
<?php
declare(strict_types=1);
namespace App\ValueObject;
use App\Exception\InvalidPointException;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Embeddable]
class Point
{
#[ORM\Column(name: 'lat', type: Types::FLOAT)]
private float $lat;
#[ORM\Column(name: 'lng', type: Types::FLOAT)]
private float $lng;
public function __construct(float $lat, float $lng)
{
$this->assertValid($lat, $lng);
$this->lat = $lat;
$this->lng = $lng;
}
public static function fromArray(array $coordinates): self
{
if (! isset($coordinates['lat'], $coordinates['lng'])) {
throw InvalidPointException::missingCoordinates();
}
return new self((float) $coordinates['lat'], (float) $coordinates['lng']);
}
public static function fromLatLng(float $lat, float $lng): self
{
return new self($lat, $lng);
}
public function getLat(): float
{
return $this->lat;
}
public function getLng(): float
{
return $this->lng;
}
private function assertValid(float $lat, float $lng): void
{
if (! is_finite($lat) || ! is_finite($lng)) {
throw InvalidPointException::invalidNumber();
}
if ($lat < -90 || $lat > 90) {
throw InvalidPointException::latitudeOutOfRange($lat);
}
if ($lng < -180 || $lng > 180) {
throw InvalidPointException::longitudeOutOfRange($lng);
}
}
}