Skip to content

Commit

Permalink
only use 1 message broker
Browse files Browse the repository at this point in the history
Signed-off-by: Andre Dietisheim <[email protected]>
  • Loading branch information
adietish committed Dec 12, 2023
1 parent f0ba36d commit 9995b13
Show file tree
Hide file tree
Showing 13 changed files with 275 additions and 420 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,8 @@
public interface IMessageBroker {
void send(Event event);
void dispose();

interface IMessageBrokerFactory {
IMessageBroker create(boolean isDebug, ClassLoader classLoader);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ private void ensureCountry() {
}
}

class Buildable {
public class Buildable {
public Environment build() {
ensureIDE();
ensurePlatform();
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,7 @@

public class FeedbackServiceFactory implements DumbAware {

public IService create(ClassLoader classLoader) {
TelemetryConfiguration configuration = TelemetryConfiguration.getInstance();
IMessageBroker broker = createSegmentBroker(configuration.isDebug(), classLoader);
public IService create(IMessageBroker broker) {
return new FeedbackService(broker);
}

private IMessageBroker createSegmentBroker(boolean isDebug, ClassLoader classLoader) {
SegmentConfiguration brokerConfiguration = new SegmentConfiguration(classLoader);
return new SegmentBroker(
isDebug,
UserId.INSTANCE.get(),
null,
brokerConfiguration);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,17 @@
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.util.messages.MessageBusConnection;
import com.redhat.devtools.intellij.telemetry.core.IMessageBroker;
import com.redhat.devtools.intellij.telemetry.core.IService;
import com.redhat.devtools.intellij.telemetry.core.configuration.TelemetryConfiguration;
import com.redhat.devtools.intellij.telemetry.core.service.Event.Type;
import com.redhat.devtools.intellij.telemetry.core.service.segment.SegmentBrokerFactory;
import com.redhat.devtools.intellij.telemetry.core.util.Lazy;
import com.redhat.devtools.intellij.telemetry.core.util.TimeUtils;

import java.time.Duration;
import java.time.LocalDateTime;
import java.util.concurrent.SubmissionPublisher;
import java.util.function.Supplier;

import static com.redhat.devtools.intellij.telemetry.core.service.Event.Type.ACTION;
Expand All @@ -33,18 +37,31 @@ public class TelemetryMessageBuilder {

private static final Logger LOGGER = Logger.getInstance(TelemetryMessageBuilder.class);

private final TelemetryServiceFacade service;
private final IService telemetryFacade;
private final IService feedbackFacade;

public TelemetryMessageBuilder(ClassLoader classLoader) {
this(new TelemetryServiceFacade(classLoader));
TelemetryMessageBuilder(ClassLoader classLoader) {
this(new SegmentBrokerFactory().create(TelemetryConfiguration.getInstance().isDebug(), classLoader));
}

TelemetryMessageBuilder(TelemetryServiceFacade serviceFacade) {
this.service = serviceFacade;
TelemetryMessageBuilder(IMessageBroker messageBroker) {
this(
new TelemetryServiceFacade(TelemetryConfiguration.getInstance(), messageBroker),
new FeedbackServiceFacade(messageBroker)
);
}

TelemetryMessageBuilder(IService telemetryFacade, IService feedbackFacade) {
this.telemetryFacade = telemetryFacade;
this.feedbackFacade = feedbackFacade;
}

public ActionMessage action(String name) {
return new ActionMessage(name, service);
return new ActionMessage(name, telemetryFacade);
}

public FeedbackMessage feedback(String name) {
return new FeedbackMessage(name, feedbackFacade);
}

static class StartupMessage extends TelemetryMessage<StartupMessage> {
Expand Down Expand Up @@ -194,12 +211,17 @@ protected TelemetryMessage(Type type, String name, IService service) {

static class TelemetryServiceFacade extends Lazy<IService> implements IService {

protected TelemetryServiceFacade(final ClassLoader classLoader) {
this(() -> ApplicationManager.getApplication().getService(TelemetryServiceFactory.class).create(classLoader));
private final MessageBusConnection messageBusConnection;

protected TelemetryServiceFacade(final TelemetryConfiguration configuration, IMessageBroker broker) {
this(() -> ApplicationManager.getApplication().getService(TelemetryServiceFactory.class).create(configuration, broker),
ApplicationManager.getApplication().getMessageBus().connect()
);
}

protected TelemetryServiceFacade(final Supplier<IService> supplier) {
protected TelemetryServiceFacade(final Supplier<IService> supplier, MessageBusConnection connection) {
super(supplier);
this.messageBusConnection = connection;
}

@Override
Expand All @@ -213,8 +235,7 @@ private void sendStartup() {
}

private void onShutdown() {
MessageBusConnection connection = createMessageBusConnection();
connection.subscribe(AppLifecycleListener.TOPIC, new AppLifecycleListener() {
messageBusConnection.subscribe(AppLifecycleListener.TOPIC, new AppLifecycleListener() {
@Override
public void appWillBeClosed(boolean isRestart) {
sendShutdown();
Expand All @@ -226,13 +247,33 @@ protected void sendShutdown() {
new ShutdownMessage(TelemetryServiceFacade.this).send();
}

protected MessageBusConnection createMessageBusConnection() {
return ApplicationManager.getApplication().getMessageBus().connect();
@Override
public void send(Event event) {
get().send(event);
}
}

static class FeedbackServiceFacade extends Lazy<IService> implements IService {

protected FeedbackServiceFacade(final IMessageBroker broker) {
this(() -> ApplicationManager.getApplication().getService(FeedbackServiceFactory.class).create(broker));
}

protected FeedbackServiceFacade(final Supplier<IService> supplier) {
super(supplier);
}

@Override
public void send(Event event) {
get().send(event);
}
}

public static class FeedbackMessage extends Message<FeedbackMessage>{

FeedbackMessage(String name, IService service) {
super(ACTION, name, service);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,32 +13,10 @@
import com.intellij.openapi.project.DumbAware;
import com.redhat.devtools.intellij.telemetry.core.IMessageBroker;
import com.redhat.devtools.intellij.telemetry.core.configuration.TelemetryConfiguration;
import com.redhat.devtools.intellij.telemetry.core.service.segment.SegmentBroker;
import com.redhat.devtools.intellij.telemetry.core.service.segment.SegmentConfiguration;

public class TelemetryServiceFactory implements DumbAware {

private final IDE ide = new IDE.Factory()
.create()
.setJavaVersion();

public TelemetryService create(ClassLoader classLoader) {
Environment environment = new Environment.Builder()
.ide(ide)
.plugin(classLoader)
.build();
TelemetryConfiguration configuration = TelemetryConfiguration.getInstance();
IMessageBroker broker = createSegmentBroker(configuration.isDebug(), classLoader, environment);
public TelemetryService create(TelemetryConfiguration configuration, IMessageBroker broker) {
return new TelemetryService(configuration, broker);
}

private IMessageBroker createSegmentBroker(boolean isDebug, ClassLoader classLoader, Environment environment) {
SegmentConfiguration brokerConfiguration = new SegmentConfiguration(classLoader);
return new SegmentBroker(
isDebug,
UserId.INSTANCE.get(),
environment,
brokerConfiguration);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.redhat.devtools.intellij.telemetry.core.service.segment;

import com.redhat.devtools.intellij.telemetry.core.IMessageBroker;
import com.redhat.devtools.intellij.telemetry.core.service.Environment;
import com.redhat.devtools.intellij.telemetry.core.service.IDE;
import com.redhat.devtools.intellij.telemetry.core.service.UserId;

import static com.redhat.devtools.intellij.telemetry.core.IMessageBroker.*;

public class SegmentBrokerFactory implements IMessageBrokerFactory {

@Override
public IMessageBroker create(boolean isDebug, ClassLoader classLoader) {
Environment environment = createEnvironment(classLoader);
SegmentConfiguration configuration = new SegmentConfiguration(classLoader);
return new SegmentBroker(
isDebug,
UserId.INSTANCE.get(),
environment,
configuration);
}

private static Environment createEnvironment(ClassLoader classLoader) {
IDE ide = new IDE.Factory()
.create()
.setJavaVersion();
return new Environment.Builder()
.ide(ide)
.plugin(classLoader)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,16 @@ public static ISegmentConfiguration segmentConfiguration(String normalWriteKey,
});
return configuration;
}

public static TelemetryConfiguration telemetryConfiguration(TelemetryConfiguration.Mode mode) {
TelemetryConfiguration configuration = mock(TelemetryConfiguration.class);
when(configuration.isEnabled())
.thenReturn(mode != TelemetryConfiguration.Mode.DISABLED);
when(configuration.isConfigured())
.thenReturn(true);
when(configuration.getMode())
.thenReturn(mode);
return configuration;
}

}
Loading

0 comments on commit 9995b13

Please sign in to comment.