mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2025-12-06 19:19:29 +00:00
Pass parts object directly to BulkSearchRequestDTO and added some syntax hints
This commit is contained in:
parent
0e99faee0a
commit
41a7238ab7
5 changed files with 60 additions and 40 deletions
|
|
@ -182,7 +182,7 @@ class BulkInfoProviderImportController extends AbstractController
|
|||
$searchRequest = new BulkSearchRequestDTO(
|
||||
fieldMappings: $fieldMappings,
|
||||
prefetchDetails: $prefetchDetails,
|
||||
partIds: $partIds
|
||||
parts: $parts
|
||||
);
|
||||
|
||||
$searchResults = $this->bulkService->performBulkSearch($searchRequest);
|
||||
|
|
@ -444,7 +444,7 @@ class BulkInfoProviderImportController extends AbstractController
|
|||
$searchRequest = new BulkSearchRequestDTO(
|
||||
fieldMappings: $fieldMappings,
|
||||
prefetchDetails: $prefetchDetails,
|
||||
partIds: [$partId]
|
||||
parts: [$part]
|
||||
);
|
||||
|
||||
try {
|
||||
|
|
@ -501,14 +501,16 @@ class BulkInfoProviderImportController extends AbstractController
|
|||
}
|
||||
|
||||
// Get all part IDs that are not completed or skipped
|
||||
$parts = [];
|
||||
$partIds = [];
|
||||
foreach ($job->getJobParts() as $jobPart) {
|
||||
if (!$jobPart->isCompleted() && !$jobPart->isSkipped()) {
|
||||
$partIds[] = $jobPart->getPart()->getId();
|
||||
$parts[] = $jobPart->getPart();
|
||||
$partsIds[] = $jobPart->getPart()->getId();
|
||||
}
|
||||
}
|
||||
|
||||
if (empty($partIds)) {
|
||||
if (empty($parts)) {
|
||||
return $this->json([
|
||||
'success' => true,
|
||||
'message' => 'No parts to research',
|
||||
|
|
@ -523,13 +525,13 @@ class BulkInfoProviderImportController extends AbstractController
|
|||
// Process in batches to reduce memory usage for large operations
|
||||
$batchSize = 20; // Configurable batch size for memory management
|
||||
$allResults = [];
|
||||
$batches = array_chunk($partIds, $batchSize);
|
||||
$batches = array_chunk($parts, $batchSize);
|
||||
|
||||
foreach ($batches as $batch) {
|
||||
$searchRequest = new BulkSearchRequestDTO(
|
||||
fieldMappings: $fieldMappings,
|
||||
prefetchDetails: $prefetchDetails,
|
||||
partIds: $batch
|
||||
parts: $batch
|
||||
);
|
||||
|
||||
$batchResults = $this->bulkService->performBulkSearch($searchRequest);
|
||||
|
|
@ -552,8 +554,8 @@ class BulkInfoProviderImportController extends AbstractController
|
|||
|
||||
return $this->json([
|
||||
'success' => true,
|
||||
'researched_count' => count($partIds),
|
||||
'message' => sprintf('Successfully researched %d parts', count($partIds))
|
||||
'researched_count' => count($parts),
|
||||
'message' => sprintf('Successfully researched %d parts', count($parts))
|
||||
]);
|
||||
|
||||
} catch (\Exception $e) {
|
||||
|
|
@ -562,7 +564,7 @@ class BulkInfoProviderImportController extends AbstractController
|
|||
500,
|
||||
[
|
||||
'job_id' => $jobId,
|
||||
'part_ids' => $partIds,
|
||||
'part_ids' => $partsIds,
|
||||
'exception' => $e->getMessage()
|
||||
]
|
||||
);
|
||||
|
|
|
|||
|
|
@ -397,6 +397,10 @@ class BulkInfoProviderImportJob extends AbstractDBElement
|
|||
return $completed >= $total;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $searchResults
|
||||
* @return array{part_id: int, search_results: array{dto: array{provider_key: string, provider_id: string, name: string, description: string, manufacturer: string, mpn: string, provider_url: string, preview_image_url: string, _source_field: string|null, _source_keyword: string|null}, localPart: int|null}[], errors: string[]}[]
|
||||
*/
|
||||
public function serializeSearchResults(array $searchResults): array
|
||||
{
|
||||
$serialized = [];
|
||||
|
|
@ -433,6 +437,10 @@ class BulkInfoProviderImportJob extends AbstractDBElement
|
|||
return $serialized;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param EntityManagerInterface|null $entityManager
|
||||
* @return array{part: Part, search_results: array{dto: SearchResultDTO, localPart: Part|null, source_field: string|null, source_keyword: string|null}[], errors: string[]}[]
|
||||
*/
|
||||
public function deserializeSearchResults(?EntityManagerInterface $entityManager = null): array
|
||||
{
|
||||
if (empty($this->searchResults)) {
|
||||
|
|
|
|||
|
|
@ -27,11 +27,7 @@ final class BulkInfoProviderService
|
|||
|
||||
public function performBulkSearch(BulkSearchRequestDTO $request): array
|
||||
{
|
||||
// Convert string IDs to integers
|
||||
$partIds = array_map('intval', $request->partIds);
|
||||
|
||||
$partRepository = $this->entityManager->getRepository(Part::class);
|
||||
$parts = $partRepository->getElementsFromIDArray($partIds);
|
||||
$parts = $request->parts;
|
||||
|
||||
if (empty($parts)) {
|
||||
throw new \InvalidArgumentException('No valid parts found for bulk import');
|
||||
|
|
@ -145,6 +141,13 @@ final class BulkInfoProviderService
|
|||
return $batchResults;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Part[] $parts
|
||||
* @param array $fieldMappings
|
||||
* @param array $regularProviders
|
||||
* @param array $excludeResults
|
||||
* @return array
|
||||
*/
|
||||
private function processRegularProviders(array $parts, array $fieldMappings, array $regularProviders, array $excludeResults): array
|
||||
{
|
||||
$regularResults = [];
|
||||
|
|
|
|||
|
|
@ -4,11 +4,18 @@ declare(strict_types=1);
|
|||
|
||||
namespace App\Services\InfoProviderSystem\DTOs;
|
||||
|
||||
class BulkSearchRequestDTO
|
||||
use App\Entity\Parts\Part;
|
||||
|
||||
readonly class BulkSearchRequestDTO
|
||||
{
|
||||
/**
|
||||
* @param array $fieldMappings
|
||||
* @param bool $prefetchDetails
|
||||
* @param Part[] $parts The parts for which the bulk search should be performed.
|
||||
*/
|
||||
public function __construct(
|
||||
public readonly array $fieldMappings,
|
||||
public readonly bool $prefetchDetails = false,
|
||||
public readonly array $partIds = []
|
||||
public array $fieldMappings,
|
||||
public bool $prefetchDetails = false,
|
||||
public array $parts = []
|
||||
) {}
|
||||
}
|
||||
|
|
@ -664,7 +664,7 @@ class BulkInfoProviderImportControllerTest extends WebTestCase
|
|||
['field' => 'mpn', 'providers' => ['test'], 'priority' => 2]
|
||||
],
|
||||
prefetchDetails: false,
|
||||
partIds: [$part->getId()]
|
||||
parts: [$part->getId()]
|
||||
);
|
||||
|
||||
// The service may return an empty result or throw when no results are found
|
||||
|
|
@ -799,7 +799,7 @@ class BulkInfoProviderImportControllerTest extends WebTestCase
|
|||
['field' => 'test_supplier_spn', 'providers' => ['test'], 'priority' => 2]
|
||||
],
|
||||
prefetchDetails: false,
|
||||
partIds: [$part->getId()]
|
||||
parts: [$part->getId()]
|
||||
);
|
||||
|
||||
// The service should be able to process the request and throw an exception when no results are found
|
||||
|
|
@ -833,7 +833,7 @@ class BulkInfoProviderImportControllerTest extends WebTestCase
|
|||
['field' => 'name', 'providers' => ['lcsc'], 'priority' => 1]
|
||||
],
|
||||
prefetchDetails: false,
|
||||
partIds: [$part->getId()]
|
||||
parts: [$part->getId()]
|
||||
);
|
||||
|
||||
// The service should be able to process the request and throw an exception when no results are found
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue