optimize tag and rebuilding

This commit is contained in:
team2
2026-02-23 08:51:21 +01:00
parent 8c58d6777d
commit dceeaeee52
8 changed files with 678 additions and 167 deletions

View File

@@ -6,27 +6,33 @@ namespace App\Controller\Admin;
use App\Entity\Document;
use App\Entity\Tag;
use App\Entity\TagRebuildJob;
use App\Service\TagRebuildJobService;
use Doctrine\DBAL\Types\Types;
use App\Tag\TagService;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Uid\Uuid;
#[Route('/admin/documents')]
final class DocumentTagController extends AbstractController
{
#[Route('/{id}/tags', name: 'admin_document_tags_edit', methods: ['GET'])]
public function edit(string $id, EntityManagerInterface $em): Response
{
public function edit(
string $id,
EntityManagerInterface $em,
TagRebuildJobService $jobs
): Response {
$document = $em->getRepository(Document::class)->find($id);
if (!$document instanceof Document) {
throw $this->createNotFoundException('Document not found');
}
// 🔹 Alle verfügbaren Tags laden (fehlte!)
$allTags = $em->createQueryBuilder()
->select('t')
->from(Tag::class, 't')
@@ -34,15 +40,16 @@ final class DocumentTagController extends AbstractController
->getQuery()
->getResult();
$assigned = [];
foreach ($document->getTags() as $tag) {
$assigned[(string)$tag->getId()] = true;
}
$latestJob = $jobs->getLatestJob();
return $this->render('admin/document_tags/edit.html.twig', [
'document' => $document,
'allTags' => $allTags,
'assigned' => $assigned,
'document' => $document,
'allTags' => $allTags, // ✅ jetzt vorhanden
'latestJob' => $latestJob,
'statusRunning' => TagRebuildJob::STATUS_RUNNING,
'statusQueued' => TagRebuildJob::STATUS_QUEUED,
'statusCompleted' => TagRebuildJob::STATUS_COMPLETED,
'statusFailed' => TagRebuildJob::STATUS_FAILED,
]);
}
@@ -51,7 +58,7 @@ final class DocumentTagController extends AbstractController
string $id,
Request $request,
EntityManagerInterface $em,
TagRebuildJobService $jobs
TagService $tagService
): RedirectResponse {
$document = $em->getRepository(Document::class)->find($id);
@@ -61,33 +68,23 @@ final class DocumentTagController extends AbstractController
$selected = $request->request->all('tag_ids') ?? [];
$uuidObjects = [];
foreach ($selected as $value) {
try {
$uuidObjects[] = \Symfony\Component\Uid\Uuid::fromString($value);
} catch (\Throwable) {
continue;
}
try {
$tagService->syncDocumentTags($document, $selected);
$this->addFlash('success', 'Tags wurden aktualisiert. Rebuild läuft im Hintergrund.');
} catch (\Throwable $e) {
$this->addFlash('danger', $e->getMessage());
}
// Remove
foreach ($document->getTags() as $tag) {
if (!in_array($tag->getId(), $uuidObjects, false)) {
$document->removeTag($tag);
}
}
// Add
foreach ($uuidObjects as $uuid) {
$tag = $em->find(\App\Entity\Tag::class, $uuid);
if ($tag && !$document->hasTag($tag)) {
$document->addTag($tag);
}
}
$em->flush();
$jobs->enqueueAndStartAsync();
return $this->redirectToRoute('admin_document_tags_edit', ['id' => $id]);
}
#[Route('/admin/tags/status', name: 'admin_tags_status', methods: ['GET'])]
public function status(TagRebuildJobService $jobs): JsonResponse
{
$job = $jobs->getLatestJob();
return $this->json([
'status' => $job?->getStatus(),
]);
}
}

View File

@@ -5,7 +5,8 @@ declare(strict_types=1);
namespace App\Controller\Admin;
use App\Entity\Tag;
use App\Service\TagRebuildJobService;
use App\Entity\TagRebuildJob;
use App\Tag\TagService;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\RedirectResponse;
@@ -17,85 +18,76 @@ use Symfony\Component\Routing\Attribute\Route;
final class TagController extends AbstractController
{
#[Route('', name: 'admin_tags_index', methods: ['GET'])]
public function index(EntityManagerInterface $em): Response
{
$tags = $em->createQueryBuilder()
->select('t')
->from(Tag::class, 't')
->orderBy('t.label', 'ASC')
->getQuery()
->getResult();
public function index(
EntityManagerInterface $em,
\App\Service\TagRebuildJobService $jobs
): Response {
$tags = $em->getRepository(\App\Entity\Tag::class)
->findBy([], ['label' => 'ASC']);
return $this->render('admin/tag/index.html.twig', [
'tags' => $tags,
'latestJob' => $jobs->getLatestJob(),
'hasActiveJob' => $jobs->hasActiveJob(),
'statusRunning' => TagRebuildJob::STATUS_RUNNING,
'statusQueued' => TagRebuildJob::STATUS_QUEUED,
'statusCompleted' => TagRebuildJob::STATUS_COMPLETED,
'statusFailed' => TagRebuildJob::STATUS_FAILED,
]);
}
#[Route('/create', name: 'admin_tags_create', methods: ['POST'])]
public function create(Request $request, EntityManagerInterface $em, TagRebuildJobService $jobs): RedirectResponse
{
$token = (string)$request->request->get('_token', '');
public function create(
Request $request,
TagService $tagService
): RedirectResponse {
$token = (string) $request->request->get('_token', '');
if (!$this->isCsrfTokenValid('admin_tag_create', $token)) {
$this->addFlash('danger', 'Ungültiges CSRF Token.');
return $this->redirectToRoute('admin_tags_index');
}
$label = trim((string)$request->request->get('label', ''));
$slug = trim((string)$request->request->get('slug', ''));
$desc = trim((string)$request->request->get('description', ''));
try {
$tagService->create(
(string) $request->request->get('slug', ''),
(string) $request->request->get('label', ''),
$request->request->get('description')
? (string) $request->request->get('description')
: null
);
if ($label === '' || $slug === '') {
$this->addFlash('danger', 'Label und Slug sind Pflichtfelder.');
return $this->redirectToRoute('admin_tags_index');
$this->addFlash('success', 'Tag wurde erstellt. Rebuild läuft im Hintergrund.');
} catch (\Throwable $e) {
$this->addFlash('danger', $e->getMessage());
}
$exists = (int)$em->createQueryBuilder()
->select('COUNT(t.id)')
->from(Tag::class, 't')
->where('t.slug = :slug')
->setParameter('slug', $slug)
->getQuery()
->getSingleScalarResult();
if ($exists > 0) {
$this->addFlash('danger', 'Slug existiert bereits.');
return $this->redirectToRoute('admin_tags_index');
}
$tag = new Tag($slug, $label, $desc !== '' ? $desc : null);
$em->persist($tag);
$em->flush();
// enqueue async rebuild
$jobs->enqueueAndStartAsync();
$this->addFlash('success', 'Tag wurde erstellt. Rebuild läuft im Hintergrund.');
return $this->redirectToRoute('admin_tags_index');
}
#[Route('/{id}/delete', name: 'admin_tags_delete', methods: ['POST'])]
public function delete(string $id, Request $request, EntityManagerInterface $em, TagRebuildJobService $jobs): RedirectResponse
{
$token = (string)$request->request->get('_token', '');
public function delete(
string $id,
Request $request,
TagService $tagService
): RedirectResponse {
$token = (string) $request->request->get('_token', '');
if (!$this->isCsrfTokenValid('admin_tag_delete_' . $id, $token)) {
$this->addFlash('danger', 'Ungültiges CSRF Token.');
return $this->redirectToRoute('admin_tags_index');
}
$tag = $em->getRepository(Tag::class)->find($id);
if (!$tag instanceof Tag) {
$this->addFlash('danger', 'Tag nicht gefunden.');
return $this->redirectToRoute('admin_tags_index');
try {
$tagService->deleteById($id);
$this->addFlash('success', 'Tag wurde gelöscht. Rebuild läuft im Hintergrund.');
} catch (\Throwable $e) {
$this->addFlash('danger', $e->getMessage());
}
$em->remove($tag);
$em->flush();
// enqueue async rebuild
$jobs->enqueueAndStartAsync();
$this->addFlash('success', 'Tag wurde gelöscht. Rebuild läuft im Hintergrund.');
return $this->redirectToRoute('admin_tags_index');
}
}

View File

@@ -0,0 +1,62 @@
<?php
declare(strict_types=1);
namespace App\Controller\Admin;
use App\Entity\TagRebuildJob;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\StreamedResponse;
use Symfony\Component\Routing\Attribute\Route;
final class TagRebuildStreamController
{
#[Route('/admin/tags/rebuild/stream', name: 'admin_tags_rebuild_stream')]
public function stream(EntityManagerInterface $em): StreamedResponse
{
$response = new StreamedResponse(function () use ($em) {
// Sofort erstes Event senden (wichtig!)
echo "event: ping\n";
echo "data: " . json_encode(['init' => true]) . "\n\n";
@ob_flush();
@flush();
while (!connection_aborted()) {
$em->clear();
$job = $em->createQueryBuilder()
->select('j')
->from(TagRebuildJob::class, 'j')
->orderBy('j.createdAt', 'DESC')
->setMaxResults(1)
->getQuery()
->getOneOrNullResult();
if ($job) {
echo "event: message\n";
echo "data: " . json_encode([
'status' => $job->getStatus(),
'startedAt' => $job->getStartedAt()?->format(DATE_ATOM),
'finishedAt' => $job->getFinishedAt()?->format(DATE_ATOM),
'error' => $job->getErrorMessage(),
]) . "\n\n";
@ob_flush();
@flush();
}
sleep(2);
}
});
$response->headers->set('Content-Type', 'text/event-stream');
$response->headers->set('Cache-Control', 'no-cache');
$response->headers->set('Connection', 'keep-alive');
$response->headers->set('X-Accel-Buffering', 'no'); // 🔥 wichtig bei nginx
return $response;
}
}