Skip to content

Commit bbdea77

Browse files
authored
Bug Fix: Return correct count values for RC/DRC and PC/DPC (#2405)
Fixes #2296
1 parent faa0f57 commit bbdea77

File tree

7 files changed

+106
-4
lines changed

7 files changed

+106
-4
lines changed

pom.xml

+6
Original file line numberDiff line numberDiff line change
@@ -769,6 +769,12 @@
769769
<artifactId>json-smart</artifactId>
770770
<version>2.4.9</version>
771771
</dependency>
772+
<dependency>
773+
<groupId>net.minidev</groupId>
774+
<artifactId>asm</artifactId>
775+
<version>1.0.2</version>
776+
<scope>test</scope>
777+
</dependency>
772778
<dependency>
773779
<groupId>org.apache.commons</groupId>
774780
<artifactId>commons-lang3</artifactId>

src/main/java/org/ohdsi/webapi/cdmresults/service/CDMCacheService.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -145,9 +145,9 @@ private void cacheRecordsById(Source source, List<Integer> ids) {
145145
PreparedStatementRenderer psr = getPartialPreparedStatementRenderer(source, idsSlice);
146146
Set<Integer> cachedIds = loadCache(source, psr, mapper, jdbcTemplate);
147147
// in this batch, need to save any concepts that were not found when loading cache
148-
idsSlice.removeAll(cachedIds);
149-
if (!idsSlice.isEmpty()) { // store zeros in cache
150-
List<CDMCacheEntity> zeroConcepts = idsSlice.stream().map(id -> {
148+
List<Integer> notFoundIds = new ArrayList<Integer>(CollectionUtils.subtract(idsSlice, cachedIds));
149+
if (!notFoundIds.isEmpty()) { // store zeros in cache
150+
List<CDMCacheEntity> zeroConcepts = notFoundIds.stream().map(id -> {
151151
CDMCacheEntity ce = new CDMCacheEntity();
152152
ce.setConceptId(id);
153153
ce.setRecordCount(0L);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
IF OBJECT_ID('@results_schema.achilles_result_concept_count', 'U') IS NULL
2+
CREATE TABLE @results_schema.achilles_result_concept_count (
3+
concept_id int,
4+
record_count bigint,
5+
descendant_record_count bigint,
6+
person_count bigint,
7+
descendant_person_count bigint
8+
);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package org.ohdsi.webapi.test;
2+
3+
import com.odysseusinc.arachne.commons.types.DBMSType;
4+
5+
import static org.junit.Assert.assertEquals;
6+
import static org.junit.Assert.assertTrue;
7+
8+
import java.util.ArrayList;
9+
import java.util.Arrays;
10+
import java.util.HashMap;
11+
import java.util.LinkedHashMap;
12+
import java.util.List;
13+
import java.util.Map;
14+
15+
import org.junit.Before;
16+
import org.junit.Test;
17+
import org.ohdsi.circe.helper.ResourceHelper;
18+
import org.ohdsi.sql.SqlRender;
19+
import org.ohdsi.sql.SqlTranslate;
20+
import org.ohdsi.webapi.source.SourceRepository;
21+
import org.springframework.beans.factory.annotation.Autowired;
22+
import org.springframework.beans.factory.annotation.Value;
23+
import org.springframework.http.ResponseEntity;
24+
25+
public class CDMResultsServiceIT extends WebApiIT {
26+
private static final String CDM_RESULTS_FILE_PATH = "/database/cdm_results.sql";
27+
28+
@Value("${cdmResultsService.endpoint.conceptRecordCount}")
29+
private String conceptRecordCountEndpoint;
30+
31+
@Autowired
32+
private SourceRepository sourceRepository;
33+
34+
@Before
35+
public void init() throws Exception {
36+
truncateTable(String.format("%s.%s", "public", "source"));
37+
resetSequence(String.format("%s.%s", "public", "source_sequence"));
38+
sourceRepository.saveAndFlush(getCdmSource());
39+
prepareCdmSchema();
40+
prepareResultSchema();
41+
addCDMResults();
42+
}
43+
44+
private void addCDMResults() {
45+
String resultSql = SqlRender.renderSql(ResourceHelper.GetResourceAsString(
46+
CDM_RESULTS_FILE_PATH),
47+
new String[] { "results_schema" }, new String[] { RESULT_SCHEMA_NAME });
48+
String sql = SqlTranslate.translateSql(resultSql, DBMSType.POSTGRESQL.getOhdsiDB());
49+
jdbcTemplate.execute(sql);
50+
}
51+
52+
@Test
53+
public void requestConceptRecordCounts_firstTime_returnsResults() {
54+
55+
// Arrange
56+
List<Integer> conceptIds = Arrays.asList(1);
57+
Map<String, String> queryParameters = new HashMap<String, String>();
58+
queryParameters.put("sourceName", SOURCE_KEY);
59+
60+
List<LinkedHashMap<String, List<Integer>>> list = new ArrayList<>();
61+
@SuppressWarnings("unchecked")
62+
Class<List<LinkedHashMap<String, List<Integer>>>> returnClass = (Class<List<LinkedHashMap<String, List<Integer>>>>)list.getClass();
63+
64+
// Act
65+
final ResponseEntity<List<LinkedHashMap<String, List<Integer>>>> entity = getRestTemplate().postForEntity(this.conceptRecordCountEndpoint, conceptIds,
66+
returnClass, queryParameters );
67+
68+
// Assertion
69+
assertOK(entity);
70+
List<LinkedHashMap<String, List<Integer>>> results = entity.getBody();
71+
assertEquals(1, results.size());
72+
LinkedHashMap<String, List<Integer>> resultHashMap = results.get(0);
73+
assertEquals(1, resultHashMap.size());
74+
assertTrue(resultHashMap.containsKey("1"));
75+
List<Integer> counts = resultHashMap.get("1");
76+
assertEquals(100, counts.get(0).intValue());
77+
assertEquals(101, counts.get(1).intValue());
78+
assertEquals(102, counts.get(2).intValue());
79+
assertEquals(103, counts.get(3).intValue());
80+
}
81+
}

src/test/java/org/ohdsi/webapi/test/WebApiIT.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ public abstract class WebApiIT {
6464
"/ddl/results/pathway_analysis_codes.sql",
6565
"/ddl/results/pathway_analysis_events.sql",
6666
"/ddl/results/pathway_analysis_paths.sql",
67-
"/ddl/results/pathway_analysis_stats.sql"
67+
"/ddl/results/pathway_analysis_stats.sql",
68+
"/ddl/results/achilles_result_concept_count.sql"
6869
);
6970

7071

src/test/resources/application-test.properties

+4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
baseUri=http://localhost:${local.server.port}${server.context-path}
22
security.db.datasource.url=http://localhost:${datasource.url}/arachne_portal_enterprise
33
vocabularyservice.endpoint=${baseUri}/vocabulary
4+
cdmResultsService.endpoint=${baseUri}/cdmresults
45
#GET vocabularies
56
vocabularyservice.endpoint.vocabularies=${vocabularyservice.endpoint}/vocabularies
67
#GET domains
@@ -10,6 +11,9 @@ vocabularyservice.endpoint.concept=${vocabularyservice.endpoint}/concept/1
1011
#GET cohortdefinitions
1112
cohortdefinitionservice.endpoint.cohortdefinitions=${baseUri}/cohortdefinition
1213

14+
#POST cdmResults
15+
cdmResultsService.endpoint.conceptRecordCount=${cdmResultsService.endpoint}/{sourceName}/conceptRecordCount
16+
1317
#Example application service
1418
exampleservice.endpoint=${baseUri}/example
1519

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
insert into @results_schema.achilles_result_concept_count (concept_id, record_count, descendant_record_count, person_count, descendant_person_count)
2+
values (1,100,101,102,103);

0 commit comments

Comments
 (0)