Skip to content

Commit c7f7186

Browse files
authored
fix: stats (#1033)
* fix: stats * fix: test * fix: version
1 parent 8912d2b commit c7f7186

File tree

4 files changed

+101
-87
lines changed

4 files changed

+101
-87
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

88
## Unreleased
99

10+
## [9.2.1] - 2024-08-02
11+
12+
- Removes the stats that were resulting in high CPU consumption
13+
1014
## [9.2.0] - 2024-08-20
1115

1216
- Adds `SECURITY` feature in `EE_FEATURES`.

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ compileTestJava { options.encoding = "UTF-8" }
1919
// }
2020
//}
2121

22-
version = "9.2.0"
22+
version = "9.2.1"
2323

2424

2525
repositories {

ee/src/main/java/io/supertokens/ee/EEFeatureFlag.java

Lines changed: 56 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -200,29 +200,34 @@ private JsonObject getMFAStats() throws StorageQueryException, TenantOrAppNotFou
200200
// TODO: Active users are present only on public tenant and MFA users may be
201201
// present on different storages
202202
JsonObject result = new JsonObject();
203-
Storage[] storages = StorageLayer.getStoragesForApp(main, this.appIdentifier);
204203

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

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

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

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

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

223-
result.addProperty("totalUserCountWithMoreThanOneLoginMethodOrTOTPEnabled",
224-
totalUserCountWithMoreThanOneLoginMethod);
225-
result.add("mauWithMoreThanOneLoginMethodOrTOTPEnabled", new Gson().toJsonTree(maus));
219+
// for (int i = 1; i <= 31; i++) {
220+
// long timestamp = now - (i * 24 * 60 * 60 * 1000L);
221+
222+
// // `maus[i-1]` since i starts from 1
223+
// maus[i - 1] += ((ActiveUsersStorage) storage)
224+
// .countUsersThatHaveMoreThanOneLoginMethodOrTOTPEnabledAndActiveSince(appIdentifier, timestamp);
225+
// }
226+
// }
227+
228+
// result.addProperty("totalUserCountWithMoreThanOneLoginMethodOrTOTPEnabled",
229+
// totalUserCountWithMoreThanOneLoginMethod);
230+
// result.add("mauWithMoreThanOneLoginMethodOrTOTPEnabled", new Gson().toJsonTree(maus));
226231
return result;
227232
}
228233

@@ -305,36 +310,41 @@ private JsonObject getAccountLinkingStats() throws StorageQueryException, Tenant
305310
}
306311

307312
result.addProperty("usesAccountLinking", usesAccountLinking);
308-
if (!usesAccountLinking) {
309-
result.addProperty("totalUserCountWithMoreThanOneLoginMethod", 0);
310-
JsonArray mauArray = new JsonArray();
311-
for (int i = 0; i < 31; i++) {
312-
mauArray.add(new JsonPrimitive(0));
313-
}
314-
result.add("mauWithMoreThanOneLoginMethod", mauArray);
315-
return result;
316-
}
317-
318-
int totalUserCountWithMoreThanOneLoginMethod = 0;
319-
int[] maus = new int[31];
320-
321-
long now = System.currentTimeMillis();
322-
323-
for (Storage storage : storages) {
324-
totalUserCountWithMoreThanOneLoginMethod += ((AuthRecipeStorage) storage).getUsersCountWithMoreThanOneLoginMethod(
325-
this.appIdentifier);
326-
327-
for (int i = 1; i <= 31; i++) {
328-
long timestamp = now - (i * 24 * 60 * 60 * 1000L);
329-
330-
// `maus[i-1]` because i starts from 1
331-
maus[i - 1] += ((ActiveUsersStorage) storage).countUsersThatHaveMoreThanOneLoginMethodAndActiveSince(
332-
appIdentifier, timestamp);
333-
}
334-
}
335313

