Skip to content

Commit 479e3a0

Browse files
committed
Refactor
1 parent 1f1bb4b commit 479e3a0

1 file changed

Lines changed: 18 additions & 23 deletions

File tree

src/main/java/org/prebid/server/auction/externalortb/ProfilesProcessor.java

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -76,15 +76,15 @@ public ProfilesProcessor(int maxProfiles,
7676
}
7777

7878
public Future<BidRequest> process(AuctionContext auctionContext, BidRequest bidRequest) {
79-
final AllProfilesIds profilesIds = profilesIds(bidRequest, auctionContext);
80-
if (profilesIds.isEmpty()) {
81-
return Future.succeededFuture(bidRequest);
82-
}
83-
8479
final String accountId = Optional.ofNullable(auctionContext.getAccount())
8580
.map(Account::getId)
8681
.orElse(StringUtils.EMPTY);
8782

83+
final AllProfilesIds profilesIds = profilesIds(bidRequest, auctionContext, accountId);
84+
if (profilesIds.isEmpty()) {
85+
return Future.succeededFuture(bidRequest);
86+
}
87+
8888
return fetchProfiles(accountId, profilesIds, timeoutMillis(bidRequest))
8989
.compose(profiles -> emitMetrics(accountId, profiles, auctionContext))
9090
.map(profiles -> mergeResults(
@@ -94,7 +94,7 @@ public Future<BidRequest> process(AuctionContext auctionContext, BidRequest bidR
9494
new InvalidRequestException("Error during processing profiles: " + e.getMessage())));
9595
}
9696

97-
private AllProfilesIds profilesIds(BidRequest bidRequest, AuctionContext auctionContext) {
97+
private AllProfilesIds profilesIds(BidRequest bidRequest, AuctionContext auctionContext, String accountId) {
9898
final AllProfilesIds initialProfilesIds = new AllProfilesIds(
9999
requestProfilesIds(bidRequest),
100100
bidRequest.getImp().stream().map(this::impProfilesIds).toList());
@@ -109,7 +109,7 @@ private AllProfilesIds profilesIds(BidRequest bidRequest, AuctionContext auction
109109

110110
if (auctionContext.getDebugContext().isDebugEnabled() && !profilesIds.equals(initialProfilesIds)) {
111111
auctionContext.getDebugWarnings().add("Profiles exceeded the limit.");
112-
metrics.updateProfileMetric(MetricName.limit_exceeded);
112+
metrics.updateAccountProfileMetric(accountId, MetricName.limit_exceeded);
113113
}
114114

115115
return profilesIds;
@@ -217,15 +217,14 @@ private <T> T applyProfiles(List<String> profilesIds,
217217
for (String profileId : profilesIds) {
218218
try {
219219
final Profile profile = idToProfile.get(profileId);
220-
result = profile != null
221-
? mergeProfile(result, profile, profileId)
222-
: result;
223-
} catch (InvalidProfileException e) {
224-
metrics.updateProfileMetric(MetricName.invalid);
225-
conditionalLogger.error(e.getMessage(), logSamplingRate);
220+
result = profile != null ? mergeProfile(result, profile) : result;
221+
} catch (InvalidRequestException e) {
222+
final String message = "Can't merge with profile %s: %s".formatted(profileId, e.getMessage());
226223

224+
metrics.updateProfileMetric(MetricName.invalid);
225+
conditionalLogger.error(message, logSamplingRate);
227226
if (failOnUnknown) {
228-
throw new InvalidProfileException(e.getMessage());
227+
throw new InvalidProfileException(message);
229228
}
230229
}
231230
}
@@ -237,19 +236,15 @@ private <T> T applyProfiles(List<String> profilesIds,
237236
}
238237
}
239238

240-
private ObjectNode mergeProfile(ObjectNode original, Profile profile, String profileId) {
239+
private ObjectNode mergeProfile(ObjectNode original, Profile profile) {
241240
return switch (profile.getMergePrecedence()) {
242-
case REQUEST -> merge(original, profile.getBody(), profileId);
243-
case PROFILE -> merge(profile.getBody(), original, profileId);
241+
case REQUEST -> merge(original, profile.getBody());
242+
case PROFILE -> merge(profile.getBody(), original);
244243
};
245244
}
246245

247-
private ObjectNode merge(JsonNode takePrecedence, JsonNode other, String profileId) {
248-
try {
249-
return (ObjectNode) jsonMerger.merge(takePrecedence, other);
250-
} catch (InvalidRequestException e) {
251-
throw new InvalidProfileException("Can't merge with profile %s: %s".formatted(profileId, e.getMessage()));
252-
}
246+
private ObjectNode merge(JsonNode takePrecedence, JsonNode other) {
247+
return (ObjectNode) jsonMerger.merge(takePrecedence, other);
253248
}
254249

255250
private List<Imp> applyImpsProfiles(List<List<String>> profilesIds,

0 commit comments

Comments
 (0)