Skip to content

Commit

Permalink
Refactor summary data structure
Browse files Browse the repository at this point in the history
  • Loading branch information
vjik committed Jan 25, 2025
1 parent e29bcf3 commit 85759ec
Show file tree
Hide file tree
Showing 23 changed files with 118 additions and 111 deletions.
1 change: 1 addition & 0 deletions src/Collector/CollectorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ interface CollectorInterface
{
/**
* @return string Collector's name.
* @psalm-return non-empty-string
*/
public function getName(): string;

Expand Down
6 changes: 6 additions & 0 deletions src/Collector/CollectorTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

namespace Yiisoft\Yii\Debug\Collector;

/**
* @psalm-require-implements CollectorInterface
*/
trait CollectorTrait
{
private bool $isActive = false;
Expand All @@ -19,6 +22,9 @@ public function shutdown(): void
$this->isActive = false;

Check warning on line 22 in src/Collector/CollectorTrait.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.2-ubuntu-latest

Escaped Mutant for Mutator "FalseValue": --- Original +++ New @@ @@ public function shutdown() : void { $this->reset(); - $this->isActive = false; + $this->isActive = true; } /** * @psalm-return non-empty-string
}

/**
* @psalm-return non-empty-string
*/
public function getName(): string
{
return self::class;
Expand Down
10 changes: 4 additions & 6 deletions src/Collector/Console/CommandCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,10 @@ public function getSummary(): array
}

return [

Check warning on line 120 in src/Collector/Console/CommandCollector.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.2-ubuntu-latest

Escaped Mutant for Mutator "ArrayItemRemoval": --- Original +++ New @@ @@ if ($commandEvent === null) { return []; } - return ['name' => $commandEvent['name'], 'class' => $commandEvent['command'] instanceof Command ? $commandEvent['command']::class : null, 'input' => $commandEvent['input'], 'exitCode' => $commandEvent['exitCode'] ?? self::UNDEFINED_EXIT_CODE]; + return ['class' => $commandEvent['command'] instanceof Command ? $commandEvent['command']::class : null, 'input' => $commandEvent['input'], 'exitCode' => $commandEvent['exitCode'] ?? self::UNDEFINED_EXIT_CODE]; } private function reset() : void {
'command' => [
'name' => $commandEvent['name'],
'class' => $commandEvent['command'] instanceof Command ? $commandEvent['command']::class : null,
'input' => $commandEvent['input'],
'exitCode' => $commandEvent['exitCode'] ?? self::UNDEFINED_EXIT_CODE,
],
'name' => $commandEvent['name'],
'class' => $commandEvent['command'] instanceof Command ? $commandEvent['command']::class : null,
'input' => $commandEvent['input'],
'exitCode' => $commandEvent['exitCode'] ?? self::UNDEFINED_EXIT_CODE,
];
}

Expand Down
20 changes: 9 additions & 11 deletions src/Collector/Console/ConsoleAppInfoCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,17 +74,15 @@ public function getSummary(): array
return [];
}
return [
'console' => [
'php' => [
'version' => PHP_VERSION,
],
'request' => [
'startTime' => $this->requestProcessingTimeStarted,
'processingTime' => $this->requestProcessingTimeStopped - $this->requestProcessingTimeStarted,
],
'memory' => [
'peakUsage' => memory_get_peak_usage(),
],
'php' => [
'version' => PHP_VERSION,
],
'request' => [
'startTime' => $this->requestProcessingTimeStarted,
'processingTime' => $this->requestProcessingTimeStopped - $this->requestProcessingTimeStarted,
],
'memory' => [
'peakUsage' => memory_get_peak_usage(),
],
];
}
Expand Down
6 changes: 3 additions & 3 deletions src/Collector/EventCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use Yiisoft\Yii\Console\Event\ApplicationStartup as ConsoleApplicationStartup;
use Yiisoft\Yii\Http\Event\ApplicationStartup as HttpApplicationStartup;

use function count;

final class EventCollector implements SummaryCollectorInterface
{
use CollectorTrait;
Expand Down Expand Up @@ -53,9 +55,7 @@ public function getSummary(): array
return [];
}
return [
'event' => [
'total' => count($this->events),
],
'total' => count($this->events),
];
}

