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

agma: bugfixes #3495

Merged
merged 6 commits into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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 @@ -16,6 +16,7 @@
import io.vertx.core.Vertx;
import io.vertx.core.http.HttpHeaders;
import io.vertx.core.http.HttpMethod;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.prebid.server.analytics.AnalyticsReporter;
import org.prebid.server.analytics.model.AmpEvent;
Expand Down Expand Up @@ -97,7 +98,12 @@ public AgmaAnalyticsReporter(AgmaAnalyticsProperties agmaAnalyticsProperties,

@Override
public void initialize(Promise<Void> initializePromise) {
vertx.setPeriodic(bufferTimeoutMs, ignored -> sendEvents(buffer.pollAll()));
vertx.setPeriodic(bufferTimeoutMs, ignored -> {
final List<String> toFlush = buffer.pollAll();
if (!toFlush.isEmpty()) {
sendEvents(toFlush);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, move this check to sendEvents method.

});
initializePromise.complete();
}

Expand Down Expand Up @@ -200,7 +206,9 @@ private static String getPublisherId(BidRequest bidRequest) {
return null;
}

return publisherId;
return StringUtils.isNotBlank(appSiteId)
? String.format("%s_%s", StringUtils.defaultString(publisherId), appSiteId)
: publisherId;
}

private void sendEvents(List<String> events) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import lombok.NoArgsConstructor;
import org.apache.commons.collections4.ListUtils;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
import org.prebid.server.analytics.AnalyticsReporter;
import org.prebid.server.analytics.reporter.AnalyticsReporterDelegator;
import org.prebid.server.analytics.reporter.agma.AgmaAnalyticsReporter;
Expand Down Expand Up @@ -111,8 +112,15 @@ private static class AgmaAnalyticsConfigurationProperties {
public AgmaAnalyticsProperties toComponentProperties() {
final Map<String, String> accountsByPublisherId = accounts.stream()
.collect(Collectors.toMap(
AgmaAnalyticsAccountProperties::getPublisherId,
AgmaAnalyticsAccountProperties::getCode));
account -> {
final String publisherId = account.getPublisherId();
final String siteAppId = account.getSiteAppId();
return StringUtils.isNotBlank(siteAppId)
? String.format("%s_%s", publisherId, siteAppId)
: publisherId;
},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, extract to separate method.

AgmaAnalyticsAccountProperties::getCode
));

return AgmaAnalyticsProperties.builder()
.url(endpoint.getUrl())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,124 @@ public void processEventShouldNotSendAnythingWhenAccountsDoesNotHaveConfiguredPu
assertThat(result.succeeded()).isTrue();
}

@Test
public void processEventShouldSendWhenAccountsHasConfiguredAppsOrSites() {
// given
final AgmaAnalyticsProperties properties = AgmaAnalyticsProperties.builder()
.url("http://endpoint.com")
.gzip(false)
.bufferSize(100000)
.bufferTimeoutMs(10000L)
.maxEventsCount(0)
.httpTimeoutMs(1000L)
.accounts(Map.of("publisherId_bundleId", "accountCode"))
.build();

target = new AgmaAnalyticsReporter(properties, versionProvider, jacksonMapper, clock, httpClient, vertx);

// given
final App givenApp = App.builder().bundle("bundleId")
.publisher(Publisher.builder().id("publisherId").build()).build();
final Device givenDevice = Device.builder().build();
final User givenUser = User.builder().build();

final AuctionEvent auctionEvent = AuctionEvent.builder()
.auctionContext(AuctionContext.builder()
.privacyContext(PrivacyContext.of(
null, TcfContext.builder().consent(PARSED_VALID_CONSENT).build()))
.timeoutContext(TimeoutContext.of(clock.millis(), null, 1))
.bidRequest(BidRequest.builder()
.id("requestId")
.app(givenApp)
.app(givenApp)
.device(givenDevice)
.user(givenUser)
.build())
.build())
.build();

// when
final Future<Void> result = target.processEvent(auctionEvent);

// then
final AgmaEvent expectedEvent = AgmaEvent.builder()
.eventType("auction")
.accountCode("accountCode")
.requestId("requestId")
.app(givenApp)
.device(givenDevice)
.user(givenUser)
.startTime(ZonedDateTime.parse("2024-09-03T15:00:00+05:00"))
.build();

final String expectedEventPayload = "[" + jacksonMapper.encodeToString(expectedEvent) + "]";

verify(httpClient).request(
eq(POST),
eq("http://endpoint.com"),
any(),
eq(expectedEventPayload),
eq(1000L));
}

@Test
public void processEventShouldSendWhenAccountsHasConfiguredAppsOrSitesOnly() {
// given
final AgmaAnalyticsProperties properties = AgmaAnalyticsProperties.builder()
.url("http://endpoint.com")
.gzip(false)
.bufferSize(100000)
.bufferTimeoutMs(10000L)
.maxEventsCount(0)
.httpTimeoutMs(1000L)
.accounts(Map.of("_mySite", "accountCode"))
.build();

target = new AgmaAnalyticsReporter(properties, versionProvider, jacksonMapper, clock, httpClient, vertx);

// given
final Site givenSite = Site.builder().id("mySite").build();
final Device givenDevice = Device.builder().build();
final User givenUser = User.builder().build();

final AuctionEvent auctionEvent = AuctionEvent.builder()
.auctionContext(AuctionContext.builder()
.privacyContext(PrivacyContext.of(
null, TcfContext.builder().consent(PARSED_VALID_CONSENT).build()))
.timeoutContext(TimeoutContext.of(clock.millis(), null, 1))
.bidRequest(BidRequest.builder()
.id("requestId")
.site(givenSite)
.device(givenDevice)
.user(givenUser)
.build())
.build())
.build();

// when
final Future<Void> result = target.processEvent(auctionEvent);

// then
final AgmaEvent expectedEvent = AgmaEvent.builder()
.eventType("auction")
.accountCode("accountCode")
.requestId("requestId")
.site(givenSite)
.device(givenDevice)
.user(givenUser)
.startTime(ZonedDateTime.parse("2024-09-03T15:00:00+05:00"))
.build();

final String expectedEventPayload = "[" + jacksonMapper.encodeToString(expectedEvent) + "]";

verify(httpClient).request(
eq(POST),
eq("http://endpoint.com"),
any(),
eq(expectedEventPayload),
eq(1000L));
}

@Test
public void processEventShouldSendEncodingGzipHeaderAndCompressedPayload() {
// given
Expand Down
Loading