Skip to content
This repository has been archived by the owner on May 20, 2023. It is now read-only.

Commit

Permalink
Add projects for events
Browse files Browse the repository at this point in the history
Default project is assigned to events by default.
For sentry events project id is taken from
projectId param in sentry.event.store route.
Events can by filtered by setting projectId
in events.type route.
  • Loading branch information
js361014 committed Aug 1, 2022
1 parent 2b45d6b commit e063e6e
Show file tree
Hide file tree
Showing 47 changed files with 439 additions and 60 deletions.
3 changes: 2 additions & 1 deletion app/Application/Commands/FindAllEvents.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ final class FindAllEvents implements Query
{
// TODO: use readonly property
public function __construct(
public ?string $type = null
public ?string $type = null,
public ?int $projectId = null,
) {
}
}
9 changes: 9 additions & 0 deletions app/Application/Commands/FindAllProjects.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace App\Commands;

use App\Contracts\Query\Query;

class FindAllProjects implements Query
{
}
13 changes: 13 additions & 0 deletions app/Application/Commands/FindProjectByName.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace App\Commands;

use App\Contracts\Query\Query;

class FindProjectByName implements Query
{
public function __construct(
public string $name
) {
}
}
2 changes: 2 additions & 0 deletions app/Application/Commands/HandleReceivedEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ final class HandleReceivedEvent implements Command
public int $timestamp;

public function __construct(
public int $projectId,
public string $type,
public array $payload,
public bool $sendToConsole = false
Expand All @@ -24,6 +25,7 @@ public function __construct(
public function toArray(): array
{
return [
'projectId' => $this->projectId,
'type' => $this->type,
'payload' => $this->payload,
'uuid' => (string) $this->uuid,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public function __construct(
public Uuid $uuid,
public DateTimeImmutable $date,
public array $payload,
public int $projectId,
) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ public function handle(Command $command): void
$command->uuid,
$command->type,
new Json($command->payload),
$command->date
$command->date,
$command->projectId
)
);

Expand All @@ -44,13 +45,14 @@ public function __invoke(HandleReceivedEvent $command): void
type: $command->type,
uuid: $command->uuid,
date: Carbon::createFromTimestamp($command->timestamp)->toDateTimeImmutable(),
payload: $command->payload
payload: $command->payload,
projectId: $command->projectId
)
);

