-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
84 lines (73 loc) · 2.92 KB
/
main.py
File metadata and controls
84 lines (73 loc) · 2.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
from flask import Flask, request
from sqlalchemy import create_engine
from flask_cors import CORS, cross_origin
import json
app = Flask(__name__)
CORS(app)
@app.route("/service1")
def process1():
return "Hello from process 1!"
@app.route("/query")
def QueryRaw():
queryRaw = request.args.get('raw')
queryFormat = "SELECT * FROM omop_v5.depression_results LIMIT 10"
response = engine.execute(queryFormat)
# response_dict = [u[0:2] for u in response.fetchall()]
# strResponse = ""
# for row in response:
# strResponse +=str(row)
# strResponse +='|'
# return strResponse
return json.dumps(response_dict)
def executeAndDump(query):
print 'DB Query ===================>' + query
response = engine.execute(query)
response_dict = [dict(row) for row in response]
print 'DB Response ================>' + str(response_dict)
return json.dumps(response_dict)
def accessDB(outColumns,tableName,whereQueries={},schemaName='omop_v5', LIMIT = 100):
query = "SELECT "
query += ','.join(outColumns)
if len(whereQueries) != 0:
query += ','
print 'Where Query Keys ==========>' + str(whereQueries.keys())
query += ','.join(whereQueries.keys())
query += (" FROM " + '{}.{}'.format(schemaName,tableName))
if len(whereQueries) != 0:
query += " WHERE "
i = 0;
for condition in whereQueries:
if ',' in whereQueries[condition]:
conditionArray = whereQueries[condition].split(',')
query += condition + ' in (' + ','.join(conditionArray) + ')'
elif 'name' in condition:
query += condition + " = '" + whereQueries[condition] + "'"
else:
query += condition + " = " + whereQueries[condition]
if i < len(whereQueries)-1:
query += ' AND '
i = i + 1
query += " LIMIT {} ;".format(LIMIT)
return executeAndDump(query);
def setQueryParams(request,inColumns):
whereQueries = {}
for queryParam in request.args:
print 'queryParam ============>' + queryParam
if queryParam in inColumns:
whereQueries[queryParam] = request.args.get(queryParam)
return whereQueries
@app.route("/depression_results")
def depressionResults():
outColumns = ['calci95lb','calci95ub','calrr','calp','treated','outcomename','targetname','comparatorname','outcomeid','targetid','comparatorid']
inColumns = ['outcomeid','targetid','comparatorid','outcomename','targetname','comparatorname']
whereQueries = setQueryParams(request,inColumns)
return accessDB(outColumns,'depression_results',whereQueries)
@app.route('/irs')
def irs():
inColumns = ['drug_concept_id','drug_name','condition_concept_id','condition_concept_name']
outColumns = ['num_persons_post_itt','pt_itt','ir_itt_1000pp','drug_concept_id','drug_name','condition_concept_id','condition_concept_name']
whereQueries = setQueryParams(request,inColumns)
return accessDB(outColumns,'irs_clean',whereQueries)
if __name__ == "__main__":
engine = create_engine("postgresql+psycopg2://postgres:postgres@localhost:5432/postgres")
app.run()