Skip to content

Commit

Permalink
Don't send events within 2 seconds of the start event
Browse files Browse the repository at this point in the history
Signed-off-by: David Kwon <dakwon@redhat.com>
  • Loading branch information
dkwon17 authored and ibuziuk committed May 31, 2022
1 parent 52adedb commit d3b37c6
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 4 deletions.
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@
<artifactId>rest-assured</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-junit5-mockito</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.eclipse.che.incubator.workspace.telemetry.finder.UsernameFinder;
import org.slf4j.Logger;

import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.context.Dependent;
import javax.enterprise.inject.Alternative;
import java.io.BufferedReader;
Expand Down Expand Up @@ -64,14 +65,17 @@
*/
@SuppressWarnings("JavadocReference")
@Alternative
@Dependent
@ApplicationScoped
public class AnalyticsManager extends AbstractAnalyticsManager {
private static final Logger LOG = getLogger(AnalyticsManager.class);

private static final Logger LOG = getLogger(AnalyticsManager.class);
private static final String pingRequestFormat = "http://www.woopra.com/track/ping?host={0}&cookie={1}&timeout={2}&ka={3}&ra={4}";
private static final long startEventDebounceTime = 2000L;

private final Analytics analytics;

private long startEventTime;

String segmentWriteKey;
String woopraDomain;

Expand Down Expand Up @@ -182,6 +186,7 @@ private void checkActivity() {
});
}

@Override
public void onActivity() {
try {
dispatchers.get(userId).onActivity();
Expand All @@ -190,6 +195,24 @@ public void onActivity() {
}
}

@Override
public void doSendEvent(AnalyticsEvent event, String ownerId, String ip, String userAgent, String resolution,
Map<String, Object> properties) {
if (!isWithinWorkspaceStartDebounceTime(event)) {
super.doSendEvent(event, ownerId, ip, userAgent, resolution, properties);
}
}

private boolean isWithinWorkspaceStartDebounceTime(AnalyticsEvent event) {
long currentTime = System.currentTimeMillis();
if (event == AnalyticsEvent.WORKSPACE_STARTED) {
this.startEventTime = currentTime;
return false;
}
return currentTime - this.startEventTime < startEventDebounceTime;
}

@Override
public void onEvent(AnalyticsEvent event, String ownerId, String ip, String userAgent, String resolution,
Map<String, Object> properties) {
try {
Expand Down Expand Up @@ -387,6 +410,7 @@ void close() {
}
}

@Override
public void destroy() {
if (getUserId() != null) {
EventDispatcher dispatcher;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* Copyright (c) 2022 Red Hat, Inc.
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Red Hat, Inc. - initial API and implementation
*/
package com.redhat.devworkspace.services.telemetry.woopra;

import com.segment.analytics.Analytics;
import io.fabric8.kubernetes.api.model.GenericKubernetesResource;
import io.quarkus.test.Mock;
import io.quarkus.test.junit.QuarkusTest;
import io.quarkus.test.junit.mockito.InjectSpy;
import org.eclipse.che.incubator.workspace.telemetry.base.AbstractAnalyticsManager;
import org.eclipse.che.incubator.workspace.telemetry.base.AnalyticsEvent;
import org.eclipse.che.incubator.workspace.telemetry.finder.DevWorkspaceFinder;
import org.eclipse.che.incubator.workspace.telemetry.finder.UsernameFinder;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutorService;

import static java.util.Map.entry;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;

@QuarkusTest
class AbstractAnalyticsManagerEventTest {

@InjectSpy
AbstractAnalyticsManager analyticsManager;

@Test
public void testOnEventNotCalledAfterWorkspaceStart() {
String ownerId = "/default-theia-plugins/telemetry_plugin";
String ip = "192.0.0.0";
String userAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7)";
String resolution = "2560x1440";

Map<String, Object> properties = Collections.emptyMap();

analyticsManager.doSendEvent(AnalyticsEvent.WORKSPACE_STARTED, ownerId, ip, userAgent, resolution, properties);
analyticsManager.doSendEvent(AnalyticsEvent.EDITOR_USED, ownerId, ip, userAgent, resolution, properties);

Mockito.verify(analyticsManager, Mockito.times(1))
.onEvent(eq(AnalyticsEvent.WORKSPACE_STARTED), any(), any(), any(), any(), any());
}
}

@Mock
class MockUsernameFinder implements UsernameFinder {
@Override
public String findUsername() {
return "test-username";
}
}

@Mock
class MockDevworkspaceFinder implements DevWorkspaceFinder {
@Override
public GenericKubernetesResource findDevWorkspace(String devworkspaceId) {
return null;
}
}

@Mock
class MockAnalyticsProvider {
public Analytics getAnalytics(String segmentWriteKey, ExecutorService networkExecutor) {
return mock(Analytics.class);
}
}

@Mock
class HttpUrlConnectionProvider {
public HttpURLConnection getHttpUrlConnection(String uri)
throws MalformedURLException, IOException, URISyntaxException {
return mock(HttpURLConnection.class);
}
}

4 changes: 2 additions & 2 deletions src/test/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
%test.devworkspace.id=fake-devworkspace
%test.devworkspace.id=fake-devworkspace
%test.devworkspace.id=fake-devworkspace
%test.segment.write.key=writekey
%test.woopra.domain=domain

0 comments on commit d3b37c6

Please sign in to comment.