-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement file size aggregation services for assets (#187)
- Loading branch information
1 parent
196371f
commit beef99f
Showing
12 changed files
with
212 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
src/Model/Search/Modifier/Aggregation/Asset/FileSizeSumAggregation.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
/** | ||
* Pimcore | ||
* | ||
* This source file is available under two different licenses: | ||
* - GNU General Public License version 3 (GPLv3) | ||
* - Pimcore Commercial License (PCL) | ||
* Full copyright and license information is available in | ||
* LICENSE.md which is distributed with this source code. | ||
* | ||
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org) | ||
* @license http://www.pimcore.org/license GPLv3 and PCL | ||
*/ | ||
|
||
namespace Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Modifier\Aggregation\Asset; | ||
|
||
use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Modifier\SearchModifierInterface; | ||
|
||
final readonly class FileSizeSumAggregation implements SearchModifierInterface | ||
{ | ||
public function __construct( | ||
private string $aggregationName, | ||
) { | ||
} | ||
|
||
public function getAggregationName(): string | ||
{ | ||
return $this->aggregationName; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
src/Service/Search/SearchService/Asset/Aggregation/FileSizeAggregationService.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
/** | ||
* Pimcore | ||
* | ||
* This source file is available under two different licenses: | ||
* - GNU General Public License version 3 (GPLv3) | ||
* - Pimcore Commercial License (PCL) | ||
* Full copyright and license information is available in | ||
* LICENSE.md which is distributed with this source code. | ||
* | ||
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org) | ||
* @license http://www.pimcore.org/license GPLv3 and PCL | ||
*/ | ||
|
||
namespace Pimcore\Bundle\GenericDataIndexBundle\Service\Search\SearchService\Asset\Aggregation; | ||
|
||
use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Asset\AssetSearch; | ||
use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Modifier\Aggregation\Asset\FileSizeSumAggregation; | ||
use Pimcore\Bundle\GenericDataIndexBundle\Service\Search\SearchService\Asset\AssetSearchServiceInterface; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
final readonly class FileSizeAggregationService implements FileSizeAggregationServiceInterface | ||
{ | ||
public function __construct( | ||
private AssetSearchServiceInterface $assetSearchService, | ||
) { | ||
} | ||
|
||
public function getFileSizeSum(AssetSearch $assetSearch): int | ||
{ | ||
$aggregation = new FileSizeSumAggregation('fileSizeSum'); | ||
$assetSearch | ||
->addModifier($aggregation) | ||
->setAggregationsOnly(true) | ||
; | ||
|
||
$result = $this->assetSearchService->search($assetSearch); | ||
|
||
$sum = $result->getAggregation($aggregation->getAggregationName())?->getAggregationResult()['value'] ?? 0; | ||
|
||
return (int) $sum; | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
src/Service/Search/SearchService/Asset/Aggregation/FileSizeAggregationServiceInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
/** | ||
* Pimcore | ||
* | ||
* This source file is available under two different licenses: | ||
* - GNU General Public License version 3 (GPLv3) | ||
* - Pimcore Commercial License (PCL) | ||
* Full copyright and license information is available in | ||
* LICENSE.md which is distributed with this source code. | ||
* | ||
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org) | ||
* @license http://www.pimcore.org/license GPLv3 and PCL | ||
*/ | ||
|
||
namespace Pimcore\Bundle\GenericDataIndexBundle\Service\Search\SearchService\Asset\Aggregation; | ||
|
||
use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Asset\AssetSearch; | ||
|
||
interface FileSizeAggregationServiceInterface | ||
{ | ||
/** | ||
* Returns the sum of the file sizes of all assets that match the given search criteria in bytes. | ||
*/ | ||
public function getFileSizeSum(AssetSearch $assetSearch): int; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
tests/Functional/Search/SearchService/Asset/Aggregation/FileSizeAggregationServiceTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
|
||
/** | ||
* Pimcore | ||
* | ||
* This source file is available under two different licenses: | ||
* - GNU General Public License version 3 (GPLv3) | ||
* - Pimcore Commercial License (PCL) | ||
* Full copyright and license information is available in | ||
* LICENSE.md which is distributed with this source code. | ||
* | ||
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org) | ||
* @license http://www.pimcore.org/license GPLv3 and PCL | ||
*/ | ||
|
||
namespace Pimcore\Bundle\GenericDataIndexBundle\Tests\Functional\Search\SearchService\Asset\Aggregation; | ||
|
||
use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Asset\AssetSearch; | ||
use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Modifier\Filter\Basic\IdsFilter; | ||
use Pimcore\Bundle\GenericDataIndexBundle\Service\Search\SearchService\Asset\Aggregation\FileSizeAggregationServiceInterface; | ||
use Pimcore\Tests\Support\Util\TestHelper; | ||
|
||
final class FileSizeAggregationServiceTest extends \Codeception\Test\Unit | ||
{ | ||
/** | ||
* @var \Pimcore\Bundle\GenericDataIndexBundle\Tests\IndexTester | ||
*/ | ||
protected $tester; | ||
|
||
protected function _before() | ||
{ | ||
$this->tester->enableSynchronousProcessing(); | ||
} | ||
|
||
protected function _after() | ||
{ | ||
TestHelper::cleanUp(); | ||
$this->tester->flushIndex(); | ||
$this->tester->cleanupIndex(); | ||
$this->tester->flushIndex(); | ||
} | ||
|
||
public function testGetFileSizeSum(): void | ||
{ | ||
$asset = TestHelper::createImageAsset(); | ||
$asset2 = TestHelper::createImageAsset(); | ||
$asset3 = TestHelper::createImageAsset(); | ||
|
||
/** @var FileSizeAggregationServiceInterface $fileSizeAggregationService */ | ||
$fileSizeAggregationService = $this->tester->grabService(FileSizeAggregationServiceInterface::class); | ||
|
||
$fileSizeSum = $asset->getFileSize() + $asset2->getFileSize() + $asset3->getFileSize(); | ||
|
||
$assetSearch = (new AssetSearch()) | ||
->addModifier(new IdsFilter([$asset->getId(), $asset2->getId(), $asset3->getId()])) | ||
->setPageSize(3); | ||
|
||
$this->assertEquals($fileSizeSum, $fileSizeAggregationService->getFileSizeSum($assetSearch)); | ||
$this->assertNotSame(0, $fileSizeSum); | ||
} | ||
} |