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

Commit

Permalink
Merge pull request #100 from js361014/performance
Browse files Browse the repository at this point in the history
Added performance module
  • Loading branch information
butschster authored Oct 28, 2022
2 parents 9de3cf1 + 158c594 commit 6578ce4
Show file tree
Hide file tree
Showing 64 changed files with 1,303 additions and 77 deletions.
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
<?php

declare(strict_types=1);

namespace App\Commands;

use App\Contracts\Query\Query;

final class FindAllEvents implements Query
abstract class AskEvents implements Query
{
// TODO: use readonly property
public function __construct(
public ?string $type = null,
public ?int $projectId = null,
public ?int $transactionId = null,
) {
}
}
11 changes: 11 additions & 0 deletions app/Application/Commands/CountEvents.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace App\Commands;

use App\Contracts\Query\Query;

final class CountEvents extends AskEvents implements Query
{
}
9 changes: 9 additions & 0 deletions app/Application/Commands/FindAllTransactions.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 FindAllTransactions implements Query
{
}
21 changes: 21 additions & 0 deletions app/Application/Commands/FindEvents.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace App\Commands;

use App\Contracts\Query\Query;

final class FindEvents extends AskEvents implements Query
{
// TODO: use readonly property
public function __construct(
public ?string $type = null,
public ?int $projectId = null,
public ?int $transactionId = null,
public ?int $offset = null,
public ?int $limit = null,
) {
parent::__construct($this->type, $this->projectId, $this->transactionId);
}
}
13 changes: 13 additions & 0 deletions app/Application/Commands/FindTransactionByName.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 FindTransactionByName implements Query
{
public function __construct(
public string $name
) {
}
}
4 changes: 3 additions & 1 deletion app/Application/Commands/HandleReceivedEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ public function __construct(
public int $projectId,
public string $type,
public array $payload,
public bool $sendToConsole = false
public bool $sendToConsole = false,
public ?int $transactionId = null,
) {
$this->uuid = Uuid::generate();
$this->timestamp = time();
Expand All @@ -26,6 +27,7 @@ public function toArray(): array
{
return [
'projectId' => $this->projectId,
'transactionId' => $this->transactionId,
'type' => $this->type,
'payload' => $this->payload,
'uuid' => (string) $this->uuid,
Expand Down
19 changes: 15 additions & 4 deletions app/Infrastructure/CycleOrm/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,22 @@

class Repository extends BaseRepository
{
public function findAll(array $scope = [], array $orderBy = []): Collection
public function findAll(array $scope = [], array $orderBy = [], $limit = null, $offset = null): Collection
{
return $this->newCollection(
$this->select()->where($scope)->orderBy($orderBy)->fetchAll()
);
$select = $this->select()->where($scope)->orderBy($orderBy);
if (! is_null($limit)) {
$select = $select->limit($limit);
}
if (! is_null($offset)) {
$select = $select->offset($offset);
}

return $this->newCollection($select->fetchAll());
}

public function count(array $scope = []): int
{
return $this->select()->where($scope)->count();
}

private function newCollection(array $items): Collection
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ public function __invoke(Command $command): void
$scope = [];
if ($command->type) {
$scope['type'] = $command->type;
} else {
$scope['type'] = ['<>' => 'sentryTransaction'];
}

// TODO: make more optimized
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public function __construct(
public DateTimeImmutable $date,
public array $payload,
public int $projectId,
public ?int $transactionId,
) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ public function handle(Command $command): void
$command->type,
new Json($command->payload),
$command->date,
$command->projectId
$command->projectId,
$command->transactionId
)
);

$this->entityManager->run();
}

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

$this->dispatcher->dispatch(
new EventWasReceived(
$command->projectId, $command->uuid, $command->type, $command->payload, $command->timestamp, $command->sendToConsole
$command->projectId, $command->uuid, $command->type, $command->payload, $command->timestamp, $command->sendToConsole, $command->transactionId
)
);
}
Expand Down
24 changes: 24 additions & 0 deletions app/Modules/Events/Application/Queries/CountEvents/Handler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

namespace Modules\Events\Application\Queries\CountEvents;

use App\Commands\CountEvents;
use App\Contracts\Query\QueryHandler;
use Modules\Events\Application\Queries\EventsHandler;
use Modules\Events\Domain\EventRepository;

