|
| 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.storage.Account; |
| 8 | +import org.whispersystems.textsecuregcm.storage.Device; |
| 9 | +import reactor.core.publisher.Flux; |
| 10 | +import javax.annotation.Nullable; |
| 11 | +import java.time.Clock; |
| 12 | +import java.time.Duration; |
| 13 | +import java.time.Instant; |
| 14 | +import java.util.Collections; |
| 15 | +import java.util.EnumMap; |
| 16 | +import java.util.Map; |
| 17 | + |
| 18 | +abstract class IdleDevicePushNotificationExperiment implements PushNotificationExperiment<DeviceLastSeenState> { |
| 19 | + |
| 20 | + private final Clock clock; |
| 21 | + |
| 22 | + private final Logger log = LoggerFactory.getLogger(getClass()); |
| 23 | + |
| 24 | + @VisibleForTesting |
| 25 | + enum Population { |
| 26 | + APNS_CONTROL, |
| 27 | + APNS_EXPERIMENT, |
| 28 | + FCM_CONTROL, |
| 29 | + FCM_EXPERIMENT |
| 30 | + } |
| 31 | + |
| 32 | + @VisibleForTesting |
| 33 | + enum Outcome { |
| 34 | + DELETED, |
| 35 | + UNINSTALLED, |
| 36 | + REACTIVATED, |
| 37 | + UNCHANGED |
| 38 | + } |
| 39 | + |
| 40 | + protected IdleDevicePushNotificationExperiment(final Clock clock) { |
| 41 | + this.clock = clock; |
| 42 | + } |
| 43 | + |
| 44 | + protected abstract Duration getMinIdleDuration(); |
| 45 | + |
| 46 | + protected abstract Duration getMaxIdleDuration(); |
| 47 | + |
| 48 | + @VisibleForTesting |
| 49 | + boolean isIdle(final Device device) { |
| 50 | + final Duration idleDuration = Duration.between(Instant.ofEpochMilli(device.getLastSeen()), clock.instant()); |
| 51 | + |
| 52 | + return idleDuration.compareTo(getMinIdleDuration()) >= 0 && idleDuration.compareTo(getMaxIdleDuration()) < 0; |
| 53 | + } |
| 54 | + |
| 55 | + @VisibleForTesting |
| 56 | + boolean hasPushToken(final Device device) { |
| 57 | + // Exclude VOIP tokens since they have their own, distinct delivery mechanism |
| 58 | + return !StringUtils.isAllBlank(device.getApnId(), device.getGcmId()) && StringUtils.isBlank(device.getVoipApnId()); |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public DeviceLastSeenState getState(@Nullable final Account account, @Nullable final Device device) { |
| 63 | + if (account != null && device != null) { |
| 64 | + final DeviceLastSeenState.PushTokenType pushTokenType; |
| 65 | + |
| 66 | + if (StringUtils.isNotBlank(device.getApnId())) { |
| 67 | + pushTokenType = DeviceLastSeenState.PushTokenType.APNS; |
| 68 | + } else if (StringUtils.isNotBlank(device.getGcmId())) { |
| 69 | + pushTokenType = DeviceLastSeenState.PushTokenType.FCM; |
| 70 | + } else { |
| 71 | + pushTokenType = null; |
| 72 | + } |
| 73 | + |
| 74 | + return new DeviceLastSeenState(true, device.getCreated(), hasPushToken(device), device.getLastSeen(), pushTokenType); |
| 75 | + } else { |
| 76 | + return DeviceLastSeenState.MISSING_DEVICE_STATE; |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + public void analyzeResults(final Flux<PushNotificationExperimentSample<DeviceLastSeenState>> samples) { |
| 82 | + final Map<Population, Map<Outcome, Integer>> contingencyTable = new EnumMap<>(Population.class); |
| 83 | + |
| 84 | + samples.doOnNext(sample -> |
| 85 | + contingencyTable.computeIfAbsent(getPopulation(sample), ignored -> new EnumMap<>(Outcome.class)) |
| 86 | + .merge(getOutcome(sample), 1, Integer::sum)) |
| 87 | + .then() |
| 88 | + .block(); |
| 89 | + |
| 90 | + final StringBuilder reportBuilder = new StringBuilder("population,deleted,uninstalled,reactivated,unchanged\n"); |
| 91 | + |
| 92 | + for (final Population population : Population.values()) { |
| 93 | + final Map<Outcome, Integer> countsByOutcome = contingencyTable.getOrDefault(population, Collections.emptyMap()); |
| 94 | + |
| 95 | + reportBuilder.append(population.name()); |
| 96 | + reportBuilder.append(","); |
| 97 | + reportBuilder.append(countsByOutcome.getOrDefault(Outcome.DELETED, 0)); |
| 98 | + reportBuilder.append(","); |
| 99 | + reportBuilder.append(countsByOutcome.getOrDefault(Outcome.UNINSTALLED, 0)); |
| 100 | + reportBuilder.append(","); |
| 101 | + reportBuilder.append(countsByOutcome.getOrDefault(Outcome.REACTIVATED, 0)); |
| 102 | + reportBuilder.append(","); |
| 103 | + reportBuilder.append(countsByOutcome.getOrDefault(Outcome.UNCHANGED, 0)); |
| 104 | + reportBuilder.append("\n"); |
| 105 | + } |
| 106 | + |
| 107 | + log.info(reportBuilder.toString()); |
| 108 | + } |
| 109 | + |
| 110 | + @VisibleForTesting |
| 111 | + static Population getPopulation(final PushNotificationExperimentSample<DeviceLastSeenState> sample) { |
| 112 | + assert sample.initialState() != null && sample.initialState().pushTokenType() != null; |
| 113 | + |
| 114 | + return switch (sample.initialState().pushTokenType()) { |
| 115 | + case APNS -> sample.inExperimentGroup() ? Population.APNS_EXPERIMENT : Population.APNS_CONTROL; |
| 116 | + case FCM -> sample.inExperimentGroup() ? Population.FCM_EXPERIMENT : Population.FCM_CONTROL; |
| 117 | + }; |
| 118 | + } |
| 119 | + |
| 120 | + @VisibleForTesting |
| 121 | + static Outcome getOutcome(final PushNotificationExperimentSample<DeviceLastSeenState> sample) { |
| 122 | + final Outcome outcome; |
| 123 | + |
| 124 | + assert sample.finalState() != null; |
| 125 | + |
| 126 | + if (!sample.finalState().deviceExists() || sample.initialState().createdAtMillis() != sample.finalState().createdAtMillis()) { |
| 127 | + outcome = Outcome.DELETED; |
| 128 | + } else if (!sample.finalState().hasPushToken()) { |
| 129 | + outcome = Outcome.UNINSTALLED; |
| 130 | + } else if (sample.initialState().lastSeenMillis() != sample.finalState().lastSeenMillis()) { |
| 131 | + outcome = Outcome.REACTIVATED; |
| 132 | + } else { |
| 133 | + outcome = Outcome.UNCHANGED; |
| 134 | + } |
| 135 | + |
| 136 | + return outcome; |
| 137 | + } |
| 138 | +} |
0 commit comments