Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Greenbids: Populate AppliedTo and change structure of analytics results in Analytics Tag #3728

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,8 @@ public GreenbidsInvocationResult createGreenbidsInvocationResult(
final InvocationAction invocationAction = isExploration
? InvocationAction.no_action
: InvocationAction.update;
final Map<String, Map<String, Boolean>> impsBiddersFilterMapToAnalyticsTag = isExploration
? keepAllBiddersForAnalyticsResult(impsBiddersFilterMap)
: impsBiddersFilterMap;
final Map<String, Ortb2ImpExtResult> ort2ImpExtResultMap = createOrtb2ImpExtForImps(
bidRequest, impsBiddersFilterMapToAnalyticsTag, greenbidsId, isExploration);
bidRequest, impsBiddersFilterMap, greenbidsId, isExploration);
final AnalyticsResult analyticsResult = AnalyticsResult.of(
"success", ort2ImpExtResultMap, null, null);

Expand Down Expand Up @@ -83,16 +80,6 @@ private ObjectNode updateImpExt(ObjectNode impExt, Map<String, Boolean> bidderFi
return updatedExt;
}

private Map<String, Map<String, Boolean>> keepAllBiddersForAnalyticsResult(
Map<String, Map<String, Boolean>> impsBiddersFilterMap) {

return impsBiddersFilterMap.entrySet().stream()
.collect(Collectors.toMap(
Map.Entry::getKey,
entry -> entry.getValue().entrySet().stream()
.collect(Collectors.toMap(Map.Entry::getKey, e -> true))));
}

