Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/poc test cases #1642

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package ca.bc.gov.oracleapi.entity.projection;

/** This projection consists of a mix of joined column from various tables. */
public interface ParentTreePropsProj {

Long getParentTreeId();

String getParentTreeNumber();

Character getTested();
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,7 @@ List<ParentTreeGeneticQuality> findAllByListOfSpuAndId(
String geneticTypeCode,
List<Long> spuList,
List<Long> parentTreeIdList);

@Query("from ParentTreeGeneticQuality where parentTreeId in ?1 and seedPlanningUnitId in ?2 and toBeUsedInCalculations = true")
List<ParentTreeGeneticQuality> findAllGenQualityProps(List<Long> parentIds, List<Integer> spuIds);
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ public interface ParentTreeOrchardRepository
extends JpaRepository<ParentTreeOrchard, ParentTreeOrchardId> {

List<ParentTreeOrchard> findByIdOrchardId(String orchardId);

List<ParentTreeOrchard> findAllById_parentTreeIdIn(List<Long> parentTreeIds);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import ca.bc.gov.oracleapi.entity.ParentTreeEntity;
import ca.bc.gov.oracleapi.entity.projection.ParentTreeProj;
import ca.bc.gov.oracleapi.entity.projection.ParentTreePropsProj;

import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
Expand All @@ -13,26 +15,60 @@ public interface ParentTreeRepository extends JpaRepository<ParentTreeEntity, Lo

@Query(
value =
"""
SELECT PT.PARENT_TREE_ID AS \"parentTreeId\",
PT.PARENT_TREE_NUMBER AS \"parentTreeNumber\",
PTO.ORCHARD_ID AS \"orchardId\",
PTSPU.SEED_PLAN_UNIT_ID AS \"spu\",
PT.TESTED_IND AS \"tested\",
Q.GENETIC_TYPE_CODE AS \"geneticTypeCode\",
Q.GENETIC_WORTH_CODE AS \"geneticWorthCode\",
Q.GENETIC_QUALITY_VALUE AS \"geneticQualityValue\"
FROM parent_tree PT
JOIN parent_tree_orchard PTO ON PTO.parent_tree_id = PT.parent_tree_id
LEFT JOIN parent_tree_seed_plan_unit PTSPU ON PTSPU.parent_tree_id = PT.parent_tree_id
LEFT JOIN parent_tree_genetic_quality Q ON PT.parent_tree_id = Q.parent_tree_id
AND Q.seed_plan_unit_id = PTSPU.seed_plan_unit_id
AND Q.genetic_worth_calc_ind = 'Y'
WHERE PT.VEGETATION_CODE = ?1
AND PT.ACTIVE_IND = 'Y'
AND PT.parent_tree_reg_status_code = 'APP'
ORDER BY PT.parent_tree_id
""",
"""
WITH filtered_parent_tree AS (
SELECT
PT.PARENT_TREE_ID,
PT.PARENT_TREE_NUMBER,
PT.TESTED_IND
FROM parent_tree PT
WHERE PT.VEGETATION_CODE = ?1
AND PT.ACTIVE_IND = 'Y'
AND PT.parent_tree_reg_status_code = 'APP'
),
orchard_data AS (
SELECT
PTO.parent_tree_id,
PTO.ORCHARD_ID
FROM parent_tree_orchard PTO
WHERE PTO.parent_tree_id IN (SELECT PARENT_TREE_ID FROM filtered_parent_tree)
),
seed_plan_unit_data AS (
SELECT
PTSPU.parent_tree_id,
PTSPU.SEED_PLAN_UNIT_ID
FROM parent_tree_seed_plan_unit PTSPU
WHERE PTSPU.parent_tree_id IN (SELECT PARENT_TREE_ID FROM filtered_parent_tree)
),
genetic_quality_data AS (
SELECT
Q.parent_tree_id,
Q.seed_plan_unit_id,
Q.GENETIC_TYPE_CODE,
Q.GENETIC_WORTH_CODE,
Q.GENETIC_QUALITY_VALUE
FROM parent_tree_genetic_quality Q
WHERE Q.genetic_worth_calc_ind = 'Y'
)
SELECT
fpt.PARENT_TREE_ID AS \"parentTreeId\",
fpt.PARENT_TREE_NUMBER AS \"parentTreeNumber\",
od.ORCHARD_ID AS \"orchardId\",
spud.SEED_PLAN_UNIT_ID AS \"spu\",
fpt.TESTED_IND AS \"tested\",
gqd.GENETIC_TYPE_CODE AS \"geneticTypeCode\",
gqd.GENETIC_WORTH_CODE AS \"geneticWorthCode\",
gqd.GENETIC_QUALITY_VALUE AS \"geneticQualityValue\"
FROM filtered_parent_tree fpt
JOIN orchard_data od ON od.parent_tree_id = fpt.PARENT_TREE_ID
LEFT JOIN seed_plan_unit_data spud ON spud.parent_tree_id = fpt.PARENT_TREE_ID
LEFT JOIN genetic_quality_data gqd ON gqd.parent_tree_id = fpt.PARENT_TREE_ID
AND gqd.seed_plan_unit_id = spud.SEED_PLAN_UNIT_ID
ORDER BY fpt.PARENT_TREE_ID
""",
nativeQuery = true)
List<ParentTreeProj> findAllParentTreeWithVegCode(String vegCode);

@Query("select pt.id, pt.parentTreeNumber, pt.tested from ParentTreeEntity pt where pt.vegetationCode = ?1")
List<ParentTreePropsProj> findAllParentTreePropsForVegCode(String vegCode);
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ public interface ParentTreeSpuRepository
extends JpaRepository<ParentTreeSpuEntity, ParentTreeSpuId> {

List<ParentTreeSpuEntity> findByIdSpuIdIn(List<Integer> spuId);

List<ParentTreeSpuEntity> findAllById_parentTreeIdIn(List<Long> parentTreeIds);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,24 @@
import ca.bc.gov.oracleapi.dto.ParentTreeGeoNodeDto;
import ca.bc.gov.oracleapi.dto.ParentTreeNodeDto;
import ca.bc.gov.oracleapi.entity.ParentTreeEntity;
import ca.bc.gov.oracleapi.entity.ParentTreeGeneticQuality;
import ca.bc.gov.oracleapi.entity.ParentTreeOrchard;
import ca.bc.gov.oracleapi.entity.ParentTreeSpuEntity;
import ca.bc.gov.oracleapi.entity.projection.ParentTreeProj;
import ca.bc.gov.oracleapi.entity.projection.ParentTreePropsProj;
import ca.bc.gov.oracleapi.repository.ParentTreeGeneticQualityRepository;
import ca.bc.gov.oracleapi.repository.ParentTreeOrchardRepository;
import ca.bc.gov.oracleapi.repository.ParentTreeRepository;
import ca.bc.gov.oracleapi.repository.ParentTreeSpuRepository;

import java.time.Instant;
import java.time.Duration;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Optional;
import java.util.stream.Stream;
import lombok.RequiredArgsConstructor;
Expand All @@ -27,6 +39,12 @@ public class ParentTreeService {

private final ParentTreeRepository parentTreeRepository;

private final ParentTreeSpuRepository parentTreeSpuRepository;

private final ParentTreeOrchardRepository parentTreeOrchardRepository;

private final ParentTreeGeneticQualityRepository parentTreeGeneticQualityRepository;

private static final Integer MAX_LEVELS = 5;

/**
Expand Down Expand Up @@ -187,6 +205,15 @@ private boolean parentTreeListHasAnyElevationMissing(List<ParentTreeEntity> list
return list.stream().filter(tree -> tree.getElevation() == null).count() > 0;
}

private String milliToFmt(long milli) {
Duration duration = Duration.ofMillis(milli);
long HH = duration.toHours();
long MM = duration.toMinutesPart();
long SS = duration.toSecondsPart();
long MS = duration.toMillisPart();
return String.format("%02d:%02d:%02d:%04d", HH, MM, SS, MS);
}

/**
* Find all parent trees under a vegCode.
*
Expand All @@ -199,9 +226,41 @@ public Map<String, ParentTreeByVegCodeDto> findParentTreesWithVegCode(String veg
Map<String, ParentTreeByVegCodeDto> ptMap = new HashMap<>();

// Step 1: Get all the parent trees under a species
/* Ric 1: Parent tree props - id, number, tested */
long start = Instant.now().toEpochMilli();
List<ParentTreePropsProj> ptProps = parentTreeRepository.findAllParentTreePropsForVegCode(vegCode);
List<Long> ptIdList = ptProps.stream().map(ParentTreePropsProj::getParentTreeId).toList();
long ending = Instant.now().toEpochMilli();
SparLog.info("Time elapsed for step 1: {}", milliToFmt(ending - start));

/* Ric 2: Parent Tree SPU props - spu id, parent tree id */
start = Instant.now().toEpochMilli();
List<ParentTreeSpuEntity> spuProps = parentTreeSpuRepository.findAllById_parentTreeIdIn(ptIdList);
spuProps.sort((o1, o2) -> o1.getId().getSpuId().compareTo(o2.getId().getSpuId()));
Set<Integer> spuIds = new HashSet<>(spuProps.stream().map((p) -> p.getId().getSpuId()).toList());
ending = Instant.now().toEpochMilli();
SparLog.info("Time elapsed for step 2: {}", milliToFmt(ending - start));

/* Ric 3: Orchards */
start = Instant.now().toEpochMilli();
List<ParentTreeOrchard> orchardProps = parentTreeOrchardRepository.findAllById_parentTreeIdIn(ptIdList);
orchardProps.sort((o1, o2) -> o1.getId().getOrchardId().compareTo(o2.getId().getOrchardId()));
ending = Instant.now().toEpochMilli();
SparLog.info("Time elapsed for step 3: {}", milliToFmt(ending - start));

/* Ric 4: Parent Tree */
start = Instant.now().toEpochMilli();
List<ParentTreeGeneticQuality> genQualityProps = parentTreeGeneticQualityRepository.findAllGenQualityProps(ptIdList, spuIds.stream().toList());
ending = Instant.now().toEpochMilli();
SparLog.info("Time elapsed for step 3: {}", milliToFmt(ending - start));

start = Instant.now().toEpochMilli();
List<ParentTreeProj> parentTreesProjList =
parentTreeRepository.findAllParentTreeWithVegCode(vegCode);

ending = Instant.now().toEpochMilli();
SparLog.info("Time elapsed for old query: {}", milliToFmt(ending - start));

YesNoConverter yesNoConverter = new YesNoConverter();

// Step 2: Aggregate by parent tree number
Expand Down
Loading