|
4 | 4 |
|
5 | 5 | namespace Neo4j\Neo4jBundle\Collector;
|
6 | 6 |
|
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; |
12 | 15 |
|
13 | 16 | /**
|
14 | 17 | * @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 | + * } |
15 | 33 | */
|
16 |
| -class QueryLogger implements \Countable |
| 34 | +class QueryLogger implements Countable |
17 | 35 | {
|
18 |
| - /** |
19 |
| - * @var int |
20 |
| - */ |
21 |
| - private $nbQueries = 0; |
22 |
| - |
23 |
| - /** |
24 |
| - * @var array |
25 |
| - */ |
26 |
| - private $statements = []; |
| 36 | + private int $nbQueries = 0; |
27 | 37 |
|
28 | 38 | /**
|
29 |
| - * @var array |
| 39 | + * @var array<StatementInfo> |
30 | 40 | */
|
31 |
| - private $statementsHash = []; |
| 41 | + private array $statements = []; |
32 | 42 |
|
33 |
| - public function record(StatementInterface $statement) |
| 43 | + public function record(Statement $statement): void |
34 | 44 | {
|
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); |
43 | 47 |
|
44 |
| - $idx = $this->nbQueries++; |
45 |
| - $this->statements[$idx] = [ |
| 48 | + $this->statements[] = [ |
46 | 49 | 'start_time' => microtime(true) * 1000,
|
47 | 50 | 'query' => $statementText,
|
48 | 51 | 'parameters' => $statementParams,
|
49 |
| - 'tag' => $statement->getTag(), |
50 | 52 |
|
51 | 53 | // Add dummy data in case we never run logException or finish
|
52 | 54 | 'end_time' => microtime(true) * 1000, // same
|
53 | 55 | 'nb_results' => 0,
|
54 | 56 | 'statistics' => [],
|
55 | 57 | 'scheme' => '',
|
56 | 58 | 'success' => false,
|
57 |
| - 'exceptionCode' => 0, |
| 59 | + 'exceptionCode' => '', |
58 | 60 | 'exceptionMessage' => '',
|
59 | 61 | ];
|
60 |
| - $this->statementsHash[$statementText][$statementParams][$tag] = $idx; |
61 | 62 | }
|
62 | 63 |
|
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 |
64 | 69 | {
|
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(), |
89 | 78 | 'success' => true,
|
90 | 79 | ]);
|
91 | 80 | }
|
92 | 81 |
|
93 |
| - public function reset() |
| 82 | + public function reset(): void |
94 | 83 | {
|
95 |
| - $this->nbQueries = 0; |
96 | 84 | $this->statements = [];
|
97 |
| - $this->statementsHash = []; |
98 | 85 | }
|
99 | 86 |
|
100 |
| - public function logException(Neo4jExceptionInterface $exception) |
| 87 | + public function logException(Neo4jException $exception): void |
101 | 88 | {
|
102 |
| - $idx = $this->nbQueries - 1; |
| 89 | + $classification = explode('.', $exception->getNeo4jCode())[1] ?? ''; |
| 90 | + $idx = count($this->statements) - 1; |
103 | 91 | $this->statements[$idx] = array_merge($this->statements[$idx], [
|
104 | 92 | '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(), |
107 | 95 | 'success' => false,
|
108 | 96 | ]);
|
109 | 97 | }
|
110 | 98 |
|
111 | 99 | /**
|
112 | 100 | * {@inheritdoc}
|
113 | 101 | */
|
114 |
| - public function count() |
| 102 | + public function count(): int |
115 | 103 | {
|
116 | 104 | return $this->nbQueries;
|
117 | 105 | }
|
118 | 106 |
|
119 | 107 | /**
|
120 |
| - * @return array[] |
| 108 | + * @return array<StatementInfo> |
121 | 109 | */
|
122 |
| - public function getStatements() |
| 110 | + public function getStatements(): array |
123 | 111 | {
|
124 | 112 | return $this->statements;
|
125 | 113 | }
|
126 | 114 |
|
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 |
139 | 116 | {
|
140 | 117 | $time = 0;
|
141 | 118 |
|
142 | 119 | foreach ($this->statements as $statement) {
|
143 |
| - if (!isset($statement['start_time'], $statement['end_time'])) { |
144 |
| - continue; |
145 |
| - } |
146 |
| - |
147 | 120 | $time += $statement['end_time'] - $statement['start_time'];
|
148 | 121 | }
|
149 | 122 |
|
150 | 123 | return $time;
|
151 | 124 | }
|
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 |
| - } |
175 | 125 | }
|
0 commit comments