private Map<String, Ortb2ImpExtResult> createOrtb2ImpExtForImps(
BidRequest bidRequest,
Map<String, Map<String, Boolean>> impsBiddersFilterMap,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.iab.openrtb.request.BidRequest;
import io.vertx.core.Future;
import org.apache.commons.collections4.CollectionUtils;
import org.prebid.server.analytics.reporter.greenbids.model.ExplorationResult;
import org.prebid.server.analytics.reporter.greenbids.model.Ortb2ImpExtResult;
import org.prebid.server.auction.model.AuctionContext;
import org.prebid.server.exception.PreBidException;
Expand Down Expand Up @@ -189,21 +190,36 @@ private Tags toAnalyticsTags(List<AnalyticsResult> analyticsResults) {

private List<Result> toResults(List<AnalyticsResult> analyticsResults) {
return analyticsResults.stream()
.map(this::toResult)
.flatMap(analyticsResult -> toResult(analyticsResult).stream())
.toList();
EvgeniiMunin marked this conversation as resolved.
Show resolved Hide resolved
}

private Result toResult(AnalyticsResult analyticsResult) {
return ResultImpl.of(
analyticsResult.getStatus(),
toObjectNode(analyticsResult.getValues()),
AppliedToImpl.builder()
.bidders(Collections.singletonList(analyticsResult.getBidder()))
.impIds(Collections.singletonList(analyticsResult.getImpId()))
.build());
private List<Result> toResult(AnalyticsResult analyticsResult) {
EvgeniiMunin marked this conversation as resolved.
Show resolved Hide resolved
return analyticsResult.getValues().entrySet().stream()
.map(entry -> {
final String impId = entry.getKey();
final Ortb2ImpExtResult ortb2ImpExtResult = entry.getValue();

final List<String> bidders = Optional.ofNullable(ortb2ImpExtResult)
.map(Ortb2ImpExtResult::getGreenbids)
.map(ExplorationResult::getKeptInAuction)
.orElse(Collections.emptyMap())
.keySet().stream().toList();

final ObjectNode objectNode = toObjectNode(entry);

return (Result) ResultImpl.of(
analyticsResult.getStatus(),
objectNode,
AppliedToImpl.builder()
.impIds(Collections.singletonList(impId))
.bidders(bidders)
.build());
})
.toList();
EvgeniiMunin marked this conversation as resolved.
Show resolved Hide resolved
}

private ObjectNode toObjectNode(Map<String, Ortb2ImpExtResult> values) {
private ObjectNode toObjectNode(Map.Entry<String, Ortb2ImpExtResult> values) {
return values != null ? mapper.valueToTree(values) : null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ public void createGreenbidsInvocationResultShouldReturnNoActionWhenExploration()
assertThat(ortb2ImpExtResult.getGreenbids().getIsExploration()).isTrue();
assertThat(ortb2ImpExtResult.getGreenbids().getFingerprint()).isNotNull();
assertThat(keptInAuction.get("rubicon")).isTrue();
assertThat(keptInAuction.get("appnexus")).isTrue();
assertThat(keptInAuction.get("pubmatic")).isTrue();
assertThat(keptInAuction.get("appnexus")).isFalse();
assertThat(keptInAuction.get("pubmatic")).isFalse();
}

private Map<String, Map<String, Boolean>> givenImpsBiddersFilterMap() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@
import org.prebid.server.hooks.v1.auction.AuctionInvocationContext;
import org.prebid.server.hooks.v1.auction.AuctionRequestPayload;
import org.prebid.server.model.HttpRequestContext;
import org.prebid.server.settings.model.Account;
import org.prebid.server.settings.model.AccountHooksConfiguration;

import java.io.IOException;
import java.net.InetAddress;
Expand Down Expand Up @@ -216,7 +214,7 @@ public void callShouldFilterBiddersWhenPartnerActivatedInBidRequest()
}

@Test
public void callShouldNotFilterBiddersAndReturnAnalyticsTagWhenExploration() throws OrtException, IOException {
public void callShouldFilterBiddersAndReturnAnalyticsTagWhenExploration() throws OrtException, IOException {
// given
final Banner banner = givenBanner();

Expand All @@ -238,7 +236,7 @@ public void callShouldNotFilterBiddersAndReturnAnalyticsTagWhenExploration() thr
when(thresholdsCacheWithExpiration.getIfPresent("throttlingThresholds_test-pbuid"))
.thenReturn(givenThrottlingThresholds());

final AnalyticsResult expectedAnalyticsResult = expectedAnalyticsResult(true, true);
final AnalyticsResult expectedAnalyticsResult = expectedAnalyticsResult(true, false);

// when
final Future<InvocationResult<AuctionRequestPayload>> future = target
Expand Down Expand Up @@ -507,8 +505,8 @@ private Result toResult(AnalyticsResult analyticsResult) {
analyticsResult.getStatus(),
toObjectNode(analyticsResult.getValues()),
AppliedToImpl.builder()
.bidders(Collections.singletonList(analyticsResult.getBidder()))
.impIds(Collections.singletonList(analyticsResult.getImpId()))
.impIds(Collections.singletonList("adunitcodevalue"))
.bidders(List.of("appnexus", "pubmatic", "rubicon"))
.build());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.google.common.collect.Streams;
import com.iab.openrtb.request.Banner;
import com.iab.openrtb.request.BidRequest;
import com.iab.openrtb.request.Device;
Expand Down Expand Up @@ -42,7 +43,6 @@
import org.prebid.server.hooks.execution.model.HookExecutionOutcome;
import org.prebid.server.hooks.execution.model.Stage;
import org.prebid.server.hooks.execution.model.StageExecutionOutcome;
import org.prebid.server.hooks.v1.analytics.Activity;
import org.prebid.server.hooks.v1.analytics.Result;
import org.prebid.server.hooks.v1.analytics.Tags;
import org.prebid.server.json.EncodeException;
Expand All @@ -65,8 +65,6 @@
import java.time.Clock;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
Expand Down Expand Up @@ -228,9 +226,7 @@ private Map<String, Ortb2ImpExtResult> extractAnalyticsResultFromAnalyticsTag(Au
.map(Tags::activities)
.flatMap(Collection::stream)
.filter(activity -> "greenbids-filter".equals(activity.name()))
.map(Activity::results)
.map(List::getFirst)
.map(Result::values)
.flatMap(activity -> activity.results().stream())
EvgeniiMunin marked this conversation as resolved.
Show resolved Hide resolved
.map(this::parseAnalyticsResult)
.flatMap(map -> map.entrySet().stream())
.collect(Collectors.toMap(
Expand All @@ -239,21 +235,21 @@ private Map<String, Ortb2ImpExtResult> extractAnalyticsResultFromAnalyticsTag(Au
(existing, replacement) -> existing));
}

private Map<String, Ortb2ImpExtResult> parseAnalyticsResult(ObjectNode analyticsResult) {
private Map<String, Ortb2ImpExtResult> parseAnalyticsResult(Result result) {
return Optional.ofNullable(result)
.map(Result::values)
.filter(valuesNode -> valuesNode.fieldNames().hasNext())
.map(valuesNode -> Streams.stream(valuesNode.fields())
.collect(Collectors.toMap(
Map.Entry::getKey,
entry -> parseOrtb2ImpExtResult(entry.getValue()),
(existing, replacement) -> existing)))
.orElseGet(Collections::emptyMap);
}

private Ortb2ImpExtResult parseOrtb2ImpExtResult(JsonNode node) {
EvgeniiMunin marked this conversation as resolved.
Show resolved Hide resolved
try {
final Map<String, Ortb2ImpExtResult> parsedAnalyticsResult = new HashMap<>();
final Iterator<Map.Entry<String, JsonNode>> fields = analyticsResult.fields();

while (fields.hasNext()) {
final Map.Entry<String, JsonNode> field = fields.next();
final String impId = field.getKey();
final JsonNode explorationResultNode = field.getValue();
final Ortb2ImpExtResult ortb2ImpExtResult = jacksonMapper.mapper()
.treeToValue(explorationResultNode, Ortb2ImpExtResult.class);
parsedAnalyticsResult.put(impId, ortb2ImpExtResult);
}

return parsedAnalyticsResult;
return jacksonMapper.mapper().treeToValue(node, Ortb2ImpExtResult.class);
} catch (JsonProcessingException e) {
throw new PreBidException("Analytics result parsing error", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,10 @@
import org.prebid.server.hooks.execution.model.Stage;
import org.prebid.server.hooks.execution.model.StageExecutionOutcome;
import org.prebid.server.hooks.execution.v1.analytics.ActivityImpl;
import org.prebid.server.hooks.execution.v1.analytics.AppliedToImpl;
import org.prebid.server.hooks.execution.v1.analytics.ResultImpl;
import org.prebid.server.hooks.execution.v1.analytics.TagsImpl;
import org.prebid.server.hooks.v1.analytics.AppliedTo;
import org.prebid.server.json.EncodeException;
import org.prebid.server.json.JacksonMapper;
import org.prebid.server.model.HttpRequestContext;
Expand Down Expand Up @@ -733,11 +735,16 @@ private static HookExecutionContext givenHookExecutionContextWithAnalyticsTag()
"adunitcodevalue",
createAnalyticsResultNode()));

final AppliedTo appliedTo = AppliedToImpl.builder()
.impIds(Collections.singletonList("adunitcodevalue"))
.bidders(Collections.singletonList("seat1"))
.build();

final ActivityImpl activity = ActivityImpl.of(
"greenbids-filter",
"success",
Collections.singletonList(
ResultImpl.of("success", analyticsResultNode, null)));
ResultImpl.of("success", analyticsResultNode, appliedTo)));

final TagsImpl tags = TagsImpl.of(Collections.singletonList(activity));

Expand Down
Loading