Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add REST endpoint, which generates DDL for Result schema (#285)
- Loading branch information
1 parent
efaca11
commit c92863d
Showing
9 changed files
with
523 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* | ||
* | ||
* Copyright 2017 Observational Health Data Sciences and Informatics | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
* Authors: Maria Pozhidaeva | ||
* | ||
*/ | ||
package org.ohdsi.webapi.service; | ||
|
||
import static org.ohdsi.webapi.service.SqlRenderService.translateSQL; | ||
|
||
import java.util.Arrays; | ||
import java.util.Collection; | ||
import java.util.HashMap; | ||
import javax.ws.rs.DefaultValue; | ||
import javax.ws.rs.GET; | ||
import javax.ws.rs.Path; | ||
import javax.ws.rs.Produces; | ||
import javax.ws.rs.QueryParam; | ||
import org.ohdsi.circe.helper.ResourceHelper; | ||
import org.ohdsi.webapi.sqlrender.SourceStatement; | ||
import org.ohdsi.webapi.sqlrender.TranslatedStatement; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Path("/ddl/") | ||
@Component | ||
public class DDLService { | ||
|
||
private static final Collection<String> COMMON_COHORT_RESULT_FILE_PATHS = Arrays.asList( | ||
"/db/cohort_results/cohort_feasiblity.sql", | ||
"/db/cohort_results/cohort_features_results.sql", | ||
"/db/cohort_results/ir_analysis.sql", | ||
"/db/cohort_results/createHeraclesTables.sql"); | ||
|
||
private static final String INDEXES_COHORT_RESULT_FILE_PATHS = "/db/cohort_results/heracles_indexes.sql"; | ||
private static final Collection<String> DBMS_NO_INDEXES = Arrays.asList("redshift", "impala", "netezza"); | ||
|
||
@GET | ||
@Path("results") | ||
@Produces("text/plain") | ||
public String generateResultSQL(@QueryParam("dialect") String dialect, @DefaultValue("results") @QueryParam("schema") String schema) { | ||
|
||
StringBuilder sqlBuilder = new StringBuilder(); | ||
for (String fileName : COMMON_COHORT_RESULT_FILE_PATHS){ | ||
sqlBuilder.append("\n").append(ResourceHelper.GetResourceAsString(fileName)); | ||
} | ||
if (dialect == null || DBMS_NO_INDEXES.stream().noneMatch(dbms -> dbms.equals(dialect.toLowerCase()))) { | ||
sqlBuilder.append("\n").append(ResourceHelper.GetResourceAsString(INDEXES_COHORT_RESULT_FILE_PATHS)); | ||
} | ||
String result = sqlBuilder.toString(); | ||
if(dialect != null){ | ||
result = translateSqlFile(result, dialect, schema); | ||
} | ||
return result.replaceAll(";", ";\n"); | ||
} | ||
|
||
private String translateSqlFile(String sql, String dialect, String schema) { | ||
|
||
SourceStatement statement = new SourceStatement(); | ||
statement.targetDialect = dialect.toLowerCase(); | ||
statement.sql = sql; | ||
HashMap<String, String> parameters = new HashMap<>(); | ||
parameters.put("results_schema", schema); | ||
statement.parameters = parameters; | ||
TranslatedStatement translatedStatement = translateSQL(statement); | ||
return translatedStatement.targetSQL; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
src/main/resources/db/cohort_results/cohort_feasiblity.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
IF OBJECT_ID('@results_schema.cohort_inclusion', 'U') IS NULL | ||
CREATE TABLE @results_schema.cohort_inclusion( | ||
cohort_definition_id int NOT NULL, | ||
rule_sequence int NOT NULL, | ||
name varchar(255) NULL, | ||
description varchar(1000) NULL | ||
); | ||
|
||
IF OBJECT_ID('@results_schema.cohort_inclusion_result', 'U') IS NULL | ||
CREATE TABLE @results_schema.cohort_inclusion_result( | ||
cohort_definition_id int NOT NULL, | ||
inclusion_rule_mask bigint NOT NULL, | ||
person_count bigint NOT NULL | ||
); | ||
|
||
IF OBJECT_ID('@results_schema.cohort_inclusion_stats', 'U') IS NULL | ||
CREATE TABLE @results_schema.cohort_inclusion_stats( | ||
cohort_definition_id int NOT NULL, | ||
rule_sequence int NOT NULL, | ||
person_count bigint NOT NULL, | ||
gain_count bigint NOT NULL, | ||
person_total bigint NOT NULL | ||
); | ||
|
||
IF OBJECT_ID('@results_schema.cohort_summary_stats', 'U') IS NULL | ||
CREATE TABLE @results_schema.cohort_summary_stats( | ||
cohort_definition_id int NOT NULL, | ||
base_count bigint NOT NULL, | ||
final_count bigint NOT NULL | ||
); | ||
|
||
IF OBJECT_ID('@results_schema.feas_study_result', 'U') IS NULL | ||
CREATE TABLE $results_schema.feas_study_result( | ||
study_id int NOT NULL, | ||
inclusion_rule_mask bigint NOT NULL, | ||
person_count bigint NOT NULL | ||
); | ||
|
||
IF OBJECT_ID('@results_schema.feas_study_inclusion_stats', 'U') IS NULL | ||
CREATE TABLE $results_schema.feas_study_inclusion_stats( | ||
study_id int NOT NULL, | ||
rule_sequence int NOT NULL, | ||
name varchar(255) NOT NULL, | ||
person_count bigint NOT NULL, | ||
gain_count bigint NOT NULL, | ||
person_total bigint NOT NULL | ||
); | ||
|
||
IF OBJECT_ID('@results_schema.feas_study_index_stats', 'U') IS NULL | ||
CREATE TABLE $results_schema.feas_study_index_stats( | ||
study_id int NOT NULL, | ||
person_count bigint NOT NULL, | ||
match_count bigint NOT NULL | ||
); |
48 changes: 48 additions & 0 deletions
48
src/main/resources/db/cohort_results/cohort_features_results.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
IF OBJECT_ID('@results_schema.cohort_features_dist', 'U') IS NULL | ||
CREATE TABLE @results_schema.cohort_features_dist | ||
( | ||
cohort_definition_id bigint, | ||
covariate_id bigint, | ||
count_value float, | ||
min_value float, | ||
max_value float, | ||
average_value float, | ||
standard_deviation float, | ||
median_value float, | ||
p10_value float, | ||
p25_value float, | ||
p75_value float, | ||
p90_value float | ||
); | ||
|
||
IF OBJECT_ID('@results_schema.cohort_features', 'U') IS NULL | ||
CREATE TABLE @results_schema.cohort_features | ||
( | ||
cohort_definition_id bigint, | ||
covariate_id bigint, | ||
sum_value bigint, | ||
average_value float | ||
); | ||
|
||
IF OBJECT_ID('@results_schema.cohort_features_ref', 'U') IS NULL | ||
CREATE TABLE @results_schema.cohort_features_ref | ||
( | ||
cohort_definition_id bigint, | ||
covariate_id bigint, | ||
covariate_name varchar(1000), | ||
analysis_id int, | ||
concept_id int | ||
); | ||
|
||
IF OBJECT_ID('@results_schema.cohort_features_analysis_ref', 'U') IS NULL | ||
CREATE TABLE @results_schema.cohort_features_analysis_ref | ||
( | ||
cohort_definition_id bigint, | ||
analysis_id int, | ||
analysis_name varchar(1000), | ||
domain_id varchar(100), | ||
start_day int, | ||
end_day int, | ||
is_binary char, | ||
missing_means_zero char | ||
); |
Oops, something went wrong.