8000 [Messenger] Improve the profiler panel by ogizanagi · Pull Request #27202 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Messenger] Improve the profiler panel #27202

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

Merged
merged 1 commit into from
May 13, 2018
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,33 @@

{% block toolbar %}
{% if collector.messages|length > 0 %}
{% set status_color = collector.exceptionsCount ? 'red' %}
{% set icon %}
{{ include('@WebProfiler/Icon/messenger.svg') }}
<span class="sf-toolbar-value">{{ collector.messages|length }}</span>
{% endset %}

{{ include('@WebProfiler/Profiler/toolbar_item.html.twig', { link: 'messenger' }) }}
{% set text %}
{% for bus in collector.buses %}
{% set exceptionsCount = collector.exceptionsCount(bus) %}
<div class="sf-toolbar-info-piece">
<b>{{ bus }}</b>
<span
title="{{ exceptionsCount }} message(s) with exceptions"
class="sf-toolbar-status sf-toolbar-status-{{ exceptionsCount ? 'red' }}"
>
{{ collector.messages(bus)|length }}
</span>
</div>
{% endfor %}
{% endset %}

{{ include('@WebProfiler/Profiler/toolbar_item.html.twig', { link: 'messenger', status: status_color }) }}
{% endif %}
{% endblock %}

{% block menu %}
<span class="label">
<span class="label {{ collector.exceptionsCount ? 'label-status-error' }}">
<span class="icon">{{ include('@WebProfiler/Icon/messenger.svg') }}</span>
<strong>Messages</strong>

Expand All @@ -24,49 +40,128 @@
</span>
{% endblock %}

{% block head %}
{{ parent() }}
<style>
.message-item thead th { position: relative; cursor: pointer; user-select: none; padding-right: 35px; }
.message-item tbody tr td:first-child { width: 115px; }

.message-item .label { float: right; padding: 1px 5px; opacity: .75; margin-left: 5px; }
.message-item .toggle-button { position: absolute; right: 6px; top: 6px; opacity: .5; pointer-events: none }
.message-item .icon svg { height: 24px; width: 24px; }

.message-item .sf-toggle-off .icon-close, .sf-toggle-on .icon-open { display: none; }
.message-item .sf-toggle-off .icon-open, .sf-toggle-on .icon-close { display: block; }

