Skip to content

Commit

Permalink
fix: telemetry data
Browse files Browse the repository at this point in the history
  • Loading branch information
sattvikc committed Feb 19, 2024
1 parent 2c6eafd commit acafbfc
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 6 deletions.
55 changes: 50 additions & 5 deletions src/main/java/io/supertokens/cronjobs/telemetry/Telemetry.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,26 @@

package io.supertokens.cronjobs.telemetry;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import io.supertokens.Main;
import io.supertokens.ProcessState;
import io.supertokens.authRecipe.AuthRecipe;
import io.supertokens.config.Config;
import io.supertokens.cronjobs.CronTask;
import io.supertokens.cronjobs.CronTaskTest;
import io.supertokens.dashboard.Dashboard;
import io.supertokens.httpRequest.HttpRequest;
import io.supertokens.httpRequest.HttpRequestMocking;
import io.supertokens.pluginInterface.ActiveUsersStorage;
import io.supertokens.pluginInterface.KeyValueInfo;
import io.supertokens.pluginInterface.STORAGE_TYPE;
import io.supertokens.pluginInterface.Storage;
import io.supertokens.pluginInterface.dashboard.DashboardUser;
import io.supertokens.pluginInterface.exceptions.StorageQueryException;
import io.supertokens.pluginInterface.multitenancy.AppIdentifier;
import io.supertokens.pluginInterface.multitenancy.AppIdentifierWithStorage;
import io.supertokens.pluginInterface.multitenancy.TenantIdentifier;
import io.supertokens.pluginInterface.multitenancy.exceptions.TenantOrAppNotFoundException;
import io.supertokens.storageLayer.StorageLayer;
Expand Down Expand Up @@ -90,14 +96,53 @@ protected void doTaskPerApp(AppIdentifier app) throws Exception {
json.addProperty("telemetryId", telemetryId.value);
json.addProperty("superTokensVersion", coreVersion);

ActiveUsersStorage activeUsersStorage = (ActiveUsersStorage) StorageLayer.getStorage(
app.getAsPublicTenantIdentifier(), main);

json.addProperty("appId", app.getAppId());
json.addProperty("connectionUriDomain", app.getConnectionUriDomain());

Storage[] storages = StorageLayer.getStoragesForApp(main, app);
AppIdentifierWithStorage appIdentifierWithAllTenantStorages = new AppIdentifierWithStorage(
app.getConnectionUriDomain(), app.getAppId(),
StorageLayer.getStorage(app.getAsPublicTenantIdentifier(), main), storages
);

if (StorageLayer.getBaseStorage(main).getType() == STORAGE_TYPE.SQL) {
ActiveUsersStorage activeUsersStorage = (ActiveUsersStorage) StorageLayer.getStorage(app.getAsPublicTenantIdentifier(), main);
json.addProperty("mau", activeUsersStorage.countUsersActiveSince(app, System.currentTimeMillis() - 30 * 24 * 3600 * 1000L));

// Users count across all tenants
json.addProperty("usersCount", AuthRecipe.getUsersCountAcrossAllTenants(appIdentifierWithAllTenantStorages, null));

{ // Dashboard user emails
DashboardUser[] dashboardUsers = Dashboard.getAllDashboardUsers(
appIdentifierWithAllTenantStorages, main);
JsonArray dashboardUserEmails = new JsonArray();
for (DashboardUser user : dashboardUsers) {
dashboardUserEmails.add(new JsonPrimitive(user.email));
}

json.add("dashboardUserEmails", dashboardUserEmails);
}

{ // MAUs
JsonArray mauArr = new JsonArray();

for (int i = 0; i < 30; i++) {
long now = System.currentTimeMillis();
long today = now - (now % (24 * 60 * 60 * 1000L));
long timestamp = today - (i * 24 * 60 * 60 * 1000L);
int mau = activeUsersStorage.countUsersActiveSince(app, timestamp);
mauArr.add(new JsonPrimitive(mau));
}

json.add("maus", mauArr);
}
} else {
json.addProperty("mau", -1);
json.addProperty("usersCount", -1);
json.add("dashboardUserEmails", new JsonArray());
json.add("maus", new JsonArray());
}
json.addProperty("appId", app.getAppId());
json.addProperty("connectionUriDomain", app.getConnectionUriDomain());


String url = "https://api.supertokens.io/0/st/telemetry";

Expand Down
16 changes: 15 additions & 1 deletion src/test/java/io/supertokens/test/TelemetryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import io.supertokens.ProcessState;
import io.supertokens.ProcessState.PROCESS_STATE;
import io.supertokens.cronjobs.telemetry.Telemetry;
import io.supertokens.dashboard.Dashboard;
import io.supertokens.httpRequest.HttpRequestMocking;
import io.supertokens.test.TestingProcessManager.TestingProcess;
import io.supertokens.version.Version;
Expand Down Expand Up @@ -111,6 +112,14 @@ public void testThatTelemetryWorks() throws Exception {
String[] args = { "../" };

TestingProcess process = TestingProcessManager.start(args, false);
process.startProcess();
assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STARTED));

Dashboard.signUpDashboardUser(process.getProcess(), "[email protected]", "password123");

// Restarting the process to send telemetry again
process.kill(false);
process = TestingProcessManager.start(args, false);

ByteArrayOutputStream output = new ByteArrayOutputStream();
final HttpURLConnection mockCon = mock(HttpURLConnection.class);
Expand Down Expand Up @@ -149,13 +158,18 @@ protected URLConnection openConnection(URL u) {
assertNotNull(process.checkOrWaitForEvent(PROCESS_STATE.SENT_TELEMETRY));

JsonObject telemetryData = new JsonParser().parse(output.toString()).getAsJsonObject();
assertEquals(7, telemetryData.entrySet().size());

assertTrue(telemetryData.has("telemetryId"));
assertEquals(telemetryData.get("superTokensVersion").getAsString(),
Version.getVersion(process.getProcess()).getCoreVersion());
assertEquals(telemetryData.get("appId").getAsString(), "public");
assertEquals(telemetryData.get("connectionUriDomain").getAsString(), "");
assertTrue(telemetryData.has("mau"));
assertTrue(telemetryData.has("maus"));
assertTrue(telemetryData.has("dashboardUserEmails"));
assertEquals(1, telemetryData.get("dashboardUserEmails").getAsJsonArray().size());
assertEquals("[email protected]", telemetryData.get("dashboardUserEmails").getAsJsonArray().get(0).getAsString());
assertEquals(30, telemetryData.get("maus").getAsJsonArray().size());

process.kill();
assertNotNull(process.checkOrWaitForEvent(PROCESS_STATE.STOPPED));
Expand Down

0 comments on commit acafbfc

Please sign in to comment.