Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code refactoring #100

Merged
merged 6 commits into from
Dec 1, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Code refactoring
1. Use microtime instead of timestamp for incoming events.
2. Restructure code
  • Loading branch information
butschster committed Dec 1, 2023
commit 818ea8269904831ff10fde7221b32b0d43c0a747
2 changes: 1 addition & 1 deletion .rr.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version: '2.7'
version: '3'

rpc:
listen: tcp:https://127.0.0.1:6001
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function up(): void
->addColumn('uuid', 'string', ['nullable' => false, 'default' => null])
->addColumn('type', 'string', ['nullable' => false, 'default' => null])
->addColumn('payload', 'longText', ['nullable' => false, 'default' => null])
->addColumn('date', 'datetime', ['nullable' => false, 'default' => null])
->addColumn('timestamp', 'float', ['nullable' => false, 'default' => null])
->addColumn('project_id', 'integer', ['nullable' => true, 'default' => null])
->setPrimaryKeys(['uuid'])
->create();
Expand Down
8 changes: 4 additions & 4 deletions app/modules/Events/Domain/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ public function __construct(
#[Column(type: 'longText', typecast: 'json')]
private Json $payload,

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

#[Column(type: 'integer', nullable: true)]
private ?int $projectId,
Expand All @@ -49,9 +49,9 @@ public function getPayload(): Json
return $this->payload;
}

public function getDate(): DateTimeImmutable
public function getTimestamp(): float
{
return $this->date;
return $this->timestamp;
}

public function getProjectId(): ?int
Expand Down
2 changes: 1 addition & 1 deletion app/modules/Events/Domain/EventRepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Cycle\ORM\RepositoryInterface;

/**
* @template TEntity of Event
* @extends RepositoryInterface<Event>
*/
interface EventRepositoryInterface extends RepositoryInterface
{
Expand Down
2 changes: 1 addition & 1 deletion app/modules/Events/Domain/Events/EventWasReceived.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public function __construct(
public readonly Uuid $uuid,
public readonly string $type,
public readonly array $payload,
public readonly int $timestamp,
public readonly float $timestamp,
public readonly ?int $projectId = null,
) {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Modules\Events\Application\Commands;
namespace Modules\Events\Interfaces\Commands;

use App\Application\Commands\ClearEvents;
use Modules\Events\Domain\EventRepositoryInterface;
Expand All @@ -14,7 +14,7 @@ final class ClearEventsHandler
{
public function __construct(
private readonly EventRepositoryInterface $events,
private readonly EventDispatcherInterface $dispatcher
private readonly EventDispatcherInterface $dispatcher,
) {
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Modules\Events\Application\Commands;
namespace Modules\Events\Interfaces\Commands;

use App\Application\Commands\DeleteEvent;
use Modules\Events\Domain\EventRepositoryInterface;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Modules\Events\Application\Commands;
namespace Modules\Events\Interfaces\Commands;

use App\Application\Commands\FindProjectByName;
use App\Application\Commands\HandleReceivedEvent;
Expand All @@ -20,7 +20,7 @@ final class StoreEventHandler
public function __construct(
private readonly EventDispatcherInterface $dispatcher,
private readonly EventRepositoryInterface $events,
private readonly QueryBusInterface $queryBus
private readonly QueryBusInterface $queryBus,
) {
}

Expand All @@ -37,9 +37,9 @@ public function handle(HandleReceivedEvent $command): void
$command->uuid,
$command->type,
new Json($command->payload),
Carbon::createFromTimestamp($command->timestamp)->toDateTimeImmutable(),
$command->timestamp,
$projectId,
)
),
);

$this->dispatcher->dispatch(
Expand All @@ -49,7 +49,7 @@ public function handle(HandleReceivedEvent $command): void
payload: $command->payload,
timestamp: $command->timestamp,
projectId: $projectId,
)
),
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ protected function mapData(ServerRequestInterface $request): array|\JsonSerializ
'uuid' => (string)$this->data->getUuid(),
'type' => $this->data->getType(),
'payload' => $this->data->getPayload(),
'timestamp' => $this->data->getDate()->getTimestamp(),
'timestamp' => $this->data->getTimestamp(),
'project_id' => $this->data->getProjectId(),
];
}
Expand Down
6 changes: 3 additions & 3 deletions app/modules/Events/Interfaces/Http/Controllers/ListAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@
use Spiral\Cqrs\QueryBusInterface;
use Spiral\Router\Annotation\Route;

