* } */ public function execute(): array { $taxonomyStats = $this->taxonomyRepository->getStats(); $chunkCoverage = $this->calculateChunkCoverage(); $entityValidation = $this->entityTaxonomyRepository->getValidationStats(); $termUsage = $this->chunkTaxonomyRepository->countByTaxonomyTerm(); return [ 'taxonomy_stats' => $taxonomyStats, 'chunk_coverage' => $chunkCoverage, 'entity_validation' => $entityValidation, 'term_usage' => $termUsage, ]; } /** * Get chunks that have no taxonomy mapping. * * @return array */ public function getUnmappedChunks(int $limit = 100): array { return $this->chunkTaxonomyRepository->getUnmappedChunks($limit); } /** * Get entity mappings that need validation. * * @return array<\Domain\Entity\EntityTaxonomyMapping> */ public function getUnvalidatedEntityMappings(int $limit = 100): array { return $this->entityTaxonomyRepository->getUnvalidatedMappings($limit); } /** * Get usage statistics for a specific taxonomy term. * * @return array{ * term: array|null, * chunk_count: int, * entity_count: int, * chunks: array, * entities: array * } */ public function getTaxonomyTermUsage(int $termId): array { $term = $this->taxonomyRepository->find($termId); $chunks = $this->chunkTaxonomyRepository->findByTaxonomyTermId($termId); $entities = $this->entityTaxonomyRepository->findByTaxonomyTermId($termId); return [ 'term' => $term, 'chunk_count' => count($chunks), 'entity_count' => count($entities), 'chunks' => $chunks, 'entities' => $entities, ]; } /** * Get all taxonomy terms with their usage counts. * * @return array */ public function getTermsWithUsage(): array { $terms = $this->taxonomyRepository->findAll(); $chunkCounts = $this->chunkTaxonomyRepository->countByTaxonomyTerm(); $result = []; foreach ($terms as $term) { $termId = (int) $term['id']; $entityMappings = $this->entityTaxonomyRepository->findByTaxonomyTermId($termId); $result[] = [ 'id' => $termId, 'name' => $term['name'], 'path' => $term['path'], 'chunk_count' => $chunkCounts[$termId] ?? 0, 'entity_count' => count($entityMappings), ]; } return $result; } /** * Calculate chunk coverage statistics. * * @return array{total: int, mapped: int, percentage: float} */ private function calculateChunkCoverage(): array { $taxonomyStats = $this->taxonomyRepository->getStats(); $mappedChunks = $taxonomyStats['tagged_chunks'] ?? 0; // Get total chunks from unmapped query (limit 0 would give us count) // For efficiency, we estimate based on the stats $unmappedSample = $this->chunkTaxonomyRepository->getUnmappedChunks(1); $hasUnmapped = count($unmappedSample) > 0; // Use a heuristic: if there are unmapped chunks, estimate total // In production, this would be a dedicated count query $total = $hasUnmapped ? max($mappedChunks + 1, $mappedChunks) : $mappedChunks; $percentage = $total > 0 ? round(($mappedChunks / $total) * 100, 2) : 100.0; return [ 'total' => $total, 'mapped' => $mappedChunks, 'percentage' => $percentage, ]; } }