Skip to content

Commit

Permalink
fix: stats (#1033)
Browse files Browse the repository at this point in the history
* fix: stats

* fix: test

* fix: version
  • Loading branch information
sattvikc authored Sep 2, 2024
1 parent 8912d2b commit c7f7186
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 87 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## Unreleased

## [9.2.1] - 2024-08-02

- Removes the stats that were resulting in high CPU consumption

## [9.2.0] - 2024-08-20

- Adds `SECURITY` feature in `EE_FEATURES`.
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ compileTestJava { options.encoding = "UTF-8" }
// }
//}

version = "9.2.0"
version = "9.2.1"


repositories {
Expand Down
102 changes: 56 additions & 46 deletions ee/src/main/java/io/supertokens/ee/EEFeatureFlag.java
Original file line number Diff line number Diff line change
Expand Up @@ -200,29 +200,34 @@ private JsonObject getMFAStats() throws StorageQueryException, TenantOrAppNotFou
// TODO: Active users are present only on public tenant and MFA users may be
// present on different storages
JsonObject result = new JsonObject();
Storage[] storages = StorageLayer.getStoragesForApp(main, this.appIdentifier);

int totalUserCountWithMoreThanOneLoginMethod = 0;
int[] maus = new int[31];
// Commenting out these stats for now as they are very CPU intensive and reduces the performance
// of other API calls while this is running.
// Also, we are not currently using these stats.

long now = System.currentTimeMillis();
// Storage[] storages = StorageLayer.getStoragesForApp(main, this.appIdentifier);

for (Storage storage : storages) {
totalUserCountWithMoreThanOneLoginMethod += ((AuthRecipeStorage) storage)
.getUsersCountWithMoreThanOneLoginMethodOrTOTPEnabled(this.appIdentifier);
// int totalUserCountWithMoreThanOneLoginMethod = 0;
// int[] maus = new int[31];

for (int i = 1; i <= 31; i++) {
long timestamp = now - (i * 24 * 60 * 60 * 1000L);
// long now = System.currentTimeMillis();

// `maus[i-1]` since i starts from 1
maus[i - 1] += ((ActiveUsersStorage) storage)
.countUsersThatHaveMoreThanOneLoginMethodOrTOTPEnabledAndActiveSince(appIdentifier, timestamp);
}
}
// for (Storage storage : storages) {
// totalUserCountWithMoreThanOneLoginMethod += ((AuthRecipeStorage) storage)
// .getUsersCountWithMoreThanOneLoginMethodOrTOTPEnabled(this.appIdentifier);

result.addProperty("totalUserCountWithMoreThanOneLoginMethodOrTOTPEnabled",
totalUserCountWithMoreThanOneLoginMethod);
result.add("mauWithMoreThanOneLoginMethodOrTOTPEnabled", new Gson().toJsonTree(maus));
// for (int i = 1; i <= 31; i++) {
// long timestamp = now - (i * 24 * 60 * 60 * 1000L);

// // `maus[i-1]` since i starts from 1
// maus[i - 1] += ((ActiveUsersStorage) storage)
// .countUsersThatHaveMoreThanOneLoginMethodOrTOTPEnabledAndActiveSince(appIdentifier, timestamp);
// }
// }

// result.addProperty("totalUserCountWithMoreThanOneLoginMethodOrTOTPEnabled",
// totalUserCountWithMoreThanOneLoginMethod);
// result.add("mauWithMoreThanOneLoginMethodOrTOTPEnabled", new Gson().toJsonTree(maus));
return result;
}

Expand Down Expand Up @@ -305,36 +310,41 @@ private JsonObject getAccountLinkingStats() throws StorageQueryException, Tenant
}

result.addProperty("usesAccountLinking", usesAccountLinking);
if (!usesAccountLinking) {
result.addProperty("totalUserCountWithMoreThanOneLoginMethod", 0);
JsonArray mauArray = new JsonArray();
for (int i = 0; i < 31; i++) {
mauArray.add(new JsonPrimitive(0));
}
result.add("mauWithMoreThanOneLoginMethod", mauArray);
return result;
}

int totalUserCountWithMoreThanOneLoginMethod = 0;
int[] maus = new int[31];

long now = System.currentTimeMillis();

for (Storage storage : storages) {
totalUserCountWithMoreThanOneLoginMethod += ((AuthRecipeStorage) storage).getUsersCountWithMoreThanOneLoginMethod(
this.appIdentifier);

for (int i = 1; i <= 31; i++) {
long timestamp = now - (i * 24 * 60 * 60 * 1000L);

// `maus[i-1]` because i starts from 1
maus[i - 1] += ((ActiveUsersStorage) storage).countUsersThatHaveMoreThanOneLoginMethodAndActiveSince(
appIdentifier, timestamp);
}
}

