Skip to content

Commit

Permalink
refactor: optimize version fetching with fastFetchLatestReleaseVersio…
Browse files Browse the repository at this point in the history
…n method
  • Loading branch information
Jean-François Lamy committed Jan 16, 2025
1 parent 802bd18 commit 6482613
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
38 changes: 35 additions & 3 deletions src/main/java/app/owlcms/fly/flydata/VersionInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public VersionInfo(String currentVersionString) {

public void updateReferenceVersionString(boolean preRelease) {
String apiUrl = "https://api.github.com/repos/owlcms/owlcms4/releases";
this.referenceVersionString = fetchLatestReleaseVersion(apiUrl);
this.referenceVersionString = fastFetchLatestReleaseVersion(apiUrl);

if (!"latest".equals(currentVersionString)) {
ComparableVersion currentVersion = new ComparableVersion(this.currentVersionString);
Expand Down Expand Up @@ -62,7 +62,7 @@ public String getCurrentVersionString() {
return currentVersionString;
}

public static String fetchLatestReleaseVersion(String apiUrl) {
public static String fullFetchLatestReleaseVersion(String apiUrl) {
long now = System.currentTimeMillis();
try {
URL url = new URL(apiUrl);
Expand Down Expand Up @@ -92,12 +92,44 @@ public static String fetchLatestReleaseVersion(String apiUrl) {
}

Collections.sort(versions, Comparator.reverseOrder());
logger.info("fetchLatestReleaseVersion took {} ms", System.currentTimeMillis() - now);
logger.info("fullFetchLatestReleaseVersion took {} ms", System.currentTimeMillis() - now);
return versions.get(0).getValue();

} catch (IOException e) {
e.printStackTrace();
return "unknown";
}
}

public static String fastFetchLatestReleaseVersion(String apiUrl) {
long now = System.currentTimeMillis();
try {
URL url = new URL(apiUrl + "/latest");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept", "application/vnd.github.v3+json");

if (conn.getResponseCode() != 200) {
throw new RuntimeException("Failed : HTTP error code : " + conn.getResponseCode());
}

Scanner scanner = new Scanner(url.openStream());
String inline = "";
while (scanner.hasNext()) {
inline += scanner.nextLine();
}
scanner.close();

JsonParser parser = new JsonParser();
JsonObject release = parser.parse(inline).getAsJsonObject();
String latestVersion = release.get("tag_name").getAsString();

logger.info("fastFetchLatestReleaseVersion took {} ms", System.currentTimeMillis() - now);
return latestVersion;

} catch (IOException e) {
e.printStackTrace();
return "unknown";
}
}
}
2 changes: 1 addition & 1 deletion src/main/java/app/owlcms/fly/ui/AppsView.java
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,6 @@ public String generateRandomString(int length) {
}

private String getLatestReleaseVersion() {
return VersionInfo.fetchLatestReleaseVersion("https://api.github.com/repos/owlcms/owlcms4/releases");
return VersionInfo.fastFetchLatestReleaseVersion("https://api.github.com/repos/owlcms/owlcms4/releases");
}
}

0 comments on commit 6482613

Please sign in to comment.