Skip to content

Commit

Permalink
Шаблоны для кастомного обработчика DataCollector
Browse files Browse the repository at this point in the history
  • Loading branch information
ProklUng committed Aug 17, 2021
1 parent e6a6ec3 commit 35d5512
Show file tree
Hide file tree
Showing 3 changed files with 269 additions and 0 deletions.
70 changes: 70 additions & 0 deletions Resources/views/Custom/list.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
{% import '@CsaGuzzle/Custom/macros.html.twig' as macros %}

{% include '@CsaGuzzle/Custom/toolbar.html.twig' %}

<div class="accordion" id="accordionGuzzle">
{% for call in calls %}
<section class="call">
<header class="accordion-header {{ call.request.method|lower }} mb-5">
<span class="method-name">{{ call.request.method }}</span>
{% if call.request.method == 'GET' %}
<a href="{{ call.uri }}" target="_blank" class="path">{{ call.uri }}</a>
{% else %}
<span class="path">{{ call.uri }}</span>
{% endif %}
{% set statusCode = call.httpCode %}
<span class="badge status-code {{ statusCode|csa_guzzle_status_code_class }}">
{{ statusCode }} - {{ call.response is defined ? call.response.reasonPhrase : 'Unknown error' }}
</span>
{% if call.cache is defined %}<span class="badge cache {{ call.cache|lower }}">
Cache {{ call.cache }}</span>{% endif %}
{% if call.mock is defined %}<span class="badge mock {{ call.mock|lower }}">
Mock {{ call.mock }}</span>{% endif %}
{% if call.info %}
{% if call.info.total_time > 1 %}
{% set duration_color = 'error' %}
{% elseif call.info.total_time < 0.2 %}
{% set duration_color = 'success' %}
{% else %}
{% set duration_color = 'warning' %}
{% endif %}
<span class="badge duration {{ duration_color }}">{{ call.info.total_time|csa_guzzle_format_duration }}</span>
{% endif %}
</header>

<div class="accordion-content{{ loop.first ? ' expanded': '' }}">
<div class="sf-tabs">
<div class="tab">
<h3 class="tab-title">Request</h3>
<div class="tab-content">
{{ macros.render_infos(call.info) }}
{{ macros.render_headers(call.request.headers, call.uri) }}
{{ macros.render_body(call.request.body) }}
{% if call.curl is defined %}
{{ macros.render_curl(call.curl) }}
{% endif %}
</div>
</div>
{% if call.response is defined %}
<div class="tab">
<h3 class="tab-title">Response</h3>
<div class="tab-content">
{{ macros.render_headers(call.response.headers, call.uri) }}
{{ macros.render_body(call.response.body) }}
</div>
</div>
{% endif %}
{% if call.error is not null %}
<div class="tab">
<h3 class="tab-title">Error</h3>
<div class="tab-content">
{{ macros.render_error(call.error) }}
</div>
</div>
{% endif %}
</div>
</div>
</section>
{% endfor %}
</div>

108 changes: 108 additions & 0 deletions Resources/views/Custom/macros.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
{% macro render_headers(headers, uri) %}
<h4>Headers</h4>
<table>
<thead>
<tr>
<th scope="col" class="key">Header</th>
<th scope="col">Value</th>
</tr>
</thead>
{% for header, values in headers %}
<tr>
<td>{{ header }}</td>
<td>
{% if values|length > 1 %}
<ul>
{% for value in values %}
<li>{{ value }}</li>
{% endfor %}
</ul>
{% else %}
{% if header == 'X-Debug-Token-Link' %}
<a href="{{ values.0 }}" target="_blank">{{ values.0 }}</a>
{% else %}
{{ values.0 }}
{% endif %}
{% endif %}
</td>
</tr>
{% endfor %}
</table>
{% endmacro %}

{% macro render_body(body) %}
{% if body is not empty %}
<h4>Content</h4>

{% set lang = csa_guzzle_detect_lang(body) %}

<pre><code class="language-{{ lang }}">{{ body|csa_guzzle_pretty_print(lang) }}</code></pre>
{% endif %}
{% endmacro %}

{% macro render_infos(info) %}
{% if info and info.total_time %}
<h4>Time</h4>

{% set wait_time = info.namelookup_time + info.connect_time + info.redirect_time %}
{% set process_time = info.total_time - wait_time %}

<table>
<thead>
<tr>
<th>Measure</th>
<th>Duration</th>
</tr>
</thead>
<tr>
<td>Total</td>
<td>{{ info.total_time|csa_guzzle_format_duration }}</td>
</tr>
<tr>
<td>Name lookup</td>
<td>{{ info.namelookup_time|csa_guzzle_format_duration }}</td>
</tr>
<tr>
<td>Connection</td>
<td>{{ info.connect_time|csa_guzzle_format_duration }}</td>
</tr>
{% if info.redirect_time %}
<tr>
<td>Redirect</td>
<td>{{ info.redirect_time|csa_guzzle_format_duration }}</td>
</tr>
{% endif %}
<tr>
<td>Process</td>
<td>{{ process_time | csa_guzzle_format_duration }}</td>
</tr>
</table>

<div class="progress">
<div class="progress-bar progress-bar-warning" style="width: {{ wait_time/info.total_time * 100 }}%">
<span class="sr-only">Wait</span>
</div>
<div class="progress-bar progress-bar-success" style="width: {{ process_time/info.total_time * 100 }}%">
<span class="sr-only">Process</span>
</div>
</div>
{% endif %}

{% endmacro %}

{% macro render_error(error) %}
<h4>Message</h4>
<p><pre>{{ error.message }}</pre></p>

<h4>Origin</h4>
<p><pre>{{ error.file }}({{ error.line }})</pre></p>

<h4>Stack trace</h4>
<pre><code class="language-text">{{error.trace|nl2br}}</code></pre>
{% endmacro %}

{% macro render_curl(curl) %}
<h4>cURL</h4>

<pre><code>{{ curl }}</code></pre>
{% endmacro %}
91 changes: 91 additions & 0 deletions Resources/views/Custom/toolbar.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
{% set callCount = calls|length %}
{% set errorCount = errors|length %}

{% set cacheHitCount = 0 %}
{% set cacheMissCount = 0 %}

{% for call in calls|filter(call => call.cache is defined and call.cache == 'HIT') %}
{% if call.cache == 'HIT' %}
{% set cacheHitCount = cacheHitCount + 1 %}
{% else %}
{% set cacheMissCount = cacheMissCount + 1 %}
{% endif %}
{% endfor %}

{% set mockReplayCount = 0 %}
{% set mockRecordCount = 0 %}

{% for call in calls|filter(call => call.mock is defined) %}
{% if call.mock == 'REPLAY' %}
{% set mockReplayCount = mockReplayCount + 1 %}
{% else %}
{% set mockRecordCount = mockRecordCount + 1 %}
{% endif %}
{% endfor %}

{% if callCount == 0 %}
{% set color_code = '' %}
{% elseif errorCount > 0 %}
{% set color_code = 'red' %}
{% else %}
{% set color_code = 'green' %}
{% endif %}


<div class="">
<strong>HTTP calls </strong>
{% if calls is not empty %}
<span class="count">
<span>{{ calls|length }}</span>
{% if totalTime > 0 %}
<span>{{ totalTime|csa_guzzle_format_duration }}</span>
{% endif %}
</span>
{% endif %}
</div>

<div class="sf-toolbar-block">
<div class="sf-toolbar-info-piece">
<b>Success</b>
<span class="sf-toolbar-status sf-toolbar-status-green">{{ callCount - errorCount }}</span>
</div>

{% if cacheHitCount %}
<div class="sf-toolbar-info-piece">
<b>Cache HIT</b>
<span class="sf-toolbar-status sf-toolbar-status-green">{{ cacheHitCount }}</span>
</div>
{% endif %}
{% if cacheMissCount %}
<div class="sf-toolbar-info-piece">
<b>Cache MISS</b>
<span class="sf-toolbar-status sf-toolbar-status-yellow">{{ cacheMissCount }}</span>
</div>
{% endif %}
{% if mockReplayCount %}
<div class="sf-toolbar-info-piece">
<b>Mocks (replayed)</b>
<span class="sf-toolbar-status sf-toolbar-status-green">{{ mockReplayCount }}</span>
</div>
{% endif %}
{% if mockRecordCount %}
<div class="sf-toolbar-info-piece">
<b>Mocks (recorded)</b>
<span class="sf-toolbar-status sf-toolbar-status-green">{{ mockRecordCount }}</span>
</div>
{% endif %}
{% if errorCount %}
<div class="sf-toolbar-info-piece">
<b>Errors</b>
<span class="sf-toolbar-status sf-toolbar-status-red">{{ errorCount }}</span>
</div>
{% endif %}
</div>

<style>
.count {
margin-left: 1rem;
}
.count span { margin-left: 1rem; }
</style>

0 comments on commit 35d5512

Please sign in to comment.