Skip to content

Commit

Permalink
minor profiler updates (#201)
Browse files Browse the repository at this point in the history
* minor profiler updates

    - don't display empty client tabs in profiler.
    - return only collector root stacks.
    - better count of client messages.

* use strict comparison in getClients
  • Loading branch information
fbourigault authored and Nyholm committed Aug 28, 2017
1 parent 33ed6ae commit 1627ff4
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 6 deletions.
38 changes: 35 additions & 3 deletions Collector/Collector.php
Expand Up @@ -131,23 +131,55 @@ public function getFailedStacks()
*/
public function getClients()
{
$stacks = array_filter($this->data['stacks'], function (Stack $stack) {
return $stack->getParent() === null;
});

return array_unique(array_map(function (Stack $stack) {
return $stack->getClient();
}, $this->data['stacks']));
}, $stacks));
}

/**
* @param $client
*
* @return Stack[]
*/
public function getClientStacks($client)
public function getClientRootStacks($client)
{
return array_filter($this->data['stacks'], function (Stack $stack) use ($client) {
return $stack->getClient() == $client;
return $stack->getClient() == $client && $stack->getParent() == null;
});
}

/**
* Count all messages for a client.
*
* @param $client
*
* @return int
*/
public function countClientMessages($client)
{
return array_sum(array_map(function (Stack $stack) {
return $this->countStackMessages($stack);
}, $this->getClientRootStacks($client)));
}

/**
* Recursively count message in stack.
*
* @param Stack $stack
*
* @return int
*/
private function countStackMessages(Stack $stack)
{
return 1 + array_sum(array_map(function (Stack $child) {
return $this->countStackMessages($child);
}, $this->getChildrenStacks($stack)));
}

/**
* @return int
*/
Expand Down
4 changes: 2 additions & 2 deletions Resources/views/webprofiler.html.twig
Expand Up @@ -71,14 +71,14 @@
<div class="sf-tabs">
{% for client in collector.clients %}
<div class="tab">
<h3 class="tab-title">{{ client }} <span class="badge">{{ collector.clientStacks(client)|length }}</span></h3>
<h3 class="tab-title">{{ client }} <span class="badge">{{ collector.countClientMessages(client) }}</span></h3>

<div class="tab-content">
<p class="help">
These messages are sent by client named "{{ client }}".
</p>

{% for stack in collector.clientStacks(client) if not stack.parent %}
{% for stack in collector.clientRootStacks(client) %}
<div class="httplug-stack">
{% include '@Httplug/stack.html.twig' with {
'collector': collector,
Expand Down
2 changes: 1 addition & 1 deletion Tests/Unit/Collector/CollectorTest.php
Expand Up @@ -66,6 +66,6 @@ public function testAddStack()
$collector->addStack($stack);

$this->assertEquals(['acme'], $collector->getClients());
$this->assertEquals([$stack], $collector->getClientStacks('acme'));
$this->assertEquals([$stack], $collector->getClientRootStacks('acme'));
}
}

0 comments on commit 1627ff4

Please sign in to comment.