Commit 9eb1dc7f authored by Matija Obreza's avatar Matija Obreza

PDCI fixes

- Reset PDCI values on no records
parent 47537a5e
......@@ -101,7 +101,7 @@ public class PDCIStatistics implements Serializable {
int index = (int) Math.round(((Number) h[0]).doubleValue() * 2);
long count = ((Number) h[1]).longValue();
histogram[index] += count;
histogram[index] = count;
}
}
......
......@@ -310,6 +310,10 @@ public class FaoInstitute extends BasicModel implements GeoReferencedEntity, Acl
}
public PDCIStatistics getStatisticsPDCI() {
if (this.pdciMin == null || this.pdciMax == null || this.pdciAvg == null) {
return null;
}
PDCIStatistics stats = new PDCIStatistics();
stats.setMin(this.pdciMin);
stats.setMax(this.pdciMax);
......
......@@ -215,6 +215,7 @@ public class AdminController {
@RequestMapping(value = "/pdci", method = RequestMethod.POST, params = "updatePdciStats")
public String updatePDCI() {
for (FaoInstitute institute: instituteService.listActive(new PageRequest(0, Integer.MAX_VALUE))) {
LOG.info("Updating PDCI for {}", institute.getCode());
genesysService.updatePDCI(institute);
}
......
......@@ -40,7 +40,7 @@ public interface PDCIRepository extends JpaRepository<PDCI, Long> {
* @return [ Float min(pdci.score), Float max(pdci.score), Double
* avg(pdci.score), Long count(pdci) ]
*/
@Query("select min(pdci.score), max(pdci.score), avg(pdci.score), count(pdci) from Accession a, PDCI pdci where a.institute = ?1 and a.historic = false and pdci.accession=a")
@Query("select min(pdci.score), max(pdci.score), avg(pdci.score), count(pdci) from Accession a inner join a.accessionId.pdci pdci where a.institute = ?1 and a.historic = false")
Object statistics(FaoInstitute faoInstitute);
/**
......@@ -50,7 +50,7 @@ public interface PDCIRepository extends JpaRepository<PDCI, Long> {
* @param faoInstitute
* @return List of [ scoreHist, count ]
*/
@Query("select pdci.scoreHist, count(pdci) from Accession a, PDCI pdci where a.institute = ?1 and pdci.accession=a group by pdci.scoreHist")
@Query("select pdci.scoreHist, count(pdci) from Accession a inner join a.accessionId.pdci pdci where a.institute = ?1 and a.historic = false group by pdci.scoreHist")
List<Object[]> histogram(FaoInstitute faoInstitute);
}
......@@ -144,7 +144,7 @@ public interface GenesysService {
void updateAccessionCount(FaoInstitute institute);
void updatePDCI(FaoInstitute institute);
FaoInstitute updatePDCI(FaoInstitute institute);
List<SvalbardDeposit> getSvalbardData(AccessionId accession);
......
......@@ -1465,26 +1465,33 @@ public class GenesysServiceImpl implements GenesysService, DatasetService {
return stats;
}
@Transactional
@Transactional(readOnly = false)
@Override
public void updatePDCI(FaoInstitute faoInstitute) {
public FaoInstitute updatePDCI(FaoInstitute faoInstitute) {
// Reload
faoInstitute = instituteRepository.findOne(faoInstitute.getId());
Object[] overall = (Object[]) repoPdci.statistics(faoInstitute);
if (overall == null) {
LOG.warn("No PDCI statistics for {}", faoInstitute);
return;
}
List<Object[]> histogram = repoPdci.histogram(faoInstitute);
if (overall == null || overall[3] == null || ((Number) overall[3]).longValue() == 0) {
LOG.warn("Clearing PDCI statistics for {}", faoInstitute);
faoInstitute.setPdciMin(null);
faoInstitute.setPdciMax(null);
faoInstitute.setPdciAvg(null);
faoInstitute.setPdciHistogram(null);
PDCIStatistics pdciStatistics = new PDCIStatistics();
pdciStatistics.makeHistogram(histogram);
faoInstitute.setPdciMin((Double) overall[0]);
faoInstitute.setPdciMax((Double) overall[1]);
faoInstitute.setPdciAvg((Double) overall[2]);
faoInstitute.setPdciHistogram(Arrays.toString(pdciStatistics.getHistogram()));
} else {
faoInstitute.setPdciMin(((Number) overall[0]).doubleValue());
faoInstitute.setPdciMax(((Number) overall[1]).doubleValue());
faoInstitute.setPdciAvg(((Number) overall[2]).doubleValue());
PDCIStatistics pdciStatistics = new PDCIStatistics();
List<Object[]> histogram = repoPdci.histogram(faoInstitute);
pdciStatistics.makeHistogram(histogram);
faoInstitute.setPdciHistogram(Arrays.toString(pdciStatistics.getHistogram()));
}
instituteRepository.save(faoInstitute);
return instituteRepository.save(faoInstitute);
}
@Transactional
......
......@@ -998,8 +998,8 @@
<spring:message code="accession.pdci.jumbo" arguments="${pdci.score}"/>
</h3>
<small>
<c:if test="${institutePDCI ne null and institutePDCI.accessionCount > 0}">
<spring:message code="accession.pdci.institute-avg" arguments="${institutePDCI.pdciAvg}"/>
<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"/></a>
</small>
......
......@@ -23,8 +23,8 @@
<form method="post" action="<c:url value="/admin/pdci" />">
<input type="submit" class="btn btn-default" name="updatePdciStats" value="Update all institute PDCI stats" />
<button name="action" value="missing-pdci" class="btn btn-default">Calculate missing PDCI</button>
<button name="action" value="full-recalc" class="btn btn-default">Clean and recalculate</button>
<%-- <button name="action" value="missing-pdci" class="btn btn-default">Calculate missing PDCI</button>
<button name="action" value="full-recalc" class="btn btn-default">Clean and recalculate</button> --%>
<!-- CSRF protection -->
<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}" />
</form>
......
......@@ -129,24 +129,21 @@ public class PDCIStatisticsTest {
PDCIStatistics stats = new PDCIStatistics();
/*@formatter:off*/
List<Object[]> hist = Lists.newArrayList(
new Object[] { .2, 1d }, new Object[] { .5, 5d },
new Object[] { .5, 1d }, new Object[] { 1, 5d },
new Object[] { 1, 10 }, new Object[] { 2, 20 },
new Object[] { 3f, 30 }, new Object[] { 4d, 40d },
new Object[] { 5d, 50 }, new Object[] { 6f, 60f },
new Object[] { 7, 70f }, new Object[] { 8d, 80f },
new Object[] { 9f, 40d }, new Object[] { 10, 50 },
// extras
new Object[] { 9.1f, 50d }, new Object[] { 9.9, 50 }
new Object[] { 9f, 40d }, new Object[] { 10, 50 }
);
/*@formatter:on*/
stats.makeHistogram(hist);
assertThat(stats.getHistogram().length, is(21));
assertThat(stats.getHistogram()[9*2], is(90l));
assertThat(stats.getHistogram()[9*2], is(40l));
assertThat(stats.getHistogram()[(int) (2*9.5)], is(0l));
assertThat(stats.getHistogram()[10*2], is(100l));
assertThat(Arrays.toString(stats.getHistogram()), is("[1, 5, 10, 0, 20, 0, 30, 0, 40, 0, 50, 0, 60, 0, 70, 0, 80, 0, 90, 0, 100]"));
assertThat(stats.getHistogram()[10*2], is(50l));
assertThat(Arrays.toString(stats.getHistogram()), is("[0, 1, 10, 0, 20, 0, 30, 0, 40, 0, 50, 0, 60, 0, 70, 0, 80, 0, 40, 0, 50]"));
}
/**
......
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