Skip to content

Commit 3a0ed58

Browse files
committed
Added ability to search by time when using the profiler
1 parent f9297c1 commit 3a0ed58

File tree

13 files changed

+142
-27
lines changed

13 files changed

+142
-27
lines changed

src/Symfony/Bundle/WebProfilerBundle/Controller/ProfilerController.php

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -224,12 +224,16 @@ public function searchBarAction(Request $request)
224224
$ip =
225225
$method =
226226
$url =
227+
$start =
228+
$end =
227229
$limit =
228230
$token = null;
229231
} else {
230232
$ip = $session->get('_profiler_search_ip');
231233
$method = $session->get('_profiler_search_method');
232234
$url = $session->get('_profiler_search_url');
235+
$start = $session->get('_profiler_search_start');
236+
$end = $session->get('_profiler_search_end');
233237
$limit = $session->get('_profiler_search_limit');
234238
$token = $session->get('_profiler_search_token');
235239
}
@@ -239,6 +243,8 @@ public function searchBarAction(Request $request)
239243
'ip' => $ip,
240244
'method' => $method,
241245
'url' => $url,
246+
'start' => $start,
247+
'end' => $end,
242248
'limit' => $limit,
243249
)));
244250
}
@@ -260,17 +266,21 @@ public function searchResultsAction(Request $request, $token)
260266
$ip = $request->query->get('ip');
261267
$method = $request->query->get('method');
262268
$url = $request->query->get('url');
269+
$start = $request->query->get('start');
270+
$end = $request->query->get('end');
263271
$limit = $request->query->get('limit');
264272

265273
return new Response($this->twig->render('@WebProfiler/Profiler/results.html.twig', array(
266-
'token' => $token,
267-
'profile' => $profile,
268-
'tokens' => $this->profiler->find($ip, $url, $limit, $method),
269-
'ip' => $ip,
270-
'method' => $method,
271-
'url' => $url,
272-
'limit' => $limit,
273-
'panel' => null,
274+
'token' => $token,
275+
'profile' => $profile,
276+
'tokens' => $this->profiler->find($ip, $url, $start, $end, $limit, $method),
277+
'ip' => $ip,
278+
'method' => $method,
279+
'url' => $url,
280+
'start' => $start,
281+
'end' => $end,
282+
'limit' => $limit,
283+
'panel' => null,
274284
)));
275285
}
276286

@@ -288,13 +298,17 @@ public function searchAction(Request $request)
288298
$ip = preg_replace('/[^:\d\.]/', '', $request->query->get('ip'));
289299
$method = $request->query->get('method');
290300
$url = $request->query->get('url');
301+
$start = $request->query->get('start');
302+
$end = $request->query->get('end');
291303
$limit = $request->query->get('limit');
292304
$token = $request->query->get('token');
293305

294306
if (null !== $session = $request->getSession()) {
295307
$session->set('_profiler_search_ip', $ip);
296308
$session->set('_profiler_search_method', $method);
297309
$session->set('_profiler_search_url', $url);
310+
$session->set('_profiler_search_start', $start);
311+
$session->set('_profiler_search_end', $end);
298312
$session->set('_profiler_search_limit', $limit);
299313
$session->set('_profiler_search_token', $token);
300314
}
@@ -303,13 +317,15 @@ public function searchAction(Request $request)
303317
return new RedirectResponse($this->generator->generate('_profiler', array('token' => $token)));
304318
}
305319

306-
$tokens = $this->profiler->find($ip, $url, $limit, $method);
320+
$tokens = $this->profiler->find($ip, $url, $start, $end, $limit, $method);
307321

308322
return new RedirectResponse($this->generator->generate('_profiler_search_results', array(
309323
'token' => $tokens ? $tokens[0]['token'] : 'empty',
310324
'ip' => $ip,
311325
'method' => $method,
312326
'url' => $url,
327+
'start' => $start,
328+
'end' => $end,
313329
'limit' => $limit,
314330
)));
315331
}

src/Symfony/Bundle/WebProfilerBundle/Resources/views/Profiler/search.html.twig

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@
2020
<label for="token">Token</label>
2121
<input type="text" name="token" id="token" value="{{ token }}" />
2222
<div class="clear-fix"></div>
23+
<label for="token">From</label>
24+
<input type="text" name="start" id="start" value="{{ start }}" />
25+
<div class="clear-fix"></div>
26+
<label for="token">Until</label>
27+
<input type="text" name="end" id="end" value="{{ end }}" />
28+
<div class="clear-fix"></div>
2329
<label for="limit">Limit</label>
2430
<select name="limit" id="limit">
2531
{% for l in [10, 50, 100] %}

