Skip to content

Commit ae22ba9

Browse files
committed
fix: Add recursive toArray, and add stopwatch events
1 parent ecf6c76 commit ae22ba9

File tree

4 files changed

+46
-27
lines changed

4 files changed

+46
-27
lines changed

src/Collector/Neo4jDataCollector.php

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -29,30 +29,21 @@ public function __construct(
2929

3030
public function collect(Request $request, Response $response, ?\Throwable $exception = null): void
3131
{
32+
$t = $this;
3233
$profiledSummaries = $this->subscriber->getProfiledSummaries();
33-
$successfulStatements = array_map(
34-
static function (string $key, mixed $value) {
35-
if ('result' !== $key && /* Is always array */ is_array($value)) {
36-
return [
37-
...$value,
38-
'status' => 'success',
39-
];
34+
$successfulStatements = [];
35+
foreach ($profiledSummaries as $summary) {
36+
$statement = ['status' => 'success'];
37+
foreach ($summary as $key => $value) {
38+
if (!is_array($value) && !is_object($value)) {
39+
$statement[$key] = $value;
40+
continue;
4041
}
4142

42-
return array_map(
43-
static function (mixed $obj) {
44-
if (is_object($obj) && method_exists($obj, 'toArray')) {
45-
return $obj->toArray();
46-
}
47-
48-
return $obj;
49-
},
50-
$value['result']->toArray()
51-
);
52-
},
53-
array_keys($profiledSummaries),
54-
array_values($profiledSummaries)
55-
);
43+
$statement[$key] = $t->recursiveToArray($value);
44+
}
45+
$successfulStatements[] = $statement;
46+
}
5647

5748
$failedStatements = array_map(
5849
static fn (array $x) => [
@@ -95,6 +86,7 @@ public function getName(): string
9586
return 'neo4j';
9687
}
9788

89+
/** @api */
9890
public function getStatements(): array
9991
{
10092
return $this->data['statements'];
@@ -116,11 +108,13 @@ public function getFailedStatements(): array
116108
);
117109
}
118110

111+
/** @api */
119112
public function getFailedStatementsCount(): array
120113
{
121114
return $this->data['failed_statements_count'];
122115
}
123116

117+
/** @api */
124118
public function getSuccessfulStatementsCount(): array
125119
{
126120
return $this->data['successful_statements_count'];
@@ -135,4 +129,20 @@ public static function getTemplate(): ?string
135129
{
136130
return '@Neo4j/web_profiler.html.twig';
137131
}
132+
133+
private function recursiveToArray(mixed $obj): mixed
134+
{
135+
if (is_array($obj)) {
136+
return array_map(
137+
fn (mixed $x) => $this->recursiveToArray($x),
138+
$obj
139+
);
140+
}
141+
142+
if (is_object($obj) && method_exists($obj, 'toArray')) {
143+
return $obj->toArray();
144+
}
145+
146+
return $obj;
147+
}
138148
}

src/EventHandler.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,18 @@
1818
use Neo4j\Neo4jBundle\Event\FailureEvent;
1919
use Neo4j\Neo4jBundle\Event\PostRunEvent;
2020
use Neo4j\Neo4jBundle\Event\PreRunEvent;
21+
use Symfony\Component\Stopwatch\Stopwatch;
2122
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
2223

2324
class EventHandler
2425
{
2526
private ?EventDispatcherInterface $dispatcher;
2627

27-
public function __construct(?EventDispatcherInterface $dispatcher, private readonly string $alias)
28-
{
28+
public function __construct(
29+
?EventDispatcherInterface $dispatcher,
30+
private readonly string $alias,
31+
private readonly Stopwatch $stopwatch
32+
) {
2933
$this->dispatcher = $dispatcher;
3034
}
3135

@@ -46,13 +50,17 @@ public function handle(callable $runHandler, Statement $statement, ?string $alia
4650
$time = new \DateTimeImmutable('now', new \DateTimeZone(date_default_timezone_get()));
4751
$this->dispatcher->dispatch(new PreRunEvent($alias, $statement, $time), PreRunEvent::EVENT_ID);
4852

53+
$stopWatchName = sprintf('neo4j.%s.query', $alias ?? $this->alias);
4954
try {
55+
$this->stopwatch->start($stopWatchName);
5056
$tbr = $runHandler($statement);
57+
$this->stopwatch->stop($stopWatchName);
5158
$this->dispatcher->dispatch(
5259
new PostRunEvent($alias ?? $this->alias, $tbr->getSummary(), $time),
5360
PostRunEvent::EVENT_ID
5461
);
5562
} catch (Neo4jException $e) {
63+
$this->stopwatch->stop($stopWatchName);
5664
/** @noinspection PhpUnhandledExceptionInspection */
5765
$time = new \DateTimeImmutable('now', new \DateTimeZone(date_default_timezone_get()));
5866
$event = new FailureEvent($alias ?? $this->alias, $statement, $e, $time);

src/Resources/views/web_profiler.html.twig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@
8686

8787
<div id="neo4j-details-{{ idx }}">
8888
<div>
89-
<strong class="font-normal text-small">Parameters</strong>: {{ statement.result.parameters|default([])|yaml_encode }}
89+
<strong class="font-normal text-small">Parameters</strong>: {{ statement.result.statement.parameters|default([])|json_encode }}
9090
</div>
9191
<div>
9292
<strong class="font-normal text-small">Tag</strong>: {{ statement.result.tag|default('N/A')|yaml_encode }}
@@ -99,7 +99,7 @@
9999
<strong class="font-normal text-small">Scheme</strong>: {{ statement.result.scheme|default('N/A') }}
100100
</div>
101101
<div>
102-
<strong class="font-normal text-small">Statistics</strong>: {{ statement|yaml_encode }}
102+
<strong class="font-normal text-small">Statistics</strong>: {{ statement|json_encode }}
103103
</div>
104104
{# {% else %}#}
105105
{# <div>#}

tests/App/Controller/TestController.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
77
use Symfony\Component\HttpFoundation\Response;
88
use Symfony\Component\HttpKernel\Profiler\Profiler;
9+
use Symfony\Component\Stopwatch\Stopwatch;
910

1011
class TestController extends AbstractController
1112
{
@@ -14,10 +15,10 @@ public function __construct(
1415
) {
1516
}
1617

17-
public function __invoke(Profiler $profiler): Response
18+
public function __invoke(Profiler $profiler, Stopwatch $stopwatch): Response
1819
{
1920
// Successful statement
20-
$this->client->run('MATCH (n) RETURN n');
21+
$this->client->run('MATCH (n {foo: $bar}) RETURN n', ['bar' => 'baz']);
2122
try {
2223
// Failing statement
2324
$this->client->run('MATCH (n) {x: $x}', ['x' => 1]);

0 commit comments

Comments
 (0)