Skip to content

Commit

Permalink
Merge pull request #345 from North-Two-Five/340_nullSentAt
Browse files Browse the repository at this point in the history
Add option to sentAt field to be null
  • Loading branch information
pooyaj authored May 5, 2022
2 parents 535aa53 + 7ff5479 commit b578343
Show file tree
Hide file tree
Showing 12 changed files with 108 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,22 @@ private Builder(String previousId) {
protected AliasMessage realBuild(
Type type,
String messageId,
Date sentAt,
Date timestamp,
Map<String, ?> context,
String anonymousId,
String userId,
Map<String, Object> integrations) {
return new AutoValue_AliasMessage(
type, messageId, timestamp, context, anonymousId, userId, integrations, previousId);
type,
messageId,
sentAt,
timestamp,
context,
anonymousId,
userId,
integrations,
previousId);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,14 @@ public abstract class Batch {
private static final AtomicInteger SEQUENCE_GENERATOR = new AtomicInteger();

public static Batch create(Map<String, ?> context, List<Message> batch) {
return new AutoValue_Batch(batch, new Date(), context, SEQUENCE_GENERATOR.incrementAndGet());
Message sentAtNull =
batch.stream().filter(message -> message.sentAt() != null).findAny().orElse(null);

return new AutoValue_Batch(
batch,
sentAtNull == null ? new Date() : sentAtNull.sentAt(),
context,
SEQUENCE_GENERATOR.incrementAndGet());
}

public abstract List<Message> batch();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,23 @@ public Builder traits(Map<String, ?> traits) {
protected GroupMessage realBuild(
Type type,
String messageId,
Date sentAt,
Date timestamp,
Map<String, ?> context,
String anonymousId,
String userId,
Map<String, Object> integrations) {
return new AutoValue_GroupMessage(
type, messageId, timestamp, context, anonymousId, userId, integrations, groupId, traits);
type,
messageId,
sentAt,
timestamp,
context,
anonymousId,
userId,
integrations,
groupId,
traits);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public Builder traits(Map<String, ?> traits) {
protected IdentifyMessage realBuild(
Type type,
String messageId,
Date sentAt,
Date timestamp,
Map<String, ?> context,
String anonymousId,
Expand All @@ -70,7 +71,7 @@ protected IdentifyMessage realBuild(
}

return new AutoValue_IdentifyMessage(
type, messageId, timestamp, context, anonymousId, userId, integrations, traits);
type, messageId, sentAt, timestamp, context, anonymousId, userId, integrations, traits);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ public interface Message {
@Nonnull
String messageId();

@Nullable
Date sentAt();

@Nonnull
Date timestamp();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
public abstract class MessageBuilder<T extends Message, V extends MessageBuilder> {
private final Message.Type type;
private String messageId;
private Date sentAt;
private Date timestamp;
private Map<String, ?> context;
private String anonymousId;
Expand Down Expand Up @@ -72,6 +73,17 @@ public V messageId(String messageId) {
return self();
}

/**
* Set a sentAt for the event. By default, the current sentAt is used, but you may override it for
* historical import.
*
* @see <a href="https://segment.com/docs/spec/common/#-sentAt-">SentAt</a>
*/
public V sentAt(Date sentAt) {
this.sentAt = sentAt;
return self();
}

/**
* Set a timestamp for the event. By default, the current timestamp is used, but you may override
* it for historical import.
Expand Down Expand Up @@ -180,6 +192,7 @@ public V integrationOptions(String key, Map<String, ?> options) {
protected abstract T realBuild(
Message.Type type,
String messageId,
Date sentAt,
Date timestamp,
Map<String, ?> context,
String anonymousId,
Expand All @@ -203,6 +216,8 @@ public T build() {
timestamp = new Date();
}

Date sentAt = this.sentAt;

String messageId = this.messageId;
if (messageId == null) {
messageId = UUID.randomUUID().toString();
Expand All @@ -215,7 +230,8 @@ public T build() {
integrations = ImmutableMap.copyOf(this.integrations);
}

return realBuild(type, messageId, timestamp, context, anonymousId, userId, integrations);
return realBuild(
type, messageId, sentAt, timestamp, context, anonymousId, userId, integrations);
}

/** Returns the {@link Message.Type} of the message this builder is constructing. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ Builder self() {
protected PageMessage realBuild(
Type type,
String messageId,
Date sentAt,
Date timestamp,
Map<String, ?> context,
String anonymousId,
Expand All @@ -100,6 +101,7 @@ protected PageMessage realBuild(
return new AutoValue_PageMessage(
type,
messageId,
sentAt,
timestamp,
context,
anonymousId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,23 @@ Builder self() {
protected ScreenMessage realBuild(
Type type,
String messageId,
Date sentAt,
Date timestamp,
Map<String, ?> context,
String anonymousId,
String userId,
Map<String, Object> integrations) {
return new AutoValue_ScreenMessage(
type, messageId, timestamp, context, anonymousId, userId, integrations, name, properties);
type,
messageId,
sentAt,
timestamp,
context,
anonymousId,
userId,
integrations,
name,
properties);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ Builder self() {
protected TrackMessage realBuild(
Type type,
String messageId,
Date sentAt,
Date timestamp,
Map<String, ?> context,
String anonymousId,
Expand All @@ -87,6 +88,7 @@ protected TrackMessage realBuild(
return new AutoValue_TrackMessage(
type,
messageId,
sentAt,
timestamp,
context,
anonymousId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
import com.google.common.collect.ImmutableMap;
import com.segment.analytics.TestUtils;
import com.squareup.burst.BurstJUnit4;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import org.junit.Test;
Expand All @@ -26,6 +29,33 @@ public void create(TestUtils.MessageBuilderFactory factory) {
assertThat(batch.context()).isEqualTo(context);
}

@Test
public void createWithSentAt(TestUtils.MessageBuilderFactory factory) throws ParseException {
Message message =
factory
.get()
.userId("userId")
.sentAt(new SimpleDateFormat("yyyy-MM-dd").parse("2022-04-01"))
.build();
Map<String, String> context = ImmutableMap.of("foo", "bar");
List<Message> messages = ImmutableList.of(message);

Batch batch = Batch.create(context, messages);

assertThat(batch.sentAt()).isEqualTo(messages.get(0).sentAt());
}

@Test
public void createWithSentAtNull(TestUtils.MessageBuilderFactory factory) throws ParseException {
Message message = factory.get().userId("userId").sentAt(null).build();
Map<String, String> context = ImmutableMap.of("foo", "bar");
List<Message> messages = ImmutableList.of(message);

Batch batch = Batch.create(context, messages);

assertThat(batch.sentAt()).isEqualTo(new Date());
}

@Test
public void sequence(TestUtils.MessageBuilderFactory factory) {
Message message = factory.get().userId("userId").build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ public String messageId() {
throw new UnsupportedOperationException();
}

@Nullable
@Override
public Date sentAt() {
throw new UnsupportedOperationException();
}

@Nonnull
@Override
public Date timestamp() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ public String messageId() {
throw new UnsupportedOperationException();
}

@Nullable
@Override
public Date sentAt() {
throw new UnsupportedOperationException();
}

@Nonnull
@Override
public Date timestamp() {
Expand Down

0 comments on commit b578343

Please sign in to comment.