1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Facile\MongoDbBundle\DataCollector; |
6
|
|
|
|
7
|
|
|
use Facile\MongoDbBundle\Models\Query; |
8
|
|
|
use Facile\MongoDbBundle\Services\Loggers\DataCollectorLoggerInterface; |
9
|
|
|
use Symfony\Component\HttpFoundation\Request; |
10
|
|
|
use Symfony\Component\HttpFoundation\Response; |
11
|
|
|
use Symfony\Component\HttpKernel\DataCollector\DataCollector; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class MongoDbDataCollector. |
15
|
|
|
* |
16
|
|
|
* @internal |
17
|
|
|
*/ |
18
|
|
|
class MongoDbDataCollector extends DataCollector |
19
|
|
|
{ |
20
|
|
|
const QUERY_KEYWORD = 'queries'; |
21
|
|
|
|
22
|
|
|
const CONNECTION_KEYWORD = 'connections'; |
23
|
|
|
|
24
|
|
|
const TIME_KEYWORD = 'totalTime'; |
25
|
|
|
|
26
|
1 |
|
/** @var DataCollectorLoggerInterface */ |
27
|
|
|
private $logger; |
28
|
1 |
|
|
29
|
1 |
|
/** @var array */ |
30
|
|
|
protected $data; |
31
|
1 |
|
|
32
|
|
|
public function __construct() |
33
|
1 |
|
{ |
34
|
1 |
|
$this->reset(); |
35
|
1 |
|
} |
36
|
1 |
|
|
37
|
|
|
public function reset() |
38
|
1 |
|
{ |
39
|
|
|
$this->data = [ |
40
|
|
|
self::QUERY_KEYWORD => [], |
41
|
|
|
self::TIME_KEYWORD => 0.0, |
42
|
|
|
self::CONNECTION_KEYWORD => [], |
43
|
1 |
|
]; |
44
|
|
|
} |
45
|
1 |
|
|
46
|
1 |
|
/** |
47
|
|
|
* @param DataCollectorLoggerInterface $logger |
48
|
|
|
*/ |
49
|
|
|
public function setLogger(DataCollectorLoggerInterface $logger) |
50
|
|
|
{ |
51
|
1 |
|
$this->logger = $logger; |
52
|
|
|
} |
53
|
1 |
|
|
54
|
|
|
public function collect(Request $request, Response $response, \Exception $exception = null) |
55
|
1 |
|
{ |
56
|
|
|
while ($this->logger->hasLoggedEvents()) { |
57
|
1 |
|
/** @var Query $event */ |
58
|
|
|
$event = $this->logger->getLoggedEvent(); |
59
|
1 |
|
|
60
|
1 |
|
MongoQuerySerializer::serialize($event); |
61
|
|
|
|
62
|
|
|
$this->data[self::QUERY_KEYWORD][] = $event; |
63
|
1 |
|
$this->data[self::TIME_KEYWORD] += $event->getExecutionTime(); |
64
|
1 |
|
} |
65
|
|
|
|
66
|
|
|
$this->data[self::CONNECTION_KEYWORD] = $this->logger->getConnections(); |
67
|
|
|
} |
68
|
|
|
|
69
|
1 |
|
/** |
70
|
|
|
* @return int |
71
|
1 |
|
*/ |
72
|
|
|
public function getQueryCount(): int |
73
|
|
|
{ |
74
|
|
|
return \count($this->data[self::QUERY_KEYWORD]); |
75
|
|
|
} |
76
|
|
|
|
77
|
1 |
|
/** |
78
|
|
|
* @return Query[]|array |
79
|
1 |
|
*/ |
80
|
|
|
public function getQueries(): array |
81
|
|
|
{ |
82
|
|
|
return $this->data[self::QUERY_KEYWORD]; |
83
|
|
|
} |
84
|
|
|
|
85
|
1 |
|
/** |
86
|
|
|
* @return float |
87
|
1 |
|
*/ |
88
|
|
|
public function getTime(): float |
89
|
|
|
{ |
90
|
|
|
return (float) ($this->data[self::TIME_KEYWORD] * 1000); |
91
|
|
|
} |
92
|
|
|
|
93
|
1 |
|
/** |
94
|
|
|
* @return int |
95
|
1 |
|
*/ |
96
|
|
|
public function getConnectionsCount(): int |
97
|
|
|
{ |
98
|
|
|
return \count($this->data[self::CONNECTION_KEYWORD]); |
99
|
|
|
} |
100
|
|
|
|
101
|
1 |
|
/** |
102
|
|
|
* @return array|string[] |
103
|
1 |
|
*/ |
104
|
|
|
public function getConnections(): array |
105
|
|
|
{ |
106
|
|
|
return $this->data[self::CONNECTION_KEYWORD]; |
107
|
|
|
} |
108
|
|
|
|
109
|
1 |
|
/** |
110
|
|
|
* {@inheritdoc} |
111
|
1 |
|
*/ |
112
|
|
|
public function getName() |
113
|
|
|
{ |
114
|
|
|
return 'mongodb'; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|