Count numbers that can't be normalized because another account has the normalized form of the number

This commit is contained in:
Jon Chambers 2021-10-11 17:38:09 -04:00 committed by Jon Chambers
parent c379a3d297
commit f84e7aebd0
2 changed files with 37 additions and 9 deletions

View File

@ -514,7 +514,7 @@ public class WhisperServerService extends Application<WhisperServerConfiguration
directoryServerConfiguration.getReplicationName(), directoryReconciliationClient); directoryServerConfiguration.getReplicationName(), directoryReconciliationClient);
deletedAccountsDirectoryReconcilers.add(deletedAccountsDirectoryReconciler); deletedAccountsDirectoryReconcilers.add(deletedAccountsDirectoryReconciler);
} }
accountDatabaseCrawlerListeners.add(new NonNormalizedAccountCrawlerListener(metricsCluster)); accountDatabaseCrawlerListeners.add(new NonNormalizedAccountCrawlerListener(accountsManager, metricsCluster));
accountDatabaseCrawlerListeners.add(new ContactDiscoveryWriter(accountsManager)); accountDatabaseCrawlerListeners.add(new ContactDiscoveryWriter(accountsManager));
// PushFeedbackProcessor may update device properties // PushFeedbackProcessor may update device properties
accountDatabaseCrawlerListeners.add(new PushFeedbackProcessor(accountsManager)); accountDatabaseCrawlerListeners.add(new PushFeedbackProcessor(accountsManager));

View File

