Skip to content

[HttpKernel] [WebProfilerBundle] added HTTP status to profiler search result #13034

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/Symfony/Bundle/WebProfilerBundle/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
CHANGELOG
=========

2.7.0
-----

* [BC BREAK] if you are using a DB to store profiles, the table must be dropped
* added the HTTP status code to profiles

2.3.0
-----

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<th scope="col">Method</th>
<th scope="col">URL</th>
<th scope="col">Time</th>
<th scope="col">Status</th>
</tr>
</thead>
<tbody>
Expand All @@ -22,6 +23,13 @@
<td>{{ elements.method }}</td>
<td>{{ elements.url }}</td>
<td>{{ elements.time|date('r') }}</td>
<td>
{% if elements.status_code is defined and elements.status_code %}
{{ elements.status_code }}
{% else %}
unknown
{% endif %}
</td>
</tr>
{% endfor %}
</tbody>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,66 @@ public function testReturns404onTokenNotFound()
$response = $controller->toolbarAction(Request::create('/_wdt/notFound'), 'notFound');
$this->assertEquals(404, $response->getStatusCode());
}

public function testSearchResult()
{
$urlGenerator = $this->getMock('Symfony\Component\Routing\Generator\UrlGeneratorInterface');
$twig = $this->getMock('Twig_Environment');
$profiler = $this
->getMockBuilder('Symfony\Component\HttpKernel\Profiler\Profiler')
->disableOriginalConstructor()
->getMock();

$controller = new ProfilerController($urlGenerator, $profiler, $twig, array());

$tokens = array(
array(
'token' => 'token1',
'ip' => '127.0.0.1',
'method' => 'GET',
'url' => 'http://example.com/',
'time' => 0,
'parent' => null,
'status_code' => 200,
),
array(
'token' => 'token2',
'ip' => '127.0.0.1',
'method' => 'GET',
'url' => 'http://example.com/not_found',
'time' => 0,
'parent' => null,
'status_code' => 404,
),
);
$profiler
->expects($this->once())
->method('find')
->will($this->returnValue($tokens));

$twig->expects($this->once())
->method('render')
->with($this->stringEndsWith('results.html.twig'), $this->equalTo(array(
'token' => 'empty',
'profile' => null,
'tokens' => $tokens,
'ip' => '127.0.0.1',
'method' => 'GET',
'url' => 'http://example.com/',
'start' => null,
'end' => null,
'limit' => 2,
'panel' => null,
)));

$response = $controller->searchResultsAction(
Request::create(
'/_profiler/empty/search/results',
'GET',
array('limit' => 2, 'ip' => '127.0.0.1', 'method' => 'GET', 'url' => 'http://example.com/')
),
'empty'
);
$this->assertEquals(200, $response->getStatusCode());
}
}
5 changes: 5 additions & 0 deletions src/Symfony/Component/HttpKernel/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

2.7.0
-----

* added the HTTP status code to profiles

2.6.0
-----

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ public function find($ip, $url, $limit, $method, $start = null, $end = null)
continue;
}

list($itemToken, $itemIp, $itemMethod, $itemUrl, $itemTime, $itemParent) = explode("\t", $item, 6);
$values = explode("\t", $item, 7);
list($itemToken, $itemIp, $itemMethod, $itemUrl, $itemTime, $itemParent) = $values;
$statusCode = isset($values[6]) ? $values[6] : null;

$itemTime = (int) $itemTime;

Expand All @@ -84,6 +86,7 @@ public function find($ip, $url, $limit, $method, $start = null, $end = null)
'url' => $itemUrl,
'time' => $itemTime,
'parent' => $itemParent,
'status_code' => $statusCode,
);
--$limit;
}
Expand Down Expand Up @@ -176,6 +179,7 @@ public function write(Profile $profile)
$profile->getUrl(),
$profile->getTime(),
$profile->getParentToken(),
$profile->getStatusCode(),
))."\n";

return $this->appendValue($indexName, $indexRow, $this->lifetime);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ public function find($ip, $url, $limit, $method, $start = null, $end = null)

$result = array();
while (count($result) < $limit && $line = $this->readLineFromFile($file)) {
list($csvToken, $csvIp, $csvMethod, $csvUrl, $csvTime, $csvParent) = str_getcsv($line);
$values = str_getcsv($line);
list($csvToken, $csvIp, $csvMethod, $csvUrl, $csvTime, $csvParent) = $values;
$csvStatusCode = isset($values[6]) ? $values[6] : null;

$csvTime = (int) $csvTime;

Expand All @@ -84,6 +86,7 @@ public function find($ip, $url, $limit, $method, $start = null, $end = null)
'url' => $csvUrl,
'time' => $csvTime,
'parent' => $csvParent,
'status_code' => $csvStatusCode,
);
}

