Skip to content

Commit

Permalink
WIP internal imp listener
Browse files Browse the repository at this point in the history
  • Loading branch information
gthea committed Dec 11, 2024
1 parent 3b669b3 commit 11fb586
Show file tree
Hide file tree
Showing 26 changed files with 129 additions and 97 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 @@ -13,6 +13,7 @@
import io.split.android.client.events.SplitEventTask;
import io.split.android.client.events.SplitEventsManager;
import io.split.android.client.impressions.ImpressionListener;
import io.split.android.client.impressions.SyncImpressionListener;
import io.split.android.client.service.executor.SplitTaskExecutor;
import io.split.android.client.service.mysegments.MySegmentsTaskFactory;
import io.split.android.client.service.synchronizer.SyncManager;
Expand Down Expand Up @@ -58,6 +59,7 @@ public SplitClientFactoryImpl(@NonNull SplitFactory splitFactory,
@NonNull KeyValidator keyValidator,
@NonNull EventsTracker eventsTracker,
@NonNull ImpressionListener customerImpressionListener,
@NonNull SyncImpressionListener internalImpressionListener,
@Nullable FlagSetsFilter flagSetsFilter) {
mSplitFactory = checkNotNull(splitFactory);
mClientContainer = checkNotNull(clientContainer);
Expand All @@ -80,6 +82,7 @@ public SplitClientFactoryImpl(@NonNull SplitFactory splitFactory,
keyValidator,
mSplitValidator,
customerImpressionListener,
internalImpressionListener,
config.labelsEnabled(),
new AttributesMergerImpl(),
mStorageContainer.getTelemetryStorage(),
Expand Down
7 changes: 3 additions & 4 deletions src/main/java/io/split/android/client/SplitFactoryImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -243,17 +243,16 @@ private SplitFactoryImpl(@NonNull String apiToken, @NonNull Key key, @NonNull Sp
}

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

if (config.impressionListener() != null) {
List<ImpressionListener> impressionListeners = new ArrayList<>();
impressionListeners.add(splitImpressionListener);
impressionListeners.add(config.impressionListener());
customerImpressionListener = new ImpressionListener.FederatedImpressionListener(impressionListeners);
} else {
customerImpressionListener = splitImpressionListener;
customerImpressionListener = new ImpressionListener.NoopImpressionListener();
}
EventsTracker eventsTracker = buildEventsTracker();
mUserConsentManager = new UserConsentManagerImpl(config,
Expand All @@ -268,7 +267,7 @@ private SplitFactoryImpl(@NonNull String apiToken, @NonNull Key key, @NonNull Sp
mClientContainer = new SplitClientContainerImpl(
mDefaultClientKey.matchingKey(), this, config, mSyncManager,
telemetrySynchronizer, mStorageContainer, splitTaskExecutor, splitApiFacade,
validationLogger, keyValidator, customerImpressionListener,
validationLogger, keyValidator, customerImpressionListener, splitImpressionListener,
streamingComponents.getPushNotificationManager(), componentsRegister, workManagerWrapper,
eventsTracker, flagSetsFilter);
mDestroyer = new Runnable() {
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
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 {

private final SyncManager mSyncManager;
private final ExecutorService mExecutorService;
Expand All @@ -20,16 +20,11 @@ public SyncImpressionListener(@NonNull SyncManager syncManager,
mExecutorService = checkNotNull(executorService);
}

@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
Expand Up @@ -72,7 +72,7 @@ public LocalhostSplitClient(@NonNull LocalhostSplitFactory container,
mSplitsStorage = splitsStorage;
mTreatmentManager = new TreatmentManagerImpl(mKey.matchingKey(), mKey.bucketingKey(),
new EvaluatorImpl(splitsStorage, splitParser), new KeyValidatorImpl(),
new SplitValidatorImpl(), getImpressionsListener(splitClientConfig),
new SplitValidatorImpl(), getImpressionsListener(splitClientConfig), null,
splitClientConfig.labelsEnabled(), eventsManager, attributesManager, attributesMerger,
telemetryStorageProducer, flagSetsFilter, splitsStorage, new ValidationMessageLoggerImpl(), new FlagSetsValidatorImpl());
}
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 @@ -18,6 +18,7 @@
import io.split.android.client.api.Key;
import io.split.android.client.events.SplitEventsManager;
import io.split.android.client.impressions.ImpressionListener;
import io.split.android.client.impressions.SyncImpressionListener;
import io.split.android.client.service.ServiceConstants;
import io.split.android.client.service.SplitApiFacade;
import io.split.android.client.service.executor.SplitClientEventTaskExecutor;
Expand Down Expand Up @@ -68,6 +69,7 @@ public SplitClientContainerImpl(@NonNull String defaultMatchingKey,
@NonNull ValidationMessageLogger validationLogger,
@NonNull KeyValidator keyValidator,
@NonNull ImpressionListener customerImpressionListener,
@NonNull SyncImpressionListener internalImpressionListener,
@Nullable PushNotificationManager pushNotificationManager,
@NonNull ClientComponentsRegister clientComponentsRegister,
@NonNull MySegmentsWorkManagerWrapper workManagerWrapper,
Expand All @@ -91,6 +93,7 @@ public SplitClientContainerImpl(@NonNull String defaultMatchingKey,
keyValidator,
eventsTracker,
customerImpressionListener,
internalImpressionListener,
flagSetsFilter
);
mClientComponentsRegister = checkNotNull(clientComponentsRegister);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import io.split.android.client.attributes.AttributesMerger;
import io.split.android.client.events.ListenableEventsManager;
import io.split.android.client.impressions.ImpressionListener;
import io.split.android.client.impressions.SyncImpressionListener;
import io.split.android.client.storage.splits.SplitsStorage;
import io.split.android.client.telemetry.storage.TelemetryStorageProducer;
import io.split.android.engine.experiments.SplitParser;
Expand All @@ -30,10 +31,12 @@ public class TreatmentManagerFactoryImpl implements TreatmentManagerFactory {
private final SplitsStorage mSplitsStorage;
private final ValidationMessageLogger mValidationMessageLogger;
private final SplitFilterValidator mFlagSetsValidator;
private final SyncImpressionListener mInternalImpressionListener;

public TreatmentManagerFactoryImpl(@NonNull KeyValidator keyValidator,
@NonNull SplitValidator splitValidator,
@NonNull ImpressionListener customerImpressionListener,
@Nullable SyncImpressionListener internalImpressionListener,
boolean labelsEnabled,
@NonNull AttributesMerger attributesMerger,
@NonNull TelemetryStorageProducer telemetryStorageProducer,
Expand All @@ -43,6 +46,7 @@ public TreatmentManagerFactoryImpl(@NonNull KeyValidator keyValidator,
mKeyValidator = checkNotNull(keyValidator);
mSplitValidator = checkNotNull(splitValidator);
mCustomerImpressionListener = checkNotNull(customerImpressionListener);
mInternalImpressionListener = internalImpressionListener;
mLabelsEnabled = labelsEnabled;
mAttributesMerger = checkNotNull(attributesMerger);
mTelemetryStorageProducer = checkNotNull(telemetryStorageProducer);
Expand All @@ -62,6 +66,7 @@ public TreatmentManager getTreatmentManager(Key key, ListenableEventsManager eve
mKeyValidator,
mSplitValidator,
mCustomerImpressionListener,
mInternalImpressionListener,
mLabelsEnabled,
eventsManager,
attributesManager,
Expand Down
Loading

0 comments on commit 11fb586

Please sign in to comment.