Expand Down
15 changes: 8 additions & 7 deletions src/Collector/ExceptionCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,15 @@ public function getSummary(): array
if (!$this->isActive()) {
return [];
}
if ($this->exception === null) {
return [];
}
return [
'exception' => $this->exception === null ? [] : [
'class' => $this->exception::class,
'message' => $this->exception->getMessage(),
'file' => $this->exception->getFile(),
'line' => $this->exception->getLine(),
'code' => $this->exception->getCode(),
],
'class' => $this->exception::class,
'message' => $this->exception->getMessage(),
'file' => $this->exception->getFile(),
'line' => $this->exception->getLine(),
'code' => $this->exception->getCode(),
];
}

Expand Down
18 changes: 8 additions & 10 deletions src/Collector/HttpClientCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,17 +89,15 @@ public function getSummary(): array
return [];
}
return [
'http' => [
'count' => array_sum(array_map(static fn (array $requests) => count($requests), $this->requests)),
'totalTime' => array_sum(
array_merge(
...array_map(
static fn (array $entry) => array_column($entry, 'totalTime'),
array_values($this->requests)
)
'count' => array_sum(array_map(static fn (array $requests) => count($requests), $this->requests)),
'totalTime' => array_sum(
array_merge(
...array_map(
static fn (array $entry) => array_column($entry, 'totalTime'),
array_values($this->requests)
)
),
],
)
),
];
}
}
6 changes: 3 additions & 3 deletions src/Collector/LogCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Yiisoft\Yii\Debug\Collector;

use function count;

class LogCollector implements SummaryCollectorInterface
{
use CollectorTrait;
Expand Down Expand Up @@ -50,9 +52,7 @@ public function getSummary(): array
return [];
}
return [
'logger' => [
'total' => count($this->messages),
],
'total' => count($this->messages),
];
}
}
6 changes: 3 additions & 3 deletions src/Collector/ServiceCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Yiisoft\Yii\Debug\Collector;

use function count;

final class ServiceCollector implements SummaryCollectorInterface
{
use CollectorTrait;
Expand Down Expand Up @@ -58,9 +60,7 @@ public function getSummary(): array
return [];
}
return [
'service' => [
'total' => count($this->items),
],
'total' => count($this->items),
];
}