@ -19,24 +19,29 @@ import java.util.UUID;
public class NonNormalizedAccountCrawlerListener extends AccountDatabaseCrawlerListener { public class NonNormalizedAccountCrawlerListener extends AccountDatabaseCrawlerListener {
private final AccountsManager accountsManager;
private final FaultTolerantRedisCluster metricsCluster; private final FaultTolerantRedisCluster metricsCluster;
private static final String NORMALIZED_NUMBER_COUNT_KEY = "NonNormalizedAccountCrawlerListener::normalized"; private static final String NORMALIZED_NUMBER_COUNT_KEY = "NonNormalizedAccountCrawlerListener::normalized";
private static final String NON_NORMALIZED_NUMBER_COUNT_KEY = "NonNormalizedAccountCrawlerListener::nonNormalized"; private static final String NON_NORMALIZED_NUMBER_COUNT_KEY = "NonNormalizedAccountCrawlerListener::nonNormalized";
private static final String CONFLICTING_NUMBER_COUNT_KEY = "NonNormalizedAccountCrawlerListener::conflicts";
private static final PhoneNumberUtil PHONE_NUMBER_UTIL = PhoneNumberUtil.getInstance(); private static final PhoneNumberUtil PHONE_NUMBER_UTIL = PhoneNumberUtil.getInstance();
private static final Logger log = LoggerFactory.getLogger(NonNormalizedAccountCrawlerListener.class); private static final Logger log = LoggerFactory.getLogger(NonNormalizedAccountCrawlerListener.class);
public NonNormalizedAccountCrawlerListener(final FaultTolerantRedisCluster metricsCluster) { public NonNormalizedAccountCrawlerListener(
final AccountsManager accountsManager,
final FaultTolerantRedisCluster metricsCluster) {
this.accountsManager = accountsManager;
this.metricsCluster = metricsCluster; this.metricsCluster = metricsCluster;
} }
@Override @Override
public void onCrawlStart() { public void onCrawlStart() {
metricsCluster.useCluster(connection -> { metricsCluster.useCluster(connection ->
connection.sync().del(NORMALIZED_NUMBER_COUNT_KEY, NON_NORMALIZED_NUMBER_COUNT_KEY); connection.sync().del(NORMALIZED_NUMBER_COUNT_KEY, NON_NORMALIZED_NUMBER_COUNT_KEY, CONFLICTING_NUMBER_COUNT_KEY));
});
} }
@Override @Override
@ -44,25 +49,41 @@ public class NonNormalizedAccountCrawlerListener extends AccountDatabaseCrawlerL
final int normalizedNumbers; final int normalizedNumbers;
final int nonNormalizedNumbers; final int nonNormalizedNumbers;
final int conflictingNumbers;
{ {
int workingNormalizedNumbers = 0; int workingNormalizedNumbers = 0;
int workingNonNormalizedNumbers = 0; int workingNonNormalizedNumbers = 0;
int workingConflictingNumbers = 0;
for (final Account account : chunkAccounts) { for (final Account account : chunkAccounts) {
if (hasNumberNormalized(account)) { if (hasNumberNormalized(account)) {
workingNormalizedNumbers++; workingNormalizedNumbers++;
} else { } else {
workingNonNormalizedNumbers++; workingNonNormalizedNumbers++;
try {
final Optional<Account> maybeConflictingAccount = accountsManager.get(getNormalizedNumber(account));
if (maybeConflictingAccount.isPresent()) {
workingConflictingNumbers++;
log.info("Normalized form of number for account {} conflicts with number for account {}",
account.getUuid(), maybeConflictingAccount.get().getUuid());
}
} catch (final NumberParseException e) {
log.warn("Failed to parse phone number for account {}", account.getUuid(), e);
}
} }
} }
normalizedNumbers = workingNormalizedNumbers; normalizedNumbers = workingNormalizedNumbers;
nonNormalizedNumbers = workingNonNormalizedNumbers; nonNormalizedNumbers = workingNonNormalizedNumbers;
conflictingNumbers = workingConflictingNumbers;
} }
metricsCluster.useCluster(connection -> { metricsCluster.useCluster(connection -> {
connection.sync().incrby(NORMALIZED_NUMBER_COUNT_KEY, normalizedNumbers); connection.sync().incrby(NORMALIZED_NUMBER_COUNT_KEY, normalizedNumbers);
connection.sync().incrby(NON_NORMALIZED_NUMBER_COUNT_KEY, nonNormalizedNumbers); connection.sync().incrby(NON_NORMALIZED_NUMBER_COUNT_KEY, nonNormalizedNumbers);
connection.sync().incrby(CONFLICTING_NUMBER_COUNT_KEY, conflictingNumbers);
}); });
} }
@ -74,18 +95,25 @@ public class NonNormalizedAccountCrawlerListener extends AccountDatabaseCrawlerL
final int nonNormalizedNumbers = metricsCluster.withCluster(connection -> final int nonNormalizedNumbers = metricsCluster.withCluster(connection ->
Integer.parseInt(connection.sync().get(NON_NORMALIZED_NUMBER_COUNT_KEY))); Integer.parseInt(connection.sync().get(NON_NORMALIZED_NUMBER_COUNT_KEY)));
log.info("Crawl completed. Normalized numbers: {}; non-normalized numbers: {}", final int conflictingNumbers = metricsCluster.withCluster(connection ->
normalizedNumbers, nonNormalizedNumbers); Integer.parseInt(connection.sync().get(CONFLICTING_NUMBER_COUNT_KEY)));
log.info("Crawl completed. Normalized numbers: {}; non-normalized numbers: {}; conflicting numbers: {}",
normalizedNumbers, nonNormalizedNumbers, conflictingNumbers);
} }
@VisibleForTesting @VisibleForTesting
static boolean hasNumberNormalized(final Account account) { static boolean hasNumberNormalized(final Account account) {
try { try {
final PhoneNumber phoneNumber = PHONE_NUMBER_UTIL.parse(account.getNumber(), null); return account.getNumber().equals(getNormalizedNumber(account));
return account.getNumber().equals(PHONE_NUMBER_UTIL.format(phoneNumber, PhoneNumberFormat.E164));
} catch (final NumberParseException e) { } catch (final NumberParseException e) {
log.warn("Failed to parse phone number for account {}", account.getUuid(), e); log.warn("Failed to parse phone number for account {}", account.getUuid(), e);
return false; return false;
} }
} }
private static String getNormalizedNumber(final Account account) throws NumberParseException {
final PhoneNumber phoneNumber = PHONE_NUMBER_UTIL.parse(account.getNumber(), null);
return PHONE_NUMBER_UTIL.format(phoneNumber, PhoneNumberFormat.E164);
}
} }