result.addProperty("totalUserCountWithMoreThanOneLoginMethod", totalUserCountWithMoreThanOneLoginMethod);
result.add("mauWithMoreThanOneLoginMethod", new Gson().toJsonTree(maus));
// Commenting out these stats for now as they are very CPU intensive and reduces the performance
// of other API calls while this is running.
// Also, we are not currently using these stats.

// if (!usesAccountLinking) {
// result.addProperty("totalUserCountWithMoreThanOneLoginMethod", 0);
// JsonArray mauArray = new JsonArray();
// for (int i = 0; i < 31; i++) {
// mauArray.add(new JsonPrimitive(0));
// }
// result.add("mauWithMoreThanOneLoginMethod", mauArray);
// return result;
// }

// int totalUserCountWithMoreThanOneLoginMethod = 0;
// int[] maus = new int[31];

// long now = System.currentTimeMillis();

// for (Storage storage : storages) {
// totalUserCountWithMoreThanOneLoginMethod += ((AuthRecipeStorage) storage).getUsersCountWithMoreThanOneLoginMethod(
// this.appIdentifier);

// for (int i = 1; i <= 31; i++) {
// long timestamp = now - (i * 24 * 60 * 60 * 1000L);

// // `maus[i-1]` because i starts from 1
// maus[i - 1] += ((ActiveUsersStorage) storage).countUsersThatHaveMoreThanOneLoginMethodAndActiveSince(
// appIdentifier, timestamp);
// }
// }

// result.addProperty("totalUserCountWithMoreThanOneLoginMethod", totalUserCountWithMoreThanOneLoginMethod);
// result.add("mauWithMoreThanOneLoginMethod", new Gson().toJsonTree(maus));
return result;
}

Expand Down
80 changes: 40 additions & 40 deletions src/test/java/io/supertokens/test/FeatureFlagTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -193,14 +193,14 @@ public void testThatCallingGetFeatureFlagAPIReturnsMfaStats() throws Exception {
assert maus.get(29).getAsInt() == 0;

JsonObject mfaStats = usageStats.get("mfa").getAsJsonObject();
int totalMfaUsers = mfaStats.get("totalUserCountWithMoreThanOneLoginMethodOrTOTPEnabled").getAsInt();
JsonArray mfaMaus = mfaStats.get("mauWithMoreThanOneLoginMethodOrTOTPEnabled").getAsJsonArray();
// int totalMfaUsers = mfaStats.get("totalUserCountWithMoreThanOneLoginMethodOrTOTPEnabled").getAsInt();
// JsonArray mfaMaus = mfaStats.get("mauWithMoreThanOneLoginMethodOrTOTPEnabled").getAsJsonArray();
//
// assert mfaMaus.size() == 31;
// assert mfaMaus.get(0).getAsInt() == 0;
// assert mfaMaus.get(29).getAsInt() == 0;

assert mfaMaus.size() == 31;
assert mfaMaus.get(0).getAsInt() == 0;
assert mfaMaus.get(29).getAsInt() == 0;

assert totalMfaUsers == 0;
// assert totalMfaUsers == 0;
}

// First register 2 users for emailpassword recipe.
Expand Down Expand Up @@ -253,15 +253,15 @@ public void testThatCallingGetFeatureFlagAPIReturnsMfaStats() throws Exception {
assert maus.get(0).getAsInt() == 2; // 2 users have signed up
assert maus.get(29).getAsInt() == 2;

JsonObject mfaStats = usageStats.get("mfa").getAsJsonObject();
int totalMfaUsers = mfaStats.get("totalUserCountWithMoreThanOneLoginMethodOrTOTPEnabled").getAsInt();
JsonArray mfaMaus = mfaStats.get("mauWithMoreThanOneLoginMethodOrTOTPEnabled").getAsJsonArray();

assert mfaMaus.size() == 31;
assert mfaMaus.get(0).getAsInt() == 1; // only 1 user has TOTP enabled
assert mfaMaus.get(29).getAsInt() == 1;

assert totalMfaUsers == 1;
// JsonObject mfaStats = usageStats.get("mfa").getAsJsonObject();
// int totalMfaUsers = mfaStats.get("totalUserCountWithMoreThanOneLoginMethodOrTOTPEnabled").getAsInt();
// JsonArray mfaMaus = mfaStats.get("mauWithMoreThanOneLoginMethodOrTOTPEnabled").getAsJsonArray();
//
// assert mfaMaus.size() == 31;
// assert mfaMaus.get(0).getAsInt() == 1; // only 1 user has TOTP enabled
// assert mfaMaus.get(29).getAsInt() == 1;
//
// assert totalMfaUsers == 1;
}