336-
result.addProperty("totalUserCountWithMoreThanOneLoginMethod", totalUserCountWithMoreThanOneLoginMethod);
337-
result.add("mauWithMoreThanOneLoginMethod", new Gson().toJsonTree(maus));
314+
// Commenting out these stats for now as they are very CPU intensive and reduces the performance
315+
// of other API calls while this is running.
316+
// Also, we are not currently using these stats.
317+
318+
// if (!usesAccountLinking) {
319+
// result.addProperty("totalUserCountWithMoreThanOneLoginMethod", 0);
320+
// JsonArray mauArray = new JsonArray();
321+
// for (int i = 0; i < 31; i++) {
322+
// mauArray.add(new JsonPrimitive(0));
323+
// }
324+
// result.add("mauWithMoreThanOneLoginMethod", mauArray);
325+
// return result;
326+
// }
327+
328+
// int totalUserCountWithMoreThanOneLoginMethod = 0;
329+
// int[] maus = new int[31];
330+
331+
// long now = System.currentTimeMillis();
332+
333+
// for (Storage storage : storages) {
334+
// totalUserCountWithMoreThanOneLoginMethod += ((AuthRecipeStorage) storage).getUsersCountWithMoreThanOneLoginMethod(
335+
// this.appIdentifier);
336+
337+
// for (int i = 1; i <= 31; i++) {
338+
// long timestamp = now - (i * 24 * 60 * 60 * 1000L);
339+
340+
// // `maus[i-1]` because i starts from 1
341+
// maus[i - 1] += ((ActiveUsersStorage) storage).countUsersThatHaveMoreThanOneLoginMethodAndActiveSince(
342+
// appIdentifier, timestamp);
343+
// }
344+
// }
345+
346+
// result.addProperty("totalUserCountWithMoreThanOneLoginMethod", totalUserCountWithMoreThanOneLoginMethod);
347+
// result.add("mauWithMoreThanOneLoginMethod", new Gson().toJsonTree(maus));
338348
return result;
339349
}
340350

src/test/java/io/supertokens/test/FeatureFlagTest.java

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -193,14 +193,14 @@ public void testThatCallingGetFeatureFlagAPIReturnsMfaStats() throws Exception {
193193
assert maus.get(29).getAsInt() == 0;
194194

195195
JsonObject mfaStats = usageStats.get("mfa").getAsJsonObject();
196-
int totalMfaUsers = mfaStats.get("totalUserCountWithMoreThanOneLoginMethodOrTOTPEnabled").getAsInt();
197-
JsonArray mfaMaus = mfaStats.get("mauWithMoreThanOneLoginMethodOrTOTPEnabled").getAsJsonArray();
196+
// int totalMfaUsers = mfaStats.get("totalUserCountWithMoreThanOneLoginMethodOrTOTPEnabled").getAsInt();
197+
// JsonArray mfaMaus = mfaStats.get("mauWithMoreThanOneLoginMethodOrTOTPEnabled").getAsJsonArray();
198+
//
199+
// assert mfaMaus.size() == 31;
200+
// assert mfaMaus.get(0).getAsInt() == 0;
201+
// assert mfaMaus.get(29).getAsInt() == 0;
198202

199-
assert mfaMaus.size() == 31;
200-
assert mfaMaus.get(0).getAsInt() == 0;
201-
assert mfaMaus.get(29).getAsInt() == 0;
202-
203-
assert totalMfaUsers == 0;
203+
// assert totalMfaUsers == 0;
204204
}
205205

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

256-
JsonObject mfaStats = usageStats.get("mfa").getAsJsonObject();
257-
int totalMfaUsers = mfaStats.get("totalUserCountWithMoreThanOneLoginMethodOrTOTPEnabled").getAsInt();
258-
JsonArray mfaMaus = mfaStats.get("mauWithMoreThanOneLoginMethodOrTOTPEnabled").getAsJsonArray();
259-
260-
assert mfaMaus.size() == 31;
261-
assert mfaMaus.get(0).getAsInt() == 1; // only 1 user has TOTP enabled
262-
assert mfaMaus.get(29).getAsInt() == 1;
263-
264-
assert totalMfaUsers == 1;
256+
// JsonObject mfaStats = usageStats.get("mfa").getAsJsonObject();
257+
// int totalMfaUsers = mfaStats.get("totalUserCountWithMoreThanOneLoginMethodOrTOTPEnabled").getAsInt();
258+
// JsonArray mfaMaus = mfaStats.get("mauWithMoreThanOneLoginMethodOrTOTPEnabled").getAsJsonArray();
259+
//
260+
// assert mfaMaus.size() == 31;
261+
// assert mfaMaus.get(0).getAsInt() == 1; // only 1 user has TOTP enabled
262+
// assert mfaMaus.get(29).getAsInt() == 1;
263+
//
264+
// assert totalMfaUsers == 1;
265265
}
266266

