Skip to content

Commit

Permalink
Fixed test
Browse files Browse the repository at this point in the history
  • Loading branch information
Rylern committed Feb 19, 2024
1 parent abb2ebf commit 1136cf2
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 41 deletions.
24 changes: 20 additions & 4 deletions src/main/java/qupath/ext/omero/core/apis/ApisHandler.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package qupath.ext.omero.core.apis;

import com.drew.lang.annotations.Nullable;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.ReadOnlyBooleanProperty;
import javafx.beans.property.ReadOnlyIntegerProperty;
import javafx.beans.property.SimpleBooleanProperty;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import qupath.ext.omero.core.entities.annotations.AnnotationGroup;
Expand All @@ -25,7 +27,11 @@

import java.awt.image.BufferedImage;
import java.net.URI;
import java.util.*;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutionException;
Expand Down Expand Up @@ -57,6 +63,7 @@ public class ApisHandler implements AutoCloseable {
private final WebclientApi webclientApi;
private final WebGatewayApi webGatewayApi;
private final IViewerApi iViewerApi;
private final BooleanProperty areOrphanedImagesLoading = new SimpleBooleanProperty(false);
private final Map<Long, BufferedImage> thumbnails = new ConcurrentHashMap<>();
private final Map<Class<? extends RepositoryEntity>, BufferedImage> omeroIcons = new ConcurrentHashMap<>();
private final boolean canSkipAuthentication;
Expand Down Expand Up @@ -300,14 +307,19 @@ public CompletableFuture<Integer> getNumberOfOrphanedImages() {
* be possible to add elements to this list
*/
public void populateOrphanedImagesIntoList(List<Image> children) {
getOrphanedImagesIds().thenAccept(orphanedImageIds -> jsonApi.populateOrphanedImagesIntoList(children, orphanedImageIds));
setOrphanedImagesLoading(true);

getOrphanedImagesIds()
.thenAcceptAsync(orphanedImageIds -> jsonApi.populateOrphanedImagesIntoList(children, orphanedImageIds))
.thenRun(() -> setOrphanedImagesLoading(false));
}

/**
* See {@link JsonApi#areOrphanedImagesLoading()}.
* @return whether orphaned images are currently being loaded.
* This property may be updated from any thread
*/
public ReadOnlyBooleanProperty areOrphanedImagesLoading() {
return jsonApi.areOrphanedImagesLoading();
return areOrphanedImagesLoading;
}

/**
Expand Down Expand Up @@ -564,4 +576,8 @@ public CompletableFuture<Boolean> writeROIs(long id, Collection<Shape> shapes, b
public CompletableFuture<Optional<ImageSettings>> getImageSettings(long imageId) {
return iViewerApi.getImageSettings(imageId);
}

private synchronized void setOrphanedImagesLoading(boolean orphanedImagesLoading) {
areOrphanedImagesLoading.set(orphanedImagesLoading);
}
}
53 changes: 16 additions & 37 deletions src/main/java/qupath/ext/omero/core/apis/JsonApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,8 @@
import com.google.gson.JsonObject;
import com.google.gson.JsonSyntaxException;
import com.google.gson.reflect.TypeToken;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.ReadOnlyBooleanProperty;
import javafx.beans.property.ReadOnlyIntegerProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleIntegerProperty;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -71,7 +68,6 @@ class JsonApi {
private static final String WELLS_URL = "%s/api/v0/m/plateacquisitions/%d/wellsampleindex/%d/wells/";
private static final String ROIS_URL = "%s/api/v0/m/rois/?image=%s";
private final IntegerProperty numberOfEntitiesLoading = new SimpleIntegerProperty(0);
private final BooleanProperty areOrphanedImagesLoading = new SimpleBooleanProperty(false);
private final IntegerProperty numberOfOrphanedImagesLoaded = new SimpleIntegerProperty(0);
private final URI webURI;
private final Map<String, String> urls;
Expand Down Expand Up @@ -398,7 +394,7 @@ public int getNumberOfOrphanedImages(List<Long> orphanedImagesIds) {
* <p>
* Populate all orphaned images of this server to the list specified in parameter.
* This function populates and doesn't return a list because the number of images can
* be large, so this operation can take tens of seconds.
* be large, so this operation can take tens of seconds and should be run in a background thread.
* </p>
* <p>The list can be updated from any thread.</p>
*
Expand All @@ -407,38 +403,25 @@ public int getNumberOfOrphanedImages(List<Long> orphanedImagesIds) {
* @param orphanedImagesIds the Ids of all orphaned images of the server
*/
public void populateOrphanedImagesIntoList(List<Image> children, List<Long> orphanedImagesIds) {
setOrphanedImagesLoading(true);
resetNumberOfOrphanedImagesLoaded();

List<URI> orphanedImagesURIs = getOrphanedImagesURIs(orphanedImagesIds);
CompletableFuture.runAsync(() -> {
// The number of parallel requests is limited to 16
// to avoid too many concurrent streams
List<List<URI>> batches = Lists.partition(orphanedImagesURIs, 16);
for (List<URI> batch: batches) {
children.addAll(batch.stream()
.map(this::requestImageInfo)
.map(CompletableFuture::join)
.flatMap(Optional::stream)
.map(jsonObject -> ServerEntity.createFromJsonElement(jsonObject, webURI))
.flatMap(Optional::stream)
.map(serverEntity -> (Image) serverEntity)
.toList()
);

addToNumberOfOrphanedImagesLoaded(batch.size());
}

setOrphanedImagesLoading(false);
});
}
// The number of parallel requests is limited to 16
// to avoid too many concurrent streams
List<List<URI>> batches = Lists.partition(orphanedImagesURIs, 16);
for (List<URI> batch: batches) {
children.addAll(batch.stream()
.map(this::requestImageInfo)
.map(CompletableFuture::join)
.flatMap(Optional::stream)
.map(jsonObject -> ServerEntity.createFromJsonElement(jsonObject, webURI))
.flatMap(Optional::stream)
.map(serverEntity -> (Image) serverEntity)
.toList()
);

/**
* @return whether orphaned images are currently being loaded.
* This property may be updated from any thread
*/
public ReadOnlyBooleanProperty areOrphanedImagesLoading() {
return areOrphanedImagesLoading;
addToNumberOfOrphanedImagesLoaded(batch.size());
}
}

/**
Expand Down Expand Up @@ -637,10 +620,6 @@ private static CompletableFuture<Optional<String>> getToken(Map<String, String>
}
}

private synchronized void setOrphanedImagesLoading(boolean orphanedImagesLoading) {
areOrphanedImagesLoading.set(orphanedImagesLoading);
}

private List<URI> getOrphanedImagesURIs(List<Long> orphanedImagesIds) {
return orphanedImagesIds.stream()
.map(id -> WebUtilities.createURI(urls.get(IMAGES_URL_KEY) + id))
Expand Down

0 comments on commit 1136cf2

Please sign in to comment.