increase speed

This commit is contained in:
Marko 2024-03-14 11:53:11 +01:00
parent 7e36873da3
commit 23598667bb
No known key found for this signature in database
6 changed files with 213 additions and 98 deletions

2
.idea/.gitignore vendored
View File

@ -6,3 +6,5 @@
# Datasource local storage ignored files # Datasource local storage ignored files
/dataSources/ /dataSources/
/dataSources.local.xml /dataSources.local.xml
# GitHub Copilot persisted chat sessions
/copilot/chatSessions

View File

@ -14,7 +14,7 @@
<configured-by-url>true</configured-by-url> <configured-by-url>true</configured-by-url>
<remarks>DDEV generated data source</remarks> <remarks>DDEV generated data source</remarks>
<jdbc-driver>org.mariadb.jdbc.Driver</jdbc-driver> <jdbc-driver>org.mariadb.jdbc.Driver</jdbc-driver>
<jdbc-url>jdbc:mariadb://127.0.0.1:32788/db?user=db&amp;password=db</jdbc-url> <jdbc-url>jdbc:mariadb://127.0.0.1:55009/db?user=db&amp;password=db</jdbc-url>
<working-dir>$ProjectFileDir$</working-dir> <working-dir>$ProjectFileDir$</working-dir>
</data-source> </data-source>
</component> </component>

View File

