Commit d05dc06f authored by Matija Obreza's avatar Matija Obreza

Crop API: Removed accessionCount for all crops -- it takes too long

parent 7d1d0b3e
/**
* Copyright 2014 Global Crop Diversity Trust
/*
* Copyright 2018 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.api.v1;
......@@ -54,7 +54,7 @@ import net.sf.oval.ConstraintViolation;
import net.sf.oval.Validator;
@RestController("cropApi1")
@RequestMapping(value = { CropsController.CONTROLLER_URL, "/json/v1/crops" })
@RequestMapping(value = { CropsController.CONTROLLER_URL })
@Api(tags = { "crop" })
public class CropsController extends ApiBaseController {
......
......@@ -91,7 +91,6 @@ public class AccessionRepositoryCustomImpl implements AccessionRepositoryCustom,
List<Predicate> restrictions = new ArrayList<Predicate>();
Set<String> uniqueDois = forUpdate.stream().map(aid -> aid.getDoi()).filter(doi -> doi != null).distinct().collect(Collectors.toSet());
Set<String> acceNumbs = forUpdate.stream().map(aid -> aid.getAccessionNumber()).filter(acceNumb -> acceNumb != null).distinct().collect(Collectors.toSet());
Path<Object> theDoi = root.get("doi");
Path<Object> theInstCode = root.get("instituteCode");
......@@ -100,10 +99,10 @@ public class AccessionRepositoryCustomImpl implements AccessionRepositoryCustom,
if (uniqueDois.size() > 0) {
restrictions.add(theDoi.in(uniqueDois));
LOG.trace("*** {} dois={}", uniqueDois);
LOG.trace("*** {} dois={}", uniqueDois.size(), uniqueDois);
}
// A lot of .. (acceNumb=? and genus=?)
// A lot of .. (instCode=? and acceNumb=? and genus=?)
for (AccessionIdentifier3 ah : forUpdate) {
restrictions.add(criteriaBuilder.and(criteriaBuilder.equal(theInstCode, ah.getHoldingInstitute()), criteriaBuilder.equal(theAcceNumb, ah
.getAccessionNumber()), criteriaBuilder.equal(theGenus, ah.getGenus())));
......@@ -114,7 +113,7 @@ public class AccessionRepositoryCustomImpl implements AccessionRepositoryCustom,
List<Accession> res = jpaQueryFactory.selectFrom(QAccession.accession).where(QAccession.accession.id.in(em.createQuery(cq).getResultList())).fetch();
if (LOG.isDebugEnabled())
LOG.trace("*** Loaded accessions {} of {}", res.size(), acceNumbs.size());
LOG.trace("*** Loaded accessions {} of {}", res.size(), forUpdate.size());
return res;
}
......
......@@ -134,12 +134,12 @@ public interface CropService {
@JsonView({ JsonViews.Public.class })
public Article blurb;
public long accessionCount;
public Long accessionCount;
public List<RepositoryImage> covers;
@JsonView({ JsonViews.Public.class })
public Map<String, ElasticsearchService.TermResult> overview;
public static CropDetails from(Crop crop, Article blurb, long accessionCount, List<RepositoryImage> covers, Map<String, ElasticsearchService.TermResult> overview) {
public static CropDetails from(Crop crop, Article blurb, Long accessionCount, List<RepositoryImage> covers, Map<String, ElasticsearchService.TermResult> overview) {
CropDetails cropDetails = new CropDetails();
cropDetails.crop = crop;
cropDetails.blurb = blurb;
......
......@@ -150,7 +150,7 @@ public class AccessionServiceImpl implements AccessionService {
}
@Override
@Cacheable(value = "accessionCount", keyGenerator = "shortFilterKeyGenerator")
// @Cacheable(value = "accessionCount", keyGenerator = "shortFilterKeyGenerator")
public long countAccessions(AccessionFilter filter) {
long total = elasticsearchService.count(Accession.class, filter);
......
......@@ -30,6 +30,7 @@ import java.util.stream.Collectors;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.Predicate;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.StopWatch;
import org.genesys.filerepository.InvalidRepositoryPathException;
import org.genesys.filerepository.model.ImageGallery;
import org.genesys.filerepository.model.RepositoryImage;
......@@ -121,19 +122,24 @@ public class CropServiceImpl implements CropService {
}
@Override
@Transactional
public CropDetails getDetails(String shortName, Locale locale) throws InvalidRepositoryPathException {
LOG.debug("Getting crop details for {}", shortName);
StopWatch stopWatch = StopWatch.createStarted();
Crop crop = getCrop(shortName);
LOG.trace("got crop after {}ms", stopWatch.getTime());
Article article = contentService.getArticle(crop, "blurp", locale);
LOG.trace("got article after {}ms", stopWatch.getTime());
AccessionFilter byCrop = new AccessionFilter();
byCrop.cropName = crop.getShortName();
long accessionCount = accessionService.countAccessions(byCrop);
LOG.trace("got count after {}ms", stopWatch.getTime());
ImageGallery imageGallery = imageGalleryService.loadImageGallery(Paths.get("/crop", crop.getShortName(), "covers"));
List<RepositoryImage> covers = imageGallery == null ? Collections.emptyList() : imageGallery.getImages();
LOG.trace("got covers after {}ms", stopWatch.getTime());
Map<String, ElasticsearchService.TermResult> overview = getOverviewData(byCrop);
LOG.trace("got overview after {}ms", stopWatch.getTime());
return CropDetails.from(crop, article, accessionCount, covers, overview);
}
......@@ -181,26 +187,25 @@ public class CropServiceImpl implements CropService {
@Override
@Cacheable(value = CACHE_CROPS, key = "'findAll-' + #locale.language")
@Cacheable(value = CACHE_CROPS, key = "'listDetails-' + #locale.language")
public List<CropDetails> listDetails(Locale locale) {
StopWatch stopWatch = StopWatch.createStarted();
List<Crop> crops = cropRepository.findAll();
LOG.trace("all crops after {}ms", stopWatch.getTime());
// Fetch details
return crops.stream().map(c -> {
if (c.getOtherNames() != null)
c.getOtherNames().size();
AccessionFilter byCrop = new AccessionFilter();
byCrop.cropName = c.getShortName();
long accessionCount = accessionService.countAccessions(byCrop);
List<RepositoryImage> covers = null;
try {
ImageGallery imageGallery = imageGalleryService.loadImageGallery(Paths.get("/crop", c.getShortName(), "covers"));
covers = imageGallery == null ? Collections.emptyList() : imageGallery.getImages();
LOG.trace("got gallery for {} after {}ms", c.getShortName(), stopWatch.getTime());
} catch (InvalidRepositoryPathException e) {
}
return CropDetails.from(c, null, accessionCount, covers, null);
return CropDetails.from(c, null, null, covers, null);
}).collect(Collectors.toList());
}
......
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