Skip to content

Commit

Permalink
feat(frontend): create structure of the CallGraph response
Browse files Browse the repository at this point in the history
  • Loading branch information
roxblnfk committed Jul 6, 2024
1 parent fc60022 commit ebd8188
Show file tree
Hide file tree
Showing 8 changed files with 170 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/Module/Frontend/Module/Profiler/ApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ trait ApiController
],
),
]
public function profilerTop(string $uuid, #[QueryParam] string $metric = ''): Message\TopFunctions
public function profilerTop(string $uuid, #[QueryParam] string $metric = 'wt'): Message\TopFunctions
{
$event = $this->eventsStorage->get($uuid) ?? throw new \RuntimeException('Event not found.');

Expand Down Expand Up @@ -66,7 +66,7 @@ public function profilerCallGraph(
$event?->payload instanceof ProfilerPayload or throw new \RuntimeException('Invalid payload type.');
/** @var Event<ProfilerPayload> $event */

return $this->mapper->callGraph($event);
return $this->mapper->callGraph($event, $threshold, $percentage, $metric);
}

#[RegexpRoute(Method::Get, '#^api/profiler/(?<uuid>[a-f0-9-]++)/flame-chart$#i')]
Expand Down
20 changes: 17 additions & 3 deletions src/Module/Frontend/Module/Profiler/Mapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Buggregator\Trap\Module\Frontend\Module\Profiler;

use Buggregator\Trap\Handler\Router\Attribute\QueryParam;
use Buggregator\Trap\Module\Frontend\Event;
use Buggregator\Trap\Module\Frontend\Module\Profiler\Message\CallGraph;
use Buggregator\Trap\Module\Frontend\Module\Profiler\Message\FlameChart;
Expand Down Expand Up @@ -122,9 +123,22 @@ functions: $top,
/**
* @param Event<ProfilerPayload> $event
*/
public function callGraph(Event $event): CallGraph
{
return new CallGraph();
public function callGraph(
Event $event,
float $threshold = 1,
float $percentage = 15,
string $metric = 'wt',
): CallGraph {
return new CallGraph(
toolbar: new CallGraph\Toolbar([
new CallGraph\Button('CPU', 'cpu', 'CPU time (ms)'),
new CallGraph\Button('Wall time', 'wt', 'Wall time (ms)'),
new CallGraph\Button('Memory', 'mu', 'Memory usage (bytes)'),
new CallGraph\Button('Peak memory', 'pmu', 'Peak memory usage (bytes)'),
]),
nodes: [],
edges: [],
);
}

/**
Expand Down
13 changes: 12 additions & 1 deletion src/Module/Frontend/Module/Profiler/Message/CallGraph.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,22 @@
*/
final class CallGraph implements \JsonSerializable
{
/**
* @param list<CallGraph\Node> $nodes
* @param list<CallGraph\Edge> $edges
*/
public function __construct(
private readonly CallGraph\Toolbar $toolbar,
private readonly array $nodes,
private readonly array $edges,
) {}

public function jsonSerialize(): array
{
return [];
return [
'toolbar' => $this->toolbar,
'nodes' => $this->nodes,
'edges' => $this->edges,
];
}
}
26 changes: 26 additions & 0 deletions src/Module/Frontend/Module/Profiler/Message/CallGraph/Button.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace Buggregator\Trap\Module\Frontend\Module\Profiler\Message\CallGraph;

/**
* @internal
*/
final class Button implements \JsonSerializable
{
public function __construct(
public readonly string $label,
public readonly string $metric,
public readonly string $description,
) {}

public function jsonSerialize(): array
{
return [
'label' => $this->label,
'metric' => $this->metric,
'description' => $this->description,
];
}
}
30 changes: 30 additions & 0 deletions src/Module/Frontend/Module/Profiler/Message/CallGraph/Edge.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace Buggregator\Trap\Module\Frontend\Module\Profiler\Message\CallGraph;

/**
* @internal
*/
final class Edge implements \JsonSerializable
{
public function __construct(
public readonly string $color,
public readonly string $label,
public readonly string $source,
public readonly string $target,
) {}

public function jsonSerialize(): array
{
return [
'data' => [
'color' => $this->color,
'label' => $this->label,
'source' => $this->source,
'target' => $this->target,
],
];
}
}
24 changes: 24 additions & 0 deletions src/Module/Frontend/Module/Profiler/Message/CallGraph/Metrics.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

namespace Buggregator\Trap\Module\Frontend\Module\Profiler\Message\CallGraph;

/**
* @internal
*/
final class Metrics implements \JsonSerializable
{
public function __construct(
private readonly int $cost,
private readonly float $percents,
) {}

public function jsonSerialize(): array
{
return [
'cost' => $this->cost,
'percents' => $this->percents,
];
}
}
36 changes: 36 additions & 0 deletions src/Module/Frontend/Module/Profiler/Message/CallGraph/Node.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace Buggregator\Trap\Module\Frontend\Module\Profiler\Message\CallGraph;

use Buggregator\Trap\Module\Profiler\Struct\Cost;

/**
* @internal
*/
final class Node implements \JsonSerializable
{
public function __construct(
public readonly string $id,
public readonly string $name,
public readonly Cost $cost,
public readonly Metrics $metrics,
public readonly string $color,
public readonly string $textColor,
) {}

public function jsonSerialize(): array
{
return [
'data' => [
'color' => $this->color,
'cost' => $this->cost,
'id' => $this->id,
'metrics' => $this->metrics,
'name' => $this->name,
'textColor' => $this->textColor,
],
];
}
}
23 changes: 23 additions & 0 deletions src/Module/Frontend/Module/Profiler/Message/CallGraph/Toolbar.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace Buggregator\Trap\Module\Frontend\Module\Profiler\Message\CallGraph;

/**
* @internal
*/
final class Toolbar implements \JsonSerializable
{
/**
* @param list<Button> $buttons
*/
public function __construct(
private array $buttons,
) {}

public function jsonSerialize(): array
{
return $this->buttons;
}
}

0 comments on commit ebd8188

Please sign in to comment.