267267
{
@@ -299,14 +299,14 @@ public void testThatCallingGetFeatureFlagAPIReturnsMfaStats() throws Exception {
299299

300300
{
301301
JsonObject mfaStats = usageStats.get("mfa").getAsJsonObject();
302-
int totalMfaUsers = mfaStats.get("totalUserCountWithMoreThanOneLoginMethodOrTOTPEnabled").getAsInt();
303-
JsonArray mfaMaus = mfaStats.get("mauWithMoreThanOneLoginMethodOrTOTPEnabled").getAsJsonArray();
304-
305-
assert mfaMaus.size() == 31;
306-
assert mfaMaus.get(0).getAsInt() == 2; // 1 TOTP user + 1 account linked user
307-
assert mfaMaus.get(29).getAsInt() == 2;
308-
309-
assert totalMfaUsers == 2;
302+
// int totalMfaUsers = mfaStats.get("totalUserCountWithMoreThanOneLoginMethodOrTOTPEnabled").getAsInt();
303+
// JsonArray mfaMaus = mfaStats.get("mauWithMoreThanOneLoginMethodOrTOTPEnabled").getAsJsonArray();
304+
//
305+
// assert mfaMaus.size() == 31;
306+
// assert mfaMaus.get(0).getAsInt() == 2; // 1 TOTP user + 1 account linked user
307+
// assert mfaMaus.get(29).getAsInt() == 2;
308+
//
309+
// assert totalMfaUsers == 2;
310310
}
311311

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

343343
{ // MFA stats should still count 2 users
344344
JsonObject mfaStats = usageStats.get("mfa").getAsJsonObject();
345-
int totalMfaUsers = mfaStats.get("totalUserCountWithMoreThanOneLoginMethodOrTOTPEnabled").getAsInt();
346-
JsonArray mfaMaus = mfaStats.get("mauWithMoreThanOneLoginMethodOrTOTPEnabled").getAsJsonArray();
347-
348-
assert mfaMaus.size() == 31;
349-
assert mfaMaus.get(0).getAsInt() == 2; // 1 TOTP user + 1 account linked user
350-
assert mfaMaus.get(29).getAsInt() == 2;
351-
352-
assert totalMfaUsers == 2;
345+
// int totalMfaUsers = mfaStats.get("totalUserCountWithMoreThanOneLoginMethodOrTOTPEnabled").getAsInt();
346+
// JsonArray mfaMaus = mfaStats.get("mauWithMoreThanOneLoginMethodOrTOTPEnabled").getAsJsonArray();
347+
//
348+
// assert mfaMaus.size() == 31;
349+
// assert mfaMaus.get(0).getAsInt() == 2; // 1 TOTP user + 1 account linked user
350+
// assert mfaMaus.get(29).getAsInt() == 2;
351+
//
352+
// assert totalMfaUsers == 2;
353353
}
354354
}
355355

@@ -378,14 +378,14 @@ public void testThatCallingGetFeatureFlagAPIReturnsMfaStats() throws Exception {
378378

379379
{ // MFA stats should still count 2 users
380380
JsonObject mfaStats = usageStats.get("mfa").getAsJsonObject();
381-
int totalMfaUsers = mfaStats.get("totalUserCountWithMoreThanOneLoginMethodOrTOTPEnabled").getAsInt();
382-
JsonArray mfaMaus = mfaStats.get("mauWithMoreThanOneLoginMethodOrTOTPEnabled").getAsJsonArray();
383-
384-
assert mfaMaus.size() == 31;
385-
assert mfaMaus.get(0).getAsInt() == 2; // 1 TOTP user + 1 account linked user
386-
assert mfaMaus.get(29).getAsInt() == 2;
387-
388-
assert totalMfaUsers == 2;
381+
// int totalMfaUsers = mfaStats.get("totalUserCountWithMoreThanOneLoginMethodOrTOTPEnabled").getAsInt();
382+
// JsonArray mfaMaus = mfaStats.get("mauWithMoreThanOneLoginMethodOrTOTPEnabled").getAsJsonArray();
383+
//
384+
// assert mfaMaus.size() == 31;
385+
// assert mfaMaus.get(0).getAsInt() == 2; // 1 TOTP user + 1 account linked user
386+
// assert mfaMaus.get(29).getAsInt() == 2;
387+
//
388+
// assert totalMfaUsers == 2;
389389
}
390390
}
391391

0 commit comments

Comments
 (0)