Skip to content

Commit dc44e47

Browse files
committed
greenbarred tests
1 parent 534e49c commit dc44e47

19 files changed

+443
-380
lines changed

Collector/Neo4jDataCollector.php

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,14 @@
77
use Symfony\Component\HttpFoundation\Request;
88
use Symfony\Component\HttpFoundation\Response;
99
use Symfony\Component\HttpKernel\DataCollector\DataCollector;
10+
use Throwable;
1011

1112
/**
1213
* @author Xavier Coureau <[email protected]>
1314
*/
1415
final class Neo4jDataCollector extends DataCollector
1516
{
16-
/**
17-
* @var QueryLogger
18-
*/
19-
private $queryLogger;
17+
private QueryLogger $queryLogger;
2018

2119
public function __construct(QueryLogger $logger)
2220
{
@@ -26,17 +24,17 @@ public function __construct(QueryLogger $logger)
2624
/**
2725
* {@inheritdoc}
2826
*/
29-
public function collect(Request $request, Response $response, \Throwable $exception = null)
27+
public function collect(Request $request, Response $response, Throwable $exception = null): void
3028
{
3129
$this->data['time'] = $this->queryLogger->getElapsedTime();
3230
$this->data['nb_queries'] = count($this->queryLogger);
3331
$this->data['statements'] = $this->queryLogger->getStatements();
34-
$this->data['failed_statements'] = array_filter($this->queryLogger->getStatements(), function ($statement) {
35-
return !isset($statement['success']) || !$statement['success'];
32+
$this->data['failed_statements'] = array_filter($this->queryLogger->getStatements(), static function ($statement) {
33+
return empty($statement['success']);
3634
});
3735
}
3836

39-
public function reset()
37+
public function reset(): void
4038
{
4139
$this->data = [];
4240
$this->queryLogger->reset();
@@ -45,7 +43,7 @@ public function reset()
4543
/**
4644
* @return int
4745
*/
48-
public function getQueryCount()
46+
public function getQueryCount(): int
4947
{
5048
return $this->data['nb_queries'];
5149
}
@@ -55,7 +53,7 @@ public function getQueryCount()
5553
*
5654
* @return array
5755
*/
58-
public function getStatements()
56+
public function getStatements(): array
5957
{
6058
return $this->data['statements'];
6159
}
@@ -65,31 +63,31 @@ public function getStatements()
6563
*
6664
* @return array
6765
*/
68-
public function getFailedStatements()
66+
public function getFailedStatements(): array
6967
{
7068
return $this->data['failed_statements'];
7169
}
7270

7371
/**
7472
* @return float
7573
*/
76-
public function getTime()
74+
public function getTime(): float
7775
{
7876
return $this->data['time'];
7977
}
8078

8179
/**
8280
* @return float
8381
*/
84-
public function getTimeForQuery()
82+
public function getTimeForQuery(): float
8583
{
8684
return $this->data['time'];
8785
}
8886

8987
/**
9088
* @return string
9189
*/
92-
public function getName()
90+
public function getName(): string
9391
{
9492
return 'neo4j';
9593
}

Collector/QueryLogger.php

Lines changed: 55 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -4,172 +4,122 @@
44

55
namespace Neo4j\Neo4jBundle\Collector;
66

7-
use GraphAware\Bolt\Result\Result;
8-
use GraphAware\Common\Cypher\StatementInterface;
9-
use GraphAware\Common\Result\StatementResult as StatementResultInterface;
10-
use GraphAware\Common\Result\StatementStatisticsInterface;
11-
use GraphAware\Neo4j\Client\Exception\Neo4jExceptionInterface;
7+
use Countable;
8+
use Exception;
9+
use Laudis\Neo4j\Databags\Statement;
10+
use Laudis\Neo4j\Databags\SummarizedResult;
11+
use Laudis\Neo4j\Exception\Neo4jException;
12+
use Laudis\Neo4j\Types\CypherList;
13+
use Laudis\Neo4j\Types\CypherMap;
14+
use function iterator_to_array;
1215

1316
/**
1417
* @author Xavier Coureau <[email protected]>
18+
*
19+
* @psalm-import-type OGMTypes from \Laudis\Neo4j\Formatter\OGMFormatter
20+
*
21+
* @psalm-type StatementInfo = array{
22+
* start_time: float,
23+
* query: string,
24+
* parameters: string,
25+
* end_time: float,
26+
* nb_results: int,
27+
* statistics: array,
28+
* scheme: string,
29+
* success: bool,
30+
* exceptionCode: string,
31+
* exceptionMessage: string
32+
* }
1533
*/
16-
class QueryLogger implements \Countable
34+
class QueryLogger implements Countable
1735
{
18-
/**
19-
* @var int
20-
*/
21-
private $nbQueries = 0;
22-
23-
/**
24-
* @var array
25-
*/
26-
private $statements = [];
36+
private int $nbQueries = 0;
2737

2838
/**
29-
* @var array
39+
* @var array<StatementInfo>
3040
*/
31-
private $statementsHash = [];
41+
private array $statements = [];
3242

33-
public function record(StatementInterface $statement)
43+
public function record(Statement $statement): void
3444
{
35-
$statementText = $statement->text();
36-
$statementParams = json_encode($statement->parameters());
37-
$tag = $statement->getTag() ?: -1;
38-
39-
// Make sure we do not record the same statement twice
40-
if (isset($this->statementsHash[$statementText][$statementParams][$tag])) {
41-
return;
42-
}
45+
$statementText = $statement->getText();
46+
$statementParams = json_encode($statement->getParameters(), JSON_THROW_ON_ERROR);
4347

44-
$idx = $this->nbQueries++;
45-
$this->statements[$idx] = [
48+
$this->statements[] = [
4649
'start_time' => microtime(true) * 1000,
4750
'query' => $statementText,
4851
'parameters' => $statementParams,
49-
'tag' => $statement->getTag(),
5052

5153
// Add dummy data in case we never run logException or finish
5254
'end_time' => microtime(true) * 1000, // same
5355
'nb_results' => 0,
5456
'statistics' => [],
5557
'scheme' => '',
5658
'success' => false,
57-
'exceptionCode' => 0,
59+
'exceptionCode' => '',
5860
'exceptionMessage' => '',
5961
];
60-
$this->statementsHash[$statementText][$statementParams][$tag] = $idx;
6162
}
6263

63-
public function finish(StatementResultInterface $statementResult)
64+
/**
65+
* @param SummarizedResult<CypherList<CypherMap<OGMTypes>>> $result
66+
* @throws Exception
67+
*/
68+
public function finish(SummarizedResult $result): void
6469
{
65-
$scheme = 'Http';
66-
if ($statementResult instanceof Result) {
67-
$scheme = 'Bolt';
68-
}
69-
70-
$statement = $statementResult->statement();
71-
$statementText = $statement->text();
72-
$statementParams = $statement->parameters();
73-
$encodedParameters = json_encode($statementParams);
74-
$tag = $statement->getTag() ?: -1;
75-
76-
if (!isset($this->statementsHash[$statementText][$encodedParameters][$tag])) {
77-
$idx = $this->nbQueries++;
78-
$this->statements[$idx]['start_time'] = null;
79-
$this->statementsHash[$idx] = $idx;
80-
} else {
81-
$idx = $this->statementsHash[$statementText][$encodedParameters][$tag];
82-
}
83-
84-
$this->statements[$idx] = array_merge($this->statements[$idx], [
85-
'end_time' => microtime(true) * 1000,
86-
'nb_results' => $statementResult->size(),
87-
'statistics' => $this->statisticsToArray($statementResult->summarize()->updateStatistics()),
88-
'scheme' => $scheme,
70+
$id = count($this->statements) - 1;
71+
72+
$summary = $result->getSummary();
73+
$this->statements[$id] = array_merge($this->statements[$id], [
74+
'end_time' => $summary->getResultConsumedAfter(),
75+
'nb_results' => $result->getResult()->count(),
76+
'statistics' => iterator_to_array($summary->getCounters()->getIterator(), true),
77+
'scheme' => $summary->getServerInfo()->getAddress()->getScheme(),
8978
'success' => true,
9079
]);
9180
}
9281

93-
public function reset()
82+
public function reset(): void
9483
{
95-
$this->nbQueries = 0;
9684
$this->statements = [];
97-
$this->statementsHash = [];
9885
}
9986

100-
public function logException(Neo4jExceptionInterface $exception)
87+
public function logException(Neo4jException $exception): void
10188
{
102-
$idx = $this->nbQueries - 1;
89+
$classification = explode('.', $exception->getNeo4jCode())[1] ?? '';
90+
$idx = count($this->statements) - 1;
10391
$this->statements[$idx] = array_merge($this->statements[$idx], [
10492
'end_time' => microtime(true) * 1000,
105-
'exceptionCode' => method_exists($exception, 'classification') ? $exception->classification() : '',
106-
'exceptionMessage' => method_exists($exception, 'getMessage') ? $exception->getMessage() : '',
93+
'exceptionCode' => $classification,
94+
'exceptionMessage' => $exception->getErrors()[0]->getMessage(),
10795
'success' => false,
10896
]);
10997
}
11098

11199
/**
112100
* {@inheritdoc}
113101
*/
114-
public function count()
102+
public function count(): int
115103
{
116104
return $this->nbQueries;
117105
}
118106

119107
/**
120-
* @return array[]
108+
* @return array<StatementInfo>
121109
*/
122-
public function getStatements()
110+
public function getStatements(): array
123111
{
124112
return $this->statements;
125113
}
126114

127-
/**
128-
* @return array
129-
*/
130-
public function getStatementsHash()
131-
{
132-
return $this->statementsHash;
133-
}
134-
135-
/**
136-
* @return int
137-
*/
138-
public function getElapsedTime()
115+
public function getElapsedTime(): float
139116
{
140117
$time = 0;
141118

142119
foreach ($this->statements as $statement) {
143-
if (!isset($statement['start_time'], $statement['end_time'])) {
144-
continue;
145-
}
146-
147120
$time += $statement['end_time'] - $statement['start_time'];
148121
}
149122

150123
return $time;
151124
}
152-
153-
private function statisticsToArray(?StatementStatisticsInterface $statementStatistics)
154-
{
155-
if (!$statementStatistics) {
156-
return [];
157-
}
158-
$data = [
159-
'contains_updates' => $statementStatistics->containsUpdates(),
160-
'nodes_created' => $statementStatistics->nodesCreated(),
161-
'nodes_deleted' => $statementStatistics->nodesDeleted(),
162-
'relationships_created' => $statementStatistics->relationshipsCreated(),
163-
'relationships_deleted' => $statementStatistics->relationshipsDeleted(),
164-
'properties_set' => $statementStatistics->propertiesSet(),
165-
'labels_added' => $statementStatistics->labelsAdded(),
166-
'labels_removed' => $statementStatistics->labelsRemoved(),
167-
'indexes_added' => $statementStatistics->indexesAdded(),
168-
'indexes_removed' => $statementStatistics->indexesRemoved(),
169-
'constraints_added' => $statementStatistics->constraintsAdded(),
170-
'constraints_removed' => $statementStatistics->constraintsRemoved(),
171-
];
172-
173-
return $data;
174-
}
175125
}

Collector/Twig/Neo4jResultExtension.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace Neo4j\Neo4jBundle\Collector\Twig;
66

7-
use GraphAware\Neo4j\Client\Formatter\Type\Node;
7+
use Laudis\Neo4j\Types\Node;
88
use Twig\Extension\AbstractExtension;
99
use Twig\TwigFilter;
1010

@@ -16,9 +16,9 @@ class Neo4jResultExtension extends AbstractExtension
1616
/**
1717
* {@inheritdoc}
1818
*
19-
* @return array
19+
* @return array<TwigFilter>
2020
*/
21-
public function getFilters()
21+
public function getFilters(): array
2222
{
2323
return [
2424
new TwigFilter('neo4jResult', [$this, 'getType']),
@@ -43,8 +43,8 @@ public function getName(): string
4343
*/
4444
private function doGetType($object, bool $recursive): string
4545
{
46-
if ($object instanceof \Laudis\Neo4j\Types\Node) {
47-
return sprintf('%s: %s', $object->getId(), implode(', ', $object->getLabels()->toArray()));
46+
if ($object instanceof Node) {
47+
return sprintf('%s: %s', $object->getId(), $object->getLabels()->join(', '));
4848
}
4949

5050
if (is_array($object) && $recursive) {
@@ -59,6 +59,6 @@ private function doGetType($object, bool $recursive): string
5959
return sprintf('[%s]', implode(', ', $ret));
6060
}
6161

62-
return is_object($object) ? get_class($object) : gettype($object);
62+
return get_debug_type($object);
6363
}
6464
}

0 commit comments

Comments
 (0)