Skip to content

Commit 1627ff4

Browse files
fbourigaultNyholm
authored andcommitted
minor profiler updates (#201)
* 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
1 parent 33ed6ae commit 1627ff4

File tree

3 files changed

+38
-6
lines changed

3 files changed

+38
-6
lines changed

Collector/Collector.php

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,23 +131,55 @@ public function getFailedStacks()
131131
*/
132132
public function getClients()
133133
{
134+
$stacks = array_filter($this->data['stacks'], function (Stack $stack) {
135+
return $stack->getParent() === null;
136+
});
137+
134138
return array_unique(array_map(function (Stack $stack) {
135139
return $stack->getClient();
136-
}, $this->data['stacks']));
140+
}, $stacks));
137141
}
138142

139143
/**
140144
* @param $client
141145
*
142146
* @return Stack[]
143147
*/
144-
public function getClientStacks($client)
148+
public function getClientRootStacks($client)
145149
{
146150
return array_filter($this->data['stacks'], function (Stack $stack) use ($client) {
147-
return $stack->getClient() == $client;
151+
return $stack->getClient() == $client && $stack->getParent() == null;
148152
});
149153
}
150154

155+
/**
156+
* Count all messages for a client.
157+
*
158+
* @param $client
159+
*
160+
* @return int
161+
*/
162+
public function countClientMessages($client)
163+
{
164+
return array_sum(array_map(function (Stack $stack) {
165+
return $this->countStackMessages($stack);
166+
}, $this->getClientRootStacks($client)));
167+
}
168+
169+
/**
170+
* Recursively count message in stack.
171+
*
172+
* @param Stack $stack
173+
*
174+
* @return int
175+
*/
176+
private function countStackMessages(Stack $stack)
177+
{
178+
return 1 + array_sum(array_map(function (Stack $child) {
179+
return $this->countStackMessages($child);
180+
}, $this->getChildrenStacks($stack)));
181+
}
182+
151183
/**
152184
* @return int
153185
*/

Resources/views/webprofiler.html.twig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,14 @@
7171
<div class="sf-tabs">
7272
{% for client in collector.clients %}
7373
<div class="tab">
74-
<h3 class="tab-title">{{ client }} <span class="badge">{{ collector.clientStacks(client)|length }}</span></h3>
74+
<h3 class="tab-title">{{ client }} <span class="badge">{{ collector.countClientMessages(client) }}</span></h3>
7575

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

81-
{% for stack in collector.clientStacks(client) if not stack.parent %}
81+
{% for stack in collector.clientRootStacks(client) %}
8282
<div class="httplug-stack">
8383
{% include '@Httplug/stack.html.twig' with {
8484
'collector': collector,

Tests/Unit/Collector/CollectorTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,6 @@ public function testAddStack()
6666
$collector->addStack($stack);
6767

6868
$this->assertEquals(['acme'], $collector->getClients());
69-
$this->assertEquals([$stack], $collector->getClientStacks('acme'));
69+
$this->assertEquals([$stack], $collector->getClientRootStacks('acme'));
7070
}
7171
}

0 commit comments

Comments
 (0)