1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Neo4j\Neo4jBundle\Collector; |
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; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @author Xavier Coureau <[email protected]> |
15
|
|
|
*/ |
16
|
|
|
class QueryLogger implements \Countable |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var int |
20
|
|
|
*/ |
21
|
|
|
private $nbQueries = 0; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var array |
25
|
|
|
*/ |
26
|
|
|
private $statements = []; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var array |
30
|
|
|
*/ |
31
|
|
|
private $statementsHash = []; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param StatementInterface $statement |
35
|
|
|
*/ |
36
|
|
|
public function record(StatementInterface $statement) |
37
|
|
|
{ |
38
|
|
|
$statementText = $statement->text(); |
39
|
|
|
$statementParams = json_encode($statement->parameters()); |
40
|
|
|
$tag = $statement->getTag() ?: -1; |
41
|
|
|
|
42
|
|
|
// Make sure we do not record the same statement twice |
43
|
|
|
if (isset($this->statementsHash[$statementText][$statementParams][$tag])) { |
44
|
|
|
return; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
$idx = $this->nbQueries++; |
48
|
|
|
$this->statements[$idx] = [ |
49
|
|
|
'start_time' => microtime(true) * 1000, |
50
|
|
|
'end_time' => microtime(true) * 1000, // same |
51
|
|
|
'nb_results' => 0, |
52
|
|
|
'query' => $statementText, |
53
|
|
|
'parameters' => $statementParams, |
54
|
|
|
'tag' => $statement->getTag(), |
55
|
|
|
'statistics' => [], |
56
|
|
|
]; |
57
|
|
|
$this->statementsHash[$statementText][$statementParams][$tag] = $idx; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param StatementResultInterface $statementResult |
62
|
|
|
*/ |
63
|
|
|
public function finish(StatementResultInterface $statementResult) |
64
|
|
|
{ |
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, |
89
|
|
|
'success' => true, |
90
|
|
|
]); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param Neo4jExceptionInterface $exception |
95
|
|
|
*/ |
96
|
|
|
public function logException(Neo4jExceptionInterface $exception) |
97
|
|
|
{ |
98
|
|
|
$idx = $this->nbQueries - 1; |
99
|
|
|
$this->statements[$idx] = array_merge($this->statements[$idx], [ |
100
|
|
|
'end_time' => microtime(true) * 1000, |
101
|
|
|
'exceptionCode' => method_exists($exception, 'classification') ? $exception->classification() : '', |
102
|
|
|
'exceptionMessage' => method_exists($exception, 'getMessage') ? $exception->getMessage() : '', |
103
|
|
|
'success' => false, |
104
|
|
|
]); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* {@inheritdoc} |
109
|
|
|
*/ |
110
|
|
|
public function count() |
111
|
|
|
{ |
112
|
|
|
return $this->nbQueries; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @return array[] |
117
|
|
|
*/ |
118
|
|
|
public function getStatements() |
119
|
|
|
{ |
120
|
|
|
return $this->statements; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @return array |
125
|
|
|
*/ |
126
|
|
|
public function getStatementsHash() |
127
|
|
|
{ |
128
|
|
|
return $this->statementsHash; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @return int |
133
|
|
|
*/ |
134
|
|
|
public function getElapsedTime() |
135
|
|
|
{ |
136
|
|
|
$time = 0; |
137
|
|
|
|
138
|
|
|
foreach ($this->statements as $statement) { |
139
|
|
|
if (!isset($statement['start_time'], $statement['end_time'])) { |
140
|
|
|
continue; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
$time += $statement['end_time'] - $statement['start_time']; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
return $time; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
private function statisticsToArray(StatementStatisticsInterface $statementStatistics) |
150
|
|
|
{ |
151
|
|
|
$data = [ |
152
|
|
|
'contains_updates' => $statementStatistics->containsUpdates(), |
153
|
|
|
'nodes_created' => $statementStatistics->nodesCreated(), |
154
|
|
|
'nodes_deleted' => $statementStatistics->nodesDeleted(), |
155
|
|
|
'relationships_created' => $statementStatistics->relationshipsCreated(), |
156
|
|
|
'relationships_deleted' => $statementStatistics->relationshipsDeleted(), |
157
|
|
|
'properties_set' => $statementStatistics->propertiesSet(), |
158
|
|
|
'labels_added' => $statementStatistics->labelsAdded(), |
159
|
|
|
'labels_removed' => $statementStatistics->labelsRemoved(), |
160
|
|
|
'indexes_added' => $statementStatistics->indexesAdded(), |
161
|
|
|
'indexes_removed' => $statementStatistics->indexesRemoved(), |
162
|
|
|
'constraints_added' => $statementStatistics->constraintsAdded(), |
163
|
|
|
'constraints_removed' => $statementStatistics->constraintsRemoved(), |
164
|
|
|
]; |
165
|
|
|
|
166
|
|
|
return $data; |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|