src/Symfony/Component/HttpKernel/Profiler/BaseMemcacheProfilerStorage.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public function __construct($dsn, $username = '', $password = '', $lifetime = 86
4040
/**
4141
* {@inheritdoc}
4242
*/
43-
public function find($ip, $url, $limit, $method)
43+
public function find($ip, $url, $limit, $method, $start = null, $end = null)
4444
{
4545
$indexName = $this->getIndexName();
4646

@@ -52,6 +52,14 @@ public function find($ip, $url, $limit, $method)
5252
$profileList = explode("\n", $indexContent);
5353
$result = array();
5454

55+
if (null === $start) {
56+
$start = 0;
57+
}
58+
59+
if (null === $end) {
60+
$end = time();
61+
}
62+
5563
foreach ($profileList as $item) {
5664

5765
if ($limit === 0) {
@@ -64,7 +72,9 @@ public function find($ip, $url, $limit, $method)
6472

6573
list($itemToken, $itemIp, $itemMethod, $itemUrl, $itemTime, $itemParent) = explode("\t", $item, 6);
6674

67-
if ($ip && false === strpos($itemIp, $ip) || $url && false === strpos($itemUrl, $url) || $method && false === strpos($itemMethod, $method)) {
75+
$itemTime = (int)$itemTime;
76+
77+
if ($ip && false === strpos($itemIp, $ip) || $url && false === strpos($itemUrl, $url) || $method && false === strpos($itemMethod, $method) || $start > $itemTime || $end < $itemTime) {
6878
continue;
6979
}
7080

src/Symfony/Component/HttpKernel/Profiler/FileProfilerStorage.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public function __construct($dsn)
4646
/**
4747
* {@inheritdoc}
4848
*/
49-
public function find($ip, $url, $limit, $method)
49+
public function find($ip, $url, $limit, $method, $start = null, $end = null)
5050
{
5151
$file = $this->getIndexFilename();
5252

@@ -57,6 +57,14 @@ public function find($ip, $url, $limit, $method)
5757
$file = fopen($file, 'r');
5858
fseek($file, 0, SEEK_END);
5959

60+
if (null === $start) {
61+
$start = 0;
62+
}
63+
64+
if (null === $end) {
65+
$end = time();
66+
}
67+
6068
$result = array();
6169

6270
while ($limit > 0) {
@@ -72,7 +80,9 @@ public function find($ip, $url, $limit, $method)
7280

7381
list($csvToken, $csvIp, $csvMethod, $csvUrl, $csvTime, $csvParent) = str_getcsv($line);
7482

75-
if ($ip && false === strpos($csvIp, $ip) || $url && false === strpos($csvUrl, $url) || $method && false === strpos($csvMethod, $method)) {
83+
$csvTime = (int)$csvTime;
84+
85+
if ($ip && false === strpos($csvIp, $ip) || $url && false === strpos($csvUrl, $url) || $method && false === strpos($csvMethod, $method) || $start > $csvTime || $end < $csvTime) {
7686
continue;
7787
}
7888

src/Symfony/Component/HttpKernel/Profiler/MongoDbProfilerStorage.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,17 @@ public function __construct($dsn, $username = '', $password = '', $lifetime = 86
3434
/**
3535
* {@inheritdoc}
3636
*/
37-
public function find($ip, $url, $limit, $method)
37+
public function find($ip, $url, $limit, $method, $start = null, $end = null)
3838
{
39-
$cursor = $this->getMongo()->find($this->buildQuery($ip, $url, $method), array('_id', 'parent', 'ip', 'method', 'url', 'time'))->sort(array('time' => -1))->limit($limit);
39+
if (null === $start) {
40+
$start = 0;
41+
}
42+
43+
if (null === $end) {
44+
$end = time();
45+
}
46+
47+
$cursor = $this->getMongo()->find($this->buildQuery($ip, $url, $method, $start, $end), array('_id', 'parent', 'ip', 'method', 'url', 'time'))->sort(array('time' => -1))->limit($limit);
4048

4149
$tokens = array();
4250
foreach ($cursor as $profile) {
@@ -161,10 +169,12 @@ protected function cleanup()
161169
* @param string $ip
162170
* @param string $url
163171
* @param string $method
172+
* @param int $start
173+
* @param int $end
164174
*
165175
* @return array
166176
*/
167-
private function buildQuery($ip, $url, $method)
177+
private function buildQuery($ip, $url, $method, $start, $end)
168178
{
169179
$query = array();
170180

src/Symfony/Component/HttpKernel/Profiler/MysqlProfilerStorage.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ protected function initDb()
4444
/**
4545
* {@inheritdoc}
4646
*/
47-
protected function buildCriteria($ip, $url, $limit, $method)
47+
protected function buildCriteria($ip, $url, $start, $end, $limit, $method)
4848
{
4949
$criteria = array();
5050
$args = array();
@@ -64,6 +64,12 @@ protected function buildCriteria($ip, $url, $limit, $method)
6464
$args[':method'] = $method;
6565
}
6666

67+
$criteria[] = 'time >= :start';
68+
$args[':start'] = $start;
69+
70+
$criteria[] = 'time <= :end';
71+
$args[':end'] = $end;
72+
6773
return array($criteria, $args);
6874
}
6975
}

src/Symfony/Component/HttpKernel/Profiler/PdoProfilerStorage.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,17 @@ public function __construct($dsn, $username = '', $password = '', $lifetime = 86
4444
/**
4545
* {@inheritdoc}
4646
*/
47-
public function find($ip, $url, $limit, $method)
47+
public function find($ip, $url, $limit, $method, $start = null, $end = null)
4848
{
49-
list($criteria, $args) = $this->buildCriteria($ip, $url, $limit, $method);
49+
if (null === $start) {
50+
$start = 0;
51+
}
52+
53+
if (null === $end) {
54+
$end = time();
55+
}
56+
57+
list($criteria, $args) = $this->buildCriteria($ip, $url, $start, $end, $limit, $method);
5058

5159
$criteria = $criteria ? 'WHERE '.implode(' AND ', $criteria) : '';
5260

@@ -122,12 +130,14 @@ public function purge()
122130
*
123131
* @param string $ip The IP
124132
* @param string $url The URL
133+
* @param string $start The start period to search from
134+
* @param string $end The end period to search to
125135
* @param string $limit The maximum number of tokens to return
126136
* @param string $method The request method
127137
*
128138
* @return array An array with (criteria, args)
129139
*/
130-
abstract protected function buildCriteria($ip, $url, $limit, $method);
140+
abstract protected function buildCriteria($ip, $url, $start, $end, $limit, $method);
131141

132142
/**
133143
* Initializes the database

src/Symfony/Component/HttpKernel/Profiler/Profile.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,10 @@ public function setUrl($url)
159159
*/
160160
public function getTime()
161161
{
162+
if (null === $this->time) {
163+
return 0;
164+
}
165+
162166
return $this->time;
163167
}
164168

src/Symfony/Component/HttpKernel/Profiler/Profiler.php

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,12 +163,34 @@ public function import($data)
163163
* @param string $url The URL
164164
* @param string $limit The maximum number of tokens to return
165165
* @param string $method The request method
166+
* @param int $start The start period to search from
167+
* @param int $end The end period to search to
166168
*
167169
* @return array An array of tokens
168170
*/
169-
public function find($ip, $url, $limit, $method)
171+
public function find($ip, $url, $limit, $method, $start, $end)
170172
{
171-
return $this->storage->find($ip, $url, $limit, $method);
173+
if ($start != '' && null !== $start) {
174+
if (is_integer($start)) {
175+
$start = '@' . $start;
176+
}
177+
} else {
178+
$start = '@0';
179+
}
180+
$start = new \DateTime($start);
181+
$start = $start->getTimestamp();
182+
183+
if ($end != '' && null !== $end) {
184+
if (is_integer($end)) {
185+
$end = '@' . $end;
186+
}
187+
} else {
188+
$end = 'now';
189+
}
190+
$end = new \DateTime($end);
191+
$end = $end->getTimestamp();
192+
193+
return $this->storage->find($ip, $url, $limit, $method, $start, $end);
172194
}
173195

174196
/**

src/Symfony/Component/HttpKernel/Profiler/ProfilerStorageInterface.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,12 @@ interface ProfilerStorageInterface
2525
* @param string $url The URL
2626
* @param string $limit The maximum number of tokens to return
2727
* @param string $method The request method
28+
* @param int $start The start period to search from
29+
* @param int $end The end period to search to
2830
*
2931
* @return array An array of tokens
3032
*/
31-
public function find($ip, $url, $limit, $method);
33+
public function find($ip, $url, $limit, $method, $start = null, $end = null);
3234

3335
/**
3436
* Reads data associated with the given token.

0 commit comments

Comments
 (0)