-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add plugin installed event [IDE-736] (#211)
* feat: add plugin installed event * feat: add plugin installed analytics event and base url setting * fix: improve startup * docs: updated changelog * fix: improve startup wizard * fix: tests * fix: tests * fix: check if test in token refresher * chore: add test for plugin sending * chore: add second test for plugin install sending * chore: optimize import * chore: revert log level to info
- Loading branch information
1 parent
be26ddb
commit 4ed6db1
Showing
19 changed files
with
1,010 additions
and
649 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
plugin/src/main/java/io/snyk/eclipse/plugin/analytics/AbstractAnalyticsEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package io.snyk.eclipse.plugin.analytics; | ||
|
||
public interface AbstractAnalyticsEvent { | ||
|
||
} |
74 changes: 74 additions & 0 deletions
74
plugin/src/main/java/io/snyk/eclipse/plugin/analytics/AnalyticsEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package io.snyk.eclipse.plugin.analytics; | ||
|
||
import java.time.Instant; | ||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import io.snyk.eclipse.plugin.properties.preferences.Preferences; | ||
|
||
public class AnalyticsEvent implements AbstractAnalyticsEvent { | ||
private final String interactionType; | ||
private final List<String> category; | ||
private final String status; | ||
private final String targetId; | ||
private final long timestampMs; | ||
private final long durationMs; | ||
private final Map<String, Object> results; | ||
private final List<Object> errors; | ||
private final Map<String, Object> extension; | ||
|
||
public AnalyticsEvent(String interactionType, List<String> category, String status, String targetId, long timestampMs, long durationMs, Map<String, Object> results, List<Object> errors, Map<String, Object> extension) { | ||
this.interactionType = interactionType; | ||
this.category = category; | ||
this.status = status != null ? status : "success"; | ||
this.targetId = targetId != null ? targetId : "pkg:filesystem/scrubbed"; | ||
this.timestampMs = timestampMs != 0 ? timestampMs : Instant.now().toEpochMilli() ; | ||
this.durationMs = durationMs; | ||
this.results = results != null ? results : new HashMap<>(); | ||
this.errors = errors != null ? errors : new ArrayList<>(); | ||
this.extension = extension != null ? extension : new HashMap<>(); | ||
this.extension.put("device_id", Preferences.getInstance().getPref(Preferences.DEVICE_ID)); | ||
} | ||
|
||
public AnalyticsEvent(String interactionType, List<String> category) { | ||
this(interactionType, category, null, null, 0, 0, null, null, null); | ||
} | ||
|
||
public String getInteractionType() { | ||
return interactionType; | ||
} | ||
|
||
public List<String> getCategory() { | ||
return category; | ||
} | ||
|
||
public String getStatus() { | ||
return status; | ||
} | ||
|
||
public String getTargetId() { | ||
return targetId; | ||
} | ||
|
||
public long getTimestampMs() { | ||
return timestampMs; | ||
} | ||
|
||
public long getDurationMs() { | ||
return durationMs; | ||
} | ||
|
||
public Map<String, Object> getResults() { | ||
return results; | ||
} | ||
|
||
public List<Object> getErrors() { | ||
return errors; | ||
} | ||
|
||
public Map<String, Object> getExtension() { | ||
return extension; | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
plugin/src/main/java/io/snyk/eclipse/plugin/analytics/AnalyticsSender.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package io.snyk.eclipse.plugin.analytics; | ||
|
||
import java.util.LinkedList; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.ConcurrentLinkedQueue; | ||
import java.util.function.Consumer; | ||
|
||
import org.apache.commons.lang3.tuple.Pair; | ||
|
||
import io.snyk.eclipse.plugin.properties.preferences.Preferences; | ||
import io.snyk.eclipse.plugin.utils.SnykLogger; | ||
import io.snyk.languageserver.protocolextension.SnykExtendedLanguageClient; | ||
|
||
public class AnalyticsSender { | ||
// left = event, right = callback function | ||
private final ConcurrentLinkedQueue<Pair<AbstractAnalyticsEvent, Consumer<Void>>> eventQueue = new ConcurrentLinkedQueue<>(); | ||
|
||
private AnalyticsSender() { | ||
CompletableFuture.runAsync(() -> { start(); }); | ||
} | ||
|
||
private static AnalyticsSender instance; | ||
|
||
public static AnalyticsSender getInstance() { | ||
if (instance == null) { | ||
synchronized (AnalyticsSender.class) { | ||
if (instance == null) { | ||
instance = new AnalyticsSender(); | ||
} | ||
} | ||
} | ||
return instance; | ||
} | ||
|
||
private void start() { | ||
while (true) { | ||
String authToken = Preferences.getInstance().getAuthToken(); | ||
var lc = SnykExtendedLanguageClient.getInstance(); | ||
if (eventQueue.isEmpty() || authToken == null || authToken.isBlank() || lc == null ) { | ||
try { | ||
Thread.sleep(1000); | ||
} catch (InterruptedException e) { | ||
Thread.currentThread().interrupt(); | ||
} | ||
continue; | ||
} | ||
LinkedList<Pair<AbstractAnalyticsEvent, Consumer<Void>>> copyForSending = new LinkedList<>(eventQueue); | ||
for (Pair<AbstractAnalyticsEvent, Consumer<Void>> event : copyForSending) { | ||
try { | ||
lc.reportAnalytics(event.getLeft()); | ||
event.getRight().accept(null); | ||
} catch (Exception e) { | ||
SnykLogger.logError(e); | ||
} finally { | ||
eventQueue.remove(event); | ||
} | ||
} | ||
} | ||
} | ||
|
||
public void logEvent(AbstractAnalyticsEvent event, Consumer<Void> callback) { | ||
var pair = Pair.of(event, callback); | ||
eventQueue.add(pair); | ||
} | ||
} |
Oops, something went wrong.