Skip to content

Commit

Permalink
minor: refactor ViewHelper
Browse files Browse the repository at this point in the history
  • Loading branch information
kbond committed Nov 23, 2024
1 parent 4c1a06a commit 78cc0e5
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 106 deletions.
42 changes: 9 additions & 33 deletions src/Controller/MessengerMonitorController.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,10 @@ abstract class MessengerMonitorController extends AbstractController
#[Route(name: 'zenstruck_messenger_monitor_dashboard')]
public function dashboard(ViewHelper $helper): Response
{
if (!$helper->storage) {
throw new \LogicException('Storage must be configured to use the dashboard.');
}

return $this->render('@ZenstruckMessengerMonitor/dashboard.html.twig', [
'helper' => $helper,
'snapshot' => Specification::create(Period::IN_LAST_DAY)->snapshot($helper->storage),
'messages' => Specification::new()->snapshot($helper->storage)->messages(),
'snapshot' => Specification::create(Period::IN_LAST_DAY)->snapshot($helper->storage()),
'messages' => Specification::new()->snapshot($helper->storage())->messages(),
]);
}

Expand All @@ -53,10 +49,6 @@ public function statistics(
Request $request,
ViewHelper $helper,
): Response {
if (!$helper->storage) {
throw new \LogicException('Storage must be configured to use the dashboard.');
}

$period = Period::parse($request->query->getString('period'));
$specification = Specification::create([ // @phpstan-ignore-line
'period' => $period,
Expand All @@ -66,7 +58,7 @@ public function statistics(
'helper' => $helper,
'periods' => [...Period::inLastCases(), ...Period::absoluteCases()],
'period' => $period,
'metrics' => $specification->snapshot($helper->storage)->perMessageTypeMetrics(),
'metrics' => $specification->snapshot($helper->storage())->perMessageTypeMetrics(),
]);
}

Expand All @@ -75,10 +67,6 @@ public function history(
Request $request,
ViewHelper $helper,
): Response {
if (!$helper->storage) {
throw new \LogicException('Storage must be configured to use the dashboard.');
}

$tags = [$request->query->get('tag')];
$notTags = [];
$period = Period::parse($request->query->getString('period'));
Expand All @@ -102,26 +90,22 @@ public function history(
'helper' => $helper,
'periods' => [...Period::inLastCases(), ...Period::absoluteCases()],
'period' => $period,
'snapshot' => $specification->snapshot($helper->storage),
'filters' => $specification->filters($helper->storage),
'snapshot' => $specification->snapshot($helper->storage()),
'filters' => $specification->filters($helper->storage()),
]);
}

