Commit 349bcbb0 authored by Maxym Borodenko's avatar Maxym Borodenko Committed by Matija Obreza
Browse files

Statistics API endpoint

- closes #299
parent a4d50529
......@@ -48,6 +48,12 @@ public interface AccessionRepository extends JpaRepository<Accession, Long>, Acc
@Query("select count(a) from Accession a where a.institute = ?1 and a.historic = false")
long countByInstitute(FaoInstitute institute);
/**
* Return the number of active ({@link Accession#historic} == false) accession records held at the institute with DOI
*/
@Query("select count(a) from Accession a where a.institute = ?1 and a.historic = false and a.doi is not null")
long countByInstituteWithDoi(FaoInstitute institute);
/**
* Return the number of active ({@link Accession#historic} == false) accession records originating from specified country
*/
......@@ -147,6 +153,9 @@ public interface AccessionRepository extends JpaRepository<Accession, Long>, Acc
@Query("select year(a.lastModifiedDate), month(a.lastModifiedDate), day(a.lastModifiedDate), count(a) from Accession a where a.institute = ?1 group by year(a.lastModifiedDate), month(a.lastModifiedDate), day(a.lastModifiedDate) order by year(a.lastModifiedDate) desc, month(a.lastModifiedDate) desc, day(a.lastModifiedDate) desc")
public List<Object[]> lastUpdatedStatistics(FaoInstitute faoInstitute);
@Query("select year(a.lastModifiedDate), count(a) from Accession a where a.institute = ?1 and a.historic = ?2 group by year(a.lastModifiedDate) order by year(a.lastModifiedDate) desc")
public List<Object[]> lastUpdatedStatistics(FaoInstitute faoInstitute, boolean historic);
public Accession findByDoi(String doi);
@Query("update Accession accession set accession.doi = null where accession.doi is not null")
......
......@@ -65,6 +65,12 @@ public interface GenesysService {
*/
long countByInstitute(FaoInstitute institute);
/**
* Return the number of active ({@link Accession#historic} == false)
* accession records with DOI
*/
long countByInstituteWithDoi(FaoInstitute institute);
/**
* Return the number of active ({@link Accession#historic} == false)
* accession records
......@@ -290,6 +296,8 @@ public interface GenesysService {
List<Object[]> getLastUpdatedStatistics(FaoInstitute faoInstitute);
List<Object[]> getLastUpdatedStatistics(FaoInstitute faoInstitute, boolean historic);
Page<AccessionDetails> listAccessionsDetails(Collection<Long> accessionIds, Pageable page);
}
......@@ -258,6 +258,11 @@ public class GenesysServiceImpl implements GenesysService, DatasetService {
return accessionRepository.countByInstitute(institute);
}
@Override
public long countByInstituteWithDoi(final FaoInstitute institute) {
return accessionRepository.countByInstituteWithDoi(institute);
}
@Override
public long countByOrigin(Country country) {
return accessionRepository.countByOrigin(country);
......@@ -1904,4 +1909,10 @@ public class GenesysServiceImpl implements GenesysService, DatasetService {
return accessionRepository.lastUpdatedStatistics(faoInstitute);
}
@Override
@Cacheable(value = "statistics", unless = "#result == null", key = "'stats.' + #root.methodName + '-' + #faoInstitute.code + '-' + #historic")
public List<Object[]> getLastUpdatedStatistics(FaoInstitute faoInstitute, boolean historic) {
return accessionRepository.lastUpdatedStatistics(faoInstitute, historic);
}
}
/**
* Copyright 2014 Global Crop Diversity Trust
/*
* Copyright 2017 Global Crop Diversity Trust
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
......@@ -12,7 +12,7 @@
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
*/
package org.genesys2.server.servlet.controller;
......@@ -33,6 +33,9 @@ import java.util.stream.Stream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.genesys.filerepository.InvalidRepositoryFileDataException;
import org.genesys.filerepository.InvalidRepositoryPathException;
import org.genesys.filerepository.NoSuchRepositoryFileException;
......@@ -40,6 +43,7 @@ import org.genesys.filerepository.model.ImageGallery;
import org.genesys.filerepository.model.RepositoryFile;
import org.genesys.filerepository.service.ImageGalleryService;
import org.genesys.filerepository.service.RepositoryService;
import org.genesys2.server.model.genesys.PDCIStatistics;
import org.genesys2.server.model.genesys.Taxonomy2;
import org.genesys2.server.model.impl.FaoInstitute;
import org.genesys2.server.service.CRMException;
......@@ -196,6 +200,37 @@ public class WiewsController extends BaseController {
return "/wiews/details";
}
@GetMapping(value = "/{instCode}", produces = {MediaType.APPLICATION_JSON_VALUE})
public @ResponseBody ObjectNode statistics(@PathVariable("instCode") String instCode) throws Exception {
LOG.info("Statistics information for {}", instCode);
final FaoInstitute institute = instituteService.getInstitute(instCode);
if (institute == null) {
throw new Exception("Institute with instCode=" + instCode + " doesn't exist.");
}
final PDCIStatistics pdciStatistics = statisticsService.statisticsPDCI(institute);
final List<Object[]> updatedStats = genesysService.getLastUpdatedStatistics(institute, false);
final Long activeAccessions = institute.getAccessionCount();
final Long activeAccessionsWithDoi = genesysService.countByInstituteWithDoi(institute);
final ObjectMapper mapper = new ObjectMapper();
final ObjectNode lastUpdatedNode = mapper.createObjectNode();
for (final Object[] object : updatedStats) {
final String year = object[0] != null ? object[0].toString() : "never";
final Long updates = (Long) object[1];
lastUpdatedNode.put(year, updates);
}
final ObjectNode objectNode = mapper.createObjectNode();
objectNode.put("instCode", instCode);
objectNode.put("active", true);
objectNode.put("count", activeAccessions);
objectNode.set("lastUpdated", lastUpdatedNode);
objectNode.put("averagePdci", pdciStatistics.getAvg());
objectNode.put("withDoi", activeAccessionsWithDoi);
return objectNode;
}
@RequestMapping(value = "/{wiewsCode}/stat-cropName", produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public List<Object[]> getCropNameStats(@PathVariable(value = "wiewsCode") String wiewsCode) {
......
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