Skip to content

Commit

Permalink
Merge pull request #87 from adshares/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
m-pilarczyk authored Dec 13, 2022
2 parents dfbbefd + 82c82bf commit 084483c
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 18 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [1.2.3] - 2022-12-13
### Changed
- Find by scopes (multiple sizes)

## [1.2.2] - 2022-09-26
### Fixed
- Randomize order of banners with the same score
Expand Down Expand Up @@ -82,7 +86,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [0.1.0] - 2019-04-19
Last python version

[Unreleased]: https://github.com/adshares/adselect/compare/v1.2.2...develop
[Unreleased]: https://github.com/adshares/adselect/compare/v1.2.3...develop
[1.2.3]: https://github.com/adshares/adselect/compare/v1.2.2...v1.2.3
[1.2.2]: https://github.com/adshares/adselect/compare/v1.2.1...v1.2.2
[1.2.1]: https://github.com/adshares/adselect/compare/v1.2.0...v1.2.1
[1.2.0]: https://github.com/adshares/adselect/compare/v1.1.2...v1.2.0
Expand Down
19 changes: 11 additions & 8 deletions src/Application/Dto/QueryDto.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ final class QueryDto
private Id $siteId;
private Id $zoneId;
private Id $userId;
private Size $size;
private array $scopes;
private array $requireFilters;
private array $excludeFilters;
private array $keywords;
Expand All @@ -28,7 +28,7 @@ public function __construct(
Id $zoneId,
Id $userId,
Id $trackingId,
Size $size,
array $scopes,
array $zone_options = [],
array $filters = [],
array $keywords = []
Expand All @@ -38,7 +38,7 @@ public function __construct(
$this->zoneId = $zoneId;
$this->userId = $userId;
$this->trackingId = $trackingId;
$this->size = $size;
$this->scopes = $scopes;
$this->requireFilters = $filters['require'] ?? [];
$this->excludeFilters = $filters['exclude'] ?? [];
$this->keywords = $keywords;
Expand Down Expand Up @@ -90,9 +90,9 @@ public function getTrackingId(): string
return $this->trackingId->toString();
}

public function getSize(): string
public function getScopes(): array
{
return $this->size->toString();
return $this->scopes;
}

public static function fromArray(array $input): self
Expand All @@ -117,8 +117,11 @@ public static function fromArray(array $input): self
throw new ValidationDtoException('Field `tracking_id` is required.');
}

if (!isset($input['banner_size'])) {
throw new ValidationDtoException('Field `banner_size` is required.');
if (!isset($input['scopes'])) {
if (!isset($input['banner_size'])) {
throw new ValidationDtoException('Field `scopes` is required.');
}
$input['scopes'] = [$input['banner_size']];
}

if (!isset($input['keywords'])) {
Expand All @@ -136,7 +139,7 @@ public static function fromArray(array $input): self
new Id($input['zone_id']),
new Id($input['user_id']),
new Id($input['tracking_id']),
new Size($input['banner_size']),
$input['scopes'],
$input['zone_options'] ?? [],
$input['banner_filters'],
$input['keywords']
Expand Down
2 changes: 1 addition & 1 deletion src/Application/Service/BannerFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@

interface BannerFinder
{
public function find(QueryDto $queryDto, int $size = 1): FoundBannersCollection;
public function find(QueryDto $queryDto, int $resultCount = 1): FoundBannersCollection;
}
3 changes: 1 addition & 2 deletions src/Infrastructure/ElasticSearch/QueryBuilder/BaseQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function build(): array
);

$excludes = KeywordsToExclude::build(self::PREFIX_FILTER_EXCLUDE, $this->bannerFinderDto->getKeywords());
$sizeFilter = FilterClause::build('banner.size', [$this->bannerFinderDto->getSize()]);
$sizeFilter = FilterClause::build('banner.size', $this->bannerFinderDto->getScopes());

$requireFilter = FilterToBanner::build(
self::PREFIX_BANNER_REQUIRE,
Expand Down Expand Up @@ -141,7 +141,6 @@ public function build(): array
]
],
"boost_mode" => "replace",
//"score_mode" => "max",
],
],
"score_mode" => "max",
Expand Down
14 changes: 10 additions & 4 deletions src/Infrastructure/ElasticSearch/Service/BannerFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,15 @@ public function __construct(

public function find(
QueryDto $queryDto,
int $size = 1
int $resultCount = 1
): FoundBannersCollection {
$userHistory = $this->loadUserHistory($queryDto);
$defined = $this->getDefinedRequireKeywords();
$query = new BaseQuery($this->timeService, $queryDto, $defined);

$params = [
'index' => BannerIndex::name(),
'size' => $size,
'size' => $resultCount,
'client' => [
'timeout' => 0.5,
'connect_timeout' => 0.2
Expand Down Expand Up @@ -104,12 +104,18 @@ public function find(
}

foreach ($response['hits']['hits'] as $hit) {
$size = $hit['fields']['banner.size'][0];
foreach ($queryDto->getScopes() as $scope) {
if (in_array($scope, $hit['fields']['banner.size'], true)) {
$size = $scope;
break;
}
}
$collection->add(
new FoundBanner(
$hit['fields']['campaign_id'][0],
$hit['_id'],
in_array($queryDto->getSize(), $hit['fields']['banner.size'], true)
? $queryDto->getSize() : $hit['fields']['banner.size'][0],
$size,
fmod(floor($hit['_score']), 100_000) / 100
)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ public function testWhenKeywordsAndFiltersAreEmpty(): void
$siteId = new Id('43c567e1396b4cadb52223a51796fd04');
$zoneId = new Id('43c567e1396b4cadb52223a51796fd03');
$trackingId = new Id('43c567e1396b4cadb52223a51796fd02');
$dto = new QueryDto($publisherId, $siteId, $zoneId, $userId, $trackingId, new Size("200x100"));
$scopes = ["200x100"];
$dto = new QueryDto($publisherId, $siteId, $zoneId, $userId, $trackingId, $scopes);
$defined = [
'one',
'two',
Expand Down Expand Up @@ -126,6 +127,7 @@ public function testWhenFiltersExist(): void
$zoneId = new Id('43c567e1396b4cadb52223a51796fd03');
$userId = new Id('85f115636b384744949300571aad2a4f');
$trackingId = new Id('85f115636b384744949300571aad2a4d');
$scopes = ["160x600"];

$keywords = [
'device:type' => ['mobile'],
Expand Down Expand Up @@ -154,7 +156,7 @@ public function testWhenFiltersExist(): void
$zoneId,
$userId,
$trackingId,
new Size("160x600"),
$scopes,
$filters,
$keywords
);
Expand Down

0 comments on commit 084483c

Please sign in to comment.