From 804d4320d757b8c57ffb1beb1f34328d24af4d0c Mon Sep 17 00:00:00 2001 From: Moxie Marlinspike Date: Wed, 13 Apr 2016 21:03:34 -0700 Subject: [PATCH] Metric for fresh user vs reregistration // FREEBIE --- .../controllers/AccountController.java | 16 ++++++++++++++-- .../textsecuregcm/storage/Accounts.java | 13 +++++++------ .../textsecuregcm/storage/AccountsManager.java | 6 ++++-- 3 files changed, 25 insertions(+), 10 deletions(-) diff --git a/src/main/java/org/whispersystems/textsecuregcm/controllers/AccountController.java b/src/main/java/org/whispersystems/textsecuregcm/controllers/AccountController.java index c48afac9f..76f7486b0 100644 --- a/src/main/java/org/whispersystems/textsecuregcm/controllers/AccountController.java +++ b/src/main/java/org/whispersystems/textsecuregcm/controllers/AccountController.java @@ -16,6 +16,10 @@ */ package org.whispersystems.textsecuregcm.controllers; +import com.codahale.metrics.Meter; +import com.codahale.metrics.MetricRegistry; +import com.codahale.metrics.SharedMetricRegistries; +import com.codahale.metrics.Timer; import com.codahale.metrics.annotation.Timed; import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Optional; @@ -38,8 +42,10 @@ import org.whispersystems.textsecuregcm.storage.AccountsManager; import org.whispersystems.textsecuregcm.storage.Device; import org.whispersystems.textsecuregcm.storage.MessagesManager; import org.whispersystems.textsecuregcm.storage.PendingAccountsManager; +import org.whispersystems.textsecuregcm.util.Constants; import org.whispersystems.textsecuregcm.util.Util; import org.whispersystems.textsecuregcm.util.VerificationCode; +import org.whispersystems.textsecuregcm.websocket.WebSocketConnection; import javax.validation.Valid; import javax.ws.rs.Consumes; @@ -60,12 +66,15 @@ import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; import java.util.Map; +import static com.codahale.metrics.MetricRegistry.name; import io.dropwizard.auth.Auth; @Path("/v1/accounts") public class AccountController { - private final Logger logger = LoggerFactory.getLogger(AccountController.class); + private final Logger logger = LoggerFactory.getLogger(AccountController.class); + private final MetricRegistry metricRegistry = SharedMetricRegistries.getOrCreate(Constants.METRICS_NAME); + private final Meter newUserMeter = metricRegistry.meter(name(AccountController.class, "brand_new_user")); private final PendingAccountsManager pendingAccounts; private final AccountsManager accounts; @@ -318,7 +327,10 @@ public class AccountController { account.setNumber(number); account.addDevice(device); - accounts.create(account); + if (accounts.create(account)) { + newUserMeter.mark(); + } + messagesManager.clear(number); pendingAccounts.remove(number); } diff --git a/src/main/java/org/whispersystems/textsecuregcm/storage/Accounts.java b/src/main/java/org/whispersystems/textsecuregcm/storage/Accounts.java index d56735b3d..43a243f76 100644 --- a/src/main/java/org/whispersystems/textsecuregcm/storage/Accounts.java +++ b/src/main/java/org/whispersystems/textsecuregcm/storage/Accounts.java @@ -53,11 +53,10 @@ public abstract class Accounts { private static final ObjectMapper mapper = SystemMapper.getMapper(); @SqlUpdate("INSERT INTO accounts (" + NUMBER + ", " + DATA + ") VALUES (:number, CAST(:data AS json))") - @GetGeneratedKeys - abstract long insertStep(@AccountBinder Account account); + abstract void insertStep(@AccountBinder Account account); @SqlUpdate("DELETE FROM accounts WHERE " + NUMBER + " = :number") - abstract void removeAccount(@Bind("number") String number); + abstract int removeAccount(@Bind("number") String number); @SqlUpdate("UPDATE accounts SET " + DATA + " = CAST(:data AS json) WHERE " + NUMBER + " = :number") abstract void update(@AccountBinder Account account); @@ -87,9 +86,11 @@ public abstract class Accounts { public abstract int getUnsignedKeysCount(@Bind("since") long since); @Transaction(TransactionIsolationLevel.SERIALIZABLE) - public long create(Account account) { - removeAccount(account.getNumber()); - return insertStep(account); + public boolean create(Account account) { + int rows = removeAccount(account.getNumber()); + insertStep(account); + + return rows == 0; } @SqlUpdate("VACUUM accounts") diff --git a/src/main/java/org/whispersystems/textsecuregcm/storage/AccountsManager.java b/src/main/java/org/whispersystems/textsecuregcm/storage/AccountsManager.java index 7a552eb10..b8b526eaa 100644 --- a/src/main/java/org/whispersystems/textsecuregcm/storage/AccountsManager.java +++ b/src/main/java/org/whispersystems/textsecuregcm/storage/AccountsManager.java @@ -64,10 +64,12 @@ public class AccountsManager { return accounts.getAll(); } - public void create(Account account) { - accounts.create(account); + public boolean create(Account account) { + boolean freshUser = accounts.create(account); memcacheSet(account.getNumber(), account); updateDirectory(account); + + return freshUser; } public void update(Account account) {