|
1 | 1 | /**
|
2 | 2 | *
|
3 |
| - * Copyright 2016-2017, Optimizely and contributors |
| 3 | + * Copyright 2016-2018, Optimizely and contributors |
4 | 4 | *
|
5 | 5 | * Licensed under the Apache License, Version 2.0 (the "License");
|
6 | 6 | * you may not use this file except in compliance with the License.
|
|
16 | 16 | */
|
17 | 17 | package com.optimizely.ab.event.internal;
|
18 | 18 |
|
| 19 | +import com.optimizely.ab.annotations.VisibleForTesting; |
| 20 | +import com.optimizely.ab.bucketing.DecisionService; |
| 21 | +import com.optimizely.ab.config.EventType; |
19 | 22 | import com.optimizely.ab.config.Experiment;
|
20 | 23 | import com.optimizely.ab.config.ProjectConfig;
|
21 | 24 | import com.optimizely.ab.config.Variation;
|
22 | 25 | import com.optimizely.ab.event.LogEvent;
|
23 |
| - |
| 26 | +import com.optimizely.ab.event.internal.payload.Attribute; |
| 27 | +import com.optimizely.ab.event.internal.payload.Decision; |
| 28 | +import com.optimizely.ab.event.internal.payload.EventBatch; |
| 29 | +import com.optimizely.ab.event.internal.payload.Event; |
| 30 | +import com.optimizely.ab.event.internal.payload.Snapshot; |
| 31 | +import com.optimizely.ab.event.internal.payload.Visitor; |
| 32 | +import com.optimizely.ab.event.internal.serializer.DefaultJsonSerializer; |
| 33 | +import com.optimizely.ab.event.internal.serializer.Serializer; |
| 34 | +import com.optimizely.ab.internal.EventTagUtils; |
| 35 | +import org.slf4j.Logger; |
| 36 | +import org.slf4j.LoggerFactory; |
24 | 37 | import javax.annotation.Nonnull;
|
| 38 | +import java.util.ArrayList; |
| 39 | +import java.util.Arrays; |
| 40 | +import java.util.Collections; |
| 41 | +import java.util.List; |
25 | 42 | import java.util.Map;
|
| 43 | +import java.util.UUID; |
| 44 | + |
| 45 | +public class EventBuilder { |
| 46 | + private static final Logger logger = LoggerFactory.getLogger(EventBuilder.class); |
| 47 | + static final String ATTRIBUTE_KEY_FOR_BUCKETING_ATTRIBUTE = "optimizely_bucketing_id"; |
| 48 | + static final String EVENT_ENDPOINT = "https://logx.optimizely.com/v1/events"; |
| 49 | + static final String ACTIVATE_EVENT_KEY = "campaign_activated"; |
| 50 | + |
| 51 | + private Serializer serializer; |
| 52 | + @VisibleForTesting |
| 53 | + public final String clientVersion; |
| 54 | + @VisibleForTesting |
| 55 | + public final EventBatch.ClientEngine clientEngine; |
| 56 | + |
| 57 | + public EventBuilder() { |
| 58 | + this(EventBatch.ClientEngine.JAVA_SDK, BuildVersionInfo.VERSION); |
| 59 | + } |
| 60 | + |
| 61 | + public EventBuilder(EventBatch.ClientEngine clientEngine, String clientVersion) { |
| 62 | + this.clientEngine = clientEngine; |
| 63 | + this.clientVersion = clientVersion; |
| 64 | + this.serializer = DefaultJsonSerializer.getInstance(); |
| 65 | + } |
26 | 66 |
|
27 |
| -public abstract class EventBuilder { |
28 | 67 |
|
29 |
| - public abstract LogEvent createImpressionEvent(@Nonnull ProjectConfig projectConfig, |
| 68 | + public LogEvent createImpressionEvent(@Nonnull ProjectConfig projectConfig, |
30 | 69 | @Nonnull Experiment activatedExperiment,
|
31 | 70 | @Nonnull Variation variation,
|
32 | 71 | @Nonnull String userId,
|
33 |
| - @Nonnull Map<String, String> attributes); |
| 72 | + @Nonnull Map<String, String> attributes) { |
34 | 73 |
|
35 |
| - public abstract LogEvent createConversionEvent(@Nonnull ProjectConfig projectConfig, |
| 74 | + Decision decision = new Decision(activatedExperiment.getLayerId(), activatedExperiment.getId(), |
| 75 | + variation.getId(), false); |
| 76 | + Event impressionEvent = new Event(System.currentTimeMillis(),UUID.randomUUID().toString(), activatedExperiment.getLayerId(), |
| 77 | + ACTIVATE_EVENT_KEY, null, null, null, ACTIVATE_EVENT_KEY, null); |
| 78 | + Snapshot snapshot = new Snapshot(Arrays.asList(decision), Arrays.asList(impressionEvent)); |
| 79 | + |
| 80 | + Visitor visitor = new Visitor(userId, null, buildAttributeList(projectConfig, attributes), Arrays.asList(snapshot)); |
| 81 | + List<Visitor> visitors = Arrays.asList(visitor); |
| 82 | + EventBatch eventBatch = new EventBatch(clientEngine.getClientEngineValue(), clientVersion, projectConfig.getAccountId(), visitors, projectConfig.getAnonymizeIP(), projectConfig.getProjectId(), projectConfig.getRevision()); |
| 83 | + String payload = this.serializer.serialize(eventBatch); |
| 84 | + return new LogEvent(LogEvent.RequestMethod.POST, EVENT_ENDPOINT, Collections.<String, String>emptyMap(), payload); |
| 85 | + } |
| 86 | + |
| 87 | + public LogEvent createConversionEvent(@Nonnull ProjectConfig projectConfig, |
36 | 88 | @Nonnull Map<Experiment, Variation> experimentVariationMap,
|
37 | 89 | @Nonnull String userId,
|
38 | 90 | @Nonnull String eventId,
|
39 | 91 | @Nonnull String eventName,
|
40 | 92 | @Nonnull Map<String, String> attributes,
|
41 |
| - @Nonnull Map<String, ?> eventTags); |
| 93 | + @Nonnull Map<String, ?> eventTags) { |
| 94 | + |
| 95 | + if (experimentVariationMap.isEmpty()) { |
| 96 | + return null; |
| 97 | + } |
| 98 | + |
| 99 | + ArrayList<Decision> decisions = new ArrayList<Decision>(); |
| 100 | + for (Map.Entry<Experiment, Variation> entry : experimentVariationMap.entrySet()) { |
| 101 | + Decision decision = new Decision(entry.getKey().getLayerId(), entry.getKey().getId(), entry.getValue().getId(), false); |
| 102 | + decisions.add(decision); |
| 103 | + } |
| 104 | + |
| 105 | + EventType eventType = projectConfig.getEventNameMapping().get(eventName); |
| 106 | + |
| 107 | + Event conversionEvent = new Event(System.currentTimeMillis(),UUID.randomUUID().toString(), eventType.getId(), |
| 108 | + eventType.getKey(), null, EventTagUtils.getRevenueValue(eventTags), eventTags, eventType.getKey(), EventTagUtils.getNumericValue(eventTags)); |
| 109 | + Snapshot snapshot = new Snapshot(decisions, Arrays.asList(conversionEvent)); |
| 110 | + |
| 111 | + Visitor visitor = new Visitor(userId, null, buildAttributeList(projectConfig, attributes), Arrays.asList(snapshot)); |
| 112 | + List<Visitor> visitors = Arrays.asList(visitor); |
| 113 | + EventBatch eventBatch = new EventBatch(clientEngine.getClientEngineValue(), clientVersion, projectConfig.getAccountId(), visitors, projectConfig.getAnonymizeIP(), projectConfig.getProjectId(), projectConfig.getRevision()); |
| 114 | + String payload = this.serializer.serialize(eventBatch); |
| 115 | + return new LogEvent(LogEvent.RequestMethod.POST, EVENT_ENDPOINT, Collections.<String, String>emptyMap(), payload); |
| 116 | + } |
| 117 | + |
| 118 | + private List<Attribute> buildAttributeList(ProjectConfig projectConfig, Map<String, String> attributes) { |
| 119 | + List<Attribute> attributesList = new ArrayList<Attribute>(); |
| 120 | + |
| 121 | + Map<String, com.optimizely.ab.config.Attribute> attributeMap = projectConfig.getAttributeKeyMapping(); |
| 122 | + for (Map.Entry<String, String> entry : attributes.entrySet()) { |
| 123 | + com.optimizely.ab.config.Attribute projectAttribute = attributeMap.get(entry.getKey()); |
| 124 | + Attribute attribute = new Attribute((projectAttribute != null ? projectAttribute.getId() : null), |
| 125 | + entry.getKey(), Attribute.CUSTOM_ATTRIBUTE_TYPE, entry.getValue()); |
| 126 | + |
| 127 | + if (entry.getKey() == DecisionService.BUCKETING_ATTRIBUTE) { |
| 128 | + attribute = new Attribute(com.optimizely.ab.bucketing.DecisionService.BUCKETING_ATTRIBUTE, |
| 129 | + ATTRIBUTE_KEY_FOR_BUCKETING_ATTRIBUTE, Attribute.CUSTOM_ATTRIBUTE_TYPE, entry.getValue()); |
| 130 | + } |
| 131 | + |
| 132 | + attributesList.add(attribute); |
| 133 | + } |
| 134 | + |
| 135 | + return attributesList; |
| 136 | + } |
42 | 137 | }
|
0 commit comments