-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
275 lines (221 loc) · 9.81 KB
/
app.py
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
import os
import json
import time
import logging
from sqlalchemy import (
create_engine,
sql,
)
from uuid import UUID
from flask import (
Flask,
request,
g
)
from flask_restful import (
Resource,
Api,
reqparse
)
app = Flask(__name__)
api = Api(app)
log = logging.getLogger(__name__)
logging.basicConfig(format='%(levelname)s:%(message)s', level=logging.DEBUG)
@app.before_request
def start_timer():
g.start = time.time()
@app.after_request
def measure_request_time(response):
diff = round(time.time() - g.start, 4)
log.info(f"request time: {diff} seconds")
return response
@app.after_request
def add_cors_headers(response):
header = response.headers
header['Access-Control-Allow-Origin'] = "*"
header["Access-Control-Allow-Methods"] = "*"
header["Access-Control-Allow-Headers"] = "Content-Type"
return response
APP_SECRET = os.getenv('APP_SECRET')
@app.before_request
def check_api_key():
if os.getenv("CHECK_AUTH") == 'false':
return
auth_header = 'x-people-auth'
if auth_header not in request.headers or request.headers[auth_header] != APP_SECRET:
return 'Forbidden', 401
# TODO this is for local dev - to be removed later
DB_HOST = os.getenv("DB_HOST") or "localhost"
DB_NAME = os.getenv("DB_NAME") or "people-api"
DB_PORT = os.getenv("DB_PORT") or "5432"
DB_USER = os.getenv("DB_USER") or "people-api"
DB_PASSWORD = os.getenv("DB_PASSWORD") or "people-api"
db_string = f"postgres://{DB_USER}:{DB_PASSWORD}@{DB_HOST}:{DB_PORT}/{DB_NAME}"
db_string = os.getenv("DATABASE_URL") or db_string
db = create_engine(db_string)
# These CTEs are reused in each query and are not parametrized, hence they are safe to be passed as a format string param
SQL_STATIC_CTE_PARTS = """
person_details_outgoing_edges AS (
SELECT people.id, people.properties, edges.label, edges.head_node, edges.tail_node FROM
nodes LEFT OUTER JOIN people ON nodes.id = people.id LEFT OUTER JOIN edges ON edges.tail_node = people.id
),
person_details_incoming_edges AS (
SELECT people.id, people.properties, edges.label, edges.head_node, edges.tail_node FROM
nodes LEFT OUTER JOIN people ON nodes.id = people.id LEFT OUTER JOIN edges ON edges.head_node = people.id
),
nodes_of_outgoing_edges AS (
SELECT
person_details_outgoing_edges.id,
person_details_outgoing_edges.properties::text AS props,
COALESCE(
json_agg(
json_build_object(
'type', person_details_outgoing_edges.label,'name', nodes.properties->>'name', 'id', nodes.id
)
) FILTER (WHERE person_details_outgoing_edges.label is not null), '[]'
) AS outgoing_edges
FROM person_details_outgoing_edges LEFT OUTER JOIN NODES ON person_details_outgoing_edges.head_node = nodes.id GROUP BY 1,2
),
nodes_of_incoming_edges AS (
SELECT
person_details_incoming_edges.id,
person_details_incoming_edges.properties::text AS props,
COALESCE(
json_agg(
json_build_object(
'type', person_details_incoming_edges.label,'name', nodes.properties->>'name', 'id', nodes.id
)
) FILTER (WHERE person_details_incoming_edges.label is not null), '[]'
) AS incoming_edges
FROM person_details_incoming_edges LEFT OUTER JOIN NODES ON person_details_incoming_edges.tail_node = nodes.id GROUP BY 1,2
),
graph AS (
SELECT nodes_of_outgoing_edges.id, nodes_of_outgoing_edges.props::json, json_build_object('out', outgoing_edges, 'in', incoming_edges) AS edges
FROM nodes_of_outgoing_edges, nodes_of_incoming_edges WHERE nodes_of_outgoing_edges.id IS NOT NULL and nodes_of_outgoing_edges.id = nodes_of_incoming_edges.id
)
SELECT * FROM graph
"""
SQL_GET_PERSON_BY_NAME = sql.text(f"""
WITH people AS (SELECT id, properties FROM nodes WHERE properties->>'name' = :name), {SQL_STATIC_CTE_PARTS}
""")
SQL_GET_PERSON_BY_ID = sql.text(f"""
WITH people as (select id, properties from nodes where id = :id), {SQL_STATIC_CTE_PARTS}
;""")
SQL_GET_ALL_PEOPLE = sql.text(f"""
WITH people AS (SELECT id, properties FROM nodes), {SQL_STATIC_CTE_PARTS}
""")
SQL_GET_ALL_PEOPLE_FREE_TEXT_SEARCH= sql.text(f"""
WITH people AS (SELECT id, properties FROM nodes join json_each_text(nodes.properties) props ON True WHERE props.value ilike :search_term),
{SQL_STATIC_CTE_PARTS}
""")
SQL_INSERT_NODE = sql.text("INSERT INTO nodes (properties) VALUES (:properties) RETURNING *")
SQL_DELETE_NODE = sql.text("""DELETE FROM nodes WHERE id = :id""")
SQL_UPDATE_NODE = sql.text("""UPDATE nodes SET properties = :properties WHERE id = :id RETURNING *""")
SQL_INSERT_EDGE = sql.text("INSERT INTO edges (tail_node, head_node, label) VALUES (:tail_node, :head_node, :label) RETURNING *")
SQL_GET_EDGE_FROM_TO_NODE = sql.text("SELECT * FROM edges WHERE tail_node= :id or head_node = :id")
parser = reqparse.RequestParser()
parser.add_argument('name')
parser.add_argument('search')
parser.add_argument('properties', type=dict, location='json')
parser.add_argument('relationships', type=dict, location='json')
parser.add_argument('from', location='json')
parser.add_argument('to', location='json')
parser.add_argument('type', location='json')
class Person(Resource):
def get(self, person_id):
with db.begin() as connection:
res = connection.execute(SQL_GET_PERSON_BY_ID, id=person_id)
results = [dict(i) for i in res]
for item in results:
for key, value in item.items():
if isinstance(value, UUID):
item[key] = str(value)
return results[0]
def put(self, person_id):
arguments = parser.parse_args()
properties = arguments.get('properties')
with db.begin() as connection:
res = connection.execute(SQL_UPDATE_NODE, id=person_id, properties=json.dumps(properties))
res = [i for i in res][0]
res = dict(res)
res['id'] = str(res['id'])
return res, 200
def delete(self, person_id):
with db.begin() as connection:
res = connection.execute(SQL_DELETE_NODE, id=person_id)
class People(Resource):
def get(self):
arguments = parser.parse_args()
name = arguments.get('name')
search = arguments.get('search')
with db.begin() as connection:
if name is not None:
res = connection.execute(SQL_GET_PERSON_BY_NAME, name=name)
elif search is not None:
res = connection.execute(SQL_GET_ALL_PEOPLE_FREE_TEXT_SEARCH, search_term=f'%{search}%')
else:
res = connection.execute(SQL_GET_ALL_PEOPLE)
# This is so that people are more or less grouped together
# based on whether or not they have a connection
unsorted_results = []
ids = {}
for item in res:
item = dict(item)
item['id'] = str(item['id']) # for serializing UUIDs
unsorted_results.append(item)
ids[str(item['id'])] = item
already_in = set()
sorted_results = []
# this is used for sorting people so that they are surrounded by
# people with whom they are connected.
def get_connections_recursively(person, sorted_connections, ids, already_in):
connections = person['edges']['in'] + person['edges']['out']
if person['id'] not in already_in:
sorted_connections.append(ids[person['id']])
already_in.add(person['id'])
for conn in connections:
if conn['id'] not in already_in:
sorted_connections.append(ids[conn['id']])
already_in.add(conn['id'])
get_connections_recursively(ids[conn['id']], sorted_connections, ids, already_in)
for item in unsorted_results:
get_connections_recursively(item, sorted_results, ids, already_in)
return sorted_results
def post(self):
arguments = parser.parse_args()
properties = arguments.get('properties')
relationships = arguments.get('relationships')
with db.begin() as connection:
res = connection.execute(SQL_INSERT_NODE, properties=json.dumps(properties))
node = [i for i in res][0]
node = dict(node)
node['id'] = str(node['id'])
if relationships is not None:
head_node = relationships.get('id')
label = relationships.get('type')
connection.execute(SQL_INSERT_EDGE, tail_node=node['id'], head_node=head_node, label=label)
return node, 201
class Relationships(Resource):
def post(self):
arguments = parser.parse_args()
tail_node = arguments.get('from')
head_node = arguments.get('to')
label = arguments.get('type')
with db.begin() as connection:
edge = connection.execute(SQL_INSERT_EDGE, tail_node=tail_node, head_node=head_node, label=label)
res = [i for i in edge][0]
res = dict(res)
res['id'] = str(res['id'])
res['from'] = str(res['tail_node'])
res['to'] = str(res['head_node'])
del res['tail_node']
del res['head_node']
return res, 201
def get(self):
with db.begin() as connection:
edge = connection.execute(SQL_GET_EDGE_FROM_TO_NODE, id=node_id)
api.add_resource(Person, '/people/<person_id>')
api.add_resource(People, '/people/')
api.add_resource(Relationships, '/relationships/')
if __name__ == '__main__':
app.run(debug=True)