$this->dispatcher->dispatch(
new EventWasReceived(
$command->uuid, $command->type, $command->payload, $command->timestamp, $command->sendToConsole
$command->projectId, $command->uuid, $command->type, $command->payload, $command->timestamp, $command->sendToConsole
)
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ public function __invoke(FindAllEvents $query): iterable
if ($query->type) {
$scope['type'] = $query->type;
}
if ($query->projectId) {
$scope['project_id'] = $query->projectId;
}

return EventCollection::make(
$this->events->findAll($scope, [
Expand Down
10 changes: 9 additions & 1 deletion app/Modules/Events/Domain/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ public function __construct(
private Json $payload,

#[Column(type: 'datetime')]
private DateTimeImmutable $date
private DateTimeImmutable $date,

#[Column(type: 'integer', nullable: false)]
private int $projectId
) {
}

Expand All @@ -51,4 +54,9 @@ public function getDate(): DateTimeImmutable
{
return $this->date;
}

public function getProjectId(): int
{
return $this->projectId;
}
}
10 changes: 5 additions & 5 deletions app/Modules/Events/Interfaces/Http/Controllers/ListAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
class ListAction extends Controller
{
#[Get(uri: '/', name: 'events', middleware: 'auth')]
public function eventList(Request $request, QueryBus $bus, ActionMap $actionMap, ?string $type = null)
public function eventList(Request $request, QueryBus $bus, ActionMap $actionMap, ?string $type = null, ?int $projectId = null)
{
$action = 'Events';
if ($type) {
Expand All @@ -28,15 +28,15 @@ public function eventList(Request $request, QueryBus $bus, ActionMap $actionMap,
}

return Inertia::render($action, [
'events' => $bus->ask(new FindAllEvents(type: $type)),
'events' => $bus->ask(new FindAllEvents(type: $type, projectId: $projectId)),
'version' => config('app.version'),
'name' => config('app.name'),
]);
}

#[Get(uri: '/events/{type}', name: 'events.type')]
public function eventListByType(Request $request, QueryBus $bus, ActionMap $actionMap, string $type)
#[Get(uri: '/events/type/{type}/{projectId?}', name: 'events.type')]
public function eventListByType(Request $request, QueryBus $bus, ActionMap $actionMap, string $type, ?int $projectId = null)
{
return $this->eventList($request, $bus, $actionMap, $type);
return $this->eventList($request, $bus, $actionMap, $type, $projectId);
}
}
3 changes: 2 additions & 1 deletion app/Modules/Events/Projectors/EventProjector.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ public function onEventWasReceived(EventWasReceived $event): void
$event->type,
$event->uuid,
Carbon::createFromTimestamp($event->timestamp)->toDateTimeImmutable(),
$event->payload
$event->payload,
$event->projectId
)
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public function __invoke(HandleReceivedEvent $command): void
{
$this->repository->persist(
EventProcess::received(
$command->projectId,
$command->uuid,
$command->type,
$command->payload,
Expand Down
4 changes: 2 additions & 2 deletions app/Modules/IncommingEvents/Domain/EventProcess.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ final class EventProcess implements AggregateRoot
private DateTimeImmutable $date;
private bool $deleted = false;

public static function received(Uuid $uuid, string $type, array $payload, int $timestamp): static
public static function received(int $projectId, Uuid $uuid, string $type, array $payload, int $timestamp): static
{
$process = new static($uuid);
$process->recordThat(
new EventWasReceived($uuid, $type, $payload, $timestamp)
new EventWasReceived($projectId, $uuid, $type, $payload, $timestamp)
);

return $process;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ final class EventWasReceived implements Event, ShouldBroadcastNow
{
// TODO: use readonly property
public function __construct(
public int $projectId,
public Uuid $uuid,
public string $type,
public array $payload,
Expand All @@ -25,6 +26,7 @@ public function __construct(
public function toPayload(): array
{
return [
'projectId' => $this->projectId,
'uuid' => (string) $this->uuid,
'type' => $this->type,
'payload' => $this->payload,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@

namespace Modules\Inspector\Interfaces\Http\Controllers;

use App\Commands\FindProjectByName;
use App\Commands\HandleReceivedEvent;
use App\Contracts\Command\CommandBus;
use App\Contracts\Query\QueryBus;
use Illuminate\Http\Request;
use Interfaces\Http\Controllers\Controller;
use Spatie\RouteAttributes\Attributes\Post;
Expand All @@ -17,6 +19,7 @@ class StoreEventAction extends Controller
public function __invoke(
Request $request,
CommandBus $commands,
QueryBus $queryBus,
): void {
$data = json_decode(base64_decode($request->getContent()), true)
?? throw new HttpException(500, 'Invalid data');
Expand All @@ -27,8 +30,10 @@ public function __invoke(
abort(403);
}

$project = $queryBus->ask(new FindProjectByName('default'));
$projectId = $project->getId();
$commands->dispatch(
new HandleReceivedEvent('inspector', $data, true)
new HandleReceivedEvent((int) $projectId, 'inspector', $data, true)
);
}
}
9 changes: 7 additions & 2 deletions app/Modules/Monolog/Console/TcpHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@

namespace Modules\Monolog\Console;

use App\Commands\FindProjectByName;
use App\Commands\HandleReceivedEvent;
use App\Contracts\Command\CommandBus;
use App\Contracts\Query\QueryBus;
use App\Contracts\TCP\Handler;
use App\Contracts\TCP\Response;
use App\TCP\CloseConnection;
Expand All @@ -19,7 +21,8 @@ class TcpHandler implements Handler
{
public function __construct(
private CommandBus $commands,
private \Interfaces\Console\StreamHandler $streamHandler
private \Interfaces\Console\StreamHandler $streamHandler,
private QueryBus $queryBus,
) {
}

Expand All @@ -39,8 +42,10 @@ public function handle(Request $request, OutputInterface $output): Response
throw new RuntimeException("Unable to decode a message from [{$request->connectionUuid}] client.");
}

$project = $this->queryBus->ask(new FindProjectByName('default'));
$projectId = $project->getId();
$this->commands->dispatch(
$command = new HandleReceivedEvent('monolog', $payload)
$command = new HandleReceivedEvent((int) $projectId, 'monolog', $payload)
);

$this->streamHandler->handle($command->toArray());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@

namespace Modules\Monolog\Interfaces\Http\Controllers\Slack;

use App\Commands\FindProjectByName;
use App\Commands\HandleReceivedEvent;
use App\Contracts\Command\CommandBus;
use App\Contracts\Query\QueryBus;
use Illuminate\Http\Request;
use Interfaces\Http\Controllers\Controller;
use Spatie\RouteAttributes\Attributes\Post;
Expand All @@ -16,9 +18,12 @@ class StoreEventAction extends Controller
public function __invoke(
Request $request,
CommandBus $commands,
QueryBus $queryBus,
): void {
$project = $queryBus->ask(new FindProjectByName('default'));
$projectId = $project->getId();
$commands->dispatch(
new HandleReceivedEvent('slack', $request->all())
new HandleReceivedEvent((int) $projectId, 'slack', $request->all())
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Modules\Project\Application\Queries\FindAllProjects;

use App\Commands\FindAllProjects;
use App\Contracts\Query\QueryHandler;
use Modules\Project\Application\Resources\ProjectCollection;
use Modules\Project\Domain\ProjectRepository;

class Handler implements QueryHandler
{
public function __construct(
private ProjectRepository $projects
) {
}

#[\App\Attributes\QueryBus\QueryHandler]
public function __invoke(FindAllProjects $query): iterable
{
return ProjectCollection::make(
$this->projects->findAll()
);
}
}
31 changes: 31 additions & 0 deletions app/Modules/Project/Application/Queries/FindByName/Handler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace Modules\Project\Application\Queries\FindByName;

use App\Commands\FindProjectByName;
use App\Contracts\Query\QueryHandler;
use App\Exceptions\EntityNotFoundException;
use Modules\Project\Application\Resources\ProjectResource;
use Modules\Project\Domain\ProjectRepository;

class Handler implements QueryHandler
{
public function __construct(private ProjectRepository $projectRepository)
{
}

#[\App\Attributes\QueryBus\QueryHandler]
public function __invoke(FindProjectByName $query): ProjectResource
{
$project = $this->projectRepository->findOne(['name' => $query->name]);
if (! $project) {
throw new EntityNotFoundException(
sprintf('Project with given name [%s] was not found.', $query->name)
);
}

return new ProjectResource($project);
}
}
10 changes: 10 additions & 0 deletions app/Modules/Project/Application/Resources/ProjectCollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Modules\Project\Application\Resources;

use Illuminate\Http\Resources\Json\ResourceCollection;

class ProjectCollection extends ResourceCollection
{
public $collects = ProjectResource::class;
}
16 changes: 16 additions & 0 deletions app/Modules/Project/Application/Resources/ProjectResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Modules\Project\Application\Resources;

use Illuminate\Http\Resources\Json\JsonResource;

class ProjectResource extends JsonResource
{
public function toArray($request): array
{
return [
'id' => $this->getId(),
'name' => $this->getName(),
];
}
}
Loading

0 comments on commit e063e6e

Please sign in to comment.