Backup #1492

ID1492
Dateipfad/var/www/dev.campus.systemische-tools.de/src/Infrastructure/Docs/ChunkAnalysisService.php
Version17
Typ modified
Größe3.5 KB
Hashac1bc1c7673b51eda6e927d64618a5d5b5ab4627d5071c04b737019ac1b0646b
Datum2025-12-25 17:28:26
Geändert vonclaude-code-hook
GrundClaude Code Pre-Hook Backup vor Edit-Operation
Datei existiert Ja

Dateiinhalt

<?php

declare(strict_types=1);

namespace Infrastructure\Docs;

// @responsibility: Orchestriert Chunk-Analyse (koordiniert ChunkAnalyzer + ChunkRepository)

use RuntimeException;

final class ChunkAnalysisService implements ChunkProcessorInterface
{
    private const string TAXONOMY_MODEL = 'gemma3:4b-it-qat';
    private const int BATCH_SIZE = 10;

    public function __construct(
        private ChunkDataRepository $repository,
        private ChunkAnalyzer $analyzer
    ) {
    }

    /**
     * Analyzes a single chunk.
     *
     * @return array{taxonomy: array<string>, entities: array<array{name: string, type: string}>, keywords: array<string>}
     */
    public function analyzeChunk(int $chunkId): array
    {
        return $this->process($chunkId);
    }

    /**
     * Processes a single chunk (implements ChunkProcessorInterface).
     *
     * @return array{taxonomy: array<string>, entities: array<array{name: string, type: string}>, keywords: array<string>}
     */
    public function process(int $chunkId): array
    {
        $chunk = $this->repository->findById($chunkId);

        if ($chunk === null) {
            throw new RuntimeException("Chunk #{$chunkId} not found");
        }

        // Mark as processing
        $this->repository->updateStatus($chunkId, 'processing');

        try {
            // Get document context
            $docContext = $this->repository->getDocumentContext((int) $chunk['dokumentation_id']);

            // Perform analysis via ChunkAnalyzer
            $analysis = $this->analyzer->analyze($chunk, $docContext);

            // Store results
            $this->repository->storeAnalysisResults($chunkId, $analysis, self::TAXONOMY_MODEL);

            return $analysis;
        } catch (RuntimeException $e) {
            $this->repository->updateStatus($chunkId, 'failed', $e->getMessage());

            throw $e;
        }
    }

    /**
     * Analyzes all pending chunks in batches.
     *
     * @return array{analyzed: int, failed: int, errors: array<string>}
     */
    public function analyzeAllPending(int $limit = 100): array
    {
        return $this->processBatch($limit);
    }

    /**
     * Processes multiple chunks in batch (implements ChunkProcessorInterface).
     *
     * @return array{processed: int, failed: int, errors: array<string>}
     */
    public function processBatch(int $limit): array
    {
        $results = ['processed' => 0, 'failed' => 0, 'errors' => []];

        $chunks = $this->repository->findPending($limit);

        foreach ($chunks as $chunk) {
            try {
                $this->process((int) $chunk['id']);
                $results['processed']++;

                // Progress output
                if ($results['processed'] % self::BATCH_SIZE === 0) {
                    echo "Analyzed {$results['processed']} chunks...\n";
                }
            } catch (RuntimeException $e) {
                $results['failed']++;
                $results['errors'][] = "Chunk #{$chunk['id']}: " . $e->getMessage();
            }
        }

        // Return with legacy key 'analyzed' for backward compatibility
        $results['analyzed'] = $results['processed'];

        return $results;
    }

    /**
     * Gets analysis statistics (implements ChunkProcessorInterface).
     *
     * @return array{pending: int, processing: int, completed: int, failed: int, by_category: array<array{category: string, count: int}>}
     */
    public function getStats(): array
    {
        return $this->repository->getStats();
    }
}

Vollständig herunterladen

Aktionen

Herunterladen

Andere Versionen dieser Datei

ID Version Typ Größe Datum
1864 19 modified 3.5 KB 2025-12-27 23:48
1860 18 modified 3.5 KB 2025-12-27 23:47
1492 17 modified 3.5 KB 2025-12-25 17:28
1441 16 modified 3.5 KB 2025-12-25 17:00
1425 15 modified 4.5 KB 2025-12-25 16:59
1419 14 modified 13.2 KB 2025-12-25 16:59
1406 13 modified 12.8 KB 2025-12-25 16:58
1399 12 modified 12.4 KB 2025-12-25 16:58
1395 11 modified 12.5 KB 2025-12-25 16:58
856 10 modified 12.6 KB 2025-12-23 08:46
855 9 modified 12.7 KB 2025-12-23 08:46
786 8 modified 12.9 KB 2025-12-23 08:05
398 7 modified 12.9 KB 2025-12-22 08:49
397 6 modified 13.0 KB 2025-12-22 08:49
328 5 modified 13.0 KB 2025-12-22 08:08
327 4 modified 12.9 KB 2025-12-22 08:08
326 3 modified 12.9 KB 2025-12-22 08:08
36 2 modified 13.7 KB 2025-12-20 17:23
27 1 modified 13.7 KB 2025-12-20 17:18

← Zurück zur Übersicht