Skip to content

Commit

Permalink
Merge 2941298 into d8ea489
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexBenny committed Jun 16, 2020
2 parents d8ea489 + 2941298 commit 6739665
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 15 deletions.
Expand Up @@ -20,6 +20,7 @@
import com.snowplowanalytics.snowplow.tracker.constants.Parameters;
import com.snowplowanalytics.snowplow.tracker.emitter.BufferOption;
import com.snowplowanalytics.snowplow.tracker.events.ScreenView;
import com.snowplowanalytics.snowplow.tracker.events.Timing;
import com.snowplowanalytics.snowplow.tracker.tracker.ExceptionHandler;
import com.snowplowanalytics.snowplow.tracker.tracker.ScreenState;
import com.snowplowanalytics.snowplow.tracker.utils.LogLevel;
Expand All @@ -30,6 +31,7 @@
import java.io.IOException;
import java.util.Map;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

import okhttp3.mockwebserver.MockResponse;
Expand Down Expand Up @@ -161,6 +163,17 @@ public void testDataCollectionSwitch() {
assertTrue(tracker.getDataCollection());
}

public void testTrackEventMultipleTimes() {
Timing event = Timing.builder()
.category("category")
.variable("variable")
.timing(100)
.build();
UUID id1 = new TrackerEvent(event).eventId;
UUID id2 = new TrackerEvent(event).eventId;
assertFalse(id1.equals(id2));
}

