-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathLdapToolsDataCollector.php
More file actions
156 lines (135 loc) · 3.63 KB
/
Copy pathLdapToolsDataCollector.php
File metadata and controls
156 lines (135 loc) · 3.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<?php
/**
* This file is part of the LdapToolsBundle package.
*
* (c) Chad Sikorra <Chad.Sikorra@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace LdapTools\Bundle\LdapToolsBundle\DataCollector;
use LdapTools\Bundle\LdapToolsBundle\Log\LdapProfilerLogger;
use LdapTools\LdapManager;
use LdapTools\Log\LogOperation;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\DataCollector\DataCollector;
/**
* Data Collector for LdapTools.
*
* @author Chad Sikorra <Chad.Sikorra@gmail.com>
*/
class LdapToolsDataCollector extends DataCollector
{
/**
* @var LdapManager
*/
protected $ldap;
/**
* @var LdapProfilerLogger
*/
protected $logger;
/**
* LdapToolsDataCollector constructor.
* @param LdapProfilerLogger $logger
* @param LdapManager|null $ldap
*/
public function __construct(LdapProfilerLogger $logger, LdapManager $ldap = null)
{
$this->ldap = $ldap;
$this->logger = $logger;
$this->reset();
}
/**
* {@inheritdoc}
*/
public function getName()
{
return 'ldaptools';
}
/**
* {@inheritdoc}
*/
public function collect(Request $request, Response $response, \Throwable $exception = null)
{
if (!$this->ldap) {
return;
}
$this->data['domains'] = $this->ldap->getDomains();
$this->data['operations_by_domain'] = [];
foreach ($this->data['domains'] as $domain) {
$this->data['operations_by_domain'][$domain] = $this->addOperationToData(...$this->logger->getOperations($domain));
}
$this->data['operations'] = $this->addOperationToData(...$this->logger->getOperations());
$this->data['errors'] = $this->addOperationToData(...$this->logger->getErrors());
}
/**
* {@inheritdoc}
*/
public function reset()
{
$this->data['domains'] = [];
$this->data['errors'] = [];
$this->data['operations'] = [];
$this->data['operations_by_domain'] = [];
}
/**
* @return array
*/
public function getOperationsByDomain()
{
return $this->data['operations_by_domain'];
}
/**
* @return int
*/
public function getTime()
{
$time = 0;
foreach ($this->data['operations'] as $operation) {
$time += $operation['duration'];
}
return $time;
}
/**
* @return array
*/
public function getOperations()
{
return $this->data['operations'];
}
/**
* @return array
*/
public function getErrors()
{
return $this->data['errors'];
}
/**
* @return string[]
*/
public function getDomains()
{
return $this->data['domains'];
}
/**
* @param \LdapTools\Log\LogOperation[] ...$logs
* @return array
*/
protected function addOperationToData(LogOperation ...$logs)
{
$logData = [];
foreach ($logs as $log) {
$data = [];
$data['data'] = $log->getOperation()->getLogArray();
$data['startTime'] = $log->getStartTime();
$data['stopTime'] = $log->getStopTime();
$data['domain'] = $log->getDomain();
$data['error'] = $log->getError();
$data['name'] = $log->getOperation()->getName();
$data['duration'] = ($data['stopTime'] - $data['startTime']) * 1000;
$logData[] = $data;
}
return $logData;
}
}