Skip to content

Commit

Permalink
fixed complaints in review
Browse files Browse the repository at this point in the history
Signed-off-by: Andre Dietisheim <[email protected]>
  • Loading branch information
adietish committed Jun 26, 2024
1 parent 8a10723 commit c0d2b72
Show file tree
Hide file tree
Showing 13 changed files with 280 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
******************************************************************************/
package com.redhat.devtools.intellij.telemetry.core.configuration;

import org.jetbrains.annotations.Nullable;

import java.io.FileNotFoundException;
import java.io.InputStream;
import java.nio.file.Path;
Expand All @@ -28,7 +30,7 @@ public ClasspathConfiguration(Path file, ClassLoader classLoader) {
}

@Override
protected InputStream createInputStream(Path path) throws FileNotFoundException {
protected InputStream createInputStream(Path path) {
if (path == null) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@
******************************************************************************/
package com.redhat.devtools.intellij.telemetry.core.configuration;

import org.jetbrains.annotations.Nullable;

import java.util.List;
import java.util.Objects;

public abstract class CompositeConfiguration implements IConfiguration {

@Nullable
@Override
public String get(final String key) {
List<IConfiguration> configurations = getConfigurations();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.redhat.devtools.intellij.telemetry.core.service.Event;
import org.apache.commons.lang.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.time.LocalDateTime;
import java.time.ZonedDateTime;
Expand Down Expand Up @@ -71,6 +72,7 @@ public void initializeComponent() {
PersistentStateComponent.super.initializeComponent();
}

@Nullable
public Count get(Event event) {
if (event == null) {
return null;
Expand All @@ -86,7 +88,7 @@ public void put(Event event) {

EventCounts put(Event event, Count count) {
if (event == null) {
return null;
return this;
}
counts.put(event.getName(), toString(count));
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,10 @@ List<PluginLimits> getAllLimits() {
PluginLimits defaults = getDefaultLimits(limits);
Duration refreshAfter = getRefreshAfter(defaults);
FileTime lastModified = configuration.getLocalLastModified();
if (this.limits == null
|| needsRefresh(refreshAfter, lastModified)) {
this.limits = createLimits(configuration.getRemote(), factory);
if (needsRefresh(refreshAfter, lastModified)) {
this.limits = downloadRemote(configuration, factory);
} else if (limits == null) {
this.limits = readLocal(configuration, factory);
}
return limits;
}
Expand Down Expand Up @@ -144,16 +145,35 @@ private Duration getRefreshAfter(PluginLimits defaults) {
return Duration.ofHours(defaults.getRefresh());
}

private List<PluginLimits> createLimits(String config, PluginLimitsFactory factory) {
private List<PluginLimits> readLocal(LimitsConfigurations configuration, PluginLimitsFactory factory) {
try {
String config = configuration.readLocal();
if (StringUtil.isEmptyOrSpaces(config)) {
// null to trigger reload upon next #getAllLimits
return null;
return downloadRemote(configuration, factory);
}
return factory.create(config);
} catch (Exception e) {
LOGGER.warn("Could not deserialize config", e);
// null to trigger reload upon next #getAllLimits
return downloadRemote(configuration, factory);
}
}

@Nullable
private List<PluginLimits> downloadRemote(LimitsConfigurations configuration, PluginLimitsFactory factory) {
try {
String config = configuration.downloadRemote();
if (StringUtil.isEmptyOrSpaces(config)) {
return createEmbeddedLimits(configuration, factory);
}
return factory.create(config);
} catch (Exception e) {
return createEmbeddedLimits(configuration, factory);
}
}

private List<PluginLimits> createEmbeddedLimits(LimitsConfigurations configuration, PluginLimitsFactory factory) {
try {
return factory.create(configuration.readEmbedded());
} catch (IOException e) {
return null;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@

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 okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.jetbrains.annotations.Nullable;

import java.io.BufferedReader;
import java.io.IOException;
Expand All @@ -33,15 +35,22 @@ class LimitsConfigurations {
static final Path LOCAL = Directories.RED_HAT.resolve("telemetry-config.json");
static final String EMBEDDED = "/telemetry-config.json";
static final String REMOTE = "https://raw.githubusercontent.com/adietish/intellij-redhat-telemetry/issue-82/src/main/resources/telemetry-config.json";
static final String SYSTEM_PROP_REMOTE = "REDHAT_TELEMETRY_REMOTE_CONFIG_URL";

private final OkHttpClient client = new OkHttpClient.Builder()
protected final OkHttpClient client = new OkHttpClient.Builder()
.connectTimeout(5, TimeUnit.SECONDS)
.readTimeout(5, TimeUnit.SECONDS)
.writeTimeout(5, TimeUnit.SECONDS)
.build();

public String getRemote() {
String url = System.getProperty("REDHAT_TELEMETRY_REMOTE_CONFIG_URL", REMOTE);
@Nullable
public String downloadRemote() {
String url = System.getProperty(SYSTEM_PROP_REMOTE, REMOTE);

if (FileUtils.isFileUrl(url)) {
return readFile(FileUtils.getPathForFileUrl(url));
}

Request request = new Request.Builder()
.url(url)
.addHeader("Content-Type", "application/json")
Expand All @@ -50,13 +59,27 @@ public String getRemote() {
if (response.body() != null) {
Files.copy(response.body().byteStream(), LOCAL, StandardCopyOption.REPLACE_EXISTING);
}
return getLocal();
} catch (Throwable e) {
return readLocal();
} catch (Exception e) {
LOGGER.warn("Could not download remote limits configurations from " + url, e);
return null;
}
}

@Nullable
private String readFile(Path path) {
if (path == null) {
return null;
}
try {
return toString(Files.newInputStream(path));
} catch (IOException e) {
LOGGER.warn("Could not read remote limits configurations file from " + path, e);
return null;
}
}

@Nullable
public FileTime getLocalLastModified() {
try {
if (!Files.exists(LOCAL)) {
Expand All @@ -68,15 +91,16 @@ public FileTime getLocalLastModified() {
}
}

public String getLocal() {
@Nullable
public String readLocal() {
try {
return toString(Files.newInputStream(LOCAL));
} catch (IOException e) {
return null;
}
}

public String getEmbedded() throws IOException {
public String readEmbedded() throws IOException {
return toString(LimitsConfigurations.class.getResourceAsStream(EMBEDDED));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.intellij.openapi.diagnostic.Logger;
import com.redhat.devtools.intellij.telemetry.core.util.Lazy;
import org.jetbrains.annotations.Nullable;

import java.io.IOException;
import java.io.InputStream;
Expand Down Expand Up @@ -46,13 +47,15 @@ protected Country() {
// for testing purposes
}

@Nullable
public String get(TimeZone timeZone) {
if (timeZone == null) {
return null;
}
return get(timeZone.getID());
}

@Nullable
public String get(String timezoneId) {
Map<String, String> timezone = timezones.get().get(timezoneId);
if (timezone == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.segment.analytics.messages.MessageBuilder;
import com.segment.analytics.messages.PageMessage;
import com.segment.analytics.messages.TrackMessage;
import org.jetbrains.annotations.Nullable;

import java.util.Collections;
import java.util.Map;
Expand Down Expand Up @@ -252,6 +253,7 @@ private static class AnalyticsFactory implements Function<String, Analytics> {
private static final int FLUSH_INTERVAL = 10000;
private static final int FLUSH_QUEUE_SIZE = 10;

@Nullable
@Override
public Analytics apply(String writeKey) {
if (writeKey == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
*/
package com.redhat.devtools.intellij.telemetry.core.util;

import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
Expand Down Expand Up @@ -56,6 +58,7 @@ public boolean offer(E element) {
return false;
}

@Nullable
public E poll() {
if (!isEmpty()) {
E nextValue = data[readSequence % capacity];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,19 @@
******************************************************************************/
package com.redhat.devtools.intellij.telemetry.core.util;

import org.apache.commons.lang.StringUtils;
import org.jetbrains.annotations.Nullable;

import java.io.IOException;
import java.io.Writer;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Path;

public class FileUtils {

private static final String FILE_URL_PREFIX = "file://";

private FileUtils() {
}

Expand All @@ -42,4 +48,19 @@ public static void write(String content, Path file) throws IOException {
writer.append(content);
}
}

public static boolean isFileUrl(String url) {
return !StringUtils.isEmpty(url)
&& url.startsWith(FILE_URL_PREFIX);
}

@Nullable
public static Path getPathForFileUrl(String url) {
if (!isFileUrl(url)) {
return null;
}
String path = url.substring(FILE_URL_PREFIX.length() -1);
return Path.of(path);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
******************************************************************************/
package com.redhat.devtools.intellij.telemetry.core.util;

import org.jetbrains.annotations.Nullable;

import java.time.Duration;
import java.time.Instant;
import java.time.LocalDate;
Expand Down Expand Up @@ -64,6 +66,7 @@ public static String toString(Duration duration) {
*
* @see #toString(Duration)
*/
@Nullable
public static Duration toDuration(String hoursMinutesSeconds) {
Matcher matcher = HH_MM_SS_DURATION.matcher(hoursMinutesSeconds);
if (!matcher.matches()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import com.intellij.notification.NotificationGroup;
import com.intellij.openapi.application.ApplicationInfo;
import com.intellij.openapi.diagnostic.Logger;
import org.jetbrains.annotations.Nullable;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
Expand All @@ -39,6 +40,7 @@ public class NotificationGroupFactory {

private NotificationGroupFactory() {}

@Nullable
public static NotificationGroup create(String displayId, NotificationDisplayType type, boolean logByDefault) {
try {
// < IC-2021.3
Expand Down
Loading

0 comments on commit c0d2b72

Please sign in to comment.