Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
gthea committed Dec 12, 2024
1 parent 23d0758 commit ad32409
Show file tree
Hide file tree
Showing 30 changed files with 149 additions and 123 deletions.
4 changes: 2 additions & 2 deletions src/androidTest/java/fake/SyncManagerStub.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package fake;

import io.split.android.client.dtos.Event;
import io.split.android.client.impressions.Impression;
import io.split.android.client.impressions.DecoratedImpression;
import io.split.android.client.service.synchronizer.SyncManager;
import io.split.android.client.shared.UserConsent;

Expand Down Expand Up @@ -42,6 +42,6 @@ public void pushEvent(Event event) {
}

@Override
public void pushImpression(Impression impression) {
public void pushImpression(DecoratedImpression impression) {
}
}
4 changes: 2 additions & 2 deletions src/androidTest/java/fake/SynchronizerSpyImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import io.split.android.client.api.Key;
import io.split.android.client.dtos.Event;
import io.split.android.client.impressions.Impression;
import io.split.android.client.impressions.DecoratedImpression;
import io.split.android.client.service.synchronizer.Synchronizer;
import io.split.android.client.service.synchronizer.SynchronizerSpy;
import io.split.android.client.service.synchronizer.attributes.AttributesSynchronizer;
Expand Down Expand Up @@ -88,7 +88,7 @@ public void pushEvent(Event event) {
}

