Commit ac3bd24b authored by Matija Obreza's avatar Matija Obreza
Browse files

Batch update PDCI from accessionDetails

parent 088a805d
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
package org.genesys2.server.persistence.domain; package org.genesys2.server.persistence.domain;
import java.util.List; import java.util.List;
import java.util.Set;
import org.genesys2.server.model.genesys.PDCI; import org.genesys2.server.model.genesys.PDCI;
import org.genesys2.server.model.impl.FaoInstitute; import org.genesys2.server.model.impl.FaoInstitute;
...@@ -28,6 +29,9 @@ public interface PDCIRepository extends JpaRepository<PDCI, Long> { ...@@ -28,6 +29,9 @@ public interface PDCIRepository extends JpaRepository<PDCI, Long> {
@Query("select pdci from PDCI pdci where pdci.accession.id=?1") @Query("select pdci from PDCI pdci where pdci.accession.id=?1")
PDCI findByAccessionId(Long accessionId); PDCI findByAccessionId(Long accessionId);
@Query("select pdci from PDCI pdci where pdci.accession.id in (?1)")
List<PDCI> findByAccessionId(Set<Long> accessionIds);
/** /**
* Gets [ min(pdci.score), max(pdci.score), avg(pdci.score), count(pdci) ] * Gets [ min(pdci.score), max(pdci.score), avg(pdci.score), count(pdci) ]
* for PDCI of {@link FaoInstitute} * for PDCI of {@link FaoInstitute}
......
...@@ -235,6 +235,6 @@ public interface GenesysService { ...@@ -235,6 +235,6 @@ public interface GenesysService {
PDCIStatistics statisticsPDCI(FaoInstitute faoInstitute); PDCIStatistics statisticsPDCI(FaoInstitute faoInstitute);
void updatePDCI(Set<Long> accessionIds); void updatePDCI(Set<AccessionDetails> ads);
} }
...@@ -303,6 +303,8 @@ public class ElasticsearchSearchServiceImpl implements ElasticService, Initializ ...@@ -303,6 +303,8 @@ public class ElasticsearchSearchServiceImpl implements ElasticService, Initializ
} }
Set<AccessionDetails> ads = genesysService.getAccessionDetails(ids); Set<AccessionDetails> ads = genesysService.getAccessionDetails(ids);
// If Accession, update PDCI
genesysService.updatePDCI(ads);
for (AccessionDetails ad : ads) { for (AccessionDetails ad : ads) {
if (ad == null) if (ad == null)
......
...@@ -1574,19 +1574,35 @@ public class GenesysServiceImpl implements GenesysService, DatasetService { ...@@ -1574,19 +1574,35 @@ public class GenesysServiceImpl implements GenesysService, DatasetService {
@Override @Override
@Transactional @Transactional
public void updatePDCI(Set<Long> accessionIds) { public void updatePDCI(Set<AccessionDetails> ads) {
if (pdciCalculator == null) { if (pdciCalculator == null) {
return; return;
} }
Set<Long> accessionIds=new HashSet<Long>();
for (AccessionDetails accessionDetails : ads) {
if (accessionDetails!=null) {
accessionIds.add(accessionDetails.getId());
}
}
List<PDCI> pdcis = repoPdci.findByAccessionId(accessionIds);
List<PDCI> pdcis = new ArrayList<PDCI>(); for (AccessionDetails accessionDetails : ads) {
for (Long accessionId : accessionIds) { if (accessionDetails==null) {
PDCI pdci = repoPdci.findByAccessionId(accessionId); continue;
}
PDCI pdci = null;
for (PDCI existing : pdcis) {
if (existing.getAccession().getId().equals(accessionDetails.getId())) {
pdci = existing;
break;
}
}
if (pdci == null) { if (pdci == null) {
pdci = new PDCI(); pdci = new PDCI();
pdci.setAccession(accessionIdRepository.findOne(accessionId)); pdci.setAccession(accessionIdRepository.findOne(accessionDetails.getId()));
} }
pdcis.add(pdciCalculator.updatePdci(pdci, accessionId)); pdciCalculator.updatePdci(pdci, accessionDetails);
} }
repoPdci.save(pdcis); repoPdci.save(pdcis);
} }
......
...@@ -58,20 +58,24 @@ public class PDCICalculator implements InitializingBean { ...@@ -58,20 +58,24 @@ public class PDCICalculator implements InitializingBean {
public PDCI makePDCI(long accessionId) { public PDCI makePDCI(long accessionId) {
return calculatePDCI(new PDCI(), accessionId); return calculatePDCI(new PDCI(), accessionId);
} }
public PDCI updatePdci(PDCI pdci, long accessionId) { public PDCI updatePdci(PDCI pdci, long accessionId) {
return calculatePDCI(pdci, accessionId); return calculatePDCI(pdci, accessionId);
} }
private PDCI calculatePDCI(PDCI pdci, long accessionId) { private PDCI calculatePDCI(PDCI pdci, long accessionId) {
pdci.reset();
AccessionDetails accessionDetails = genesysService.getAccessionDetails(accessionId); AccessionDetails accessionDetails = genesysService.getAccessionDetails(accessionId);
if (accessionDetails == null) { if (accessionDetails == null) {
LOG.warn("No such accession " + accessionId); LOG.warn("No such accession " + accessionId);
return null; return null;
} }
return updatePdci(pdci, accessionDetails);
}
public PDCI updatePdci(PDCI pdci, AccessionDetails accessionDetails) {
pdci.reset();
independentDescriptors(pdci, accessionDetails); independentDescriptors(pdci, accessionDetails);
switch (accessionDetails.getSampStat() == null ? -1 : accessionDetails.getSampStat() / 100) { switch (accessionDetails.getSampStat() == null ? -1 : accessionDetails.getSampStat() / 100) {
...@@ -437,4 +441,5 @@ public class PDCICalculator implements InitializingBean { ...@@ -437,4 +441,5 @@ public class PDCICalculator implements InitializingBean {
} }
return false; return false;
} }
} }
...@@ -64,8 +64,8 @@ public class StatisticsServiceImpl implements StatisticsService { ...@@ -64,8 +64,8 @@ public class StatisticsServiceImpl implements StatisticsService {
@Override @Override
@Cacheable(unless = "#result == null", value = "statistics", key = "'stats.' + #root.methodName + '-' + #faoInstitute.id") @Cacheable(unless = "#result == null", value = "statistics", key = "'stats.' + #root.methodName + '-' + #faoInstitute.id")
public PDCIStatistics statisticsPDCI(FaoInstitute faoInstitute) { public PDCIStatistics statisticsPDCI(FaoInstitute faoInstitute) {
if (LOG.isInfoEnabled()) { if (LOG.isDebugEnabled()) {
LOG.info("Regenerating PDCI statistics for " + faoInstitute); LOG.debug("Regenerating PDCI statistics for " + faoInstitute);
} }
return genesysService.statisticsPDCI(faoInstitute); return genesysService.statisticsPDCI(faoInstitute);
} }
......
...@@ -8,7 +8,6 @@ import javax.annotation.Resource; ...@@ -8,7 +8,6 @@ import javax.annotation.Resource;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.genesys2.server.model.genesys.Accession;
import org.genesys2.server.service.ElasticService; import org.genesys2.server.service.ElasticService;
import org.genesys2.server.service.GenesysService; import org.genesys2.server.service.GenesysService;
import org.genesys2.server.service.worker.ElasticUpdater.ElasticNode; import org.genesys2.server.service.worker.ElasticUpdater.ElasticNode;
...@@ -111,11 +110,6 @@ class ElasticUpdaterProcessor implements Runnable, InitializingBean, DisposableB ...@@ -111,11 +110,6 @@ class ElasticUpdaterProcessor implements Runnable, InitializingBean, DisposableB
for (String className : buckets.keySet()) { for (String className : buckets.keySet()) {
Set<Long> bucket = buckets.get(className); Set<Long> bucket = buckets.get(className);
// If Accession, update PDCI
if (Accession.class.getName().equals(className)) {
genesysService.updatePDCI(bucket);
}
elasticService.updateAll(className, bucket); elasticService.updateAll(className, bucket);
bucket.clear(); bucket.clear();
} }
...@@ -147,11 +141,6 @@ class ElasticUpdaterProcessor implements Runnable, InitializingBean, DisposableB ...@@ -147,11 +141,6 @@ class ElasticUpdaterProcessor implements Runnable, InitializingBean, DisposableB
} }
bucket.add(toUpdate.getId()); bucket.add(toUpdate.getId());
if (bucket.size() >= BATCH_SIZE) { if (bucket.size() >= BATCH_SIZE) {
// If Accession, update PDCI
if (Accession.class.getName().equals(className)) {
genesysService.updatePDCI(bucket);
}
elasticService.updateAll(className, bucket); elasticService.updateAll(className, bucket);
bucket.clear(); bucket.clear();
} }
......
...@@ -655,7 +655,7 @@ ...@@ -655,7 +655,7 @@
<h3> <h3>
<spring:message code="accession.pdci.jumbo" arguments="${pdci.score}" /> <spring:message code="accession.pdci.jumbo" arguments="${pdci.score}" />
</h3> </h3>
<small> <c:if test="${institutePDCI ne null}"> <small> <c:if test="${institutePDCI ne null and institutePDCI.count > 0}">
<spring:message code="accession.pdci.institute-avg" arguments="${institutePDCI.avg}" /> <spring:message code="accession.pdci.institute-avg" arguments="${institutePDCI.avg}" />
</c:if> <a href="<c:url value="/content/passport-data-completeness-index" />"><spring:message </c:if> <a href="<c:url value="/content/passport-data-completeness-index" />"><spring:message
code="accession.pdci.about-link" code="accession.pdci.about-link"
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment