|
| 1 | +package org.whispersystems.textsecuregcm.experiment; |
| 2 | + |
| 3 | +import com.google.common.annotations.VisibleForTesting; |
| 4 | +import org.apache.commons.lang3.StringUtils; |
| 5 | +import org.slf4j.Logger; |
| 6 | +import org.slf4j.LoggerFactory; |
| 7 | +import org.whispersystems.textsecuregcm.identity.IdentityType; |
| 8 | +import org.whispersystems.textsecuregcm.push.IdleDeviceNotificationScheduler; |
| 9 | +import org.whispersystems.textsecuregcm.storage.Account; |
| 10 | +import org.whispersystems.textsecuregcm.storage.Device; |
| 11 | +import org.whispersystems.textsecuregcm.storage.MessagesManager; |
| 12 | +import reactor.core.publisher.Flux; |
| 13 | +import javax.annotation.Nullable; |
| 14 | +import java.util.EnumMap; |
| 15 | +import java.util.Map; |
| 16 | +import java.time.LocalTime; |
| 17 | +import java.util.concurrent.CompletableFuture; |
| 18 | + |
| 19 | +public class NotifyIdleDevicesWithoutMessagesPushNotificationExperiment implements PushNotificationExperiment<DeviceLastSeenState> { |
| 20 | + |
| 21 | + private final MessagesManager messagesManager; |
| 22 | + private final IdleDeviceNotificationScheduler idleDeviceNotificationScheduler; |
| 23 | + |
| 24 | + private static final LocalTime PREFERRED_NOTIFICATION_TIME = LocalTime.of(14, 0); |
| 25 | + |
| 26 | + private static final Logger log = LoggerFactory.getLogger(NotifyIdleDevicesWithoutMessagesPushNotificationExperiment.class); |
| 27 | + |
| 28 | + @VisibleForTesting |
| 29 | + enum Population { |
| 30 | + APNS_CONTROL, |
| 31 | + APNS_EXPERIMENT, |
| 32 | + FCM_CONTROL, |
| 33 | + FCM_EXPERIMENT |
| 34 | + } |
| 35 | + |
| 36 | + @VisibleForTesting |
| 37 | + enum Outcome { |
| 38 | + DELETED, |
| 39 | + UNINSTALLED, |
| 40 | + REACTIVATED, |
| 41 | + UNCHANGED |
| 42 | + } |
| 43 | + |
| 44 | + public NotifyIdleDevicesWithoutMessagesPushNotificationExperiment(final MessagesManager messagesManager, |
| 45 | + final IdleDeviceNotificationScheduler idleDeviceNotificationScheduler) { |
| 46 | + |
| 47 | + this.messagesManager = messagesManager; |
| 48 | + this.idleDeviceNotificationScheduler = idleDeviceNotificationScheduler; |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + public String getExperimentName() { |
| 53 | + return "notify-idle-devices-without-messages"; |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + public CompletableFuture<Boolean> isDeviceEligible(final Account account, final Device device) { |
| 58 | + |
| 59 | + if (!hasPushToken(device)) { |
| 60 | + return CompletableFuture.completedFuture(false); |
| 61 | + } |
| 62 | + |
| 63 | + if (!idleDeviceNotificationScheduler.isIdle(device)) { |
| 64 | + return CompletableFuture.completedFuture(false); |
| 65 | + } |
| 66 | + |
| 67 | + return messagesManager.mayHaveMessages(account.getIdentifier(IdentityType.ACI), device) |
| 68 | + .thenApply(mayHaveMessages -> !mayHaveMessages); |
| 69 | + } |
| 70 | + |
| 71 | + @VisibleForTesting |
| 72 | + static boolean hasPushToken(final Device device) { |
| 73 | + // Exclude VOIP tokens since they have their own, distinct delivery mechanism |
| 74 | + return !StringUtils.isAllBlank(device.getApnId(), device.getGcmId()) && StringUtils.isBlank(device.getVoipApnId()); |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + public DeviceLastSeenState getState(@Nullable final Account account, @Nullable final Device device) { |
| 79 | + if (account != null && device != null) { |
| 80 | + final DeviceLastSeenState.PushTokenType pushTokenType = StringUtils.isNotBlank(device.getApnId()) |
| 81 | + ? DeviceLastSeenState.PushTokenType.APNS |
| 82 | + : DeviceLastSeenState.PushTokenType.FCM; |
| 83 | + |
| 84 | + return new DeviceLastSeenState(true, device.getCreated(), hasPushToken(device), device.getLastSeen(), pushTokenType); |
| 85 | + } else { |
| 86 | + return DeviceLastSeenState.MISSING_DEVICE_STATE; |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + @Override |
| 91 | + public CompletableFuture<Void> applyExperimentTreatment(final Account account, final Device device) { |
| 92 | + return idleDeviceNotificationScheduler.scheduleNotification(account, device.getId(), PREFERRED_NOTIFICATION_TIME); |
| 93 | + } |
| 94 | + |
| 95 | + @Override |
| 96 | + public void analyzeResults(final Flux<PushNotificationExperimentSample<DeviceLastSeenState>> samples) { |
| 97 | + final Map<Population, Map<Outcome, Integer>> contingencyTable = new EnumMap<>(Population.class); |
| 98 | + |
| 99 | + for (final Population population : Population.values()) { |
| 100 | + final Map<Outcome, Integer> countsByOutcome = new EnumMap<>(Outcome.class); |
| 101 | + |
| 102 | + for (final Outcome outcome : Outcome.values()) { |
| 103 | + countsByOutcome.put(outcome, 0); |
| 104 | + } |
| 105 | + |
| 106 | + contingencyTable.put(population, countsByOutcome); |
| 107 | + } |
| 108 | + |
| 109 | + samples.doOnNext(sample -> contingencyTable.get(getPopulation(sample)).merge(getOutcome(sample), 1, Integer::sum)) |
| 110 | + .then() |
| 111 | + .block(); |
| 112 | + |
| 113 | + final StringBuilder reportBuilder = new StringBuilder("population,deleted,uninstalled,reactivated,unchanged\n"); |
| 114 | + |
| 115 | + for (final Population population : Population.values()) { |
| 116 | + final Map<Outcome, Integer> countsByOutcome = contingencyTable.get(population); |
| 117 | + |
| 118 | + reportBuilder.append(population.name()); |
| 119 | + reportBuilder.append(","); |
| 120 | + reportBuilder.append(countsByOutcome.getOrDefault(Outcome.DELETED, 0)); |
| 121 | + reportBuilder.append(","); |
| 122 | + reportBuilder.append(countsByOutcome.getOrDefault(Outcome.UNINSTALLED, 0)); |
| 123 | + reportBuilder.append(","); |
| 124 | + reportBuilder.append(countsByOutcome.getOrDefault(Outcome.REACTIVATED, 0)); |
| 125 | + reportBuilder.append(","); |
| 126 | + reportBuilder.append(countsByOutcome.getOrDefault(Outcome.UNCHANGED, 0)); |
| 127 | + reportBuilder.append("\n"); |
| 128 | + } |
| 129 | + |
| 130 | + log.info(reportBuilder.toString()); |
| 131 | + } |
| 132 | + |
| 133 | + @VisibleForTesting |
| 134 | + static Population getPopulation(final PushNotificationExperimentSample<DeviceLastSeenState> sample) { |
| 135 | + assert sample.initialState() != null && sample.initialState().pushTokenType() != null; |
| 136 | + |
| 137 | + return switch (sample.initialState().pushTokenType()) { |
| 138 | + case APNS -> sample.inExperimentGroup() ? Population.APNS_EXPERIMENT : Population.APNS_CONTROL; |
| 139 | + case FCM -> sample.inExperimentGroup() ? Population.FCM_EXPERIMENT : Population.FCM_CONTROL; |
| 140 | + }; |
| 141 | + } |
| 142 | + |
| 143 | + @VisibleForTesting |
| 144 | + static Outcome getOutcome(final PushNotificationExperimentSample<DeviceLastSeenState> sample) { |
| 145 | + final Outcome outcome; |
| 146 | + |
| 147 | + if (!sample.finalState().deviceExists() || sample.initialState().createdAtMillis() != sample.finalState().createdAtMillis()) { |
| 148 | + outcome = Outcome.DELETED; |
| 149 | + } else if (!sample.finalState().hasPushToken()) { |
| 150 | + outcome = Outcome.UNINSTALLED; |
| 151 | + } else if (sample.initialState().lastSeenMillis() != sample.finalState().lastSeenMillis()) { |
| 152 | + outcome = Outcome.REACTIVATED; |
| 153 | + } else { |
| 154 | + outcome = Outcome.UNCHANGED; |
| 155 | + } |
| 156 | + |
| 157 | + return outcome; |
| 158 | + } |
| 159 | +} |
0 commit comments