@Override
public void pushImpression(Impression impression) {
public void pushImpression(DecoratedImpression impression) {
mSynchronizer.pushImpression(impression);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
import io.split.android.client.service.mysegments.MySegmentsTaskFactory;
import io.split.android.client.service.synchronizer.SyncManager;
import io.split.android.client.shared.SplitClientContainer;
import io.split.android.client.storage.common.SplitStorageContainer;
import io.split.android.client.storage.attributes.AttributesStorage;
import io.split.android.client.storage.attributes.PersistentAttributesStorage;
import io.split.android.client.storage.common.SplitStorageContainer;
import io.split.android.client.storage.splits.SplitsStorage;
import io.split.android.client.telemetry.TelemetrySynchronizer;
import io.split.android.client.telemetry.storage.TelemetryInitProducer;
Expand All @@ -43,7 +43,7 @@ public class SplitClientFactoryImpl implements SplitClientFactory {
private final SplitParser mSplitParser;
private final AttributesManagerFactory mAttributesManagerFactory;
private final TreatmentManagerFactory mTreatmentManagerFactory;
private final ImpressionListener mCustomerImpressionListener;
private final ImpressionListener.FederatedImpressionListener mCustomerImpressionListener;
private final SplitValidatorImpl mSplitValidator;
private final EventsTracker mEventsTracker;

Expand All @@ -57,7 +57,7 @@ public SplitClientFactoryImpl(@NonNull SplitFactory splitFactory,
@NonNull ValidationMessageLogger validationLogger,
@NonNull KeyValidator keyValidator,
@NonNull EventsTracker eventsTracker,
@NonNull ImpressionListener customerImpressionListener,
@NonNull ImpressionListener.FederatedImpressionListener customerImpressionListener,
@Nullable FlagSetsFilter flagSetsFilter) {
mSplitFactory = checkNotNull(splitFactory);
mClientContainer = checkNotNull(clientContainer);
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/io/split/android/client/SplitFactoryImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import io.split.android.client.events.EventsManagerCoordinator;
import io.split.android.client.factory.FactoryMonitor;
import io.split.android.client.factory.FactoryMonitorImpl;
import io.split.android.client.impressions.DecoratedImpressionListener;
import io.split.android.client.impressions.ImpressionListener;
import io.split.android.client.impressions.SyncImpressionListener;
import io.split.android.client.lifecycle.SplitLifecycleManager;
Expand Down Expand Up @@ -243,17 +244,16 @@ private SplitFactoryImpl(@NonNull String apiToken, @NonNull Key key, @NonNull Sp
}

ExecutorService impressionsLoggingTaskExecutor = factoryHelper.getImpressionsLoggingTaskExecutor();
final ImpressionListener splitImpressionListener
final DecoratedImpressionListener splitImpressionListener
= new SyncImpressionListener(mSyncManager, impressionsLoggingTaskExecutor);
final ImpressionListener customerImpressionListener;
final ImpressionListener.FederatedImpressionListener customerImpressionListener;

List<ImpressionListener> impressionListeners = new ArrayList<>();
if (config.impressionListener() != null) {
List<ImpressionListener> impressionListeners = new ArrayList<>();
impressionListeners.add(splitImpressionListener);
impressionListeners.add(config.impressionListener());
customerImpressionListener = new ImpressionListener.FederatedImpressionListener(impressionListeners);
customerImpressionListener = new ImpressionListener.FederatedImpressionListener(splitImpressionListener, impressionListeners);
} else {
customerImpressionListener = splitImpressionListener;
customerImpressionListener = new ImpressionListener.FederatedImpressionListener(splitImpressionListener, impressionListeners);
}
EventsTracker eventsTracker = buildEventsTracker();
mUserConsentManager = new UserConsentManagerImpl(config,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
package io.split.android.client.impressions;

import java.util.Map;
public class DecoratedImpression {

public class DecoratedImpression extends Impression {

private final Impression mImpression;
private final boolean mTrackImpressions;

public DecoratedImpression(String key, String bucketingKey, String split, String treatment, long time, String appliedRule, Long changeNumber, Map<String, Object> atributes, boolean trackImpressions) {
super(key, bucketingKey, split, treatment, time, appliedRule, changeNumber, atributes);
public DecoratedImpression(Impression impression, boolean trackImpressions) {
mImpression = impression;
mTrackImpressions = trackImpressions;
}

public Impression getImpression() {
return mImpression;
}

public boolean getTrackImpressions() {
return mTrackImpressions;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package io.split.android.client.impressions;

public interface DecoratedImpressionListener {

void log(DecoratedImpression impression);
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,29 @@ public interface ImpressionListener {
*/
void close();

final class NoopImpressionListener implements ImpressionListener {
final class NoopImpressionListener implements ImpressionListener, DecoratedImpressionListener {
@Override
public void log(Impression impression) {
// noop
}

@Override
public void log(DecoratedImpression impression) {

}

@Override
public void close() {
// noop
}
}

final class FederatedImpressionListener implements ImpressionListener {
private List<ImpressionListener> _delegates;
final class FederatedImpressionListener implements ImpressionListener, DecoratedImpressionListener {
private final DecoratedImpressionListener mDecoratedImpressionListener;
private final List<ImpressionListener> _delegates;

public FederatedImpressionListener(List<ImpressionListener> delegates) {
public FederatedImpressionListener(DecoratedImpressionListener decoratedImpressionListener, List<ImpressionListener> delegates) {
mDecoratedImpressionListener = decoratedImpressionListener;
_delegates = delegates;
}

Expand All @@ -45,6 +52,11 @@ public void log(Impression impression) {
}
}

@Override
public void log(DecoratedImpression impression) {
mDecoratedImpressionListener.log(impression);
}

@Override
public void close() {
for (ImpressionListener listener : _delegates) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
class ImpressionLoggingTask implements Runnable {

private final SyncManager mSyncManager;
private final Impression mImpression;
private final DecoratedImpression mImpression;

ImpressionLoggingTask(@NonNull SyncManager syncManager,
Impression impression) {
DecoratedImpression impression) {
mSyncManager = checkNotNull(syncManager);
mImpression = impression;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import io.split.android.client.service.synchronizer.SyncManager;
import io.split.android.client.utils.logger.Logger;

public class SyncImpressionListener implements ImpressionListener {
public class SyncImpressionListener implements DecoratedImpressionListener {

private final SyncManager mSyncManager;
private final ExecutorService mExecutorService;
Expand All @@ -21,15 +21,11 @@ public SyncImpressionListener(@NonNull SyncManager syncManager,
}

@Override
public void log(Impression impression) {
public void log(DecoratedImpression impression) {
try {
mExecutorService.submit(new ImpressionLoggingTask(mSyncManager, impression));
} catch (Exception ex) {
Logger.w("Error submitting impression logging task: " + ex.getLocalizedMessage());
}
}

@Override
public void close() {
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
package io.split.android.client.localhost;

import io.split.android.client.impressions.DecoratedImpression;
import io.split.android.client.impressions.DecoratedImpressionListener;
import io.split.android.client.impressions.Impression;
import io.split.android.client.impressions.ImpressionListener;

public class LocalhostImpressionsListener implements ImpressionListener {
public class LocalhostImpressionsListener implements ImpressionListener, DecoratedImpressionListener {
@Override
public void log(Impression impression) {
}

@Override
public void log(DecoratedImpression impression) {
}

@Override
public void close() {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -263,11 +263,11 @@ public boolean track(String eventType, double value, Map<String, Object> propert
return false;
}

private ImpressionListener getImpressionsListener(SplitClientConfig config) {
private ImpressionListener.FederatedImpressionListener getImpressionsListener(SplitClientConfig config) {
if (config.impressionListener() != null) {
return config.impressionListener();
return new ImpressionListener.FederatedImpressionListener(new ImpressionListener.NoopImpressionListener(), Collections.singletonList(config.impressionListener()));
} else {
return new LocalhostImpressionsListener();
return new ImpressionListener.FederatedImpressionListener(new ImpressionListener.NoopImpressionListener(), Collections.emptyList());
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package io.split.android.client.service.impressions;

import io.split.android.client.impressions.Impression;
import io.split.android.client.impressions.DecoratedImpression;

public interface ImpressionManager {

void enableTracking(boolean enable);

void pushImpression(Impression impression);
void pushImpression(DecoratedImpression impression);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import java.util.concurrent.atomic.AtomicBoolean;

import io.split.android.client.impressions.DecoratedImpression;
import io.split.android.client.impressions.Impression;
import io.split.android.client.service.impressions.strategy.PeriodicTracker;
import io.split.android.client.service.impressions.strategy.ProcessStrategy;
import io.split.android.client.utils.logger.Logger;
Expand All @@ -34,16 +33,16 @@ public StrategyImpressionManager(Pair<ProcessStrategy, PeriodicTracker> noneComp
}

@Override
public void pushImpression(Impression impression) {
public void pushImpression(DecoratedImpression impression) {
if (!isTrackingEnabled.get()) {
Logger.v("Impression not tracked because tracking is disabled");
return;
}

if (shouldTrack(impression)) {
mProcessStrategy.apply(impression);
if (impression.getTrackImpressions()) {
mProcessStrategy.apply(impression.getImpression());
} else {
mNoneStrategy.apply(impression);
mNoneStrategy.apply(impression.getImpression());
}
}

Expand Down Expand Up @@ -72,13 +71,4 @@ public void stopPeriodicRecording() {
tracker.stopPeriodicRecording();
}
}

private static boolean shouldTrack(Impression impression) {
if (impression instanceof DecoratedImpression) {
return ((DecoratedImpression) impression).getTrackImpressions();
} else {
// default behaviour; will never get here.
return true;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package io.split.android.client.service.synchronizer;

import io.split.android.client.dtos.Event;
import io.split.android.client.impressions.Impression;
import io.split.android.client.impressions.DecoratedImpression;
import io.split.android.client.lifecycle.SplitLifecycleAware;
import io.split.android.client.shared.UserConsent;

Expand All @@ -12,7 +12,7 @@ public interface SyncManager extends SplitLifecycleAware {

void pushEvent(Event event);

void pushImpression(Impression impression);
void pushImpression(DecoratedImpression impression);

void stop();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import io.split.android.client.SplitClientConfig;
import io.split.android.client.dtos.Event;
import io.split.android.client.impressions.DecoratedImpression;
import io.split.android.client.impressions.Impression;
import io.split.android.client.service.executor.SplitTask;
import io.split.android.client.service.executor.SplitTaskExecutionInfo;
Expand Down Expand Up @@ -163,7 +164,7 @@ public void pushEvent(Event event) {
}

@Override
public void pushImpression(Impression impression) {
public void pushImpression(DecoratedImpression impression) {
mSynchronizer.pushImpression(impression);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package io.split.android.client.service.synchronizer;

import io.split.android.client.dtos.Event;
import io.split.android.client.impressions.Impression;
import io.split.android.client.impressions.DecoratedImpression;
import io.split.android.client.lifecycle.SplitLifecycleAware;

public interface Synchronizer extends SplitLifecycleAware {
Expand All @@ -28,7 +28,7 @@ public interface Synchronizer extends SplitLifecycleAware {

void pushEvent(Event event);

void pushImpression(Impression impression);
void pushImpression(DecoratedImpression impression);

void flush();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import io.split.android.client.api.Key;
import io.split.android.client.dtos.Event;
import io.split.android.client.events.ISplitEventsManager;
import io.split.android.client.impressions.DecoratedImpression;
import io.split.android.client.impressions.Impression;
import io.split.android.client.service.ServiceConstants;
import io.split.android.client.service.executor.SplitTaskExecutionInfo;
Expand Down Expand Up @@ -255,7 +256,7 @@ public void pushEvent(Event event) {
}

@Override
public void pushImpression(Impression impression) {
public void pushImpression(DecoratedImpression impression) {
mImpressionManager.pushImpression(impression);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public SplitClientContainerImpl(@NonNull String defaultMatchingKey,
@NonNull SplitApiFacade splitApiFacade,
@NonNull ValidationMessageLogger validationLogger,
@NonNull KeyValidator keyValidator,
@NonNull ImpressionListener customerImpressionListener,
@NonNull ImpressionListener.FederatedImpressionListener customerImpressionListener,
@Nullable PushNotificationManager pushNotificationManager,
@NonNull ClientComponentsRegister clientComponentsRegister,
@NonNull MySegmentsWorkManagerWrapper workManagerWrapper,
Expand Down
Loading

0 comments on commit ad32409

Please sign in to comment.