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(
|
$searchRequest = new BulkSearchRequestDTO(
|
||||||
fieldMappings: $fieldMappings,
|
fieldMappings: $fieldMappings,
|
||||||
prefetchDetails: $prefetchDetails,
|
prefetchDetails: $prefetchDetails,
|
||||||
partIds: $partIds
|
parts: $parts
|
||||||
);
|
);
|
||||||
|
|
||||||
$searchResults = $this->bulkService->performBulkSearch($searchRequest);
|
$searchResults = $this->bulkService->performBulkSearch($searchRequest);
|
||||||
|
|
@ -444,7 +444,7 @@ class BulkInfoProviderImportController extends AbstractController
|
||||||
$searchRequest = new BulkSearchRequestDTO(
|
$searchRequest = new BulkSearchRequestDTO(
|
||||||
fieldMappings: $fieldMappings,
|
fieldMappings: $fieldMappings,
|
||||||
prefetchDetails: $prefetchDetails,
|
prefetchDetails: $prefetchDetails,
|
||||||
partIds: [$partId]
|
parts: [$part]
|
||||||
);
|
);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|
@ -501,14 +501,16 @@ class BulkInfoProviderImportController extends AbstractController
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get all part IDs that are not completed or skipped
|
// Get all part IDs that are not completed or skipped
|
||||||
|
$parts = [];
|
||||||
$partIds = [];
|
$partIds = [];
|
||||||
foreach ($job->getJobParts() as $jobPart) {
|
foreach ($job->getJobParts() as $jobPart) {
|
||||||
if (!$jobPart->isCompleted() && !$jobPart->isSkipped()) {
|
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([
|
return $this->json([
|
||||||
'success' => true,
|
'success' => true,
|
||||||
'message' => 'No parts to research',
|
'message' => 'No parts to research',
|
||||||
|
|
@ -523,13 +525,13 @@ class BulkInfoProviderImportController extends AbstractController
|
||||||
// Process in batches to reduce memory usage for large operations
|
// Process in batches to reduce memory usage for large operations
|
||||||
$batchSize = 20; // Configurable batch size for memory management
|
$batchSize = 20; // Configurable batch size for memory management
|
||||||
$allResults = [];
|
$allResults = [];
|
||||||
$batches = array_chunk($partIds, $batchSize);
|
$batches = array_chunk($parts, $batchSize);
|
||||||
|
|
||||||
foreach ($batches as $batch) {
|
foreach ($batches as $batch) {
|
||||||
$searchRequest = new BulkSearchRequestDTO(
|
$searchRequest = new BulkSearchRequestDTO(
|
||||||
fieldMappings: $fieldMappings,
|
fieldMappings: $fieldMappings,
|
||||||
prefetchDetails: $prefetchDetails,
|
prefetchDetails: $prefetchDetails,
|
||||||
partIds: $batch
|
parts: $batch
|
||||||
);
|
);
|
||||||
|
|
||||||
$batchResults = $this->bulkService->performBulkSearch($searchRequest);
|
$batchResults = $this->bulkService->performBulkSearch($searchRequest);
|
||||||
|
|
@ -552,8 +554,8 @@ class BulkInfoProviderImportController extends AbstractController
|
||||||
|
|
||||||
return $this->json([
|
return $this->json([
|
||||||
'success' => true,
|
'success' => true,
|
||||||
'researched_count' => count($partIds),
|
'researched_count' => count($parts),
|
||||||
'message' => sprintf('Successfully researched %d parts', count($partIds))
|
'message' => sprintf('Successfully researched %d parts', count($parts))
|
||||||
]);
|
]);
|
||||||
|
|
||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
|
|
@ -562,7 +564,7 @@ class BulkInfoProviderImportController extends AbstractController
|
||||||
500,
|
500,
|
||||||
[
|
[
|
||||||
'job_id' => $jobId,
|
'job_id' => $jobId,
|
||||||
'part_ids' => $partIds,
|
'part_ids' => $partsIds,
|
||||||
'exception' => $e->getMessage()
|
'exception' => $e->getMessage()
|
||||||
]
|
]
|
||||||
);
|
);
|
||||||
|
|
|
||||||
|
|
@ -397,6 +397,10 @@ class BulkInfoProviderImportJob extends AbstractDBElement
|
||||||
return $completed >= $total;
|
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
|
public function serializeSearchResults(array $searchResults): array
|
||||||
{
|
{
|
||||||
$serialized = [];
|
$serialized = [];
|
||||||
|
|
@ -433,6 +437,10 @@ class BulkInfoProviderImportJob extends AbstractDBElement
|
||||||
return $serialized;
|
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
|
public function deserializeSearchResults(?EntityManagerInterface $entityManager = null): array
|
||||||
{
|
{
|
||||||
if (empty($this->searchResults)) {
|
if (empty($this->searchResults)) {
|
||||||
|
|
|
||||||
|
|
@ -27,11 +27,7 @@ final class BulkInfoProviderService
|
||||||
|
|
||||||
public function performBulkSearch(BulkSearchRequestDTO $request): array
|
public function performBulkSearch(BulkSearchRequestDTO $request): array
|
||||||
{
|
{
|
||||||
// Convert string IDs to integers
|
$parts = $request->parts;
|
||||||
$partIds = array_map('intval', $request->partIds);
|
|
||||||
|
|
||||||
$partRepository = $this->entityManager->getRepository(Part::class);
|
|
||||||
$parts = $partRepository->getElementsFromIDArray($partIds);
|
|
||||||
|
|
||||||
if (empty($parts)) {
|
if (empty($parts)) {
|
||||||
throw new \InvalidArgumentException('No valid parts found for bulk import');
|
throw new \InvalidArgumentException('No valid parts found for bulk import');
|
||||||
|
|
@ -145,6 +141,13 @@ final class BulkInfoProviderService
|
||||||
return $batchResults;
|
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
|
private function processRegularProviders(array $parts, array $fieldMappings, array $regularProviders, array $excludeResults): array
|
||||||
{
|
{
|
||||||
$regularResults = [];
|
$regularResults = [];
|
||||||
|
|
|
||||||
|
|
@ -4,11 +4,18 @@ declare(strict_types=1);
|
||||||
|
|
||||||
namespace App\Services\InfoProviderSystem\DTOs;
|
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 function __construct(
|
||||||
public readonly array $fieldMappings,
|
public array $fieldMappings,
|
||||||
public readonly bool $prefetchDetails = false,
|
public bool $prefetchDetails = false,
|
||||||
public readonly array $partIds = []
|
public array $parts = []
|
||||||
) {}
|
) {}
|
||||||
}
|
}
|
||||||
|
|
@ -664,7 +664,7 @@ class BulkInfoProviderImportControllerTest extends WebTestCase
|
||||||
['field' => 'mpn', 'providers' => ['test'], 'priority' => 2]
|
['field' => 'mpn', 'providers' => ['test'], 'priority' => 2]
|
||||||
],
|
],
|
||||||
prefetchDetails: false,
|
prefetchDetails: false,
|
||||||
partIds: [$part->getId()]
|
parts: [$part->getId()]
|
||||||
);
|
);
|
||||||
|
|
||||||
// The service may return an empty result or throw when no results are found
|
// 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]
|
['field' => 'test_supplier_spn', 'providers' => ['test'], 'priority' => 2]
|
||||||
],
|
],
|
||||||
prefetchDetails: false,
|
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
|
// 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]
|
['field' => 'name', 'providers' => ['lcsc'], 'priority' => 1]
|
||||||
],
|
],
|
||||||
prefetchDetails: false,
|
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
|
// 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