#[Route('/history/{id}', name: 'zenstruck_messenger_monitor_detail')]
public function detail(string $id, ViewHelper $helper): Response
{
if (!$helper->storage) {
throw new \LogicException('Storage must be configured to use the dashboard.');
}

if (!$message = $helper->storage->find($id)) {
if (!$message = $helper->storage()->find($id)) {
throw $this->createNotFoundException('Message not found.');
}

return $this->render('@ZenstruckMessengerMonitor/detail.html.twig', [
'helper' => $helper,
'message' => $message,
'other_attempts' => $helper->storage->filter(Specification::create(['run_id' => $message->runId()])),
'other_attempts' => $helper->storage()->filter(Specification::create(['run_id' => $message->runId()])),
]);
}

Expand Down Expand Up @@ -289,13 +273,9 @@ public function transportsWidget(
public function snapshotWidget(
ViewHelper $helper,
): Response {
if (!$helper->storage) {
throw new \LogicException('Storage must be configured to use the dashboard.');
}

return $this->render('@ZenstruckMessengerMonitor/components/snapshot.html.twig', [
'helper' => $helper,
'snapshot' => Specification::create(Period::IN_LAST_DAY)->snapshot($helper->storage),
'snapshot' => Specification::create(Period::IN_LAST_DAY)->snapshot($helper->storage()),
'subtitle' => 'Last 24 Hours',
]);
}
Expand All @@ -304,12 +284,8 @@ public function snapshotWidget(
public function recentMessagesWidget(
ViewHelper $helper,
): Response {
if (!$helper->storage) {
throw new \LogicException('Storage must be configured to use the dashboard.');
}

return $this->render('@ZenstruckMessengerMonitor/components/recent_messages.html.twig', [
'messages' => Specification::new()->snapshot($helper->storage)->messages(),
'messages' => Specification::new()->snapshot($helper->storage())->messages(),
'helper' => $helper,
]);
}
Expand Down
28 changes: 23 additions & 5 deletions src/Twig/ViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,34 @@ final class ViewHelper
public function __construct(
public readonly Transports $transports,
public readonly Workers $workers,
public readonly ?Storage $storage,
private readonly ?Storage $storage,
public readonly ?Schedules $schedules,
public readonly ?DateTimeFormatter $timeFormatter,
public readonly ?CsrfTokenManagerInterface $csrfTokenManager,
private readonly ?DateTimeFormatter $timeFormatter,
private readonly ?CsrfTokenManagerInterface $csrfTokenManager,
) {
}

public function canFormatDuration(): bool
public function storage(): Storage
{
return $this->timeFormatter && \method_exists($this->timeFormatter, 'formatDuration');
return $this->storage ?? throw new \LogicException('Storage is not enabled.');
}

public function formatTime(\DateTimeInterface $from): string
{
return $this->timeFormatter?->formatDiff($from) ?? $from->format('c');
}

public function formatDuration(float $seconds): string
{
if ($seconds < 1) {
return \sprintf('%d ms', $seconds * 1000);
}

if (!$this->timeFormatter || !\method_exists($this->timeFormatter, 'formatDuration')) {
return \sprintf('%.3f s', $seconds);
}

return $this->timeFormatter->formatDuration($seconds);
}

public function generateCsrfToken(string ...$parts): string
Expand Down
12 changes: 2 additions & 10 deletions templates/components/messages.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,7 @@
<a href="{{ path('zenstruck_messenger_monitor_history', app.request.query.all|merge({transport: message.transport})) }}" class="badge align-text-top text-bg-secondary">{{ message.transport }}</a>
</td>
<td class="text-center">
{% if helper.canFormatDuration %}
<abbr title="{{ message.timeToHandle }}s">{{ helper.timeFormatter.formatDuration(message.timeToHandle) }}</abbr>
{% else %}
{{ message.timeToHandle }}s
{% endif %}
<abbr title="{{ message.timeToHandle }}s">{{ helper.formatDuration(message.timeToHandle) }}</abbr>
</td>
<td class="text-center">
{{ message.memoryUsage }}
Expand All @@ -64,11 +60,7 @@
{% endfor %}
</td>
<td>
{% if helper.timeFormatter %}
<abbr title="{{ message.finishedAt|date('c') }}">{{ helper.timeFormatter.formatDiff(message.finishedAt) }}</abbr>
{% else %}
{{ message.finishedAt|date('c') }}
{% endif %}
<abbr title="{{ message.finishedAt|date('c') }}">{{ helper.formatTime(message.finishedAt) }}</abbr>
</td>
<td>
<a href="{{ path('zenstruck_messenger_monitor_detail', {id: message.id}) }}" class="btn btn-sm btn-secondary d-inline-flex align-items-center" data-controller="message-details" data-action="message-details#click">
Expand Down
12 changes: 2 additions & 10 deletions templates/components/snapshot.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,13 @@
<tr>
<th class="text-end">Average Wait Time</th>
<td>
{% if helper.canFormatDuration %}
<abbr title="{{ snapshot.averageWaitTime|round }}s">{{ helper.timeFormatter.formatDuration(snapshot.averageWaitTime) }}</abbr>
{% else %}
{{ snapshot.averageWaitTime|round }}s
{% endif %}
<abbr title="{{ snapshot.averageWaitTime|round }}s">{{ helper.formatDuration(snapshot.averageWaitTime) }}</abbr>
</td>
</tr>
<tr>
<th class="text-end">Average Handling Time</th>
<td>
{% if helper.canFormatDuration %}
<abbr title="{{ snapshot.averageHandlingTime|round }}s">{{ helper.timeFormatter.formatDuration(snapshot.averageHandlingTime) }}</abbr>
{% else %}
{{ snapshot.averageHandlingTime|round }}s
{% endif %}
<abbr title="{{ snapshot.averageHandlingTime|round }}s">{{ helper.formatDuration(snapshot.averageHandlingTime) }}</abbr>
</td>
</tr>
<tr>
Expand Down
30 changes: 5 additions & 25 deletions templates/detail.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -66,51 +66,31 @@
<tr>
<th class="text-end text-nowrap">Time in Queue</th>
<td>
{% if helper.canFormatDuration %}
<abbr title="{{ message.timeInQueue }}s">{{ helper.timeFormatter.formatDuration(message.timeInQueue) }}</abbr>
{% else %}
{{ message.timeInQueue }}s
{% endif %}
<abbr title="{{ message.timeInQueue }}s">{{ helper.formatDuration(message.timeInQueue) }}</abbr>
</td>
</tr>
<tr>
<th class="text-end text-nowrap">Time to Handle</th>
<td>
{% if helper.canFormatDuration %}
<abbr title="{{ message.timeToHandle }}s">{{ helper.timeFormatter.formatDuration(message.timeToHandle) }}</abbr>
{% else %}
{{ message.timeToHandle }}s
{% endif %}
<abbr title="{{ message.timeToHandle }}s">{{ helper.formatDuration(message.timeToHandle) }}</abbr>
</td>
</tr>
<tr>
<th class="text-end text-nowrap">Dispatched At</th>
<td>
{% if helper.timeFormatter %}
<abbr title="{{ message.dispatchedAt|date('c') }}">{{ helper.timeFormatter.formatDiff(message.dispatchedAt) }}</abbr>
{% else %}
{{ message.dispatchedAt|date('c') }}
{% endif %}
<abbr title="{{ message.dispatchedAt|date('c') }}">{{ helper.formatTime(message.dispatchedAt) }}</abbr>
</td>
</tr>
<tr>
<th class="text-end text-nowrap">Received At</th>
<td>
{% if helper.timeFormatter %}
<abbr title="{{ message.receivedAt|date('c') }}">{{ helper.timeFormatter.formatDiff(message.receivedAt) }}</abbr>
{% else %}
{{ message.receivedAt|date('c') }}
{% endif %}
<abbr title="{{ message.receivedAt|date('c') }}">{{ helper.formatTime(message.receivedAt) }}</abbr>
</td>
</tr>
<tr>
<th class="text-end text-nowrap">Finished Handling At</th>
<td>
{% if helper.timeFormatter %}
<abbr title="{{ message.finishedAt|date('c') }}">{{ helper.timeFormatter.formatDiff(message.finishedAt) }}</abbr>
{% else %}
{{ message.finishedAt|date('c') }}
{% endif %}
<abbr title="{{ message.finishedAt|date('c') }}">{{ helper.formatTime(message.finishedAt) }}</abbr>
</td>
</tr>
<tr>
Expand Down
12 changes: 4 additions & 8 deletions templates/schedule.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -123,19 +123,15 @@
<svg fill="currentcolor" height="1em" width="1em" class="me-1 align-text-bottom" role="img" aria-label="Success:"><use xlink:href="#check-circle-fill"/></svg>
{% endif %}

{% if message and helper.timeFormatter %}
<abbr title="{{ message.finishedAt|date('c') }}">{{ helper.timeFormatter.formatDiff(message.finishedAt) }}</abbr>
{% elseif message %}
{{ message.finishedAt|date('c') }}
{% if message %}
<abbr title="{{ message.finishedAt|date('c') }}">{{ helper.formatTime(message.finishedAt) }}</abbr>
{% else %}
<em class="text-secondary">Never</em>
{% endif %}
</td>
<td class="text-center">
{% if message and helper.canFormatDuration %}
<abbr title="{{ message.timeToHandle }}s">{{ helper.timeFormatter.formatDuration(message.timeToHandle) }}</abbr>
{% elseif message %}
{{ message.timeToHandle }}s
{% if message %}
<abbr title="{{ message.timeToHandle }}s">{{ helper.formatDuration(message.timeToHandle) }}</abbr>
{% else %}
<em class="text-secondary">-</em>
{% endif %}
Expand Down
12 changes: 2 additions & 10 deletions templates/statistics.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -82,18 +82,10 @@
{% endif %}
</td>
<td>
{% if helper.canFormatDuration %}
<abbr title="{{ metric.averageWaitTime|round }}s">{{ helper.timeFormatter.formatDuration(metric.averageWaitTime) }}</abbr>
{% else %}
{{ metric.averageWaitTime|round }}s
{% endif %}
<abbr title="{{ metric.averageWaitTime|round }}s">{{ helper.formatDuration(metric.averageWaitTime) }}</abbr>
</td>
<td>
{% if helper.canFormatDuration %}
<abbr title="{{ metric.averageHandlingTime|round }}s">{{ helper.timeFormatter.formatDuration(metric.averageHandlingTime) }}</abbr>
{% else %}
{{ metric.averageHandlingTime|round }}s
{% endif %}
<abbr title="{{ metric.averageHandlingTime|round }}s">{{ helper.formatDuration(metric.averageHandlingTime) }}</abbr>
</td>
<td>
{{ metric.handledPerMinute|round(2) }}
Expand Down
6 changes: 1 addition & 5 deletions templates/transport.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,7 @@
</td>
<td>
{% if queued.dispatchedAt %}
{% if helper.timeFormatter %}
<abbr title="{{ queued.dispatchedAt|date('c') }}">{{ helper.timeFormatter.formatDiff(queued.dispatchedAt) }}</abbr>
{% else %}
{{ queued.dispatchedAt|date('c') }}
{% endif %}
<abbr title="{{ queued.dispatchedAt|date('c') }}">{{ helper.formatTime(queued.dispatchedAt) }}</abbr>
{% else %}
<em class="text-secondary">n/a</em>
{% endif %}
Expand Down

0 comments on commit 78cc0e5

Please sign in to comment.