Skip to content

Commit

Permalink
upgrade event sourcing lib
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidBadura committed Mar 27, 2024
1 parent 78b4ccb commit 7d571b6
Show file tree
Hide file tree
Showing 14 changed files with 137 additions and 117 deletions.
34 changes: 19 additions & 15 deletions config/routes.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,29 @@ patchlevel_event_sourcing_admin_store_show:
path: /store
controller: Patchlevel\EventSourcingAdminBundle\Controller\StoreController::showAction

patchlevel_event_sourcing_admin_projection_show:
path: /projection
controller: Patchlevel\EventSourcingAdminBundle\Controller\ProjectionController::showAction
patchlevel_event_sourcing_admin_subscription_show:
path: /subscription
controller: Patchlevel\EventSourcingAdminBundle\Controller\SubscriptionController::showAction

patchlevel_eventsourcingadmin_projection_rebuild:
path: /projection/{id}/rebuild
controller: Patchlevel\EventSourcingAdminBundle\Controller\ProjectionController::rebuildAction
patchlevel_eventsourcingadmin_subscription_rebuild:
path: /subscription/{id}/rebuild
controller: Patchlevel\EventSourcingAdminBundle\Controller\SubscriptionController::rebuildAction

patchlevel_eventsourcingadmin_projection_pause:
path: /projection/{id}/pause
controller: Patchlevel\EventSourcingAdminBundle\Controller\ProjectionController::pauseAction
patchlevel_eventsourcingadmin_subscription_pause:
path: /subscription/{id}/pause
controller: Patchlevel\EventSourcingAdminBundle\Controller\SubscriptionController::pauseAction

patchlevel_eventsourcingadmin_projection_reactivate:
path: /projection/{id}/reactivate
controller: Patchlevel\EventSourcingAdminBundle\Controller\ProjectionController::reactivateAction
patchlevel_eventsourcingadmin_subscription_setup:
path: /subscription/{id}/setup
controller: Patchlevel\EventSourcingAdminBundle\Controller\SubscriptionController::setupAction

patchlevel_eventsourcingadmin_projection_remove:
path: /projection/{id}/remove
controller: Patchlevel\EventSourcingAdminBundle\Controller\ProjectionController::removeAction
patchlevel_eventsourcingadmin_subscription_reactivate:
path: /subscription/{id}/reactivate
controller: Patchlevel\EventSourcingAdminBundle\Controller\SubscriptionController::reactivateAction

patchlevel_eventsourcingadmin_subscription_remove:
path: /subscription/{id}/remove
controller: Patchlevel\EventSourcingAdminBundle\Controller\SubscriptionController::removeAction

patchlevel_event_sourcing_admin_inspection_index:
path: /inspection
Expand Down
29 changes: 13 additions & 16 deletions src/Controller/EventController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,22 @@
use Patchlevel\EventSourcing\EventBus\ListenerDescriptor;
use Patchlevel\EventSourcing\EventBus\ListenerProvider;
use Patchlevel\EventSourcing\Metadata\Event\EventRegistry;
use Patchlevel\EventSourcing\Metadata\Projector\ProjectorMetadataFactory;
use Patchlevel\EventSourcing\Metadata\Subscriber\SubscriberMetadataFactory;
use Patchlevel\EventSourcingAdminBundle\Projection\Node;
use Patchlevel\EventSourcingAdminBundle\Projection\TraceProjector;
use Symfony\Component\HttpFoundation\Response;
use Twig\Environment;

final class EventController
{
/**
* @param iterable<object> $projectors
* @param iterable<object> $subscribers
*/
public function __construct(
private readonly Environment $twig,
private readonly EventRegistry $eventRegistry,
private readonly ListenerProvider $listenerProvider,
private readonly iterable $projectors,
private readonly ProjectorMetadataFactory $projectorMetadataFactory,
private readonly TraceProjector|null $traceProjector,
private readonly Environment $twig,
private readonly EventRegistry $eventRegistry,
private readonly ListenerProvider $listenerProvider,
private readonly iterable $subscribers,
private readonly SubscriberMetadataFactory $subscriberMetadataFactory,
)
{
}
Expand All @@ -39,8 +37,7 @@ public function indexAction(): Response
'name' => $eventName,
'class' => $eventClass,
'listeners' => $this->listenerMethods($eventClass),
'projectors' => $this->projectorsMethods($eventClass),
'sources' => $this->source($eventClass),
'subscribers' => $this->subscribersMethods($eventClass),
];
}

Expand All @@ -57,22 +54,22 @@ private function listenerMethods(string $eventClass): array
);
}

