Commit ac3bd24b authored by Matija Obreza's avatar Matija Obreza

Batch update PDCI from accessionDetails

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