class ListAction
final class ListAction
{
#[Route(route: 'events', name: 'events.list', methods: 'GET', group: 'api')]
public function __invoke(EventsRequest $request, QueryBusInterface $bus): EventCollection
{
return new EventCollection(
$bus->ask(
new FindEvents(type: $request->type)
)
new FindEvents(type: $request->type),
),
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Modules\Events\Application\Queries;
namespace Modules\Events\Interfaces\Queries;

use App\Application\Commands\CountEvents;
use Modules\Events\Domain\EventRepositoryInterface;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Modules\Events\Application\Queries;
namespace Modules\Events\Interfaces\Queries;

use App\Application\Commands\AskEvents;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Modules\Events\Application\Queries;
namespace Modules\Events\Interfaces\Queries;

use App\Application\Commands\FindEventByUuid;
use App\Application\Exception\EntityNotFoundException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Modules\Events\Application\Queries;
namespace Modules\Events\Interfaces\Queries;

use App\Application\Commands\FindEvents;
use Modules\Events\Domain\EventRepositoryInterface;
Expand Down
21 changes: 6 additions & 15 deletions app/modules/HttpDumps/Application/HttpDumpsBootloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,18 @@

namespace Modules\HttpDumps\Application;

use App\Application\Service\HttpHandler\HandlerRegistryInterface;
use Modules\HttpDumps\EventHandler;
use Modules\HttpDumps\Interfaces\Http\Handler\AnyHttpRequestDump;
use Psr\Container\ContainerInterface;
use Spiral\Boot\Bootloader\Bootloader;

final class HttpDumpsBootloader extends Bootloader
{
protected const SINGLETONS = [
EventHandlerInterface::class => [self::class, 'eventHandler'],
];

public function boot(
HandlerRegistryInterface $registry,
AnyHttpRequestDump $handler
): void {
$registry->register($handler);
}

public function eventHandler(ContainerInterface $container): EventHandlerInterface
public function defineSingletons(): array
{
return new EventHandler($container, []);
return [
EventHandlerInterface::class => static function (ContainerInterface $container): EventHandlerInterface {
return new EventHandler($container, []);
},
];
}
}
9 changes: 1 addition & 8 deletions app/modules/Inspector/Application/InspectorBootloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,9 @@

namespace Modules\Inspector\Application;

use App\Application\Service\HttpHandler\HandlerRegistryInterface;
use Modules\Inspector\Interfaces\Http\Handler\EventHandler;
use Spiral\Boot\Bootloader\Bootloader;

final class InspectorBootloader extends Bootloader
{
public function boot(
HandlerRegistryInterface $registry,
EventHandler $handler
): void {
$registry->register($handler);
}

}
1 change: 0 additions & 1 deletion app/modules/Monolog/Interfaces/TCP/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use App\Application\Commands\HandleReceivedEvent;
use Psr\Log\LoggerInterface;
use Spiral\Cqrs\CommandBusInterface;
use Spiral\Exceptions\ExceptionReporterInterface;
use Spiral\RoadRunner\Tcp\Request;
use Spiral\RoadRunner\Tcp\TcpEvent;
use Spiral\RoadRunnerBridge\Tcp\Response\CloseConnection;
Expand Down
31 changes: 11 additions & 20 deletions app/modules/Profiler/Application/ProfilerBootloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@

namespace Modules\Profiler\Application;

use App\Application\Service\HttpHandler\HandlerRegistryInterface;
use Modules\Profiler\Application\Handlers\CalculateDiffsBetweenEdges;
use Modules\Profiler\Interfaces\Http\Handler\EventHandler as HttpEventHandler;
use Modules\Profiler\Application\Handlers\CleanupEvent;
use Modules\Profiler\Application\Handlers\PrepareEdges;
use Modules\Profiler\Application\Handlers\PreparePeaks;
Expand All @@ -15,24 +13,17 @@

final class ProfilerBootloader extends Bootloader
{
protected const SINGLETONS = [
EventHandlerInterface::class => [self::class, 'eventHandler'],
];

public function boot(
HandlerRegistryInterface $registry,
HttpEventHandler $handler
): void {
$registry->register($handler);
}

public function eventHandler(ContainerInterface $container): EventHandlerInterface
public function defineSingletons(): array
{
return new EventHandler($container, [
PreparePeaks::class,
CalculateDiffsBetweenEdges::class,
PrepareEdges::class,
CleanupEvent::class,
]);
return [
EventHandlerInterface::class => static function (ContainerInterface $container): EventHandlerInterface {
return new EventHandler($container, [
PreparePeaks::class,
CalculateDiffsBetweenEdges::class,
PrepareEdges::class,
CleanupEvent::class,
]);
},
];
}
}
22 changes: 11 additions & 11 deletions app/modules/Ray/Application/RayBootloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,21 @@

final class RayBootloader extends Bootloader
{
protected const SINGLETONS = [
EventHandlerInterface::class => [self::class, 'eventHandler'],
];
public function defineSingletons(): array
{
return [
EventHandlerInterface::class => static function (ContainerInterface $container): EventHandlerInterface {
return new EventHandler($container, [
MergeEventsHandler::class,
]);
},
];
}

public function boot(
HandlerRegistryInterface $registry,
HttpEventHandler $handler
HttpEventHandler $handler,
): void {
$registry->register($handler);
}

public function eventHandler(ContainerInterface $container): EventHandlerInterface
{
return new EventHandler($container, [
MergeEventsHandler::class,
]);
}
}
24 changes: 6 additions & 18 deletions app/modules/Sentry/Application/SentryBootloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,18 @@

namespace Modules\Sentry\Application;

use App\Application\Service\HttpHandler\HandlerRegistryInterface;
use Modules\Sentry\EventHandler;
use Modules\Sentry\Interfaces\Http\Handler\EventHandler as HttpEventHandler;
use Modules\Sentry\Interfaces\Http\Handler\JsEventHandler;
use Psr\Container\ContainerInterface;
use Spiral\Boot\Bootloader\Bootloader;

final class SentryBootloader extends Bootloader
{
protected const SINGLETONS = [
EventHandlerInterface::class => [self::class, 'eventHandler'],
];

public function boot(
HandlerRegistryInterface $registry,
HttpEventHandler $handler,
JsEventHandler $jsHandler
): void {
$registry->register($handler);
$registry->register($jsHandler);
}

public function eventHandler(ContainerInterface $container): EventHandlerInterface
public function defineSingletons(): array
{
return new EventHandler($container, []);
return [
EventHandlerInterface::class => static function (ContainerInterface $container): EventHandlerInterface {
return new EventHandler($container, []);
},
];
}
}
22 changes: 14 additions & 8 deletions app/src/Application/Bootloader/AppBootloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,19 @@

final class AppBootloader extends DomainBootloader
{
protected const SINGLETONS = [
CoreInterface::class => [self::class, 'domainCore'],
];
public function defineSingletons(): array
{
return [
CoreInterface::class => [self::class, 'domainCore'],
];
}

protected const INTERCEPTORS = [
StringToIntParametersInterceptor::class,
UuidParametersConverterInterceptor::class,
JsonResourceInterceptor::class,
];
protected static function defineInterceptors(): array
{
return [
StringToIntParametersInterceptor::class,
UuidParametersConverterInterceptor::class,
JsonResourceInterceptor::class,
];
}
}
Loading