Skip to content

Commit

Permalink
Refactor metadata management and logging
Browse files Browse the repository at this point in the history
Replaced single Metadata class with CategoricalMetadata and ContinuousMetadata classes to handle different metadata types separately. Introduced MetadataResultSetUtil for organizing metadata related utilities and refactored existing logic to use this new utility. Removed unnecessary logging from LegacySearchQueryMapper and cleaned up redundant annotations.
  • Loading branch information
Gcolon021 committed Nov 14, 2024
1 parent d463a1d commit 5ec9256
Show file tree
Hide file tree
Showing 9 changed files with 168 additions and 117 deletions.
6 changes: 3 additions & 3 deletions dictonaryReqeust.http
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
# -H 'origin: https://dev.picsure.biodatacatalyst.nhlbi.nih.gov'
# -H 'referer: https://dev.picsure.biodatacatalyst.nhlbi.nih.gov/'
# --data-raw '{"facets":[],"search":"","consents":[]}'
POST http://localhost:80/concepts?page_number=1&page_size=100
POST http://localhost:80/concepts?page_number=0&page_size=100
Content-Type: application/json

{"facets":[],"search":"age","consents":[]}
{"facets":[],"search":"tutorial-biolincc_digitalis"}

###

POST http://localhost:80/search
Content-Type: application/json

