Skip to content

Commit 43280f4

Browse files
committed
Add Dashboard Drawer feature
- Introduce DashboardDrawerController for drawer-related endpoints - Implement DashboardDrawerService to handle drawer logic - Create DashboardDrawerRepository for database interactions - Define DashboardDrawer and DashboardDrawerList records - Add DashboardDrawerRowMapper for result set mapping - Update DashboardRepository SQL to include dataset_id - Modify application properties to include dashboard layout configuration
1 parent cd06343 commit 43280f4

9 files changed

+215
-2
lines changed

src/main/java/edu/harvard/dbmi/avillach/dictionary/dashboard/DashboardRepository.java

+1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ public List<Map<String, String>> getHackyBDCRows() {
5252
String sql =
5353
"""
5454
SELECT
55+
dataset.dataset_id as dataset_id,
5556
dataset.abbreviation AS abbreviation,
5657
dataset.full_name AS name,
5758
CASE
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package edu.harvard.dbmi.avillach.dictionary.dashboarddrawer;
2+
3+
import java.util.List;
4+
5+
public record DashboardDrawer(
6+
int datasetId, String studyFullname, String studyAbbreviation, List<String> consentGroups, String studySummary, List<String> studyFocus,
7+
String studyDesign, String sponsor
8+
) {
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package edu.harvard.dbmi.avillach.dictionary.dashboarddrawer;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.http.ResponseEntity;
5+
import org.springframework.stereotype.Controller;
6+
import org.springframework.web.bind.annotation.*;
7+
8+
@Controller
9+
@RequestMapping("/dashboard-drawer")
10+
public class DashboardDrawerController {
11+
12+
@Autowired
13+
private DashboardDrawerService dashboardDrawerService;
14+
15+
@GetMapping
16+
public ResponseEntity<DashboardDrawerList> findAll() {
17+
return ResponseEntity.ok(dashboardDrawerService.findAll());
18+
}
19+
20+
@GetMapping("/{id}")
21+
public ResponseEntity<DashboardDrawer> findByDatasetId(@PathVariable Integer id) {
22+
return ResponseEntity.ok(dashboardDrawerService.findByDatasetId(id));
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package edu.harvard.dbmi.avillach.dictionary.dashboarddrawer;
2+
3+
import java.util.List;
4+
5+
public record DashboardDrawerList(List<DashboardDrawer> dashboardDrawerList) {
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package edu.harvard.dbmi.avillach.dictionary.dashboarddrawer;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
7+
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
8+
import org.springframework.stereotype.Repository;
9+
10+
import java.util.*;
11+
12+
@Repository
13+
public class DashboardDrawerRepository {
14+
15+
private final NamedParameterJdbcTemplate template;
16+
17+
private static final Logger log = LoggerFactory.getLogger(DashboardDrawerRepository.class);
18+
19+
@Autowired
20+
public DashboardDrawerRepository(NamedParameterJdbcTemplate template) {
21+
this.template = template;
22+
}
23+
24+
public List<DashboardDrawer> getDashboardDrawerRows() {
25+
String materializedViewSql = """
26+
select * from dictionary_db.dict.dataset_meta_materialized_view dmmv;
27+
""";
28+
29+
String fallbackSql = """
30+
SELECT d.dataset_id,
31+
MAX(d.full_name) study_fullname,
32+
MAX(d.abbreviation) study_abbreviation,
33+
ARRAY_AGG(DISTINCT c.description) consent_groups,
34+
MAX(d.description) study_summary,
35+
ARRAY_AGG(DISTINCT dm.value) FILTER (where dm.key IN ('study_focus')) study_focus,
36+
MAX(DISTINCT dm.value) FILTER (where dm.key IN ('study_design')) study_design,
37+
MAX(DISTINCT dm.value) FILTER (where dm.key IN ('sponsor')) sponsor
38+
FROM dataset d
39+
JOIN dataset_meta dm ON d.dataset_id = dm.dataset_id
40+
JOIN consent c ON d.dataset_id = c.dataset_id
41+
GROUP BY d.dataset_id
42+
""";
43+
44+
try {
45+
return template.query(materializedViewSql, new DashboardDrawerRowMapper());
46+
} catch (Exception e) {
47+
log.debug("Materialized view not available, using fallback query. Error: {}", e.getMessage());
48+
return template.query(fallbackSql, new DashboardDrawerRowMapper());
49+
}
50+
}
51+
52+
public List<DashboardDrawer> getDashboardDrawerRows(Integer datasetId) {
53+
String materializedViewSql = """
54+
select * from dictionary_db.dict.dataset_meta_materialized_view dmmv where dmmv.dataset_id = :datasetId;
55+
""";
56+
57+
String fallbackSql = """
58+
SELECT d.dataset_id dataset_id,
59+
MAX(d.full_name) study_fullname,
60+
MAX(d.abbreviation) study_abbreviation,
61+
ARRAY_AGG(DISTINCT c.description) consent_groups,
62+
MAX(d.description) study_summary,
63+
ARRAY_AGG(DISTINCT dm.value) FILTER (where dm.key IN ('study_focus')) study_focus,
64+
MAX(DISTINCT dm.value) FILTER (where dm.key IN ('study_design')) study_design,
65+
MAX(DISTINCT dm.value) FILTER (where dm.key IN ('sponsor')) sponsor
66+
FROM dataset d
67+
JOIN dataset_meta dm ON d.dataset_id = dm.dataset_id
68+
JOIN consent c ON d.dataset_id = c.dataset_id
69+
where d.dataset_id = :datasetId
70+
GROUP BY d.dataset_id
71+
""";
72+
MapSqlParameterSource params = new MapSqlParameterSource();
73+
params.addValue("datasetId", datasetId);
74+
75+
try {
76+
return template.query(materializedViewSql, params, new DashboardDrawerRowMapper());
77+
} catch (Exception e) {
78+
log.debug("Materialized view not available, using fallback query. Error: {}", e.getMessage());
79+
return template.query(fallbackSql, params, new DashboardDrawerRowMapper());
80+
}
81+
}
82+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package edu.harvard.dbmi.avillach.dictionary.dashboarddrawer;
2+
3+
import org.springframework.jdbc.core.RowMapper;
4+
5+
import java.sql.ResultSet;
6+
import java.sql.SQLException;
7+
import java.sql.Array; // For handling SQL Array
8+
import java.util.Arrays;
9+
import java.util.List;
10+
11+
public class DashboardDrawerRowMapper implements RowMapper<DashboardDrawer> {
12+
13+
@Override
14+
public DashboardDrawer mapRow(ResultSet rs, int rowNum) throws SQLException {
15+
return new DashboardDrawer(
16+
rs.getInt("dataset_id"), rs.getString("study_fullname"), rs.getString("study_abbreviation"),
17+
convertSqlArrayToList(rs.getArray("consent_groups")), rs.getString("study_summary"),
18+
convertSqlArrayToList(rs.getArray("study_focus")), rs.getString("study_design"), rs.getString("sponsor")
19+
);
20+
}
21+
22+
private List<String> convertSqlArrayToList(Array sqlArray) throws SQLException {
23+
if (sqlArray == null) {
24+
return List.of();
25+
} else {
26+
Object[] arrayContents = (Object[]) sqlArray.getArray();
27+
// Check if the array contains a single empty value
28+
if (arrayContents.length == 1 && "".equals(arrayContents[0])) {
29+
return List.of();
30+
} else {
31+
return Arrays.asList((String[]) sqlArray.getArray());
32+
}
33+
}
34+
}
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package edu.harvard.dbmi.avillach.dictionary.dashboarddrawer;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.beans.factory.annotation.Value;
5+
import org.springframework.stereotype.Service;
6+
7+
import java.util.ArrayList;
8+
import java.util.List;
9+
10+
@Service
11+
public class DashboardDrawerService {
12+
13+
private final DashboardDrawerRepository repository;
14+
private final String dashboardLayout;
15+
16+
@Autowired
17+
public DashboardDrawerService(DashboardDrawerRepository repository, @Value("${dashboard.layout.type}") String dashboardLayout) {
18+
this.repository = repository;
19+
this.dashboardLayout = dashboardLayout;
20+
}
21+
22+
/**
23+
* Retrieves the Dashboard Drawer for all datasets.
24+
*
25+
* @return a Dashboard instance with drawer-specific columns and rows.
26+
*/
27+
public DashboardDrawerList findAll() {
28+
if (dashboardLayout.equalsIgnoreCase("bdc")) {
29+
List<DashboardDrawer> records = repository.getDashboardDrawerRows();
30+
return new DashboardDrawerList(records);
31+
}
32+
33+
return new DashboardDrawerList(new ArrayList<>());
34+
}
35+
36+
/**
37+
* Retrieves the Dashboard Drawer for a specific dataset.
38+
*
39+
* @param datasetId the ID of the dataset to fetch.
40+
* @return a Dashboard instance with drawer-specific columns and rows.
41+
*/
42+
public DashboardDrawer findByDatasetId(Integer datasetId) {
43+
if (dashboardLayout.equalsIgnoreCase("bdc")) {
44+
List<DashboardDrawer> records = repository.getDashboardDrawerRows(datasetId);
45+
// Should be atomic as the query is an aggregation on the dataset table.
46+
// Probably a better way to do this.
47+
if (records.size() == 1) {
48+
return records.getFirst();
49+
}
50+
}
51+
52+
return new DashboardDrawer(-1, "", "", new ArrayList<>(), "", new ArrayList<>(), "", "");
53+
}
54+
}

src/main/resources/application.properties

+3-1
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@ spring.datasource.url=jdbc:postgresql://${POSTGRES_HOST}:5432/${POSTGRES_DB}?cur
33
spring.datasource.username=${POSTGRES_USER}
44
spring.datasource.password=${POSTGRES_PASSWORD}
55
spring.datasource.driver-class-name=org.postgresql.Driver
6+
67
server.port=80
78

89
dashboard.columns={abbreviation:'Abbreviation',name:'Name',clinvars:'Clinical Variables'}
910
dashboard.column-order=abbreviation,name,clinvars
1011
dashboard.nonmeta-columns=abbreviation,name
1112
dashboard.enable.extra_details=true
12-
dashboard.enable.bdc_hack=true
13+
dashboard.enable.bdc_hack=false
14+
dashboard.layout.type=default
1315

1416
filtering.unfilterable_concepts=stigmatized

src/test/resources/application.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ spring.datasource.driver-class-name=org.postgresql.Driver
77
dashboard.columns={abbreviation:'Abbreviation',melast:'This one goes last',name:'Name',clinvars:'Clinical Variables',participants:'Participants'}
88
dashboard.column-order=abbreviation,name,clinvars
99
dashboard.nonmeta-columns=abbreviation,name
10-
1110
dashboard.enable.extra_details=true
1211
dashboard.enable.bdc_hack=false
12+
dashboard.layout.type=default
1313

1414
filtering.unfilterable_concepts=stigmatized

0 commit comments

Comments
 (0)