public void testTrackWithNoContext() throws Exception {
Executor.setThreadCount(30);
Executor.shutdown();
Expand Down
Expand Up @@ -18,6 +18,7 @@
import com.snowplowanalytics.snowplow.tracker.events.Event;
import com.snowplowanalytics.snowplow.tracker.events.TrackerError;
import com.snowplowanalytics.snowplow.tracker.payload.SelfDescribingJson;
import com.snowplowanalytics.snowplow.tracker.utils.Util;

import java.util.List;
import java.util.Map;
Expand All @@ -36,9 +37,14 @@ class TrackerEvent {
boolean isService;

TrackerEvent(Event event) {
eventId = UUID.fromString(event.getEventId());
String userEventId = event.getActualEventId();
String newEventId = userEventId != null ? userEventId : Util.getUUIDString();
eventId = UUID.fromString(newEventId);

Long userTimestamp = event.getActualDeviceCreatedTimestamp();
timestamp = userTimestamp != null ? userTimestamp : System.currentTimeMillis();

contexts = event.getContexts();
timestamp = event.getDeviceCreatedTimestamp();
trueTimestamp = event.getTrueTimestamp();
payload = event.getDataPayload();

Expand Down
Expand Up @@ -25,6 +25,7 @@
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.UUID;

/**
* Base AbstractEvent class which contains common
Expand All @@ -36,16 +37,16 @@
public abstract class AbstractEvent implements Event {

protected final List<SelfDescribingJson> customContexts;
private final String eventId;
protected long deviceCreatedTimestamp;
private String eventId;
protected Long deviceCreatedTimestamp;
private Long trueTimestamp;

public static abstract class Builder<T extends Builder<T>> {

private List<SelfDescribingJson> customContexts = new LinkedList<>();
private String eventId = Util.getUUIDString();
private long deviceCreatedTimestamp = System.currentTimeMillis();
private Long trueTimestamp = null;
private String eventId;
private Long deviceCreatedTimestamp;
private Long trueTimestamp;

protected abstract T self();

Expand Down Expand Up @@ -146,13 +147,19 @@ protected AbstractEvent() {

// Precondition checks
Preconditions.checkNotNull(builder.customContexts);
Preconditions.checkNotNull(builder.eventId);
Preconditions.checkArgument(!builder.eventId.isEmpty(), "eventId cannot be empty");
if (builder.eventId != null) {
Preconditions.checkArgument(!builder.eventId.isEmpty(), "eventId cannot be empty");
try {
UUID.fromString(builder.eventId);
} catch(IllegalArgumentException e) {
Preconditions.fail("eventId has to be a valid UUID");
}
eventId = builder.eventId;
}

this.customContexts = builder.customContexts;
this.deviceCreatedTimestamp = builder.deviceCreatedTimestamp;
this.trueTimestamp = builder.trueTimestamp;
this.eventId = builder.eventId;
}

/**
Expand All @@ -175,11 +182,30 @@ protected AbstractEvent() {
}

/**
* @return the events timestamp
* Get the timestamp of the event.
* @apiNote If the timestamp is not set, it sets one as a side effect.
* @deprecated As of release 1.5.0, it will be removed in the version 2.0.0.
* @return the event timestamp
*/
@Override
@Deprecated
public long getDeviceCreatedTimestamp() {
return this.deviceCreatedTimestamp;
if (deviceCreatedTimestamp == null) {
deviceCreatedTimestamp = System.currentTimeMillis();
}
return deviceCreatedTimestamp;
}

/**
* Get the actual timestamp of the event.
* @apiNote It doesn't have the side effect of {@link #getDeviceCreatedTimestamp()}.
* @deprecated As of release 1.5.0, it will be removed in the version 2.0.0.
* @return the event timestamp
*/
@Override
@Deprecated
public Long getActualDeviceCreatedTimestamp() {
return deviceCreatedTimestamp;
}

/**
Expand All @@ -191,11 +217,30 @@ public Long getTrueTimestamp() {
}

/**
* Get the event id of the event.
* @apiNote If the eventId is not set, it sets one as a side effect.
* @deprecated As of release 1.5.0, it will be removed in the version 2.0.0.
* @return the event id
*/
@Override
@Deprecated
public @NonNull String getEventId() {
return this.eventId;
if (eventId == null) {
eventId = Util.getUUIDString();
}
return eventId;
}

/**
* Get the actual event id of the event.
* @apiNote It doesn't have the side effect of {@link #getEventId()}.
* @deprecated As of release 1.5.0, it will be removed in the version 2.0.0.
* @return the event id if it exist.
*/
@Override
@Deprecated
public String getActualEventId() {
return eventId;
}

/**
Expand Down
Expand Up @@ -225,7 +225,7 @@ public List<EcommerceTransactionItem> getItems() {
public void endProcessing(Tracker tracker) {
// Track each item individually
for(EcommerceTransactionItem item : items) {
item.setDeviceCreatedTimestamp(deviceCreatedTimestamp);
item.setDeviceCreatedTimestamp(getDeviceCreatedTimestamp());
tracker.track(item);
}
}
Expand Down
Expand Up @@ -154,7 +154,9 @@ public void setDeviceCreatedTimestamp(long deviceCreatedTimestamp) {
@Override
public @NonNull Map<String, Object> getDataPayload() {
HashMap<String, Object> payload = new HashMap<>();
payload.put(Parameters.DEVICE_TIMESTAMP, Long.toString(this.deviceCreatedTimestamp)); // TODO: to remove on v.2.0
if (deviceCreatedTimestamp != null) {
payload.put(Parameters.DEVICE_TIMESTAMP, Long.toString(deviceCreatedTimestamp)); // TODO: to remove on v.2.0
}
payload.put(Parameters.TI_ITEM_ID, this.itemId);
payload.put(Parameters.TI_ITEM_SKU, this.sku);
payload.put(Parameters.TI_ITEM_NAME, this.name);
Expand Down
Expand Up @@ -43,20 +43,46 @@ public interface Event {
@NonNull List<SelfDescribingJson> getContexts();

/**
* Get the timestamp of the event.
* @apiNote If the timestamp is not set, it sets one as a side effect.
* @deprecated As of release 1.5.0, it will be removed in the version 2.0.0.
* @return the event timestamp
*/
@Deprecated
long getDeviceCreatedTimestamp();

/**
* Get the actual timestamp of the event.
* @apiNote It doesn't have the side effect of {@link #getDeviceCreatedTimestamp()}.
* @deprecated As of release 1.5.0, it will be removed in the version 2.0.0.
* @return the event timestamp
*/
@Deprecated
Long getActualDeviceCreatedTimestamp();

/**
* @return the optional true events timestamp
*/
Long getTrueTimestamp();

/**
* Get the event id of the event.
* @apiNote If the eventId is not set, it sets one as a side effect.
* @deprecated As of release 1.5.0, it will be removed in the version 2.0.0.
* @return the event id
*/
@Deprecated
@NonNull String getEventId();

/**
* Get the actual event id of the event.
* @apiNote It doesn't have the side effect of {@link #getEventId()}.
* @deprecated As of release 1.5.0, it will be removed in the version 2.0.0.
* @return the event id if it exist.
*/
@Deprecated
String getActualEventId();

/**
* @deprecated As of release 1.5.0, it will be removed in the version 2.0.0.
* replaceable by use of {@link #getDataPayload()} without information about
Expand Down
Expand Up @@ -121,6 +121,17 @@ public static void checkArgument(boolean expression, Object errorMessage) {
}
}

/**
* Force a failure in the precondition check.
*
* @param errorMessage the exception message to use; will be converted to a
* string using {@link String#valueOf(Object)}
* @throws IllegalArgumentException
*/
public static void fail(Object errorMessage) {
throw new IllegalArgumentException(String.valueOf(errorMessage));
}

/**
* Ensures that an object reference passed as a parameter to the calling method is not null.
*
Expand Down

0 comments on commit 6739665

Please sign in to comment.