Expand Down Expand Up @@ -167,6 +170,7 @@ public function write(Profile $profile)
$profile->getUrl(),
$profile->getTime(),
$profile->getParentToken(),
$profile->getStatusCode(),
));
fclose($file);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function __construct($dsn, $username = '', $password = '', $lifetime = 86
*/
public function find($ip, $url, $limit, $method, $start = null, $end = null)
{
$cursor = $this->getMongo()->find($this->buildQuery($ip, $url, $method, $start, $end), array('_id', 'parent', 'ip', 'method', 'url', 'time'))->sort(array('time' => -1))->limit($limit);
$cursor = $this->getMongo()->find($this->buildQuery($ip, $url, $method, $start, $end), array('_id', 'parent', 'ip', 'method', 'url', 'time', 'status_code'))->sort(array('time' => -1))->limit($limit);

$tokens = array();
foreach ($cursor as $profile) {
Expand Down Expand Up @@ -83,6 +83,7 @@ public function write(Profile $profile)
'method' => $profile->getMethod(),
'url' => $profile->getUrl(),
'time' => $profile->getTime(),
'status_code' => $profile->getStatusCode(),
);

$result = $this->getMongo()->update(array('_id' => $profile->getToken()), array_filter($record, function ($v) { return !empty($v); }), array('upsert' => true));
Expand Down Expand Up @@ -212,6 +213,7 @@ private function getData(array $data)
'url' => isset($data['url']) ? $data['url'] : null,
'time' => isset($data['time']) ? $data['time'] : null,
'data' => isset($data['data']) ? $data['data'] : null,
'status_code' => isset($data['status_code']) ? $data['status_code'] : null,
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ protected function initDb()
}

$db = new \PDO($this->dsn, $this->username, $this->password);
$db->exec('CREATE TABLE IF NOT EXISTS sf_profiler_data (token VARCHAR(255) PRIMARY KEY, data LONGTEXT, ip VARCHAR(64), method VARCHAR(6), url VARCHAR(255), time INTEGER UNSIGNED, parent VARCHAR(255), created_at INTEGER UNSIGNED, KEY (created_at), KEY (ip), KEY (method), KEY (url), KEY (parent))');
$db->exec('CREATE TABLE IF NOT EXISTS sf_profiler_data (token VARCHAR(255) PRIMARY KEY, data LONGTEXT, ip VARCHAR(64), method VARCHAR(6), url VARCHAR(255), time INTEGER UNSIGNED, parent VARCHAR(255), created_at INTEGER UNSIGNED, status_code SMALLINT UNSIGNED, KEY (created_at), KEY (ip), KEY (method), KEY (url), KEY (parent))');

$this->db = $db;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function find($ip, $url, $limit, $method, $start = null, $end = null)
$criteria = $criteria ? 'WHERE '.implode(' AND ', $criteria) : '';

$db = $this->initDb();
$tokens = $this->fetch($db, 'SELECT token, ip, method, url, time, parent FROM sf_profiler_data '.$criteria.' ORDER BY time DESC LIMIT '.((int) $limit), $args);
$tokens = $this->fetch($db, 'SELECT token, ip, method, url, time, parent, status_code FROM sf_profiler_data '.$criteria.' ORDER BY time DESC LIMIT '.((int) $limit), $args);
$this->close($db);

return $tokens;
Expand Down Expand Up @@ -94,13 +94,14 @@ public function write(Profile $profile)
':url' => $profile->getUrl(),
':time' => $profile->getTime(),
':created_at' => time(),
':status_code' => $profile->getStatusCode(),
);

