1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Neo4j\Neo4jBundle\Collector; |
6
|
|
|
|
7
|
|
|
use GraphAware\Common\Cypher\StatementInterface; |
8
|
|
|
use GraphAware\Common\Result\StatementResult as StatementResultInterface; |
9
|
|
|
use GraphAware\Common\Result\StatementStatisticsInterface; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @author Xavier Coureau <[email protected]> |
13
|
|
|
*/ |
14
|
|
|
class QueryLogger implements \Countable |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var int |
18
|
|
|
*/ |
19
|
|
|
private $nbQueries = 0; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var array |
23
|
|
|
*/ |
24
|
|
|
private $statements = []; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var array |
28
|
|
|
*/ |
29
|
|
|
private $statementsHash = []; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param StatementInterface $statement |
33
|
|
|
*/ |
34
|
|
|
public function record(StatementInterface $statement) |
35
|
|
|
{ |
36
|
|
|
$statementText = $statement->text(); |
37
|
|
|
$statementParams = json_encode($statement->parameters()); |
38
|
|
|
$tag = $statement->getTag() ?: -1; |
39
|
|
|
|
40
|
|
|
if (isset($this->statementsHash[$statementText][$statementParams][$tag])) { |
41
|
|
|
return; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
$idx = $this->nbQueries++; |
45
|
|
|
$this->statements[$idx] = [ |
46
|
|
|
'start_time' => microtime(true) * 1000, |
47
|
|
|
'end_time' => microtime(true) * 1000, // same |
48
|
|
|
'nb_results' => 0, |
49
|
|
|
'query' => $statementText, |
50
|
|
|
'parameters' => $statementParams, |
51
|
|
|
'tag' => $statement->getTag(), |
52
|
|
|
'statistics' => [], |
53
|
|
|
]; |
54
|
|
|
$this->statementsHash[$statementText][$statementParams][$tag] = $idx; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param StatementResultInterface $statementResult |
59
|
|
|
*/ |
60
|
|
|
public function finish(StatementResultInterface $statementResult) |
61
|
|
|
{ |
62
|
|
|
$statement = $statementResult->statement(); |
63
|
|
|
$statementText = $statement->text(); |
64
|
|
|
$statementParams = $statement->parameters(); |
65
|
|
|
$encodedParameters = json_encode($statementParams); |
66
|
|
|
$tag = $statement->getTag() ?: -1; |
67
|
|
|
|
68
|
|
|
if (!isset($this->statementsHash[$statementText][$encodedParameters][$tag])) { |
69
|
|
|
$idx = $this->nbQueries++; |
70
|
|
|
$this->statements[$idx]['start_time'] = null; |
71
|
|
|
$this->statementsHash[$idx] = $idx; |
72
|
|
|
} else { |
73
|
|
|
$idx = $this->statementsHash[$statementText][$encodedParameters][$tag]; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$this->statements[$idx] = array_merge($this->statements[$idx], [ |
77
|
|
|
'end_time' => microtime(true) * 1000, |
78
|
|
|
'nb_results' => $statementResult->size(), |
79
|
|
|
'statistics' => $this->statisticsToArray($statementResult->summarize()->updateStatistics()), |
80
|
|
|
]); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* {@inheritdoc} |
85
|
|
|
*/ |
86
|
|
|
public function count() |
87
|
|
|
{ |
88
|
|
|
return $this->nbQueries; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @return array[] |
93
|
|
|
*/ |
94
|
|
|
public function getStatements() |
95
|
|
|
{ |
96
|
|
|
return $this->statements; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @return array |
101
|
|
|
*/ |
102
|
|
|
public function getStatementsHash() |
103
|
|
|
{ |
104
|
|
|
return $this->statementsHash; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @return int |
109
|
|
|
*/ |
110
|
|
|
public function getElapsedTime() |
111
|
|
|
{ |
112
|
|
|
$time = 0; |
113
|
|
|
|
114
|
|
|
foreach ($this->statements as $statement) { |
115
|
|
|
if (!isset($statement['start_time'], $statement['end_time'])) { |
116
|
|
|
continue; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
$time += $statement['end_time'] - $statement['start_time']; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
return $time; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
private function statisticsToArray(StatementStatisticsInterface $statementStatistics) |
126
|
|
|
{ |
127
|
|
|
$data = [ |
128
|
|
|
'contains_updates' => $statementStatistics->containsUpdates(), |
129
|
|
|
'nodes_created' => $statementStatistics->nodesCreated(), |
130
|
|
|
'nodes_deleted' => $statementStatistics->nodesDeleted(), |
131
|
|
|
'relationships_created' => $statementStatistics->relationshipsCreated(), |
132
|
|
|
'relationships_deleted' => $statementStatistics->relationshipsDeleted(), |
133
|
|
|
'properties_set' => $statementStatistics->propertiesSet(), |
134
|
|
|
'labels_added' => $statementStatistics->labelsAdded(), |
135
|
|
|
'labels_removed' => $statementStatistics->labelsRemoved(), |
136
|
|
|
'indexes_added' => $statementStatistics->indexesAdded(), |
137
|
|
|
'indexes_removed' => $statementStatistics->indexesRemoved(), |
138
|
|
|
'constraints_added' => $statementStatistics->constraintsAdded(), |
139
|
|
|
'constraints_removed' => $statementStatistics->constraintsRemoved(), |
140
|
|
|
]; |
141
|
|
|
|
142
|
|
|
return $data; |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|