.message-bus .badge.status-some-errors { line-height: 16px; border-bottom: 2px solid #B0413E; }

.message-item .sf-toggle-content.sf-toggle-visible { display: table-row-group; }
</style>
{% endblock %}

{% block panel %}
{% import _self as helper %}

<h2>Messages</h2>

{% if collector.messages is empty %}
<div class="empty">
<p>No messages have been collected.</p>
</div>
{% else %}
<table>
<thead>
<tr>
<th>Bus</th>
<th>Message</th>
<th>Result</th>
</tr>
</thead>
<tbody>
{% for message in collector.messages %}
<tr>
<td>{{ message.bus }}</td>
<td>
{% if message.result.object is defined %}
{{ profiler_dump(message.message.object, maxDepth=2) }}
{% else %}
{{ message.message.type }}
{% endif %}
</td>
<td>
{% if message.result.object is defined %}
{{ profiler_dump(message.result.object, maxDepth=2) }}
{% elseif message.result.type is defined %}
{{ message.result.type }}
{% if message.result.value is defined %}
8000 {{ message.result.value }}
{% endif %}
{% endif %}
{% if message.exception.type is defined %}
{{ message.exception.type }}
{% endif %}
</td>
</tr>
{% endfor %}
</tbody>
</table>

<div class="sf-tabs message-bus">
<div class="tab">
{% set messages = collector.messages %}
{% set exceptionsCount = collector.exceptionsCount %}
<h3 class="tab-title">All<span class="badge {{ exceptionsCount ? exceptionsCount == messages|length ? 'status-error' : 'status-some-errors' }}">{{ messages|length }}</span></h3>

<div class="tab-content">
<p class="text-muted">Ordered list of dispatched messages across all your buses</p>
{{ helper.render_bus_messages(messages, true) }}
</div>
</div>

{% for bus in collector.buses %}
<div class="tab message-bus">
{% set messages = collector.messages(bus) %}
{% set exceptionsCount = collector.exceptionsCount(bus) %}
<h3 class="tab-title">{{ bus }}<span class="badge {{ exceptionsCount ? exceptionsCount == messages|length ? 'status-error' : 'status-some-errors' }}">{{ messages|length }}</span></h3>

<div class="tab-content">
<p class="text-muted">Ordered list of messages dispatched on the <code>{{ bus }}</code> bus</p>
{{ helper.render_bus_messages(messages) }}
</div>
</div>
{% endfor %}
{% endif %}

{% endblock %}

{% macro render_bus_messages(messages, showBus = false) %}
{% set discr = random() %}
{% for i, dispatchCall in messages %}
<table class="message-item">
<thead>
<tr>
<th colspan="2" class="sf-toggle"
data-toggle-selector="#message-item-{{ discr }}-{{ i }}-details"
data-toggle-initial="{{ loop.first ? 'display' }}"
>
<span class="dump-inline">{{ profiler_dump(dispatchCall.message.type) }}</span>
{% if showBus %}
<span class="label">{{ dispatchCall.bus }}</span>
{% endif %}
{% if dispatchCall.exception is defined %}
<span class="label status-error">exception</span>
{% endif %}
<a class="toggle-button">
<span class="icon icon-close">{{ include('@Twig/images/icon-minus-square.svg') }}</span>
<span class="icon icon-open">{{ include('@Twig/images/icon-plus-square.svg') }}</span>
</a>
</th>
</tr>
</thead>
<tbody id="message-item-{{ discr }}-{{ i }}-details" class="sf-toggle-content">
{% if showBus %}
<tr>
<td class="text-bold">Bus</td>
<td>{{ dispatchCall.bus }}</td>
</tr>
{% endif %}
<tr>
<td class="text-bold">Message</td>
<td>{{ profiler_dump(dispatchCall.message.value, maxDepth=2) }}</td>
</tr>
<tr>
<td class="text-bold">Envelope items</td>
<td>
{% for item in dispatchCall.envelopeItems %}
{{ profiler_dump(item) }}
{% else %}
<span class="text-muted">No items</span>
{% endfor %}
</td>
</tr>
<tr>
<td class="text-bold">Result</td>
<td>
{% if dispatchCall.result is defined %}
{{ profiler_dump(dispatchCall.result.seek('value'), maxDepth=2) }}
{% elseif dispatchCall.exception is defined %}
<span class="text-danger">No result as an exception occurred</span>
{% endif %}
</td>
</tr>
{% if dispatchCall.exception is defined %}
<tr>
<td class="text-bold">Exception</td>
<td>
{{ profiler_dump(dispatchCall.exception.value, maxDepth=1) }}
</td>
</tr>
{% endif %}
</tbody>
</table>
{% endfor %}
{% endmacro %}
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,9 @@ table tbody ul {
.text-muted {
color: #999;
}
.text-danger {
color: {{ colors.error|raw }};
}
.text-bold {
font-weight: bold;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
use Symfony\Component\HttpKernel\DataCollector\DataCollector;
use Symfony\Component\HttpKernel\DataCollector\LateDataCollectorInterface;
use Symfony\Component\Messenger\TraceableMessageBus;
use Symfony\Component\VarDumper\Caster\ClassStub;
use Symfony\Component\VarDumper\Cloner\Data;

/**
* @author Samuel Roze <samuel.roze@gmail.com>
Expand Down Expand Up @@ -44,13 +46,25 @@ public function collect(Request $request, Response $response, \Exception $except
*/
public function lateCollect()
{
$this->data = array('messages' => array());
$this->data = array('messages' => array(), 'buses' => array_keys($this->traceableBuses));

$messages = array();
foreach ($this->traceableBuses as $busName => $bus) {
foreach ($bus->getDispatchedMessages() as $message) {
$this->data['messages'][] = $this->collectMessage($busName, $message);
$debugRepresentation = $this->cloneVar($this->collectMessage($busName, $message));
$messages[] = array($debugRepresentation, $message['callTime']);
}
}

// Order by call time
usort($messages, function (array $a, array $b): int {
return $a[1] > $b[1] ? 1 : -1;
});

// Keep the messages clones only
$this->data['messages'] = array_map(function (array $item): Data {
return $item[0];
}, $messages);
}

/**
Expand Down Expand Up @@ -78,47 +92,51 @@ private function collectMessage(string $busName, array $tracedMessage)

$debugRepresentation = array(
'bus' => $busName,
'envelopeItems' => $tracedMessage['envelopeItems'] ?? null,
'message' => array(
'type' => \get_class($message),
'object' => $this->cloneVar($message),
'type' => new ClassStub(\get_class($message)),
'value' => $message,
),
);

if (array_key_exists('result', $tracedMessage)) {
$result = $tracedMessage['result'];

if (\is_object($result)) {
$debugRepresentation['result'] = array(
'type' => \get_class($result),
'object' => $this->cloneVar($result),
);
} elseif (\is_array($result)) {
$debugRepresentation['result'] = array(
'type' => 'array',
'object' => $this->cloneVar($result),
);
} else {
$debugRepresentation['result'] = array(
'type' => \gettype($result),
'value' => $result,
);
}
$debugRepresentation['result'] = array(
'type' => \is_object($result) ? \get_class($result) : gettype($result),
'value' => $result,
);
}

if (isset($tracedMessage['exception'])) {
$exception = $tracedMessage['exception'];

$debugRepresentation['exception'] = array(
'type' => \get_class($exception),
'message' => $exception->getMessage(),
'value' => $exception,
);
}

return $debugRepresentation;
}

public function getMessages(): array
public function getExceptionsCount(string $bus = null): int
{
return array_reduce($this->getMessages($bus), function (int $carry, Data $message) {
return $carry += isset($message['exception']) ? 1 : 0;
}, 0);
}

public function getMessages(string $bus = null): array
{
$messages = $this->data['messages'] ?? array();

return $bus ? array_filter($messages, function (Data $message) use ($bus): bool {
return $bus === $message['bus'];
}) : $messages;
}

public function getBuses(): array
{
return $this->data['messages'] ?? array();
return $this->data['buses'];
}
}
Loading
0