Skip to content

Commit

Permalink
fix: only send identify events if traits changed (#46)
Browse files Browse the repository at this point in the history
Signed-off-by: Andre Dietisheim <adietish@redhat.com>
  • Loading branch information
adietish committed Jun 9, 2022
1 parent 027e123 commit edfe7b5
Show file tree
Hide file tree
Showing 18 changed files with 824 additions and 182 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,18 @@

import com.intellij.openapi.application.ApplicationManager;
import com.intellij.util.messages.Topic;
import com.redhat.devtools.intellij.telemetry.core.util.Directories;

import java.io.IOException;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;

public class TelemetryConfiguration extends CompositeConfiguration {

public static final String KEY_MODE = "com.redhat.devtools.intellij.telemetry.mode";

private static final SaveableFileConfiguration FILE = new SaveableFileConfiguration(Paths.get(
System.getProperty("user.home"),
".redhat",
"com.redhat.devtools.intellij.telemetry"));
private static final SaveableFileConfiguration FILE = new SaveableFileConfiguration(
Directories.RED_HAT.resolve("com.redhat.devtools.intellij.telemetry"));

private static TelemetryConfiguration INSTANCE = new TelemetryConfiguration();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

public class Application {
Expand Down Expand Up @@ -45,4 +46,19 @@ public Collection<AbstractMap.SimpleEntry<String, Object>> getProperties() {
.map(entry -> new AbstractMap.SimpleEntry<String, Object>(entry.getKey(), entry.getValue()))
.collect(Collectors.toList());
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Application)) return false;
Application that = (Application) o;
return Objects.equals(name, that.name)
&& Objects.equals(version, that.version)
&& Objects.equals(properties, that.properties);
}

@Override
public int hashCode() {
return Objects.hash(name, version, properties);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,75 +10,30 @@
******************************************************************************/
package com.redhat.devtools.intellij.telemetry.core.service;

import com.intellij.ide.plugins.IdeaPluginDescriptor;
import com.intellij.ide.plugins.PluginManagerCore;

import java.util.Arrays;
import java.util.Locale;
import java.util.Objects;
import java.util.TimeZone;

public class Environment {

public static final String UNKNOWN_COUNTRY = "ZZ";

private final Application plugin;
private final Application application;
private final Plugin plugin;
private final IDE ide;
private final Platform platform;
private final String timezone;
private final String locale;
private final String country;

private Environment(Application plugin, Application application, Platform platform, String timezone, String locale, String country) {
private Environment(Plugin plugin, IDE ide, Platform platform, String timezone, String locale, String country) {
this.plugin = plugin;
this.application = application;
this.ide = ide;
this.platform = platform;
this.timezone = timezone;
this.locale = locale;
this.country = country;
}

/**
* Returns the plugin from which Telemetry events are sent.
*/
public Application getPlugin() {
return plugin;
}

/**
* Returns the application from which Telemetry events are sent .
*/
public Application getApplication() {
return application;
}

/**
* Returns the platform (or OS) from from which Telemetry events are sent.
*/
public Platform getPlatform() {
return platform;
}

/**
* Returns the user timezone, eg. 'Europe/Paris'
*/
public String getTimezone() {
return timezone;
}

/**
* Returns the user locale, eg. 'en-US'
*/
public String getLocale() {
return locale;
}

/**
* Returns the users ISO country code, eg. 'CA' for Canada
*/
public String getCountry() {
return country;
}

public static class Builder {

private IDE ide;
Expand All @@ -99,6 +54,15 @@ private void ensureIDE() {
}
}

public Buildable plugin(ClassLoader classLoader) {
return plugin(new Plugin.Factory().create(classLoader));
}

public Buildable plugin(Plugin plugin) {
this.plugin = plugin;
return new Buildable();
}

public Builder platform(Platform platform) {
this.platform = platform;
return this;
Expand Down Expand Up @@ -153,15 +117,6 @@ private void ensureCountry() {
}
}

public Buildable plugin(ClassLoader classLoader) {
return plugin(new Plugin.Factory().create(classLoader));
}

public Buildable plugin(Plugin plugin) {
this.plugin = plugin;
return new Buildable();
}

class Buildable {
public Environment build() {
ensureIDE();
Expand All @@ -174,5 +129,62 @@ public Environment build() {
}
}

/**
* Returns the plugin from which Telemetry events are sent.
*/
public Application getPlugin() {
return plugin;
}

/**
* Returns the application from which Telemetry events are sent .
*/
public IDE getIde() {
return ide;
}

/**
* Returns the platform (or OS) from from which Telemetry events are sent.
*/
public Platform getPlatform() {
return platform;
}

/**
* Returns the user timezone, eg. 'Europe/Paris'
*/
public String getTimezone() {
return timezone;
}

/**
* Returns the user locale, eg. 'en-US'
*/
public String getLocale() {
return locale;
}

public String getCountry() {
return country;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Environment)) return false;
Environment that = (Environment) o;
return Objects.equals(plugin, that.plugin)
&& Objects.equals(ide, that.ide)
&& Objects.equals(platform, that.platform)
&& Objects.equals(timezone, that.timezone)
&& Objects.equals(locale, that.locale)
&& Objects.equals(country, that.country);
}

@Override
public int hashCode() {
return Objects.hash(plugin, ide, platform, timezone, locale, country);
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ public IDE create() {
}

public IDE setJavaVersion() {
property(PROP_JAVA_VERSION, System.getProperty("java.version"));
return setJavaVersion(System.getProperty("java.version"));
}

public IDE setJavaVersion(String version) {
property(PROP_JAVA_VERSION, version);
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

import com.intellij.openapi.util.SystemInfo;

import java.util.Objects;

public class Platform {

Platform() {
Expand Down Expand Up @@ -44,4 +46,19 @@ public String getDistribution() {
public String getVersion() {
return version;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Platform)) return false;
Platform platform = (Platform) o;
return Objects.equals(name, platform.name)
&& Objects.equals(distribution, platform.distribution)
&& Objects.equals(version, platform.version);
}

@Override
public int hashCode() {
return Objects.hash(name, distribution, version);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.redhat.devtools.intellij.telemetry.core.util.CircularBuffer;
import com.redhat.devtools.intellij.telemetry.ui.TelemetryNotifications;

import java.util.Objects;
import java.util.concurrent.atomic.AtomicBoolean;

import static com.redhat.devtools.intellij.telemetry.core.configuration.TelemetryConfiguration.*;
Expand All @@ -33,15 +34,17 @@ public class TelemetryService implements ITelemetryService {
private final TelemetryNotifications notifications;
private final TelemetryConfiguration configuration;
protected final IMessageBroker broker;
private final AtomicBoolean userInfoSent = new AtomicBoolean(false);
private final AtomicBoolean userQueried = new AtomicBoolean(false);
private final CircularBuffer<TelemetryEvent> onHold = new CircularBuffer<>(BUFFER_SIZE);

public TelemetryService(final TelemetryConfiguration configuration, final IMessageBroker broker) {
this(configuration, broker, ApplicationManager.getApplication().getMessageBus().connect(), new TelemetryNotifications());
}

TelemetryService(final TelemetryConfiguration configuration, final IMessageBroker broker, final MessageBusConnection connection, final TelemetryNotifications notifications) {
TelemetryService(final TelemetryConfiguration configuration,
final IMessageBroker broker,
final MessageBusConnection connection,
final TelemetryNotifications notifications) {
this.configuration = configuration;
this.broker = broker;
this.notifications = notifications;
Expand All @@ -65,11 +68,9 @@ public void send(TelemetryEvent event) {
}

private void sendUserInfo() {
if (userInfoSent.compareAndSet(false, true)) {
doSend(new TelemetryEvent(
Type.USER,
"Anonymous ID: " + UserId.INSTANCE.get()));
}
doSend(new TelemetryEvent(
Type.USER,
"Anonymous ID: " + UserId.INSTANCE.get()));
}

private void queryUserConsent() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@
package com.redhat.devtools.intellij.telemetry.core.service;

import com.intellij.openapi.diagnostic.Logger;
import com.redhat.devtools.intellij.telemetry.core.util.Directories;
import com.redhat.devtools.intellij.telemetry.core.util.FileUtils;
import com.redhat.devtools.intellij.telemetry.core.util.Lazy;

import java.io.IOException;
import java.io.Writer;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.UUID;
import java.util.stream.Stream;

Expand All @@ -26,8 +27,7 @@ public class UserId {
private static final Logger LOGGER = Logger.getInstance(UserId.class);

public static final UserId INSTANCE = new UserId();
private static final Path REDHAT_DIRECTORY = Paths.get(System.getProperty("user.home"), ".redhat");
private static final Path UUID_FILE = REDHAT_DIRECTORY.resolve("anonymousId");
private static final Path UUID_FILE = Directories.RED_HAT.resolve("anonymousId");

private final Lazy<String> uuid = new Lazy<>(() -> loadOrCreate(UUID_FILE));

Expand Down Expand Up @@ -62,13 +62,8 @@ private String load(Path uuidFile) {

private void write(String uuid, Path uuidFile) {
try {
if (!Files.exists(uuidFile.getParent())) {
Files.createDirectories(uuidFile.getParent());
}
Files.createFile(uuidFile);
try (Writer writer = Files.newBufferedWriter(uuidFile)) {
writer.append(uuid);
}
FileUtils.createFileAndParent(uuidFile);
FileUtils.write(uuid, uuidFile);
} catch (IOException e) {
LOGGER.warn("Could not write redhat anonymous UUID to file at " + UUID_FILE.toAbsolutePath(), e);
}
Expand Down
Loading

0 comments on commit edfe7b5

Please sign in to comment.