try {
if ($this->has($profile->getToken())) {
$this->exec($db, 'UPDATE sf_profiler_data SET parent = :parent, data = :data, ip = :ip, method = :method, url = :url, time = :time, created_at = :created_at WHERE token = :token', $args);
$this->exec($db, 'UPDATE sf_profiler_data SET parent = :parent, data = :data, ip = :ip, method = :method, url = :url, time = :time, created_at = :created_at, status_code = :status_code WHERE token = :token', $args);
} else {
$this->exec($db, 'INSERT INTO sf_profiler_data (token, parent, data, ip, method, url, time, created_at) VALUES (:token, :parent, :data, :ip, :method, :url, :time, :created_at)', $args);
$this->exec($db, 'INSERT INTO sf_profiler_data (token, parent, data, ip, method, url, time, created_at, status_code) VALUES (:token, :parent, :data, :ip, :method, :url, :time, :created_at, :status_code)', $args);
}
$this->cleanup();
$status = true;
Expand Down
17 changes: 17 additions & 0 deletions src/Symfony/Component/HttpKernel/Profiler/Profile.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class Profile
private $method;
private $url;
private $time;
private $statusCode;

/**
* @var Profile
Expand Down Expand Up @@ -171,6 +172,22 @@ public function setTime($time)
$this->time = $time;
}

/**
* @param int $statusCode
*/
public function setStatusCode($statusCode)
{
$this->statusCode = $statusCode;
}

/**
* @return int
*/
public function getStatusCode()
{
return $this->statusCode;
}

/**
* Finds children profilers.
*
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/HttpKernel/Profiler/Profiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ public function collect(Request $request, Response $response, \Exception $except
$profile->setUrl($request->getUri());
$profile->setIp($request->getClientIp());
$profile->setMethod($request->getMethod());
$profile->setStatusCode($response->getStatusCode());

$response->headers->set('X-Debug-Token', $profile->getToken());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ public function find($ip, $url, $limit, $method, $start = null, $end = null)
continue;
}

list($itemToken, $itemIp, $itemMethod, $itemUrl, $itemTime, $itemParent) = explode("\t", $item, 6);
$values = explode("\t", $item, 7);
list($itemToken, $itemIp, $itemMethod, $itemUrl, $itemTime, $itemParent) = $values;
$statusCode = isset($values[6]) ? $values[6] : null;

$itemTime = (int) $itemTime;

Expand All @@ -94,6 +96,7 @@ public function find($ip, $url, $limit, $method, $start = null, $end = null)
'url' => $itemUrl,
'time' => $itemTime,
'parent' => $itemParent,
'status_code' => $statusCode,
);
--$limit;
}
Expand Down Expand Up @@ -182,6 +185,7 @@ public function write(Profile $profile)
$profile->getUrl(),
$profile->getTime(),
$profile->getParentToken(),
$profile->getStatusCode(),
))."\n";

return $this->appendValue($indexName, $indexRow, $this->lifetime);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ protected function initDb()
}

$db->exec('PRAGMA temp_store=MEMORY; PRAGMA journal_mode=MEMORY;');
$db->exec('CREATE TABLE IF NOT EXISTS sf_profiler_data (token STRING, data STRING, ip STRING, method STRING, url STRING, time INTEGER, parent STRING, created_at INTEGER)');
$db->exec('CREATE TABLE IF NOT EXISTS sf_profiler_data (token STRING, data STRING, ip STRING, method STRING, url STRING, time INTEGER, parent STRING, created_at INTEGER, status_code INTEGER)');
$db->exec('CREATE INDEX IF NOT EXISTS data_created_at ON sf_profiler_data (created_at)');
$db->exec('CREATE INDEX IF NOT EXISTS data_ip ON sf_profiler_data (ip)');
$db->exec('CREATE INDEX IF NOT EXISTS data_method ON sf_profiler_data (method)');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,22 @@ public function testDuplicates()
$this->assertCount(3, $this->getStorage()->find('127.0.0.1', 'http://example.net/', 3, 'GET'), '->find() method returns incorrect number of entries');
}

public function testStatusCode()
{
$profile = new Profile('token1');
$profile->setStatusCode(200);
$this->getStorage()->write($profile);

$profile = new Profile('token2');
$profile->setStatusCode(404);
$this->getStorage()->write($profile);

$tokens = $this->getStorage()->find('', '', 10, '');
$this->assertCount(2, $tokens);
$this->assertContains($tokens[0]['status_code'], array(200, 404));
$this->assertContains($tokens[1]['status_code'], array(200, 404));
}

/**
* @return \Symfony\Component\HttpKernel\Profiler\ProfilerStorageInterface
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,15 @@ public function testCollect()
{
$request = new Request();
$request->query->set('foo', 'bar');
$response = new Response();
$response = new Response('', 204);
$collector = new RequestDataCollector();

$profiler = new Profiler($this->storage);
$profiler->add($collector);
$profile = $profiler->collect($request, $response);

$profile = $profiler->loadProfile($profile->getToken());
$this->assertSame(204, $profile->getStatusCode());
$this->assertSame('GET', $profile->getMethod());
$this->assertEquals(array('foo' => 'bar'), $profiler->get('request')->getRequestQuery()->all());
}

Expand Down