{
Expand Down Expand Up @@ -299,14 +299,14 @@ public void testThatCallingGetFeatureFlagAPIReturnsMfaStats() throws Exception {

{
JsonObject mfaStats = usageStats.get("mfa").getAsJsonObject();
int totalMfaUsers = mfaStats.get("totalUserCountWithMoreThanOneLoginMethodOrTOTPEnabled").getAsInt();
JsonArray mfaMaus = mfaStats.get("mauWithMoreThanOneLoginMethodOrTOTPEnabled").getAsJsonArray();

assert mfaMaus.size() == 31;
assert mfaMaus.get(0).getAsInt() == 2; // 1 TOTP user + 1 account linked user
assert mfaMaus.get(29).getAsInt() == 2;

assert totalMfaUsers == 2;
// int totalMfaUsers = mfaStats.get("totalUserCountWithMoreThanOneLoginMethodOrTOTPEnabled").getAsInt();
// JsonArray mfaMaus = mfaStats.get("mauWithMoreThanOneLoginMethodOrTOTPEnabled").getAsJsonArray();
//
// assert mfaMaus.size() == 31;
// assert mfaMaus.get(0).getAsInt() == 2; // 1 TOTP user + 1 account linked user
// assert mfaMaus.get(29).getAsInt() == 2;
//
// assert totalMfaUsers == 2;
}

// Add TOTP to the linked user
Expand Down Expand Up @@ -342,14 +342,14 @@ public void testThatCallingGetFeatureFlagAPIReturnsMfaStats() throws Exception {

{ // MFA stats should still count 2 users
JsonObject mfaStats = usageStats.get("mfa").getAsJsonObject();
int totalMfaUsers = mfaStats.get("totalUserCountWithMoreThanOneLoginMethodOrTOTPEnabled").getAsInt();
JsonArray mfaMaus = mfaStats.get("mauWithMoreThanOneLoginMethodOrTOTPEnabled").getAsJsonArray();

assert mfaMaus.size() == 31;
assert mfaMaus.get(0).getAsInt() == 2; // 1 TOTP user + 1 account linked user
assert mfaMaus.get(29).getAsInt() == 2;

assert totalMfaUsers == 2;
// int totalMfaUsers = mfaStats.get("totalUserCountWithMoreThanOneLoginMethodOrTOTPEnabled").getAsInt();
// JsonArray mfaMaus = mfaStats.get("mauWithMoreThanOneLoginMethodOrTOTPEnabled").getAsJsonArray();
//
// assert mfaMaus.size() == 31;
// assert mfaMaus.get(0).getAsInt() == 2; // 1 TOTP user + 1 account linked user
// assert mfaMaus.get(29).getAsInt() == 2;
//
// assert totalMfaUsers == 2;
}
}

Expand Down Expand Up @@ -378,14 +378,14 @@ public void testThatCallingGetFeatureFlagAPIReturnsMfaStats() throws Exception {

{ // MFA stats should still count 2 users
JsonObject mfaStats = usageStats.get("mfa").getAsJsonObject();
int totalMfaUsers = mfaStats.get("totalUserCountWithMoreThanOneLoginMethodOrTOTPEnabled").getAsInt();
JsonArray mfaMaus = mfaStats.get("mauWithMoreThanOneLoginMethodOrTOTPEnabled").getAsJsonArray();

assert mfaMaus.size() == 31;
assert mfaMaus.get(0).getAsInt() == 2; // 1 TOTP user + 1 account linked user
assert mfaMaus.get(29).getAsInt() == 2;

assert totalMfaUsers == 2;
// int totalMfaUsers = mfaStats.get("totalUserCountWithMoreThanOneLoginMethodOrTOTPEnabled").getAsInt();
// JsonArray mfaMaus = mfaStats.get("mauWithMoreThanOneLoginMethodOrTOTPEnabled").getAsJsonArray();
//
// assert mfaMaus.size() == 31;
// assert mfaMaus.get(0).getAsInt() == 2; // 1 TOTP user + 1 account linked user
// assert mfaMaus.get(29).getAsInt() == 2;
//
// assert totalMfaUsers == 2;
}
}

Expand Down

0 comments on commit c7f7186

Please sign in to comment.