Skip to content

Commit

Permalink
PN-252: Run local matches search only for patients updated/created si…
Browse files Browse the repository at this point in the history
…nce last search
  • Loading branch information
veronikaslc committed Jan 22, 2018
1 parent f569bfa commit 48a2f3e
Show file tree
Hide file tree
Showing 6 changed files with 169 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,13 @@ public interface MatchFinder
*/
List<PatientMatch> findMatches(Patient patient);

/**
* Record start time for running matches search.
*/
void recordStartMatchesSearch();

/**
* Record completed time for running matches search.
*/
void recordEndMatchesSearch();
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,14 @@ public interface MatchFinderManager
* @return list of matches
*/
List<PatientMatch> findMatches(Patient patient);

/**
* For every match finder record start time for running matches search.
*/
void recordStartMatchesSearch();

/**
* For every match finder record completed time for running matches search.
*/
void recordEndMatchesSearch();
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,20 @@ public List<PatientMatch> findMatches(Patient patient)

return matches;
}

@Override
public void recordStartMatchesSearch()
{
for (MatchFinder service : this.matchFinderProvider.get()) {
service.recordStartMatchesSearch();
}
}

@Override
public void recordEndMatchesSearch()
{
for (MatchFinder service : this.matchFinderProvider.get()) {
service.recordEndMatchesSearch();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package org.phenotips.matchingnotification.finder.internal;

import org.phenotips.data.Patient;
import org.phenotips.data.PatientData;
import org.phenotips.data.similarity.PatientSimilarityView;
import org.phenotips.matchingnotification.finder.MatchFinder;
import org.phenotips.matchingnotification.match.PatientMatch;
Expand All @@ -26,25 +27,48 @@
import org.phenotips.similarity.SimilarPatientsFinder;

import org.xwiki.component.annotation.Component;
import org.xwiki.component.phase.Initializable;
import org.xwiki.component.phase.InitializationException;
import org.xwiki.model.EntityType;
import org.xwiki.model.reference.EntityReference;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Provider;
import javax.inject.Singleton;

import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.format.DateTimeFormatter;
import org.joda.time.format.ISODateTimeFormat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.xpn.xwiki.XWikiContext;
import com.xpn.xwiki.XWikiException;
import com.xpn.xwiki.doc.XWikiDocument;
import com.xpn.xwiki.objects.BaseObject;

/**
* @version $Id$
*/
@Component
@Named("local")
@Singleton
public class LocalMatchFinder implements MatchFinder
public class LocalMatchFinder implements MatchFinder, Initializable
{
private static final EntityReference PHENOMECENTRAL_SPACE = new EntityReference("PhenomeCentral", EntityType.SPACE);

private static final EntityReference MATCHING_RUN_INFO_CLASS = new EntityReference(
"MatchingRunInfoClass", EntityType.DOCUMENT, PHENOMECENTRAL_SPACE);

private static final EntityReference MATCHING_RUN_INFO_DOCUMENT =
new EntityReference("MatchingRunInfo", EntityType.DOCUMENT, PHENOMECENTRAL_SPACE);

private Logger logger = LoggerFactory.getLogger(LocalMatchFinder.class);

@Inject
Expand All @@ -53,6 +77,38 @@ public class LocalMatchFinder implements MatchFinder
@Inject
private MatchStorageManager matchStorageManager;

@Inject
private Provider<XWikiContext> provider;

private DateTimeFormatter dateFormatter = ISODateTimeFormat.dateTime().withZone(DateTimeZone.UTC);

private Date previousStartedTime;

private XWikiDocument prefsDoc;

@Override
public void initialize() throws InitializationException
{
XWikiContext context = this.provider.get();
this.previousStartedTime = null;
this.prefsDoc = null;

try {
this.prefsDoc = context.getWiki().getDocument(MATCHING_RUN_INFO_DOCUMENT, context);

if (this.prefsDoc != null && !this.prefsDoc.isNew()) {
BaseObject object = this.prefsDoc.getXObject(MATCHING_RUN_INFO_CLASS, "serverName", "localhost", false);
if (object == null) {
object = this.prefsDoc.newXObject(MATCHING_RUN_INFO_CLASS, context);
object.setStringValue("serverName", "localhost");
context.getWiki().saveDocument(this.prefsDoc, context);
}
}
} catch (XWikiException e) {
this.logger.error("Failed to modify matching run info document: {}", e.getMessage(), e);
}
}

@Override
public int getPriority()
{
Expand All @@ -62,10 +118,18 @@ public int getPriority()
@Override
public List<PatientMatch> findMatches(Patient patient)
{
List<PatientMatch> matches = new LinkedList<>();

PatientData<String> patientData = patient.<String>getData("metadata");
String date = patientData.get("date");
DateTime lastModificationDate = this.dateFormatter.parseDateTime(date);

if (this.previousStartedTime != null && lastModificationDate.isBefore(new DateTime(this.previousStartedTime))) {
return matches;
}

this.logger.debug("Finding local matches for patient {}.", patient.getId());

List<PatientMatch> matches = new LinkedList<>();
List<PatientSimilarityView> similarPatients = this.finder.findSimilarPatients(patient);
for (PatientSimilarityView similarityView : similarPatients) {
PatientMatch match = new DefaultPatientMatch(similarityView, null, null);
Expand All @@ -76,4 +140,44 @@ public List<PatientMatch> findMatches(Patient patient)

return matches;
}

@Override
public void recordStartMatchesSearch()
{
if (this.prefsDoc == null) {
return;
}

BaseObject object = this.prefsDoc.getXObject(MATCHING_RUN_INFO_CLASS, "serverName", "localhost", false);

// cash last started search date to compare with last modification date for patient
this.previousStartedTime = object.getDateValue("startedTime");
// set new started time
object.setDateValue("startedTime", new Date());

try {
XWikiContext context = this.provider.get();
context.getWiki().saveDocument(this.prefsDoc, context);
} catch (XWikiException e) {
this.logger.error("Failed to save matching run start time for localhost {}.");
}
}

@Override
public void recordEndMatchesSearch()
{
if (this.prefsDoc == null) {
return;
}

BaseObject object = this.prefsDoc.getXObject(MATCHING_RUN_INFO_CLASS, "serverName", "localhost", false);
object.setDateValue("completedTime", new Date());

try {
XWikiContext context = this.provider.get();
context.getWiki().saveDocument(this.prefsDoc, context);
} catch (XWikiException e) {
this.logger.error("Failed to save matching run start time for localhost {}.");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,15 @@ public class DefaultMatchingNotificationManager implements MatchingNotificationM
@Override
public void findAndSaveMatches()
{
this.matchFinderManager.recordStartMatchesSearch();

List<Patient> patients = this.getPatientsList();
for (Patient patient : patients) {
this.logger.debug("Finding matches for patient {}.", patient.getId());

this.matchFinderManager.findMatches(patient);
}

this.matchFinderManager.recordEndMatchesSearch();
}

/*
Expand Down
24 changes: 24 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,30 @@
<to>void</to>
<justification>Removed unused return.</justification>
</difference>
<difference>
<className>org/phenotips/matchingnotification/finder/MatchFinder</className>
<differenceType>7012</differenceType>
<method>void recordStartMatchesSearch()</method>
<justification>Record the beginning time of matches search for each server</justification>
</difference>
<difference>
<className>org/phenotips/matchingnotification/finder/MatchFinder</className>
<differenceType>7012</differenceType>
<method>void recordEndMatchesSearch()</method>
<justification>Record the completion time of matches search for each server</justification>
</difference>
<difference>
<className>org/phenotips/matchingnotification/finder/MatchFinderManager</className>
<differenceType>7012</differenceType>
<method>void recordStartMatchesSearch()</method>
<justification>Record the beginning time of matches search for each match finder</justification>
</difference>
<difference>
<className>org/phenotips/matchingnotification/finder/MatchFinderManager</className>
<differenceType>7012</differenceType>
<method>void recordEndMatchesSearch()</method>
<justification>Record the completion time of matches search for each match finder</justification>
</difference>
</ignored>
<excludes>
<exclude>**/internal/**</exclude>
Expand Down

0 comments on commit 48a2f3e

Please sign in to comment.