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
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @author Xavier Coureau <[email protected]> |
12
|
|
|
*/ |
13
|
|
|
class QueryLogger implements \Countable |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var int |
17
|
|
|
*/ |
18
|
|
|
private $nbQueries = 0; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var array |
22
|
|
|
*/ |
23
|
|
|
private $statements = []; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var array |
27
|
|
|
*/ |
28
|
|
|
private $statementsHash = []; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param StatementInterface $statement |
32
|
|
|
*/ |
33
|
|
|
public function record(StatementInterface $statement) |
34
|
|
|
{ |
35
|
|
|
$statementText = $statement->text(); |
36
|
|
|
$statementParams = json_encode($statement->parameters()); |
37
|
|
|
$tag = $statement->getTag() ?: -1; |
38
|
|
|
|
39
|
|
|
if (isset($this->statementsHash[$statementText][$statementParams][$tag])) { |
40
|
|
|
return; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
$idx = $this->nbQueries++; |
44
|
|
|
$this->statements[$idx]['start_time'] = microtime(true) * 1000; |
45
|
|
|
$this->statementsHash[$statementText][$statementParams][$tag] = $idx; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param StatementResultInterface $statementResult |
50
|
|
|
*/ |
51
|
|
|
public function finish(StatementResultInterface $statementResult) |
52
|
|
|
{ |
53
|
|
|
$statement = $statementResult->statement(); |
54
|
|
|
$statementText = $statement->text(); |
55
|
|
|
$statementParams = $statement->parameters(); |
56
|
|
|
$encodedParameters = json_encode($statementParams); |
57
|
|
|
$tag = $statement->getTag() ?: -1; |
58
|
|
|
|
59
|
|
|
if (!isset($this->statementsHash[$statementText][$encodedParameters][$tag])) { |
60
|
|
|
$idx = $this->nbQueries++; |
61
|
|
|
$this->statements[$idx]['start_time'] = null; |
62
|
|
|
$this->statementsHash[$idx] = $idx; |
63
|
|
|
} else { |
64
|
|
|
$idx = $this->statementsHash[$statementText][$encodedParameters][$tag]; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$this->statements[$idx] += [ |
68
|
|
|
'end_time' => microtime(true) * 1000, |
69
|
|
|
'query' => $statementText, |
70
|
|
|
'parameters' => $statementParams, |
71
|
|
|
'tag' => $statement->getTag(), |
72
|
|
|
'nb_results' => $statementResult->size(), |
73
|
|
|
]; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* {@inheritdoc} |
78
|
|
|
*/ |
79
|
|
|
public function count() |
80
|
|
|
{ |
81
|
|
|
return $this->nbQueries; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @return array[] |
86
|
|
|
*/ |
87
|
|
|
public function getStatements() |
88
|
|
|
{ |
89
|
|
|
return $this->statements; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @return array |
94
|
|
|
*/ |
95
|
|
|
public function getStatementsHash() |
96
|
|
|
{ |
97
|
|
|
return $this->statementsHash; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @return int |
102
|
|
|
*/ |
103
|
|
|
public function getElapsedTime() |
104
|
|
|
{ |
105
|
|
|
$time = 0; |
106
|
|
|
|
107
|
|
|
foreach ($this->statements as $statement) { |
108
|
|
|
if (!isset($statement['start_time'], $statement['end_time'])) { |
109
|
|
|
continue; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
$time += $statement['end_time'] - $statement['start_time']; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
return $time; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|