use of de.symeda.sormas.api.statistics.StatisticsCaseCountDto in project SORMAS-Project by hzi-braunschweig.
the class CaseStatisticsFacadeEjbTest method testQueryCaseCountPopulation.
@Test
public void testQueryCaseCountPopulation() {
RDCF rdcf = creator.createRDCF("Region", "District", "Community", "Facility");
UserDto user = creator.createUser(rdcf.region.getUuid(), rdcf.district.getUuid(), rdcf.facility.getUuid(), "Surv", "Sup", UserRole.SURVEILLANCE_SUPERVISOR);
PersonDto cazePerson = creator.createPerson("Case", "Person");
cazePerson.setApproximateAge(30);
cazePerson.setApproximateAgeReferenceDate(new Date());
cazePerson.setApproximateAgeType(ApproximateAgeType.YEARS);
cazePerson = getPersonFacade().savePerson(cazePerson);
CaseDataDto caze = creator.createCase(user.toReference(), cazePerson.toReference(), Disease.EVD, CaseClassification.PROBABLE, InvestigationStatus.PENDING, new Date(), rdcf);
caze = getCaseFacade().getCaseDataByUuid(caze.getUuid());
StatisticsCaseCriteria criteria = new StatisticsCaseCriteria();
criteria.regions(Arrays.asList(rdcf.region));
List<StatisticsCaseCountDto> results = getCaseStatisticsFacade().queryCaseCount(criteria, StatisticsCaseAttribute.JURISDICTION, StatisticsCaseSubAttribute.REGION, null, null, true, false, null);
assertNull(results.get(0).getPopulation());
PopulationDataDto populationData = PopulationDataDto.build(new Date());
RegionDto region = getRegionFacade().getByUuid(rdcf.region.getUuid());
region.setGrowthRate(10f);
getRegionFacade().save(region);
populationData.setRegion(rdcf.region);
populationData.setPopulation(new Integer(10000));
getPopulationDataFacade().savePopulationData(Arrays.asList(populationData));
results = getCaseStatisticsFacade().queryCaseCount(criteria, StatisticsCaseAttribute.JURISDICTION, StatisticsCaseSubAttribute.REGION, null, null, true, false, LocalDate.now().getYear() + 2);
// List should have one entry
assertEquals(Integer.valueOf(12214), results.get(0).getPopulation());
}
use of de.symeda.sormas.api.statistics.StatisticsCaseCountDto in project SORMAS-Project by hzi-braunschweig.
the class StatisticsView method generateStatistics.
private List<StatisticsCaseCountDto> generateStatistics() {
fillCaseCriteria(showCaseIncidence);
if (showCaseIncidence) {
hasMissingPopulationData = false;
caseIncidencePossible = !hasIncidenceIncompatibleFilter() && !visualizationComponent.hasIncidenceIncompatibleGrouping();
missingPopulationDataNames = null;
if (caseIncidencePossible && !visualizationComponent.hasRegionGrouping() && !visualizationComponent.hasDistrictGrouping() && !visualizationComponent.hasCommunityGrouping()) {
// we don't have a territorial grouping, so the system will sum up the population of all regions.
// make sure the user is informed about regions with missing population data
List<Long> missingPopulationDataRegionIds = FacadeProvider.getPopulationDataFacade().getMissingPopulationDataForStatistics(caseCriteria, false, false, visualizationComponent.hasSexGrouping(), visualizationComponent.hasAgeGroupGroupingWithPopulationData());
hasMissingPopulationData = missingPopulationDataRegionIds.size() > 0;
if (hasMissingPopulationData) {
caseIncidencePossible = false;
List<String> missingPopulationDataNamesList = FacadeProvider.getRegionFacade().getNamesByIds(missingPopulationDataRegionIds);
missingPopulationDataNames = HtmlHelper.cleanHtml(String.join(", ", missingPopulationDataNamesList));
}
}
// Calculate projected population by either using the current year or, if a date filter has been selected, the maximum year from the date filter
populationReferenceYear = calculateMaximumReferenceYear(null, caseCriteria.getOnsetYears(), Comparator.naturalOrder(), e -> e.getValue());
populationReferenceYear = calculateMaximumReferenceYear(populationReferenceYear, caseCriteria.getReportYears(), Comparator.naturalOrder(), e -> e.getValue());
populationReferenceYear = calculateMaximumReferenceYear(populationReferenceYear, caseCriteria.getOnsetMonthsOfYear(), Comparator.naturalOrder(), e -> e.getYear().getValue());
populationReferenceYear = calculateMaximumReferenceYear(populationReferenceYear, caseCriteria.getReportMonthsOfYear(), Comparator.naturalOrder(), e -> e.getYear().getValue());
populationReferenceYear = calculateMaximumReferenceYear(populationReferenceYear, caseCriteria.getOnsetQuartersOfYear(), Comparator.naturalOrder(), e -> e.getYear().getValue());
populationReferenceYear = calculateMaximumReferenceYear(populationReferenceYear, caseCriteria.getReportQuartersOfYear(), Comparator.naturalOrder(), e -> e.getYear().getValue());
populationReferenceYear = calculateMaximumReferenceYear(populationReferenceYear, caseCriteria.getOnsetEpiWeeksOfYear(), Comparator.naturalOrder(), e -> e.getYear());
populationReferenceYear = calculateMaximumReferenceYear(populationReferenceYear, caseCriteria.getReportEpiWeeksOfYear(), Comparator.naturalOrder(), e -> e.getYear());
}
List<StatisticsCaseCountDto> resultData = FacadeProvider.getCaseStatisticsFacade().queryCaseCount(caseCriteria, visualizationComponent.getRowsAttribute(), visualizationComponent.getRowsSubAttribute(), visualizationComponent.getColumnsAttribute(), visualizationComponent.getColumnsSubAttribute(), showCaseIncidence && caseIncidencePossible, cbShowZeroValues.getValue(), populationReferenceYear);
StatisticsKeyComparator keyComparator = new StatisticsKeyComparator();
resultData.sort((c1, c2) -> {
int result = keyComparator.compare(c1.getRowKey(), c2.getRowKey());
if (result == 0) {
result = keyComparator.compare(c1.getColumnKey(), c2.getColumnKey());
}
return result;
});
return resultData;
}
use of de.symeda.sormas.api.statistics.StatisticsCaseCountDto in project SORMAS-Project by hzi-braunschweig.
the class CaseStatisticsFacadeEjb method queryCaseCount.
@SuppressWarnings("unchecked")
@Override
public List<StatisticsCaseCountDto> queryCaseCount(StatisticsCaseCriteria caseCriteria, StatisticsCaseAttribute rowGrouping, StatisticsCaseSubAttribute rowSubGrouping, StatisticsCaseAttribute columnGrouping, StatisticsCaseSubAttribute columnSubGrouping, boolean includePopulation, boolean includeZeroValues, Integer populationReferenceYear) {
// case counts
Pair<String, List<Object>> caseCountQueryAndParams = buildCaseCountQuery(caseCriteria, rowGrouping, rowSubGrouping, columnGrouping, columnSubGrouping);
Query caseCountQuery = em.createNativeQuery(caseCountQueryAndParams.getKey().toString());
for (int i = 0; i < caseCountQueryAndParams.getValue().size(); i++) {
caseCountQuery.setParameter(i + 1, caseCountQueryAndParams.getValue().get(i));
}
Function<Integer, RegionReferenceDto> regionProvider = id -> regionFacade.getRegionReferenceById(id);
Function<Integer, DistrictReferenceDto> districtProvider = id -> districtFacade.getDistrictReferenceById(id);
Function<Integer, CommunityReferenceDto> communityProvider = id -> communityFacade.getCommunityReferenceById(id);
Function<Integer, FacilityReferenceDto> healthFacilityProvider = id -> facilityFacade.getFacilityReferenceById(id);
List<StatisticsCaseCountDto> caseCountResults = ((Stream<Object[]>) caseCountQuery.getResultStream()).map(result -> {
Object rowKey = "".equals(result[1]) ? null : result[1];
Object columnKey = "".equals(result[2]) ? null : result[2];
return new StatisticsCaseCountDto(result[0] != null ? ((Number) result[0]).intValue() : null, null, StatisticsHelper.buildGroupingKey(rowKey, rowGrouping, rowSubGrouping, regionProvider, districtProvider, communityProvider, healthFacilityProvider), StatisticsHelper.buildGroupingKey(columnKey, columnGrouping, columnSubGrouping, regionProvider, districtProvider, communityProvider, healthFacilityProvider));
}).collect(Collectors.toList());
if (includeZeroValues) {
List<StatisticsGroupingKey> allRowKeys;
if (rowGrouping != null) {
allRowKeys = (List<StatisticsGroupingKey>) caseCriteria.getFilterValuesForGrouping(rowGrouping, rowSubGrouping);
if (allRowKeys == null) {
allRowKeys = StatisticsHelper.getAttributeGroupingKeys(rowGrouping, rowSubGrouping, diseaseConfigurationFacade, caseFacade, regionFacade, districtFacade);
}
} else {
allRowKeys = Arrays.asList((StatisticsGroupingKey) null);
}
List<StatisticsGroupingKey> allColumnKeys;
if (columnGrouping != null) {
allColumnKeys = (List<StatisticsGroupingKey>) caseCriteria.getFilterValuesForGrouping(columnGrouping, columnSubGrouping);
if (allColumnKeys == null) {
allColumnKeys = StatisticsHelper.getAttributeGroupingKeys(columnGrouping, columnSubGrouping, diseaseConfigurationFacade, caseFacade, regionFacade, districtFacade);
}
} else {
allColumnKeys = Arrays.asList((StatisticsGroupingKey) null);
}
for (StatisticsGroupingKey rowKey : allRowKeys) {
for (StatisticsGroupingKey columnKey : allColumnKeys) {
StatisticsCaseCountDto zeroDto = new StatisticsCaseCountDto(0, null, rowKey, columnKey);
if (!caseCountResults.contains(zeroDto)) {
caseCountResults.add(zeroDto);
}
}
}
}
// population
if (includePopulation) {
Pair<String, List<Object>> populationQueryAndParams = buildPopulationQuery(caseCriteria, rowGrouping, rowSubGrouping, columnGrouping, columnSubGrouping, populationReferenceYear);
Query populationQuery = em.createNativeQuery(populationQueryAndParams.getKey().toString());
for (int i = 0; i < populationQueryAndParams.getValue().size(); i++) {
populationQuery.setParameter(i + 1, populationQueryAndParams.getValue().get(i));
}
List<StatisticsCaseCountDto> populationResults = ((Stream<Object[]>) populationQuery.getResultStream()).map(result -> {
Object rowKey = "".equals(result[1]) ? null : result[1];
Object columnKey = "".equals(result[2]) ? null : result[2];
return new StatisticsCaseCountDto(null, result[0] != null ? ((Number) result[0]).intValue() : null, StatisticsHelper.buildGroupingKey(rowKey, rowGrouping, rowSubGrouping, regionProvider, districtProvider, communityProvider, healthFacilityProvider), StatisticsHelper.buildGroupingKey(columnKey, columnGrouping, columnSubGrouping, regionProvider, districtProvider, communityProvider, healthFacilityProvider));
}).collect(Collectors.toList());
boolean rowIsPopulation = rowGrouping != null && rowGrouping.isPopulationData();
boolean columnIsPopulation = columnGrouping != null && columnGrouping.isPopulationData();
if (!populationResults.isEmpty()) {
assert ((populationResults.get(0).getRowKey() != null) == rowIsPopulation);
assert ((populationResults.get(0).getColumnKey() != null) == columnIsPopulation);
}
// add the population data to the case counts
// when a key is not a population data key, we use null instead
StatisticsCaseCountDto searchDto = new StatisticsCaseCountDto(null, null, null, null);
for (StatisticsCaseCountDto caseCountResult : caseCountResults) {
if (rowIsPopulation) {
searchDto.setRowKey(caseCountResult.getRowKey());
}
if (columnIsPopulation) {
searchDto.setColumnKey(caseCountResult.getColumnKey());
}
int index = populationResults.indexOf(searchDto);
if (index >= 0) {
caseCountResult.setPopulation(populationResults.get(index).getPopulation());
}
}
}
return caseCountResults;
}
use of de.symeda.sormas.api.statistics.StatisticsCaseCountDto in project SORMAS-Project by hzi-braunschweig.
the class CaseStatisticsFacadeEjbTest method testQueryCaseCount.
@Test
public void testQueryCaseCount() {
RDCF rdcf = creator.createRDCF("Region", "District", "Community", "Facility");
UserDto user = creator.createUser(rdcf.region.getUuid(), rdcf.district.getUuid(), rdcf.facility.getUuid(), "Surv", "Sup", UserRole.SURVEILLANCE_SUPERVISOR);
PersonDto cazePerson = creator.createPerson("Case", "Person");
cazePerson.setApproximateAge(30);
cazePerson.setApproximateAgeReferenceDate(new Date());
cazePerson.setApproximateAgeType(ApproximateAgeType.YEARS);
cazePerson = getPersonFacade().savePerson(cazePerson);
CaseDataDto caze = creator.createCase(user.toReference(), cazePerson.toReference(), Disease.EVD, CaseClassification.PROBABLE, InvestigationStatus.PENDING, new Date(), rdcf);
caze.setOutcomeDate(DateHelper.addWeeks(caze.getReportDate(), 2));
caze = getCaseFacade().save(caze);
StatisticsCaseCriteria criteria = new StatisticsCaseCriteria();
int year = DateHelper8.toLocalDate(caze.getSymptoms().getOnsetDate()).getYear();
criteria.years(Arrays.asList(new Year(year), new Year(year + 1)), StatisticsCaseAttribute.ONSET_TIME);
criteria.regions(Arrays.asList(new RegionReferenceDto(rdcf.region.getUuid(), null, null)));
criteria.addAgeIntervals(Arrays.asList(new IntegerRange(10, 40)));
List<StatisticsCaseCountDto> results = getCaseStatisticsFacade().queryCaseCount(criteria, null, null, null, null, false, false, null);
// List should have one entry
assertEquals(1, results.size());
// try all groupings
for (StatisticsCaseAttribute groupingAttribute : StatisticsCaseAttribute.values()) {
StatisticsCaseSubAttribute[] subAttributes = groupingAttribute.getSubAttributes();
if (subAttributes.length == 0) {
getCaseStatisticsFacade().queryCaseCount(criteria, groupingAttribute, null, null, null, false, false, null);
} else {
for (StatisticsCaseSubAttribute subGroupingAttribute : groupingAttribute.getSubAttributes()) {
if (subGroupingAttribute.isUsedForGrouping()) {
getCaseStatisticsFacade().queryCaseCount(criteria, groupingAttribute, subGroupingAttribute, null, null, false, false, null);
}
}
}
}
}
use of de.symeda.sormas.api.statistics.StatisticsCaseCountDto in project SORMAS-Project by hzi-braunschweig.
the class CaseStatisticsFacadeEjbTest method testQueryCaseCountZeroValues.
@Test
public void testQueryCaseCountZeroValues() {
RDCF rdcf = creator.createRDCF("Region", "District", "Community", "Facility");
UserDto user = creator.createUser(rdcf.region.getUuid(), rdcf.district.getUuid(), rdcf.facility.getUuid(), "Surv", "Sup", UserRole.SURVEILLANCE_SUPERVISOR);
PersonDto cazePerson = creator.createPerson("Case", "Person");
cazePerson.setApproximateAge(30);
cazePerson.setApproximateAgeReferenceDate(new Date());
cazePerson.setApproximateAgeType(ApproximateAgeType.YEARS);
cazePerson = getPersonFacade().savePerson(cazePerson);
CaseDataDto caze = creator.createCase(user.toReference(), cazePerson.toReference(), Disease.EVD, CaseClassification.PROBABLE, InvestigationStatus.PENDING, new Date(), rdcf);
caze = getCaseFacade().getCaseDataByUuid(caze.getUuid());
StatisticsCaseCriteria criteria = new StatisticsCaseCriteria();
int year = DateHelper8.toLocalDate(caze.getSymptoms().getOnsetDate()).getYear();
criteria.years(Arrays.asList(new Year(year), new Year(year + 1)), StatisticsCaseAttribute.ONSET_TIME);
criteria.regions(Arrays.asList(new RegionReferenceDto(rdcf.region.getUuid(), null, null)));
criteria.addAgeIntervals(Arrays.asList(new IntegerRange(10, 40)));
List<StatisticsCaseCountDto> results = getCaseStatisticsFacade().queryCaseCount(criteria, StatisticsCaseAttribute.SEX, null, null, null, false, true, null);
// List should have one entry per sex
assertEquals(Sex.values().length, results.size());
}
Aggregations