private function projectorsMethods(string $eventClass): array
private function subscribersMethods(string $eventClass): array
{
$result = [];

foreach ($this->projectors as $projector) {
$metadata = $this->projectorMetadataFactory->metadata($projector::class);
foreach ($this->subscribers as $subscriber) {
$metadata = $this->subscriberMetadataFactory->metadata($subscriber::class);

if (array_key_exists($eventClass, $metadata->subscribeMethods)) {
foreach ($metadata->subscribeMethods[$eventClass] as $method) {
$result[] = sprintf('%s::%s', $projector::class, $method);
$result[] = sprintf('%s::%s', $subscriber::class, $method->name);
}
}

if (array_key_exists(Subscribe::ALL, $metadata->subscribeMethods)) {
foreach ($metadata->subscribeMethods[Subscribe::ALL] as $method) {
$result[] = sprintf('%s::%s', $projector::class, $method);
$result[] = sprintf('%s::%s', $subscriber::class, $method->name);
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/Controller/InspectionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Patchlevel\EventSourcingAdminBundle\Controller;

use Patchlevel\EventSourcing\Aggregate\AggregateHeader;
use Patchlevel\EventSourcing\Aggregate\AggregateRoot;
use Patchlevel\EventSourcing\Aggregate\CustomId;
use Patchlevel\EventSourcing\Metadata\AggregateRoot\AggregateRootMetadataFactory;
Expand Down Expand Up @@ -158,7 +159,7 @@ private function aggregate(string $aggregateName, string $aggregateId, int|null

return $aggregateClass::createFromEvents(
$this->unpack($stream, $until),
$firstMessage->playhead() - 1,
$firstMessage->header(AggregateHeader::class)->playhead - 1,
);
} finally {
$stream?->close();
Expand All @@ -169,7 +170,7 @@ private function aggregate(string $aggregateName, string $aggregateId, int|null
private function unpack(Stream $stream, int|null $until = null): Traversable
{
foreach ($stream as $message) {
if ($until !== null && $message->playhead() > $until) {
if ($until !== null && $message->header(AggregateHeader::class)->playhead > $until) {
break;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use Symfony\Component\Routing\RouterInterface;
use Twig\Environment;

final class ProjectionController
final class SubscriptionController
{
public function __construct(
private readonly Environment $twig,
Expand All @@ -36,7 +36,7 @@ public function showAction(Request $request): Response
$groups[$subscription->group()] = true;
}

$filteredProjections = [];
$filteredSubscriptions = [];
$search = $request->get('search');
$group = $request->get('group');
$mode = $request->get('mode');
Expand All @@ -60,12 +60,12 @@ public function showAction(Request $request): Response
continue;
}

$filteredProjections[] = $subscription;
$filteredSubscriptions[] = $subscription;
}

return new Response(
$this->twig->render('@PatchlevelEventSourcingAdmin/projection/show.html.twig', [
'projections' => $filteredProjections,
$this->twig->render('@PatchlevelEventSourcingAdmin/subscription/show.html.twig', [
'subscriptions' => $filteredSubscriptions,
'messageCount' => $messageCount,
'statuses' => array_map(fn (Status $status) => $status->value, Status::cases()),
'modes' => array_map(fn (RunMode $mode) => $mode->value, RunMode::cases()),
Expand All @@ -82,7 +82,7 @@ public function rebuildAction(string $id): Response
$this->engine->boot($criteria);

return new RedirectResponse(
$this->router->generate('patchlevel_event_sourcing_admin_projection_show'),
$this->router->generate('patchlevel_event_sourcing_admin_subscription_show'),
);
}

Expand All @@ -93,7 +93,7 @@ public function pauseAction(string $id): Response
$this->engine->pause($criteria);

return new RedirectResponse(
$this->router->generate('patchlevel_event_sourcing_admin_projection_show'),
$this->router->generate('patchlevel_event_sourcing_admin_subscription_show'),
);
}

Expand All @@ -104,7 +104,18 @@ public function bootAction(string $id): Response
$this->engine->boot($criteria);

return new RedirectResponse(
$this->router->generate('patchlevel_event_sourcing_admin_projection_show'),
$this->router->generate('patchlevel_event_sourcing_admin_subscription_show'),
);
}

public function setupAction(string $id): Response
{
$criteria = new SubscriptionEngineCriteria([$id]);

$this->engine->setup($criteria);

return new RedirectResponse(
$this->router->generate('patchlevel_event_sourcing_admin_subscription_show'),
);
}

Expand All @@ -115,7 +126,7 @@ public function reactivateAction(string $id): Response
$this->engine->reactivate($criteria);

return new RedirectResponse(
$this->router->generate('patchlevel_event_sourcing_admin_projection_show'),
$this->router->generate('patchlevel_event_sourcing_admin_subscription_show'),
);
}

Expand All @@ -126,7 +137,7 @@ public function removeAction(string $id): Response
$this->engine->remove($criteria);

return new RedirectResponse(
$this->router->generate('patchlevel_event_sourcing_admin_projection_show'),
$this->router->generate('patchlevel_event_sourcing_admin_subscription_show'),
);
}
}
2 changes: 2 additions & 0 deletions src/Decorator/RequestIdDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public function __construct(

public function __invoke(Message $message): Message
{
return $message;

$request = $this->requestStack->getMainRequest();

if (!$request) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
use Patchlevel\EventSourcingAdminBundle\Controller\EventController;
use Patchlevel\EventSourcingAdminBundle\Controller\GraphController;
use Patchlevel\EventSourcingAdminBundle\Controller\InspectionController;
use Patchlevel\EventSourcingAdminBundle\Controller\ProjectionController;
use Patchlevel\EventSourcingAdminBundle\Controller\SubscriptionController;
use Patchlevel\EventSourcingAdminBundle\Controller\StoreController;
use Patchlevel\EventSourcingAdminBundle\Decorator\RequestIdDecorator;
use Patchlevel\EventSourcingAdminBundle\Listener\RequestIdListener;
Expand Down Expand Up @@ -82,7 +82,7 @@ public function load(array $configs, ContainerBuilder $container): void
])
->addTag('controller.service_arguments');

$container->register(ProjectionController::class)
$container->register(SubscriptionController::class)
->setArguments([
new Reference('twig'),
new Reference(SubscriptionEngine::class),
Expand Down
7 changes: 5 additions & 2 deletions src/Twig/EventSourcingAdminExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

namespace Patchlevel\EventSourcingAdminBundle\Twig;

use Patchlevel\EventSourcing\EventBus\Message;
use Patchlevel\EventSourcing\Aggregate\AggregateHeader;
use Patchlevel\EventSourcing\Message\Message;
use Patchlevel\EventSourcing\Metadata\AggregateRoot\AggregateRootRegistry;
use Patchlevel\EventSourcing\Metadata\Event\EventRegistry;
use Patchlevel\EventSourcing\Serializer\Encoder\JsonEncoder;
Expand Down Expand Up @@ -50,7 +51,7 @@ public function shortId(string $id): string
/** @return class-string */
public function aggregateClass(Message $message): string
{
return $this->aggregateRootRegistry->aggregateClass($message->aggregateName());
return $this->aggregateRootRegistry->aggregateClass($message->header(AggregateHeader::class)->aggregateName);
}

/** @return class-string */
Expand All @@ -76,6 +77,8 @@ public function eventPayload(Message $message): string

public function profilerToken(Message $message): ?string
{
return null;

$headers = $message->customHeaders();
$requestId = $headers['requestId'] ?? null;

Expand Down
20 changes: 4 additions & 16 deletions templates/event/index.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -64,26 +64,14 @@
</dl>

<div class="px-4 py-6 bg-gray-50 text-gray-500 font-semibold">
Sources ({{ event.sources|length }})
Projectors ({{ event.subscribers|length }})
</div>

<ul>
{% for node in event.sources|sort((a, b) => a.name <=> b.name)|sort((a, b) => a.category <=> b.category) %}
{{ _self.node(node) }}
{% for subscriber in event.subscribers %}
{{ _self.subscriber(subscriber) }}
{% else %}
<li class="px-4 py-6">unknown sources</li>
{% endfor %}
</ul>

<div class="px-4 py-6 bg-gray-50 text-gray-500 font-semibold">
Projectors ({{ event.projectors|length }})
</div>

<ul>
{% for projector in event.projectors %}
{{ _self.subscriber(projector) }}
{% else %}
<li class="px-4 py-6">no projectors</li>
<li class="px-4 py-6">no subscriber</li>
{% endfor %}
</ul>

Expand Down
6 changes: 4 additions & 2 deletions templates/inspection/show.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@
<div class="flow-root">
<ul role="list" class="-mb-8">
{% for message in messages %}
{% set aggregateHeader = message.header("Patchlevel\\EventSourcing\\Aggregate\\AggregateHeader") %}

<li class="group/message relative {% if until and loop.index > until %}saturate-0{% endif %}">
<span id="{{ loop.index }}" class="absolute -top-48"></span>
<div id="marker-{{ loop.index }}" class="absolute -left-10 z-20 invisible">
Expand All @@ -184,7 +186,7 @@
data-modal="modal-{{ loop.index }}">
{{ heroicon('magnifying-glass', 'h-5 w-5 inline') }}
</button>
<a href="{{ path('patchlevel_event_sourcing_admin_inspection_show', { aggregateId: aggregateId, aggregateName: aggregateName, tab: tab, until: message.playhead }) }}#{{ message.playhead }}"
<a href="{{ path('patchlevel_event_sourcing_admin_inspection_show', { aggregateId: aggregateId, aggregateName: aggregateName, tab: tab, until: aggregateHeader.playhead }) }}#{{ aggregateHeader.playhead }}"
class="text-gray-500 hover:text-gray-900 invisible group-hover/message:visible"
>
{{ heroicon('play-pause', 'h-5 w-5 inline') }}
Expand All @@ -204,7 +206,7 @@
</dialog>
</div>
<div class="whitespace-nowrap text-right text-xs text-gray-500">
{{ message.recordedOn|date('Y-m-d H:i:s') }}
{{ aggregateHeader.recordedOn|date('Y-m-d H:i:s') }}
</div>
</div>
</div>
Expand Down
2 changes: 1 addition & 1 deletion templates/layout.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<ul role="list" class="-mx-2 space-y-1">
{{ _self.nav_point("Store", "circle-stack", "patchlevel_event_sourcing_admin_store_show") }}
{{ _self.nav_point("Inspection", "identification", "patchlevel_event_sourcing_admin_inspection_index") }}
{{ _self.nav_point("Projections", "video-camera", "patchlevel_event_sourcing_admin_projection_show") }}
{{ _self.nav_point("Subscriptions", "video-camera", "patchlevel_event_sourcing_admin_subscription_show") }}
{{ _self.nav_point("Events", "calendar", "patchlevel_event_sourcing_admin_event_index") }}
</ul>
</li>
Expand Down
16 changes: 10 additions & 6 deletions templates/store/detail.html.twig
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
<dl class="divide-y divide-gray-100">
{{ _self.text('Aggregate', message.aggregateName) }}
{% set aggregateHeader = message.header("Patchlevel\\EventSourcing\\Aggregate\\AggregateHeader") %}

{{ _self.text('Aggregate', aggregateHeader.aggregateName) }}
{{ _self.text('Aggregate Class', eventsourcing_aggregate_class(message)) }}

<div class="p-4 sm:grid sm:grid-cols-3 sm:gap-4 sm:px-0 group/parent">
<dt class="text-sm font-medium leading-6 text-gray-900">Aggregate Id</dt>
<dd class="mt-1 text-sm leading-6 text-gray-500 sm:col-span-2 sm:mt-0">
{{ message.aggregateId }}
<a class="group-hover/parent:visible invisible" href="{{ path('patchlevel_event_sourcing_admin_inspection_show', {aggregateName: message.aggregateName, aggregateId: message.aggregateId}) }}">
{{ aggregateHeader.aggregateId }}
<a class="group-hover/parent:visible invisible" href="{{ path('patchlevel_event_sourcing_admin_inspection_show', {aggregateName: aggregateHeader.aggregateName, aggregateId: aggregateHeader.aggregateId}) }}">
{{ heroicon('identification', 'h-5 w-5 -mt-1 text-gray-400 inline') }}
</a>
</dd>
</div>

{{ _self.text('Aggregate Playhead', message.playhead) }}
{{ _self.text('Aggregate Playhead', aggregateHeader.playhead) }}

<div class="p-4 sm:grid sm:grid-cols-3 sm:gap-4 sm:px-0 group/parent">
<dt class="text-sm font-medium leading-6 text-gray-900">Event Name</dt>
Expand All @@ -34,7 +36,7 @@
</dd>
</div>

{{ _self.text('Recorded on', message.recordedOn|date('Y-m-d H:i:s')) }}
{{ _self.text('Recorded on', aggregateHeader.recordedOn|date('Y-m-d H:i:s')) }}
<div class="p-4 sm:grid sm:grid-cols-3 sm:gap-4 sm:px-0">
<dt class="text-sm font-medium leading-6 text-gray-900">Profiler Token</dt>
<dd class="mt-1 text-sm leading-6 text-gray-500 sm:col-span-2 sm:mt-0">
Expand All @@ -49,7 +51,9 @@
</dd>
</div>
{{ _self.json('Event Payload', eventsourcing_event_payload(message)) }}
{% if message.customHeaders %}


{% if message.customHeaders|default(false) %}
{{ _self.json('Custom Headers', message.customHeaders|json_encode(constant('JSON_PRETTY_PRINT'))) }}
{% else %}
{{ _self.text('Custom Headers', '-') }}
Expand Down
Loading

0 comments on commit 7d571b6

Please sign in to comment.