Expand Down
2 changes: 1 addition & 1 deletion src/Collector/Stream/FilesystemStreamCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public function getSummary(): array
return [];
}
return [
'fs_stream' => array_merge(
'streams' => array_merge(
...array_map(
fn (string $operation) => [$operation => count($this->operations[$operation])],
array_keys($this->operations)
Expand Down
2 changes: 1 addition & 1 deletion src/Collector/Stream/HttpStreamCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public function getSummary(): array
return [];
}
return [
'http_stream' => array_merge(
'streams' => array_merge(
...array_map(
fn (string $operation) => [
$operation => count($this->requests[$operation]),
Expand Down
6 changes: 3 additions & 3 deletions src/Collector/VarDumperCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Yiisoft\Yii\Debug\Collector;

use function count;

final class VarDumperCollector implements SummaryCollectorInterface
{
use CollectorTrait;
Expand Down Expand Up @@ -40,9 +42,7 @@ public function getSummary(): array
}

return [
'var-dumper' => [
'total' => count($this->vars),
],
'total' => count($this->vars),
];
}
}
20 changes: 9 additions & 11 deletions src/Collector/Web/WebAppInfoCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,17 +65,15 @@ public function getSummary(): array
return [];
}
return [
'web' => [
'php' => [
'version' => PHP_VERSION,
],
'request' => [
'startTime' => $this->requestProcessingTimeStarted,
'processingTime' => $this->requestProcessingTimeStopped - $this->requestProcessingTimeStarted,
],
'memory' => [
'peakUsage' => memory_get_peak_usage(),
],
'php' => [
'version' => PHP_VERSION,
],
'request' => [
'startTime' => $this->requestProcessingTimeStarted,
'processingTime' => $this->requestProcessingTimeStopped - $this->requestProcessingTimeStarted,
],
'memory' => [
'peakUsage' => memory_get_peak_usage(),
],
];
}
Expand Down
33 changes: 25 additions & 8 deletions src/Debugger.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,19 @@

/**
* Debugger collects data from collectors and stores it in a storage.
*
* @psalm-type TSummary = array{
* id: non-empty-string,
* collectors: list<non-empty-string>,
* summary: array<non-empty-string, array>,
* }
*/
final class Debugger
{
/**
* @var CollectorInterface[] Collectors, indexed by their names.
*
* @psalm-var array<string, CollectorInterface>
* @psalm-var array<non-empty-string, CollectorInterface>
*/
private readonly array $collectors;

Expand All @@ -30,6 +36,12 @@ final class Debugger
*/
private readonly DataNormalizer $dataNormalizer;

/**
* @var string|null ID of the current request. `null` if debugger is not active.
* @psalm-var non-empty-string|null
*/
private ?string $id = null;

/**
* @param StorageInterface $storage The storage to store collected data.
* @param CollectorInterface[] $collectors Collectors to be used.
Expand Down Expand Up @@ -57,11 +69,6 @@ public function __construct(
register_shutdown_function([$this, 'stop']);
}

/**
* @var string|null ID of the current request. `null` if debugger is not active.
*/
private ?string $id = null;

/**
* Returns whether debugger is active.
*
Expand All @@ -78,6 +85,8 @@ public function isActive(): bool
* Throws `LogicException` if debugger is not started. Use {@see isActive()} to check if debugger is active.
*
* @return string ID of the current request.
*
* @psalm-return non-empty-string
*/
public function getId(): string
{
Expand All @@ -95,6 +104,7 @@ public function start(object $event): void
return;
}

/** @var non-empty-string */
$this->id = str_replace('.', '', uniqid('', true));

foreach ($this->collectors as $collector) {
Expand Down Expand Up @@ -163,18 +173,25 @@ private function deactivate(): void
}

/**
* Collects summary data of current request.
* Collects summary data of current request. Structure of the summary data is:
*
* - `id` - ID of the current request,
* - `collectors` - list of collector names used in the current request,
* - `summary` - summary data collected by collectors indexed by collector names.
*
* @psalm-return TSummary
*/
private function collectSummaryData(): array
{
$summaryData = [
'id' => $this->getId(),
'collectors' => array_keys($this->collectors),
'summary' => [],
];

foreach ($this->collectors as $collector) {
if ($collector instanceof SummaryCollectorInterface) {
$summaryData = [...$summaryData, ...$collector->getSummary()];
$summaryData['summary'][$collector->getName()] = $collector->getSummary();
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/Storage/StorageInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ interface StorageInterface
/**
* Read all data from storage
*
* @param string $type type of data being read. Available types:
* @param string $type Type of data being read. Available types:
* - {@see TYPE_SUMMARY}
* - {@see TYPE_DATA}
* - {@see TYPE_OBJECTS}
*
* @return array data from storage
* @return array Data from storage
*
* @psalm-param self::TYPE_* $type
*/
Expand Down
9 changes: 4 additions & 5 deletions tests/Unit/Collector/CommandCollectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,9 @@ protected function checkCollectedData(array $data): void

protected function checkSummaryData(array $data): void
{
$this->assertArrayHasKey('command', $data);
$this->assertArrayHasKey('input', $data['command']);
$this->assertArrayHasKey('class', $data['command']);
$this->assertEquals('test1', $data['command']['input']);
$this->assertEquals(null, $data['command']['class']);
$this->assertArrayHasKey('input', $data);
$this->assertArrayHasKey('class', $data);
$this->assertEquals('test1', $data['input']);
$this->assertEquals(null, $data['class']);
}
}
17 changes: 8 additions & 9 deletions tests/Unit/Collector/ConsoleAppInfoCollectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,14 @@ protected function checkSummaryData(array $data): void
{
parent::checkSummaryData($data);

$this->assertArrayHasKey('console', $data);
$this->assertArrayHasKey('php', $data['console']);
$this->assertArrayHasKey('version', $data['console']['php']);
$this->assertArrayHasKey('request', $data['console']);
$this->assertArrayHasKey('startTime', $data['console']['request']);
$this->assertArrayHasKey('processingTime', $data['console']['request']);
$this->assertArrayHasKey('memory', $data['console']);
$this->assertArrayHasKey('peakUsage', $data['console']['memory']);
$this->assertArrayHasKey('php', $data);
$this->assertArrayHasKey('version', $data['php']);
$this->assertArrayHasKey('request', $data);
$this->assertArrayHasKey('startTime', $data['request']);
$this->assertArrayHasKey('processingTime', $data['request']);
$this->assertArrayHasKey('memory', $data);
$this->assertArrayHasKey('peakUsage', $data['memory']);

$this->assertEquals(PHP_VERSION, $data['console']['php']['version']);
$this->assertEquals(PHP_VERSION, $data['php']['version']);
}
}
Loading

0 comments on commit 85759ec

Please sign in to comment.