{"query":{"searchTerm":"AGEDIANA","includedTags":[],"excludedTags":[],"returnTags":"true","offset":0,"limit":5}}
{"query":{"searchTerm":"tutorial-biolincc_digitalis","includedTags":[],"excludedTags":[],"returnTags":"true","offset":0,"limit":100}}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import edu.harvard.dbmi.avillach.dictionary.util.JsonBlobParser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.sql.ResultSet;
Expand All @@ -17,6 +18,7 @@ public class ConceptResultSetUtil {
private static final Logger log = LoggerFactory.getLogger(ConceptResultSetUtil.class);
private final JsonBlobParser jsonBlobParser;

@Autowired
public ConceptResultSetUtil(JsonBlobParser jsonBlobParser) {
this.jsonBlobParser = jsonBlobParser;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package edu.harvard.dbmi.avillach.dictionary.concept.model;

import edu.harvard.dbmi.avillach.dictionary.dataset.Dataset;
import jakarta.annotation.Nullable;

import java.util.List;
import java.util.Map;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
import edu.harvard.dbmi.avillach.dictionary.facet.FilterPreProcessor;
import edu.harvard.dbmi.avillach.dictionary.filter.Filter;
import edu.harvard.dbmi.avillach.dictionary.legacysearch.model.LegacySearchQuery;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.PageRequest;

import java.io.IOException;
Expand All @@ -15,7 +13,6 @@
public class LegacySearchQueryMapper {

private static final ObjectMapper objectMapper = new ObjectMapper();
private static final Logger log = LoggerFactory.getLogger(LegacySearchQueryMapper.class);

public static LegacySearchQuery mapFromJson(String jsonString) throws IOException {
JsonNode rootNode = objectMapper.readTree(jsonString);
Expand All @@ -24,7 +21,6 @@ public static LegacySearchQuery mapFromJson(String jsonString) throws IOExceptio
String searchTerm = queryNode.get("searchTerm").asText();
int limit = queryNode.get("limit").asInt();
Filter filter = FilterPreProcessor.processsFilter(new Filter(List.of(), searchTerm, List.of()));
log.info("SearchTerm: {}, Limit: {}", filter.search(), limit);
return new LegacySearchQuery(filter, PageRequest.of(0, limit));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package edu.harvard.dbmi.avillach.dictionary.legacysearch;

import edu.harvard.dbmi.avillach.dictionary.legacysearch.model.CategoricalMetadata;
import edu.harvard.dbmi.avillach.dictionary.legacysearch.model.ContinuousMetadata;
import edu.harvard.dbmi.avillach.dictionary.legacysearch.model.Result;
import edu.harvard.dbmi.avillach.dictionary.util.JsonBlobParser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.sql.ResultSet;
import java.sql.SQLException;


@Component
public class MetadataResultSetUtil {

private final static Logger log = LoggerFactory.getLogger(MetadataResultSetUtil.class);
private final JsonBlobParser jsonBlobParser;

@Autowired
public MetadataResultSetUtil(JsonBlobParser jsonBlobParser) {
this.jsonBlobParser = jsonBlobParser;
}

public Result mapContinuousMetadata(ResultSet rs) throws SQLException {
String hashedVarId = hashVarId(rs.getString("conceptPath"));
String description = getDescription(rs);
String parentName = getParentName(rs);
String parentDisplay = getParentDisplay(rs);
String max = String.valueOf(jsonBlobParser.parseMax(rs.getString("values")));
String min = String.valueOf(jsonBlobParser.parseMin(rs.getString("values")));

ContinuousMetadata metadata = new ContinuousMetadata(
rs.getString("stigmatized"), rs.getString("display"), description, min, rs.getString("conceptPath"), parentName,
rs.getString("conceptPath"), rs.getString("name"), parentDisplay, description, // changed
"{}", "", parentName, max, description, rs.getString("dataset"), hashedVarId, rs.getString("conceptType"), rs.getString("name"),
rs.getString("dataset"), rs.getString("stigmatized"), rs.getString("display"), rs.getString("studyAcronym"),
rs.getString("dsFullName"), parentName, parentDisplay, rs.getString("conceptPath"), min, max
);
return new Result(
metadata, jsonBlobParser.parseValues(rs.getString("values")), rs.getString("dataset"),
parentName == null || parentName.isBlank() ? "All Variables" : parentName, rs.getString("name"), false, true
);
}

private static String getParentDisplay(ResultSet rs) throws SQLException {
return rs.getString("parentDisplay") == null || rs.getString("parentDisplay").isBlank() ? "" : rs.getString("parentDisplay");
}

private static String getParentName(ResultSet rs) throws SQLException {
return rs.getString("parentName") == null || rs.getString("parentName").isBlank() ? "" : rs.getString("parentName");
}

private static String getDescription(ResultSet rs) throws SQLException {
return rs.getString("description") == null || rs.getString("description").isBlank() ? "" : rs.getString("description");
}

public Result mapCategoricalMetadata(ResultSet rs) throws SQLException {
String hashedVarId = hashVarId(rs.getString("conceptPath"));
String description = getDescription(rs);
String parentName = getParentName(rs);
String parentDisplay = getParentDisplay(rs);

CategoricalMetadata metadata = new CategoricalMetadata(
rs.getString("stigmatized"), rs.getString("display"), description, "", rs.getString("conceptPath"), parentName,
rs.getString("conceptPath"), rs.getString("name"), parentDisplay, description, // changed
"{}", "", parentName, "", description, rs.getString("dataset"), hashedVarId, rs.getString("conceptType"), rs.getString("name"),
rs.getString("dataset"), rs.getString("stigmatized"), rs.getString("display"), rs.getString("studyAcronym"),
rs.getString("dsFullName"), parentName, parentDisplay, rs.getString("conceptPath")
);

return new Result(
metadata, jsonBlobParser.parseValues(rs.getString("values")), rs.getString("dataset"),
parentName == null || parentName.isBlank() ? "All Variables" : parentName, rs.getString("name"), true, false
);
}

private static String hashVarId(String hpdsPath) {
String hashedVarId = "";
try {
MessageDigest digest = MessageDigest.getInstance("SHA-256");
byte[] encodedHash = digest.digest(hpdsPath.getBytes(StandardCharsets.UTF_8));
hashedVarId = bytesToHex(encodedHash);
} catch (NoSuchAlgorithmException e) {
log.error(e.getMessage());
}

return hashedVarId;
}

private static String bytesToHex(byte[] hash) {
StringBuilder hexString = new StringBuilder(2 * hash.length);
for (byte b : hash) {
String hex = Integer.toHexString(0xff & b);
if (hex.length() == 1) {
hexString.append('0');
}
hexString.append(hex);
}
return hexString.toString();
}
}
Original file line number Diff line number Diff line change
@@ -1,30 +1,23 @@
package edu.harvard.dbmi.avillach.dictionary.legacysearch;

import edu.harvard.dbmi.avillach.dictionary.legacysearch.model.Metadata;
import edu.harvard.dbmi.avillach.dictionary.concept.model.ConceptType;
import edu.harvard.dbmi.avillach.dictionary.legacysearch.model.Result;
import edu.harvard.dbmi.avillach.dictionary.legacysearch.model.SearchResult;
import edu.harvard.dbmi.avillach.dictionary.util.JsonBlobParser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.sql.ResultSet;
import java.sql.SQLException;

@Component
public class SearchResultRowMapper implements RowMapper<SearchResult> {

private static final Logger log = LoggerFactory.getLogger(SearchResultRowMapper.class);
private final JsonBlobParser jsonBlobParser;
private final MetadataResultSetUtil metadataResultSetUtil;

@Autowired
public SearchResultRowMapper(JsonBlobParser jsonBlobParser) {
this.jsonBlobParser = jsonBlobParser;
public SearchResultRowMapper(MetadataResultSetUtil metadataResultSetUtil) {
this.metadataResultSetUtil = metadataResultSetUtil;
}

@Override
Expand All @@ -33,56 +26,13 @@ public SearchResult mapRow(ResultSet rs, int rowNum) throws SQLException {
}

private SearchResult mapSearchResults(ResultSet rs) throws SQLException {
boolean isContinuous = "continuous".equalsIgnoreCase(rs.getString("conceptType"));
String hashedVarId = "";
try {
hashedVarId = hashVarId(rs.getString("conceptPath"));
} catch (NoSuchAlgorithmException e) {
log.error(e.getMessage());
}

String description =
rs.getString("description") == null || rs.getString("description").isBlank() ? "" : rs.getString("description");

String parentName = rs.getString("parentName") == null || rs.getString("parentName").isBlank() ? "" : rs.getString("parentName");
String parentDisplay =
rs.getString("parentDisplay") == null || rs.getString("parentDisplay").isBlank() ? "" : rs.getString("parentDisplay");

Metadata metadata = new Metadata(
rs.getString("stigmatized"), rs.getString("display"), description,
isContinuous ? String.valueOf(jsonBlobParser.parseMin(rs.getString("values"))) : "", rs.getString("conceptPath"), parentName,
rs.getString("conceptPath"), rs.getString("name"), parentDisplay, description, // changed
"{}", "", parentName, isContinuous ? String.valueOf(jsonBlobParser.parseMax(rs.getString("values"))) : "", description,
rs.getString("dataset"), hashedVarId, rs.getString("conceptType"), rs.getString("name"), rs.getString("dataset"),
rs.getString("stigmatized"), rs.getString("display"), rs.getString("studyAcronym"), rs.getString("dsFullName"), parentName,
parentDisplay, rs.getString("conceptPath")
);

Result result = new Result(
metadata, jsonBlobParser.parseValues(rs.getString("values")), rs.getString("dataset"),
parentName == null || parentName.isBlank() ? "All Variables" : parentName, rs.getString("name"),
"categorical".equalsIgnoreCase(rs.getString("conceptType")), isContinuous
);
Result result = switch (ConceptType.toConcept(rs.getString("conceptType"))) {
case Categorical -> this.metadataResultSetUtil.mapCategoricalMetadata(rs);
case Continuous -> this.metadataResultSetUtil.mapContinuousMetadata(rs);
};

return new SearchResult(result);
}

public static String hashVarId(String hpdsPath) throws NoSuchAlgorithmException {
MessageDigest digest = MessageDigest.getInstance("SHA-256");
byte[] encodedHash = digest.digest(hpdsPath.getBytes(StandardCharsets.UTF_8));
return bytesToHex(encodedHash);
}

private static String bytesToHex(byte[] hash) {
StringBuilder hexString = new StringBuilder(2 * hash.length);
for (byte b : hash) {
String hex = Integer.toHexString(0xff & b);
if (hex.length() == 1) {
hexString.append('0');
}
hexString.append(hex);
}
return hexString.toString();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package edu.harvard.dbmi.avillach.dictionary.legacysearch.model;

import com.fasterxml.jackson.annotation.JsonProperty;

public record CategoricalMetadata(
@JsonProperty("columnmeta_is_stigmatized") String columnmetaIsStigmatized, @JsonProperty("columnmeta_name") String columnmetaName,
@JsonProperty("description") String description, @JsonProperty("columnmeta_min") String columnmetaMin,
@JsonProperty("HPDS_PATH") String hpdsPath, @JsonProperty("derived_group_id") String derivedGroupId,
@JsonProperty("columnmeta_hpds_path") String columnmetaHpdsPath, @JsonProperty("columnmeta_var_id") String columnmetaVarId,
@JsonProperty("columnmeta_var_group_description") String columnmetaVarGroupDescription,
@JsonProperty("derived_var_description") String derivedVarDescription,
@JsonProperty("derived_variable_level_data") String derivedVariableLevelData, @JsonProperty("data_hierarchy") String dataHierarchy,
@JsonProperty("derived_group_description") String derivedGroupDescription, @JsonProperty("columnmeta_max") String columnmetaMax,
@JsonProperty("columnmeta_description") String columnmetaDescription, @JsonProperty("derived_study_id") String derivedStudyId,
@JsonProperty("hashed_var_id") String hashedVarId, @JsonProperty("columnmeta_data_type") String columnmetaDataType,
@JsonProperty("derived_var_id") String derivedVarId, @JsonProperty("columnmeta_study_id") String columnmetaStudyId,
@JsonProperty("is_stigmatized") String isStigmatized, @JsonProperty("derived_var_name") String derivedVarName,
@JsonProperty("derived_study_abv_name") String derivedStudyAbvName,
@JsonProperty("derived_study_description") String derivedStudyDescription,
@JsonProperty("columnmeta_var_group_id") String columnmetaVarGroupId, @JsonProperty("derived_group_name") String derivedGroupName,
@JsonProperty("columnmeta_HPDS_PATH") String columnmetaHpdsPathAlternate
) implements Metadata {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package edu.harvard.dbmi.avillach.dictionary.legacysearch.model;

import com.fasterxml.jackson.annotation.JsonProperty;

public record ContinuousMetadata(
@JsonProperty("columnmeta_is_stigmatized") String columnmetaIsStigmatized, @JsonProperty("columnmeta_name") String columnmetaName,
@JsonProperty("description") String description, @JsonProperty("columnmeta_min") String columnmetaMin,
@JsonProperty("HPDS_PATH") String hpdsPath, @JsonProperty("derived_group_id") String derivedGroupId,
@JsonProperty("columnmeta_hpds_path") String columnmetaHpdsPath, @JsonProperty("columnmeta_var_id") String columnmetaVarId,
@JsonProperty("columnmeta_var_group_description") String columnmetaVarGroupDescription,
@JsonProperty("derived_var_description") String derivedVarDescription,
@JsonProperty("derived_variable_level_data") String derivedVariableLevelData, @JsonProperty("data_hierarchy") String dataHierarchy,
@JsonProperty("derived_group_description") String derivedGroupDescription, @JsonProperty("columnmeta_max") String columnmetaMax,
@JsonProperty("columnmeta_description") String columnmetaDescription, @JsonProperty("derived_study_id") String derivedStudyId,
@JsonProperty("hashed_var_id") String hashedVarId, @JsonProperty("columnmeta_data_type") String columnmetaDataType,
@JsonProperty("derived_var_id") String derivedVarId, @JsonProperty("columnmeta_study_id") String columnmetaStudyId,
@JsonProperty("is_stigmatized") String isStigmatized, @JsonProperty("derived_var_name") String derivedVarName,
@JsonProperty("derived_study_abv_name") String derivedStudyAbvName,
@JsonProperty("derived_study_description") String derivedStudyDescription,
@JsonProperty("columnmeta_var_group_id") String columnmetaVarGroupId, @JsonProperty("derived_group_name") String derivedGroupName,
@JsonProperty("columnmeta_HPDS_PATH") String columnmetaHpdsPathAlternate, @JsonProperty("min") String min,
@JsonProperty("max") String max
) implements Metadata {
}
Original file line number Diff line number Diff line change
@@ -1,54 +1,4 @@
package edu.harvard.dbmi.avillach.dictionary.legacysearch.model;

import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.Map;

/**
*
* @param columnmetaIsStigmatized
* @param columnmetaName
* @param description
* @param columnmetaMin
* @param hpdsPath
* @param derivedGroupId
* @param columnmetaHpdsPath
* @param columnmetaVarId
* @param columnmetaVarGroupDescription
* @param derivedVarDescription
* @param derivedVariableLevelData
* @param dataHierarchy
* @param derivedGroupDescription
* @param columnmetaMax
* @param columnmetaDescription
* @param derivedStudyId
* @param hashedVarId
* @param columnmetaDataType
* @param derivedVarId
* @param columnmetaStudyId
* @param isStigmatized
* @param derivedVarName
* @param derivedStudyAbvName
* @param derivedStudyDescription
* @param columnmetaVarGroupId
* @param derivedGroupName
* @param columnmetaHpdsPathAlternate
*/
public record Metadata(
@JsonProperty("columnmeta_is_stigmatized") String columnmetaIsStigmatized, @JsonProperty("columnmeta_name") String columnmetaName,
@JsonProperty("description") String description, @JsonProperty("columnmeta_min") String columnmetaMin,
@JsonProperty("HPDS_PATH") String hpdsPath, @JsonProperty("derived_group_id") String derivedGroupId,
@JsonProperty("columnmeta_hpds_path") String columnmetaHpdsPath, @JsonProperty("columnmeta_var_id") String columnmetaVarId,
@JsonProperty("columnmeta_var_group_description") String columnmetaVarGroupDescription,
@JsonProperty("derived_var_description") String derivedVarDescription,
@JsonProperty("derived_variable_level_data") String derivedVariableLevelData, @JsonProperty("data_hierarchy") String dataHierarchy,
@JsonProperty("derived_group_description") String derivedGroupDescription, @JsonProperty("columnmeta_max") String columnmetaMax,
@JsonProperty("columnmeta_description") String columnmetaDescription, @JsonProperty("derived_study_id") String derivedStudyId,
@JsonProperty("hashed_var_id") String hashedVarId, @JsonProperty("columnmeta_data_type") String columnmetaDataType,
@JsonProperty("derived_var_id") String derivedVarId, @JsonProperty("columnmeta_study_id") String columnmetaStudyId,
@JsonProperty("is_stigmatized") String isStigmatized, @JsonProperty("derived_var_name") String derivedVarName,
@JsonProperty("derived_study_abv_name") String derivedStudyAbvName,
@JsonProperty("derived_study_description") String derivedStudyDescription,
@JsonProperty("columnmeta_var_group_id") String columnmetaVarGroupId, @JsonProperty("derived_group_name") String derivedGroupName,
@JsonProperty("columnmeta_HPDS_PATH") String columnmetaHpdsPathAlternate
) {
public sealed interface Metadata permits ContinuousMetadata, CategoricalMetadata {
}

0 comments on commit 5ec9256

Please sign in to comment.