Skip to content

Commit

Permalink
feat: added feedback service (#73)
Browse files Browse the repository at this point in the history
Signed-off-by: Andre Dietisheim <[email protected]>
  • Loading branch information
adietish committed Dec 8, 2023
1 parent dcd80bf commit f0ba36d
Show file tree
Hide file tree
Showing 30 changed files with 776 additions and 278 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ dependencies {
testImplementation(
"org.junit.jupiter:junit-jupiter:5.9.1",
"org.junit.platform:junit-platform-launcher:1.9.1",
"org.assertj:assertj-core:3.22.0",
"org.assertj:assertj-core:3.24.2",
"org.mockito:mockito-inline:4.5.1"
)
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine'
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ ideaVersion = IC-2020.3
# https://www.jetbrains.org/intellij/sdk/docs/basics/getting_started/build_number_ranges.html
sinceIdeaBuild=203
projectVersion=1.0.1-SNAPSHOT
intellijPluginVersion=1.13.3
intellijPluginVersion=1.16.1
jetBrainsToken=invalid
jetBrainsChannel=stable
nexusUser=invalid
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
******************************************************************************/
package com.redhat.devtools.intellij.telemetry.core;

import com.redhat.devtools.intellij.telemetry.core.service.TelemetryEvent;
import com.redhat.devtools.intellij.telemetry.core.service.Event;

public interface IMessageBroker {
void send(TelemetryEvent event);
void send(Event event);
void dispose();
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2021 Red Hat, Inc.
* Copyright (c) 2023 Red Hat, Inc.
* Distributed under license by Red Hat, Inc. All rights reserved.
* This program is made available under the terms of the
* Eclipse Public License v2.0 which accompanies this distribution,
Expand All @@ -10,8 +10,8 @@
******************************************************************************/
package com.redhat.devtools.intellij.telemetry.core;

import com.redhat.devtools.intellij.telemetry.core.service.TelemetryEvent;
import com.redhat.devtools.intellij.telemetry.core.service.Event;

public interface ITelemetryService {
void send(TelemetryEvent event);
public interface IService {
void send(Event event);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2021 Red Hat, Inc.
* Copyright (c) 2023 Red Hat, Inc.
* Distributed under license by Red Hat, Inc. All rights reserved.
* This program is made available under the terms of the
* Eclipse Public License v2.0 which accompanies this distribution,
Expand All @@ -13,7 +13,7 @@
import java.util.HashMap;
import java.util.Map;

public class TelemetryEvent {
public class Event {

public enum Type {
USER, ACTION, STARTUP, SHUTDOWN
Expand All @@ -23,11 +23,11 @@ public enum Type {
private final String name;
private final Map<String, String> properties;

public TelemetryEvent(Type type, String name) {
public Event(Type type, String name) {
this(type, name, new HashMap<>());
}

public TelemetryEvent(Type type, String name, Map<String, String> properties) {
public Event(Type type, String name, Map<String, String> properties) {
this.type = type;
this.name = name;
this.properties = properties;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*******************************************************************************
* Copyright (c) 2023 Red Hat, Inc.
* Distributed under license by Red Hat, Inc. All rights reserved.
* This program is made available under the terms of the
* Eclipse Public License v2.0 which accompanies this distribution,
* and is available at http://www.eclipse.org/legal/epl-v20.html
*
* Contributors:
* Red Hat, Inc. - initial API and implementation
******************************************************************************/
package com.redhat.devtools.intellij.telemetry.core.service;

import java.util.HashMap;
import java.util.Map;

public class FeedbackEvent extends Event {

public FeedbackEvent(String name) {
this(name, new HashMap<>());
}

public FeedbackEvent(String name, Map<String, String> properties) {
super(Type.ACTION, name, properties);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*******************************************************************************
* Copyright (c) 2023 Red Hat, Inc.
* Distributed under license by Red Hat, Inc. All rights reserved.
* This program is made available under the terms of the
* Eclipse Public License v2.0 which accompanies this distribution,
* and is available at http://www.eclipse.org/legal/epl-v20.html
*
* Contributors:
* Red Hat, Inc. - initial API and implementation
******************************************************************************/
package com.redhat.devtools.intellij.telemetry.core.service;

import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.diagnostic.Logger;
import com.redhat.devtools.intellij.telemetry.core.IService;
import com.redhat.devtools.intellij.telemetry.core.util.Lazy;

import java.util.function.Supplier;

import static com.redhat.devtools.intellij.telemetry.core.service.Event.Type.ACTION;

public class FeedbackMessageBuilder {

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

private final IService serviceFacade;

public FeedbackMessageBuilder(ClassLoader classLoader) {
this(new FeedbackServiceFacade(classLoader));
}

FeedbackMessageBuilder(IService serviceFacade) {
this.serviceFacade = serviceFacade;
}

static class FeedbackServiceFacade extends Lazy<IService> implements IService {

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

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

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

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

public static class FeedbackMessage extends Message<FeedbackMessage>{

private FeedbackMessage(String name, IService service) {
super(ACTION, name, service);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*******************************************************************************
* Copyright (c) 2023 Red Hat, Inc.
* Distributed under license by Red Hat, Inc. All rights reserved.
* This program is made available under the terms of the
* Eclipse Public License v2.0 which accompanies this distribution,
* and is available at http://www.eclipse.org/legal/epl-v20.html
*
* Contributors:
* Red Hat, Inc. - initial API and implementation
******************************************************************************/
package com.redhat.devtools.intellij.telemetry.core.service;

import com.intellij.openapi.diagnostic.Logger;
import com.redhat.devtools.intellij.telemetry.core.IMessageBroker;
import com.redhat.devtools.intellij.telemetry.core.IService;

public class FeedbackService implements IService {

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

protected final IMessageBroker broker;

public FeedbackService(final IMessageBroker broker) {
this.broker = broker;
}

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

public void dispose() {
broker.dispose();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*******************************************************************************
* Copyright (c) 2023 Red Hat, Inc.
* Distributed under license by Red Hat, Inc. All rights reserved.
* This program is made available under the terms of the
* Eclipse Public License v2.0 which accompanies this distribution,
* and is available at http://www.eclipse.org/legal/epl-v20.html
*
* Contributors:
* Red Hat, Inc. - initial API and implementation
******************************************************************************/
package com.redhat.devtools.intellij.telemetry.core.service;

import com.intellij.openapi.project.DumbAware;
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.segment.SegmentBroker;
import com.redhat.devtools.intellij.telemetry.core.service.segment.SegmentConfiguration;

public class FeedbackServiceFactory implements DumbAware {

public IService create(ClassLoader classLoader) {
TelemetryConfiguration configuration = TelemetryConfiguration.getInstance();
IMessageBroker broker = createSegmentBroker(configuration.isDebug(), classLoader);
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
@@ -0,0 +1,69 @@
/*******************************************************************************
* Copyright (c) 2023 Red Hat, Inc.
* Distributed under license by Red Hat, Inc. All rights reserved.
* This program is made available under the terms of the
* Eclipse Public License v2.0 which accompanies this distribution,
* and is available at http://www.eclipse.org/legal/epl-v20.html
*
* Contributors:
* Red Hat, Inc. - initial API and implementation
******************************************************************************/
package com.redhat.devtools.intellij.telemetry.core.service;

import com.intellij.openapi.diagnostic.Logger;
import com.redhat.devtools.intellij.telemetry.core.IService;

import java.util.HashMap;
import java.util.Map;

abstract class Message<T extends Message> {

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

private final Event.Type type;
private final Map<String, String> properties = new HashMap<>();
private final String name;
private final IService service;

protected Message(Event.Type type, String name, IService service) {
this.name = name;
this.type = type;
this.service = service;
}

String getName() {
return name;
}

Event.Type getType() {
return type;
}

public T property(String key, String value) {
if (key == null
|| value == null) {
LOGGER.warn("Ignored property with key: " + key + " value: " + value);
} else {
properties.put(key, value);
}
return (T) this;
}

String getProperty(String key) {
return properties.get(key);
}

Map<String, String> properties() {
return properties;
}

protected boolean hasProperty(String key) {
return properties.containsKey(key);
}

public Event send() {
Event event = new Event(type, name, new HashMap<>(properties));
service.send(event);
return event;
}
}
Loading

0 comments on commit f0ba36d

Please sign in to comment.