Skip to content

Commit

Permalink
feat(kobo): Add device info into logs and improve UX
Browse files Browse the repository at this point in the history
  • Loading branch information
ragusa87 committed Nov 29, 2024
1 parent 1bb2bf3 commit fb27bc6
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 32 deletions.
4 changes: 4 additions & 0 deletions config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ services:
tags:
- { name: kernel.event_listener, event: kernel.request, method: onKernelRequest, priority: 101 }

App\Kobo\LogProcessor\KoboContextProcessor:
tags:
- { name: monolog.processor }

when@dev:
services:
Symfony\Component\HttpKernel\Profiler\Profiler: '@profiler'
31 changes: 0 additions & 31 deletions src/Controller/Kobo/KoboDeviceController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@
use App\Entity\User;
use App\Form\KoboType;
use App\Repository\KoboDeviceRepository;
use Devdot\Monolog\Parser;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
Expand All @@ -30,35 +28,6 @@ public function index(KoboDeviceRepository $koboDeviceRepository): Response
]);
}

#[Route('/logs', name: 'app_kobodevice_user_logs', methods: ['GET'])]
public function logs(ParameterBagInterface $parameterBag): Response
{
if (!$this->getUser() instanceof UserInterface) {
throw $this->createAccessDeniedException();
}

$records = [];

try {
$logDir = $parameterBag->get('kernel.logs_dir');
$env = $parameterBag->get('kernel.environment');

if (!is_string($logDir) || !is_string($env)) {
throw new \RuntimeException('Invalid log directory or environment');
}

$parser = new Parser($logDir.'/kobo.'.$env.'-'.date('Y-m-d').'.log');

$records = $parser->get();
} catch (\Exception $e) {
$this->addFlash('warning', $e->getMessage());
}

return $this->render('kobodevice_user/logs.html.twig', [
'records' => $records,
]);
}

#[Route('/new', name: 'app_kobodevice_user_new', methods: ['GET', 'POST'])]
public function new(Request $request, EntityManagerInterface $entityManager): Response
{
Expand Down
44 changes: 44 additions & 0 deletions src/Controller/Kobo/KoboDeviceLogsController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace App\Controller\Kobo;

use Devdot\Monolog\Parser;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Security\Core\User\UserInterface;

#[Route('/user/kobo')]
class KoboDeviceLogsController extends AbstractController
{
public function __construct(
#[Autowire(param: 'kernel.logs_dir')]
protected string $kernelLogsDir,
#[Autowire(param: 'kernel.environment')]
protected string $kernelEnvironment,
) {
}

#[Route('/logs', name: 'app_kobodevice_user_logs', methods: ['GET'])]
public function logs(): Response
{
if (!$this->getUser() instanceof UserInterface) {
throw $this->createAccessDeniedException();
}

$records = [];

try {
$parser = new Parser($this->kernelLogsDir.'/kobo.'.$this->kernelEnvironment.'-'.date('Y-m-d').'.log');

$records = $parser->get();
} catch (\Exception $e) {
$this->addFlash('warning', $e->getMessage());
}

return $this->render('kobodevice_user/logs.html.twig', [
'records' => $records,
]);
}
}
47 changes: 47 additions & 0 deletions src/Kobo/LogProcessor/KoboContextProcessor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace App\Kobo\LogProcessor;

use App\Entity\KoboDevice;
use App\Kobo\ParamConverter\KoboParamConverter;
use Monolog\LogRecord;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;

class KoboContextProcessor
{
public function __construct(
private readonly RequestStack $requestStack,
private readonly KoboParamConverter $koboParamConverter,
) {
}

public function __invoke(LogRecord $record): LogRecord
{
$request = $this->requestStack->getCurrentRequest();

if (!$request instanceof Request) {
return $record;
}

if (false === $request->attributes->has('isKoboRequest')) {
return $record;
}

$kobo = $this->getKoboFromRequest($request);
$koboString = $kobo?->getId() ?? 'unknown';
$record->extra['kobo'] = $koboString;

return $record;
}

private function getKoboFromRequest(Request $request): ?KoboDevice
{
$device = $request->attributes->get('koboDevice');
if ($device instanceof KoboDevice) {
return $device;
}

return $this->koboParamConverter->apply($request);
}
}
3 changes: 3 additions & 0 deletions src/Kobo/ParamConverter/KoboParamConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ public function supports(Request $request, ArgumentMetadata $argument): bool
public function apply(Request $request): ?KoboDevice
{
$value = $this->getFieldValue($request);
if ($value === null) {
return null;
}

return $this->bookRepository->findOneBy([$this->getFieldName() => $value]);
}
Expand Down
10 changes: 9 additions & 1 deletion templates/kobodevice_user/logs.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,22 @@
<thead>
<tr>
<th>Date</th>
<th>Device</th>
<th>channel</th>
<th>Message</th>
</tr>
</thead>
<tbody>
{% for record in records %}
<tr style="font-size: 0.8em">
<td>
{{ record.datetime|date('d.m.Y') }}&nbsp;{{ record.datetime|date('H:i:s') }}
{{ record.datetime|date('d.m.Y') }}&nbsp;{{ record.datetime|date('H:i:s') }}
</td>
<td>
{{ record.extra.kobo|default('unkown') }}
</td>
<td>
{{ record.channel }}
</td>
<td>
<details>
Expand Down

0 comments on commit fb27bc6

Please sign in to comment.