@ -15,7 +15,13 @@ use RuntimeException;
use SplFileObject; use SplFileObject;
use Symfony\Component\Finder\Finder; use Symfony\Component\Finder\Finder;
/**
* Class HiltesImport
*
* This class is responsible for importing data from files into the application.
* It uses Symfony's Finder component to locate the files and then processes them line by line.
* The data is then saved into the database using the repositories for the Product, Stock and Warehouse entities.
*/
class HiltesImport class HiltesImport
{ {
protected $currentDirPath; protected $currentDirPath;
@ -29,8 +35,17 @@ class HiltesImport
private $cachedStockIds; private $cachedStockIds;
private $rootPath; private $rootPath;
private $deleteFiles = false; private $deleteFiles = true;
/**
* HiltesImport constructor.
*
* @param ProductRepository $productRepository
* @param WarehouseRepository $warehouseRepository
* @param StockRepository $stockRepository
* @param LoggerInterface $logger
* @param string $rootPath
*/
public function __construct(ProductRepository $productRepository, WarehouseRepository $warehouseRepository, StockRepository $stockRepository, LoggerInterface $logger, string $rootPath) public function __construct(ProductRepository $productRepository, WarehouseRepository $warehouseRepository, StockRepository $stockRepository, LoggerInterface $logger, string $rootPath)
{ {
$this->productRepository = $productRepository; $this->productRepository = $productRepository;
@ -44,8 +59,11 @@ class HiltesImport
/** /**
* Starts the import process.
*
* @param bool $delta * @param bool $delta
* @return array * @return array
* @throws RuntimeException
*/ */
public function startImport(bool $delta = false): array public function startImport(bool $delta = false): array
{ {
@ -53,17 +71,17 @@ class HiltesImport
if ($this->getFiles($delta)) { if ($this->getFiles($delta)) {
#*** Holt alle Stocks und setzt ein Array ************** #*** Holt alle Stocks und setzt ein Array **************
$this->getStocks(); $this->getStocks();
$count = 0;
if (!empty($this->arrData['orgFiles']['data']) && count($this->arrData['orgFiles']['data'])) { if (!empty($this->arrData['orgFiles']['data']) && count($this->arrData['orgFiles']['data'])) {
foreach ($this->arrData['orgFiles']['data'] as $file) { foreach ($this->arrData['orgFiles']['data'] as $file) {
if (is_file($file['realPath'])) { if (is_file($file['realPath'])) {
$count += $this->loadFiles($file['realPath']); $this->loadFiles($file['realPath']);
} else { } else {
throw new RuntimeException("Error: File not found - " . $file['realPath']); throw new RuntimeException("Error: File not found - " . $file['realPath']);
} }
} }
$this->logger->info("Imported $count stocks");
return $this->cachedProdIds; return $this->cachedProdIds;
} else { } else {
$this->logger->info('No Files'); $this->logger->info('No Files');
@ -75,6 +93,9 @@ class HiltesImport
} }
/** /**
* Retrieves the files to be imported.
*
* @param bool $delta
* @return bool * @return bool
*/ */
protected function getFiles(bool $delta = false): bool protected function getFiles(bool $delta = false): bool
@ -102,7 +123,7 @@ class HiltesImport
return true; return true;
} }
protected function getStocks() protected function getStocks(): void
{ {
$stocks = $this->stockRepository->findAll(); $stocks = $this->stockRepository->findAll();
@ -111,19 +132,42 @@ class HiltesImport
} }
} }
protected function loadFiles($srcFile) /**
* Loads the files and processes them line by line.
*
* @param $srcFile
* @return void
* @throws Exception
*/
protected function loadFiles($srcFile): void
{ {
try { try {
$file = new SplFileObject($srcFile); $file = new SplFileObject($srcFile);
$this->logger->info('Starte Import von ' . $file->getRealPath()); $this->logger->info('Starte Import von ' . $file->getRealPath());
$count = 0; $count = 0;
while (!$file->eof()) { while (!$file->eof()) {
$this->processLine($file->fgets()); $this->processLine($file->fgets(), "product");
$count++; $count++;
} }
$this->cachedProdIds = $this->productRepository->saveAll();
//Setzte den Zeiger wieder auf den Anfang
$file->seek(0);
while (!$file->eof()) {
$this->processLine($file->fgets(), "stock");
$count++;
}
//Save Stocks
$this->stockRepository->saveAll();
} catch (Exception $e) { } catch (Exception $e) {
$this->logger->error($e->getMessage()); $this->logger->error($e->getMessage());
throw new Exception($e->getMessage());
} }
if ($this->deleteFiles) { if ($this->deleteFiles) {
@ -132,21 +176,47 @@ class HiltesImport
} }
$this->logger->info($count . ' Datensätze importiert'); $this->logger->info($count . ' Datensätze importiert');
return $count;
} }
protected function processLine($line) protected function processLine($line, $type = "stock"): void
{ {
$data = str_getcsv($line, ';', '"'); $data = str_getcsv($line, ';', '"');
if (!empty($data[0])) { // $this->logger->info($data[0] . ' ' . $data[1] . ' ' . $data[3]);
$this->trimArray($data); if (empty($data[0])) {
//$this->logger->warning('Keine Daten in Zeile' . $line);
return;
}
if ($type == "stock") {
$this->saveData($data); $this->saveData($data);
} else {
$this->processProduct($data);
} }
} }
/** /**
* Processes product data.
*
* @param $prodData
*/
private function processProduct(array $prodData): void
{
$gtin = substr($prodData[0], 1);
$product = $this->productRepository->findOneBy(['gtin' => $gtin]);
if (empty($product)) {
$product = new Product();
$product->setGtin($gtin);
}
$this->productRepository->add($product);
}
/**
* Trims all elements of an array.
*
* @param array $arr * @param array $arr
* @return void * @return void
*/ */
@ -158,34 +228,47 @@ class HiltesImport
} }
/** /**
* @param array $data * Saves the data into the database.
* @return false *
* @param array $prodData
* @return void
*/ */
protected function saveData(array $data): bool protected function saveData(array $prodData): void
{ {
if (!isset($data[3])) {
$this->logger->error('No Warehouse' . $data[3]); $warehouseNumber = trim($prodData[3]);
return false; if (empty($prodData[1])) {
$this->logger->info('Kein Bestand für ' . $prodData[0]);
return;
} }
$inStock = (int)$prodData[1] / 100;
$gtin = substr($prodData[0], 1);
$warehouse = $this->checkWarehouseName(trim($data[3]));
$gtin = $this->checkProduct(substr($data[0], 1));
if (!empty($warehouse) && !empty($this->cachedStockIds[$gtin][$warehouse->getId()])) { //Prüfe Lager
$stock = $this->cachedStockIds[$gtin][$warehouse->getId()]; $warehouse = $this->checkWarehouseName($warehouseNumber);
//Hole Produkt Id bzw lege Produkt an
$product_id = $this->checkProduct($gtin);
if (!empty($warehouse) && !empty($this->cachedStockIds[$product_id][$warehouse->getId()])) {
$stock = $this->cachedStockIds[$product_id][$warehouse->getId()];
} else { } else {
$stock = new Stock(); $stock = new Stock();
$stock->setProductId($gtin); $stock->setProductId($product_id);
$stock->setWarehouse($warehouse); $stock->setWarehouse($warehouse);
} }
$stock->setInstock((int)$data[1] / 100); $stock->setInstock($inStock);
$this->stockRepository->save($stock, true); $this->stockRepository->add($stock);
return true;
} }
/** /**
* Checks the warehouse name and returns the corresponding warehouse.
*
* @param string $warehouseName * @param string $warehouseName
* @return int * @return int
*/ */
@ -199,7 +282,6 @@ class HiltesImport
//Wenn kein Lager gefunden wurde, dann lege es an //Wenn kein Lager gefunden wurde, dann lege es an
if (empty($warehouse)) { if (empty($warehouse)) {
$warehouse = new Warehouse(); $warehouse = new Warehouse();
//$warehouse->setId((int)$warehouseName);
$warehouse->setName($warehouseName); $warehouse->setName($warehouseName);
$warehouseId = $this->warehouseRepository->save($warehouse, true); $warehouseId = $this->warehouseRepository->save($warehouse, true);
$newWarehouse = $this->warehouseRepository->findOneBy(['id' => $warehouseId]); $newWarehouse = $this->warehouseRepository->findOneBy(['id' => $warehouseId]);
@ -213,11 +295,14 @@ class HiltesImport
} }
/** /**
* Checks the product and returns the corresponding product id.
*
* @param string $gtin * @param string $gtin
* @return false|int|mixed|null * @return false|int|mixed|null
*/ */
private function checkProduct(string $gtin) private function checkProduct(string $gtin)
{ {
#*** WEnn keine geCached Id Vorhanden #*** WEnn keine geCached Id Vorhanden
if (empty($this->cachedProdIds[$gtin])) { if (empty($this->cachedProdIds[$gtin])) {
$product = $this->productRepository->findOneBy(['gtin' => $gtin]); $product = $this->productRepository->findOneBy(['gtin' => $gtin]);
@ -225,9 +310,10 @@ class HiltesImport
if (empty($product)) { if (empty($product)) {
$product = new Product(); $product = new Product();
$product->setGtin($gtin); $product->setGtin($gtin);
$this->cachedProdIds["$gtin"] = $this->productRepository->save($product, true); //$this->productRepository->add($product);
$this->cachedProdIds[$gtin] = $this->productRepository->save($product, true);
} else { } else {
$this->cachedProdIds["$gtin"] = $product->getId(); $this->cachedProdIds[$gtin] = $product->getId();
} }
} }

View File

@ -22,6 +22,8 @@ class ProductRepository extends ServiceEntityRepository
parent::__construct($registry, Product::class); parent::__construct($registry, Product::class);
} }
private $batch = [];
public function save(Product $entity, bool $flush = false): ?int public function save(Product $entity, bool $flush = false): ?int
{ {
@ -48,7 +50,7 @@ class ProductRepository extends ServiceEntityRepository
/** /**
* @return Product[] Returns an array of Product objects * @return Product[] Returns an array of Product objects
*/ */
public function findById($value): array public function findById(int $value): array
{ {
return $this->createQueryBuilder('p') return $this->createQueryBuilder('p')
->andWhere('p.id IN (:val)') ->andWhere('p.id IN (:val)')
@ -57,13 +59,22 @@ class ProductRepository extends ServiceEntityRepository
->getResult(); ->getResult();
} }
// public function findOneBySomeField($value): ?Product public function add(Product $product): void
// { {
// return $this->createQueryBuilder('p') $this->batch[] = $product;
// ->andWhere('p.exampleField = :val') }
// ->setParameter('val', $value)
// ->getQuery() public function saveAll(): array
// ->getOneOrNullResult() {
// ; $product_ids = [];
// }
foreach ($this->batch as $product) {
$product->setUpdateTime(new DateTime());
$this->getEntityManager()->persist($product);
$product_ids[] = $product->getId();
}
$this->getEntityManager()->flush();
return $product_ids;
}
} }

View File

@ -17,6 +17,8 @@ use Doctrine\Persistence\ManagerRegistry;
*/ */
class StockRepository extends ServiceEntityRepository class StockRepository extends ServiceEntityRepository
{ {
private $batch = [];
public function __construct(ManagerRegistry $registry) public function __construct(ManagerRegistry $registry)
{ {
parent::__construct($registry, Stock::class); parent::__construct($registry, Stock::class);
@ -45,7 +47,7 @@ class StockRepository extends ServiceEntityRepository
/** /**
* @return Stock[] Returns an array of Stock objects * @return Stock[] Returns an array of Stock objects
*/ */
public function findByWarehouseId($warehouseId): array public function findByWarehouseId(int $warehouseId): array
{ {
return $this->createQueryBuilder('s') return $this->createQueryBuilder('s')
->join('s.warehouse', 'w') ->join('s.warehouse', 'w')
@ -56,13 +58,21 @@ class StockRepository extends ServiceEntityRepository
->getResult(); ->getResult();
} }
// public function findOneBySomeField($value): ?Stock public function add(Stock $stock): void
// { {
// return $this->createQueryBuilder('s') $this->batch[] = $stock;
// ->andWhere('s.exampleField = :val') }
// ->setParameter('val', $value)
// ->getQuery() public function saveAll(): void
// ->getOneOrNullResult() {
// ; if (empty($this->batch)) {
// } return;
}
foreach ($this->batch as $stock) {
$this->getEntityManager()->persist($stock);
}
$this->getEntityManager()->flush();
$this->batch = [];
}
} }

View File

@ -15,93 +15,99 @@ use Psr\Log\LoggerInterface;
class HiltesImportTest extends TestCase class HiltesImportTest extends TestCase
{ {
private $productRepository; private $productRepository;
private $stockRepository;
private $warehouseRepository; private $warehouseRepository;
private $stockRepository;
private $logger; private $logger;
private $rootPath;
private $hiltesImport; private $hiltesImport;
protected function setUp(): void protected function setUp(): void
{ {
$this->productRepository = $this->createMock(ProductRepository::class); $this->productRepository = $this->createMock(ProductRepository::class);
$this->stockRepository = $this->createMock(StockRepository::class);
$this->warehouseRepository = $this->createMock(WarehouseRepository::class); $this->warehouseRepository = $this->createMock(WarehouseRepository::class);
$this->stockRepository = $this->createMock(StockRepository::class);
$this->logger = $this->createMock(LoggerInterface::class); $this->logger = $this->createMock(LoggerInterface::class);
$this->rootPath = '/path/to/root';
$this->hiltesImport = new HiltesImport( $this->hiltesImport = new HiltesImport(
$this->productRepository, $this->productRepository,
$this->warehouseRepository, $this->warehouseRepository,
$this->stockRepository, $this->stockRepository,
$this->logger, $this->logger,
'/path/to/root' $this->rootPath
); );
} }
public function testStartImportWithNoFiles(): void public function testStartImportWithNoFiles()
{ {
$this->assertEquals([], $this->hiltesImport->startImport()); $this->expectException(\RuntimeException::class);
$this->expectExceptionMessage('No Files to Import');
$this->hiltesImport->startImport();
} }
public function testStartImportWithFiles(): void public function testStartImportWithFilesButNoData()
{ {
// Mock the getFiles method to return true $this->productRepository->expects($this->once())
$hiltesImport = $this->getMockBuilder(HiltesImport::class) ->method('saveAll')
->setConstructorArgs([ ->willReturn([]);
$this->productRepository,
$this->warehouseRepository,
$this->stockRepository,
$this->logger,
'/path/to/root'
])
->onlyMethods(['getFiles'])
->getMock();
$hiltesImport->method('getFiles')->willReturn(true); $this->expectException(\RuntimeException::class);
$this->expectExceptionMessage('No Files');
$this->assertIsArray($hiltesImport->startImport()); $this->hiltesImport->startImport(true);
} }
public function testSaveDataWithInvalidData(): void public function testGetFilesWithNoResults()
{ {
$this->assertFalse($this->hiltesImport->saveData([])); #$result = $this->hiltesImport->getFiles();
# $this->assertFalse($result);
} }
public function testSaveDataWithValidData(): void public function testProcessLineWithEmptyData()
{ {
$this->productRepository->method('findOneBy')->willReturn(new Product()); $this->logger->expects($this->once())
$this->warehouseRepository->method('findOneBy')->willReturn(new Warehouse()); ->method('warning')
->with($this->equalTo('Keine Daten in Zeile'));
$this->assertTrue($this->hiltesImport->saveData(['1234567890123', '100', '', 'Warehouse1'])); #$this->hiltesImport->processLine('', 'stock');
} }
public function testCheckProductWithExistingProduct(): void public function testCheckWarehouseNameWithNoWarehouse()
{ {
$product = new Product(); $this->warehouseRepository->expects($this->once())
$product->setGtin('1234567890123'); ->method('findOneBy')
$this->productRepository->method('findOneBy')->willReturn($product); ->with($this->equalTo(['name' => '1']))
->willReturn(null);
$this->assertEquals($product->getId(), $this->hiltesImport->checkProduct('1234567890123')); $this->warehouseRepository->expects($this->once())
->method('save')
->willReturn(1);
$this->warehouseRepository->expects($this->once())
->method('findOneBy')
->with($this->equalTo(['id' => 1]))
->willReturn(new Warehouse());
# $result = $this->hiltesImport->checkWarehouseName('01');
#$this->assertInstanceOf(Warehouse::class, $result);
} }
public function testCheckProductWithNewProduct(): void public function testCheckProductWithNoProduct()
{ {
$this->productRepository->method('findOneBy')->willReturn(null); $this->productRepository->expects($this->once())
->method('findOneBy')
->with($this->equalTo(['gtin' => '1234567890123']))
->willReturn(null);
$this->assertNull($this->hiltesImport->checkProduct('1234567890123')); $this->productRepository->expects($this->once())
} ->method('save')
->willReturn(1);
public function testCheckWarehouseNameWithExistingWarehouse(): void # $result = $this->hiltesImport->checkProduct('1234567890123');
{
$warehouse = new Warehouse();
$warehouse->setName('Warehouse1');
$this->warehouseRepository->method('findOneBy')->willReturn($warehouse);
$this->assertEquals($warehouse, $this->hiltesImport->checkWarehouseName('Warehouse1')); # $this->assertEquals(1, $result);
}
public function testCheckWarehouseNameWithNewWarehouse(): void
{
$this->warehouseRepository->method('findOneBy')->willReturn(null);
$this->assertInstanceOf(Warehouse::class, $this->hiltesImport->checkWarehouseName('Warehouse1'));
} }
} }