Skip to content

Commit 5bb693c

Browse files
committed
implemented proper profiling per driver
1 parent fa7806b commit 5bb693c

File tree

11 files changed

+200
-280
lines changed

11 files changed

+200
-280
lines changed

src/Collector/Neo4jDataCollector.php

Lines changed: 24 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -4,66 +4,49 @@
44

55
namespace Neo4j\Neo4jBundle\Collector;
66

7+
use Laudis\Neo4j\Databags\ResultSummary;
8+
use Neo4j\Neo4jBundle\EventSubscriber\ProfileSubscriber;
79
use Symfony\Component\HttpFoundation\Request;
810
use Symfony\Component\HttpFoundation\Response;
911
use Symfony\Component\HttpKernel\DataCollector\DataCollector;
1012
use Throwable;
1113

12-
/**
13-
* @author Xavier Coureau <[email protected]>
14-
*/
1514
final class Neo4jDataCollector extends DataCollector
1615
{
1716
public function __construct(
18-
private QueryLogger $queryLogger
17+
private ProfileSubscriber $subscriber
1918
) {}
2019

2120
public function collect(Request $request, Response $response, Throwable $exception = null): void
2221
{
23-
$this->data['time'] = $this->queryLogger->getElapsedTime();
24-
$this->data['nb_queries'] = count($this->queryLogger);
25-
$this->data['statements'] = $this->queryLogger->getStatements();
26-
$this->data['failed_statements'] = array_filter($this->queryLogger->getStatements(), static function ($statement) {
27-
return empty($statement['success']);
28-
});
29-
}
30-
31-
public function reset(): void
32-
{
33-
$this->data = [];
34-
$this->queryLogger->reset();
35-
}
36-
37-
public function getQueryCount(): int
38-
{
39-
return $this->data['nb_queries'];
40-
}
22+
$this->data['successful_statements'] = array_map(
23+
static fn (ResultSummary $summary) => $summary->toArray(),
24+
$this->subscriber->getProfiledSummaries()
25+
);
4126

42-
/**
43-
* Return all statements, successful and not successful.
44-
*/
45-
public function getStatements(): array
46-
{
47-
return $this->data['statements'];
48-
}
27+
$this->data['failed_statements'] = array_map(
28+
static fn (array $x) => [
29+
'statement' => $x['statement']->toArray(),
30+
'exception' => [
31+
'code' => $x['exception']->getErrors()[0]->getCode(),
32+
'message' => $x['exception']->getErrors()[0]->getMessage(),
33+
'classification' => $x['exception']->getErrors()[0]->getClassification(),
34+
'category' => $x['exception']->getErrors()[0]->getCategory(),
35+
'title' => $x['exception']->getErrors()[0]->getTitle(),
36+
],
37+
'alias' => $x['alias']
4938

50-
/**
51-
* Return not successful statements.
52-
*/
53-
public function getFailedStatements(): array
54-
{
55-
return $this->data['failed_statements'];
39+
],
40+
$this->subscriber->getProfiledFailures()
41+
);
5642
}
5743

58-
public function getTime(): float
44+
public function reset(): void
5945
{
60-
return $this->data['time'];
46+
$this->data = [];
47+
$this->subscriber->reset();
6148
}
6249

63-
public function getTimeForQuery(): float
64-
{
65-
return $this->data['time'];
66-
}
6750

6851
public function getName(): string
6952
{

src/Collector/QueryLogger.php

Lines changed: 0 additions & 129 deletions
This file was deleted.

src/DependencyInjection/Configuration.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
* verify_peer?: bool|null,
2121
* }
2222
* @psalm-type DriverConfigArray = array{
23+
* profiling?: bool,
2324
* acquire_connection_timeout?: int|null,
2425
* user_agent?: string|null,
2526
* pool_size?: int|null,
@@ -78,6 +79,10 @@ public function getConfigTreeBuilder(): TreeBuilder
7879
->info('The alias for this driver. Default is "default".')
7980
->defaultValue('default')
8081
->end()
82+
->scalarNode('profiling')
83+
->info('Enable profiling for requests on this driver')
84+
->defaultValue('false')
85+
->end()
8186
->scalarNode('dsn')
8287
->info('The DSN for the driver. Default is "bolt://localhost:7687".')
8388
->defaultValue('bolt://localhost:7687')

src/EventHandler.php

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,17 @@
44

55
namespace Neo4j\Neo4jBundle;
66

7+
use Laudis\Neo4j\Common\Uri;
8+
use Laudis\Neo4j\Databags\DatabaseInfo;
9+
use Laudis\Neo4j\Databags\ResultSummary;
10+
use Laudis\Neo4j\Databags\ServerInfo;
711
use Laudis\Neo4j\Databags\Statement;
812
use Laudis\Neo4j\Databags\SummarizedResult;
13+
use Laudis\Neo4j\Databags\SummaryCounters;
14+
use Laudis\Neo4j\Enum\ConnectionProtocol;
15+
use Laudis\Neo4j\Enum\QueryTypeEnum;
916
use Laudis\Neo4j\Exception\Neo4jException;
1017
use Laudis\Neo4j\Types\CypherList;
11-
use Laudis\Neo4j\Types\CypherMap;
1218
use Neo4j\Neo4jBundle\Events\FailureEvent;
1319
use Neo4j\Neo4jBundle\Events\PostRunEvent;
1420
use Neo4j\Neo4jBundle\Events\PreRunEvent;
@@ -24,31 +30,48 @@ public function __construct(?EventDispatcherInterface $dispatcher)
2430
}
2531

2632
/**
27-
* @param callable():CypherList<SummarizedResult<CypherMap>> $runHandler
28-
* @param iterable<Statement> $statements
33+
* @template T
2934
*
30-
* @return CypherList<SummarizedResult<CypherMap>>
35+
* @param callable(Statement):SummarizedResult<T> $runHandler
36+
* @param iterable<Statement> $statements
37+
*
38+
* @return SummarizedResult<T>
3139
*/
32-
public function handle(callable $runHandler, iterable $statements): CypherList
40+
public function handle(callable $runHandler, Statement $statement, string|null $alias): SummarizedResult
3341
{
3442
if (null === $this->dispatcher) {
35-
return $runHandler();
43+
return $runHandler($statement);
3644
}
3745

38-
$this->dispatcher->dispatch(new PreRunEvent($statements), PreRunEvent::EVENT_ID);
46+
$this->dispatcher->dispatch(new PreRunEvent($alias, $statement), PreRunEvent::EVENT_ID);
3947

4048
try {
41-
$tbr = $runHandler();
42-
$this->dispatcher->dispatch(new PostRunEvent($tbr), PostRunEvent::EVENT_ID);
49+
$tbr = $runHandler($statement);
50+
$this->dispatcher->dispatch(new PostRunEvent($alias, $tbr->getSummary()), PostRunEvent::EVENT_ID);
4351
} catch (Neo4jException $e) {
44-
$event = new FailureEvent($e);
52+
$event = new FailureEvent($alias, $statement, $e);
4553
$event = $this->dispatcher->dispatch($event, FailureEvent::EVENT_ID);
4654

4755
if ($event->shouldThrowException()) {
4856
throw $e;
4957
}
58+
59+
$summary = new ResultSummary(
60+
new SummaryCounters(),
61+
new DatabaseInfo('n/a'),
62+
new CypherList([]),
63+
null,
64+
null,
65+
$statement,
66+
QueryTypeEnum::READ_ONLY(),
67+
0,
68+
0,
69+
new ServerInfo(Uri::create(''), ConnectionProtocol::BOLT_V5(), 'n/a'),
70+
);
71+
72+
$tbr = new SummarizedResult($summary);
5073
}
5174

52-
return new CypherList();
75+
return $tbr;
5376
}
5477
}

src/EventSubscriber/LoggerSubscriber.php

Lines changed: 0 additions & 49 deletions
This file was deleted.

0 commit comments

Comments
 (0)