diff --git a/processing/src/main/java/edu/harvard/hms/dbmi/avillach/hpds/processing/timeseries/ObservationDistanceProcessor.java b/processing/src/main/java/edu/harvard/hms/dbmi/avillach/hpds/processing/timeseries/ObservationDistanceProcessor.java index 06d595cb..5ccecaa1 100644 --- a/processing/src/main/java/edu/harvard/hms/dbmi/avillach/hpds/processing/timeseries/ObservationDistanceProcessor.java +++ b/processing/src/main/java/edu/harvard/hms/dbmi/avillach/hpds/processing/timeseries/ObservationDistanceProcessor.java @@ -7,11 +7,13 @@ import edu.harvard.hms.dbmi.avillach.hpds.processing.AbstractProcessor; import edu.harvard.hms.dbmi.avillach.hpds.processing.AsyncResult; import edu.harvard.hms.dbmi.avillach.hpds.processing.HpdsProcessor; +import org.apache.commons.io.FileUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; +import java.io.File; import java.io.IOException; import java.io.Serializable; import java.nio.file.Files; @@ -69,22 +71,32 @@ public long distance() { @Override public void runQuery(Query query, AsyncResult asyncResult) { - Set recordedJourneys = new HashSet<>(); - for (String concept : query.getAnyRecordOf()) { - LOG.info("Creating user journeys for concept {}", concept); - Optional> maybeCube = abstractProcessor.nullableGetCube(concept); - if (maybeCube.isEmpty()) { - continue; + + } + + public void writeJourney(Query query) { + try { + String tmpPath = File.createTempFile(System.nanoTime() + "journey", ".tsv").getAbsolutePath(); + Set recordedJourneys = new HashSet<>(); + for (String concept : query.getAnyRecordOf()) { + LOG.info("Creating user journeys for concept {}", concept); + Optional> maybeCube = abstractProcessor.nullableGetCube(concept); + if (maybeCube.isEmpty()) { + continue; + } + maybeCube.get().keyBasedIndex().stream() + .filter(Predicate.not(recordedJourneys::contains)) + .map(id -> new PatientJourney(concept, id)) + .map(this::updateJourney) + .sorted((a, b) -> -Long.compare(b.distance(), a.distance())) + .peek(j -> recordedJourneys.add(j.patient)) + .limit(100) + .forEach(journey -> write(tmpPath, journey)); + LOG.info("Done creating user journeys for concept {} to {}", concept, tmpPath); } - maybeCube.get().keyBasedIndex().stream() - .filter(Predicate.not(recordedJourneys::contains)) - .map(id -> new PatientJourney(concept, id)) - .map(this::updateJourney) - .sorted((a, b) -> -Long.compare(b.distance(), a.distance())) - .peek(j -> recordedJourneys.add(j.patient)) - .limit(100) - .forEach(journey -> write(asyncResult.getTempFilePath(), journey)); - LOG.info("Done creating user journeys for concept {} to {}", concept, asyncResult.getTempFilePath()); + } catch (IOException e) { + LOG.error("Can't create temp file", e); + return; } } diff --git a/service/src/main/java/edu/harvard/hms/dbmi/avillach/hpds/service/PicSureService.java b/service/src/main/java/edu/harvard/hms/dbmi/avillach/hpds/service/PicSureService.java index 686df5d2..ec53f206 100644 --- a/service/src/main/java/edu/harvard/hms/dbmi/avillach/hpds/service/PicSureService.java +++ b/service/src/main/java/edu/harvard/hms/dbmi/avillach/hpds/service/PicSureService.java @@ -12,6 +12,7 @@ import javax.ws.rs.core.Response.Status; import edu.harvard.hms.dbmi.avillach.hpds.data.query.ResultType; +import edu.harvard.hms.dbmi.avillach.hpds.processing.timeseries.ObservationDistanceProcessor; import edu.harvard.hms.dbmi.avillach.hpds.service.filesharing.FileSharingService; import edu.harvard.hms.dbmi.avillach.hpds.service.util.Paginator; import edu.harvard.hms.dbmi.avillach.hpds.service.util.QueryDecorator; @@ -46,7 +47,8 @@ public class PicSureService implements IResourceRS { @Autowired public PicSureService(QueryService queryService, TimelineProcessor timelineProcessor, CountProcessor countProcessor, VariantListProcessor variantListProcessor, AbstractProcessor abstractProcessor, - Paginator paginator, FileSharingService fileSystemService, QueryDecorator queryDecorator + Paginator paginator, FileSharingService fileSystemService, QueryDecorator queryDecorator, + ObservationDistanceProcessor observationDistanceProcessor ) { this.queryService = queryService; this.timelineProcessor = timelineProcessor; @@ -56,6 +58,7 @@ public PicSureService(QueryService queryService, TimelineProcessor timelineProce this.paginator = paginator; this.fileSystemService = fileSystemService; this.queryDecorator = queryDecorator; + this.observationDistanceProcessor = observationDistanceProcessor; Crypto.loadDefaultKey(); } @@ -78,6 +81,7 @@ public PicSureService(QueryService queryService, TimelineProcessor timelineProce private final FileSharingService fileSystemService; private final QueryDecorator queryDecorator; + private final ObservationDistanceProcessor observationDistanceProcessor; private static final String QUERY_METADATA_FIELD = "queryMetadata"; private static final int RESPONSE_CACHE_SIZE = 50; @@ -275,6 +279,11 @@ private Optional roundTripUUID(String uuid) { public Response writeQueryResult( @RequestBody() Query query, @PathParam("dataType") String datatype ) { + log.info("Writing {}", datatype); + if ("journey".equals(datatype)) { + observationDistanceProcessor.writeJourney(query); + return Response.status(400, "good luck!").build(); + } if (roundTripUUID(query.getPicSureId()).map(id -> !id.equalsIgnoreCase(query.getPicSureId())).orElse(false)) { return Response .status(400, "The query pic-sure ID is not a UUID")