class Handler extends EventsHandler implements QueryHandler
{
public function __construct(
private EventRepository $events
) {
}

#[\App\Attributes\QueryBus\QueryHandler]
public function __invoke(CountEvents $query): int
{
return $this->events->count(self::getScopeFromFindEvents($query));
}
}
26 changes: 26 additions & 0 deletions app/Modules/Events/Application/Queries/EventsHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Modules\Events\Application\Queries;

use App\Commands\AskEvents;

abstract class EventsHandler
{
protected static function getScopeFromFindEvents(AskEvents $query): array
{
$scope = [];
if ($query->type) {
$scope['type'] = $query->type;
} else {
$scope['type'] = ['<>' => 'sentryTransaction'];
}
if ($query->projectId) {
$scope['project_id'] = $query->projectId;
}
if ($query->transactionId) {
$scope['transaction_id'] = $query->transactionId;
}

return $scope;
}
}
36 changes: 0 additions & 36 deletions app/Modules/Events/Application/Queries/FindAllEvents/Handler.php

This file was deleted.

32 changes: 32 additions & 0 deletions app/Modules/Events/Application/Queries/FindEvents/Handler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace Modules\Events\Application\Queries\FindEvents;

use App\Commands\FindEvents;
use App\Contracts\Query\QueryHandler;
use Modules\Events\Application\Queries\EventsHandler;
use Modules\Events\Application\Resources\EventCollection;
use Modules\Events\Domain\EventRepository;

class Handler extends EventsHandler implements QueryHandler
{
public function __construct(
private EventRepository $events
) {
}

#[\App\Attributes\QueryBus\QueryHandler]
public function __invoke(FindEvents $query): iterable
{
return EventCollection::make(
$this->events->findAll(
self::getScopeFromFindEvents($query),
['date' => 'asc'],
$query->limit,
$query->offset,
)
);
}
}
2 changes: 2 additions & 0 deletions app/Modules/Events/Application/Resources/EventResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ public function toArray($request)
'type' => $this->getType(),
'payload' => $this->getPayload()->toArray(),
'timestamp' => $this->getDate()->getTimestamp(),
'projectId' => $this->getProjectId(),
'transactionId' => $this->getTransactionId(),
];
}
}
10 changes: 9 additions & 1 deletion app/Modules/Events/Domain/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ public function __construct(
private DateTimeImmutable $date,

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

#[Column(type: 'integer', nullable: true)]
private ?int $transactionId
) {
}

Expand Down Expand Up @@ -59,4 +62,9 @@ public function getProjectId(): int
{
return $this->projectId;
}

public function getTransactionId(): ?int
{
return $this->transactionId;
}
}
33 changes: 28 additions & 5 deletions app/Modules/Events/Interfaces/Http/Controllers/ListAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

namespace Modules\Events\Interfaces\Http\Controllers;

use App\Commands\FindAllEvents;
use App\Commands\CountEvents;
use App\Commands\FindEvents;
use App\Contracts\Query\QueryBus;
use Illuminate\Http\Request;
use Inertia\Inertia;
Expand All @@ -16,7 +17,7 @@
class ListAction extends Controller
{
#[Get(uri: '/', name: 'events', middleware: 'auth')]
public function eventList(Request $request, QueryBus $bus, ActionMap $actionMap, ?string $type = null, ?int $projectId = null)
public function eventList(Request $request, QueryBus $bus, ActionMap $actionMap, ?string $type = null, ?int $projectId = null, ?int $transactionId = null)
{
$action = 'Events';
if ($type) {
Expand All @@ -27,16 +28,38 @@ public function eventList(Request $request, QueryBus $bus, ActionMap $actionMap,
}
}

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

if ($action == 'SentryTransaction/Index') {
$props = array_merge($props, [
'eventsCount' => $bus->ask(new CountEvents(
type: $type, projectId: $projectId, transactionId: $transactionId
)),
'type' => $type,
'projectId' => $projectId,
'transactionId' => $transactionId,
]);
} else {
$props = array_merge($props, [
'events' => $bus->ask(new FindEvents(type: $type, projectId: $projectId)),
]);
}

return Inertia::render($action, $props);
}

#[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, $projectId);
}

#[Get(uri: '/events/transactions/{transactionId}/{projectId}', name: 'events.transactions', middleware: 'auth')]
public function transactionEventList(Request $request, QueryBus $bus, ActionMap $actionMap, int $transactionId, int $projectId): \Inertia\Response
{
return $this->eventList($request, $bus, $actionMap, 'sentryTransaction', $projectId, $transactionId);
}
}
Loading

0 comments on commit 6578ce4

Please sign in to comment.