-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
297 lines (246 loc) · 9.07 KB
/
app.py
File metadata and controls
297 lines (246 loc) · 9.07 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
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
"""
The service responsible for grouping collections of variables into querysets
It reflects information from the "main db", making groups of columns that can be
used for further querying.
"""
import os
from typing import Union,List,Optional,Tuple
import enum
import contextlib
from contextlib import closing
import fastapi
from fastapi import Response,Request
from starlette.responses import JSONResponse
import requests
from sqlalchemy import create_engine,Column,String,Integer,Enum,ForeignKey,Table
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker,relationship
import pydantic
from environs import Env
env = Env()
env.read_env()
engine = create_engine("sqlite:///db.sqlite")
Base = declarative_base()
Session = sessionmaker(bind=engine)
# ========================================================
def exists_remotely(table_name,column_name=None):
"""
Check if table / table column exists in the DB
"""
path = [env("BROKER_URL"),"table",table_name]
if column_name is not None:
path.append(column_name)
result = requests.get(os.path.join(*path))
if result.status_code == 404:
return False
elif result.status_code == 200:
return True
else:
raise ValueError(f"Unexpected status code from broker: {result.status_code}")
def hyperlink(request:Request,path:str,key:Union[str,int])->str:
"""
Composes a hyperlink from the provided components
"""
base = f"{request.url.scheme}://{request.url.hostname}:{request.url.port}"
return os.path.join(base,path,key)
# ========================================================
class MainTable(Base):
"""
Entry representing a Table in the main DB.
"""
__tablename__="maintable"
name = Column(String,primary_key=True)
#uoa = Column(Enum(UnitOfAnalysis),nullable=False)
variables = relationship("MainColumn",back_populates="table")
querysets = relationship("QuerySet",back_populates="table")
class MainColumn(Base):
"""
Entries corresponding to columns in the relevant tables in the DB.
Constraint checking against columns is hard, so i've instead
opted to make this system self-healing, meaning it does a consistency
check whenever a queryset is retrieved.
"""
__tablename__ = "variable"
variable_id = Column(Integer,primary_key=True,autoincrement=True)
name = Column(String,nullable=False)
table_name = Column(String,ForeignKey("maintable.name"))
table = relationship("MainTable",back_populates="variables")
def remote_validate(self):
"""
Check if a variable is available remotely
"""
url = os.path.join(env("BROKER_URL"),"table",self.table_name,self.name)
response = requests.get(url)
return response.status_code == 200
class QuerySet(Base):
"""
A queryset, which is a collection of variables
"""
__tablename__ = "queryset"
#queryset_id = Column(Integer,primary_key=True,autoincrement=True)
name = Column(String,primary_key=True)
table_name = Column(String,ForeignKey("maintable.name"))
table = relationship("MainTable",back_populates="querysets")
variables = relationship("MainColumn",secondary="querysets_variables")
def remote_validate(self):
"""
Check if registered variables are available remotely
"""
valid = exists_remotely(self.table_name)
for variable in self.variables:
valid &= exists_remotely(self.table_name,variable.name)
return valid
def remote_repair(self):
session = Session.object_session(self)
if not exists_remotely(self.table_name):
session.delete(self)
session.commit()
else:
for variable in self.variables:
if variable.remote_validate():
pass
else:
sess = Session.object_session(self)
sess.delete(variable)
sess.commit()
querysets_variables = Table("querysets_variables",Base.metadata,
Column("variable_id",Integer,ForeignKey("variable.variable_id")),
Column("queryset_name",String,ForeignKey("queryset.name"))
)
Base.metadata.create_all(engine)
# ========================================================
class ColumnPost(pydantic.BaseModel):
"""
A posted column
"""
name: str
class QuerySetPost(pydantic.BaseModel):
"""
A posted QuerySet
"""
name: str
columns: List[ColumnPost]
table: str
class QuerySetUpdate(pydantic.BaseModel):
"""
A posted QuerySet
"""
columns: List[ColumnPost]
def validate_qs_post(qs)->Tuple[bool,Optional[JSONResponse]]:
try:
assert exists_remotely(qs.table)
except AssertionError:
return False, JSONResponse({"message":f"Table {qs.table} does not exist"},status_code=400)
if qs.columns is not None:
for c in qs.columns:
try:
assert exists_remotely(qs.table,c.name)
except AssertionError:
return False, JSONResponse({
"message":f"Column {c.name} does not exist"
},status_code=400)
return True,None
# ========================================================
app = fastapi.FastAPI()
@app.delete("/queryset/{name}")
def qset_delete(name:str):
with closing(Session()) as sess:
existing = sess.query(QuerySet).get(name)
if existing is None:
return Response(status_code=404)
else:
sess.delete(existing)
sess.commit()
return Response(status_code=204)
@app.put("/queryset/{name}")
def qset_update(request:Request,queryset:QuerySetUpdate,name:str):
with closing(Session()) as sess:
existing = sess.query(QuerySet).get(name)
if not existing:
return Response(status_code=404)
columns = []
for column in queryset.columns:
try:
assert exists_remotely(existing.table_name,column.name)
except AssertionError:
return JSONResponse({"message":f"Column {column.name} does not exist"},status_code=400)
column_object = (sess.query(MainColumn)
.filter(
MainColumn.table_name==existing.table_name,
MainColumn.name==column.name)
.first()
)
if column_object is None:
column_object = MainColumn(name=column.name,table = existing.table)
columns.append(column_object)
existing.variables = columns
sess.commit()
return Response(status_code=204)
@app.post("/queryset/")
def qset_create(request:Request,queryset:QuerySetPost):
with closing(Session()) as sess:
exists = sess.query(QuerySet).get(queryset.name) is not None
if exists:
return Response(status_code=409)
try:
assert exists_remotely(queryset.table_name)
except AssertionError:
return JSONResponse({"message":f"Table {queryset.table_name} does not exist"},
status_code=400)
for c in queryset.columns:
try:
assert exists_remotely(queryset.table_name,c.name)
except AssertionError:
return JSONResponse({"message":f"Column {c.name} does not exist"},
status_code=400)
table = sess.query(MainTable).get(queryset.table)
if table is None:
table = MainTable(name=queryset.name)
sess.add(table)
columns = []
for col in queryset.columns:
column = (sess.query(MainColumn)
.filter(MainColumn.name==col.name,MainColumn.table==table)
.first()
)
if column is None:
column = MainColumn(name=col.name,table=table)
sess.add(column)
columns.append(column)
queryset = QuerySet(
name = queryset.name,
table_name = queryset.table,
variables = columns
)
sess.add(queryset)
sess.commit()
return {"queryset":hyperlink(request,"queryset",queryset.name)}
@app.get("/queryset/{name}")
def qset_detail(request:Request,name:str):
"""
Return queryset contents
"""
with closing(Session()) as sess:
queryset = sess.query(QuerySet).get(name)
if queryset is None:
return Response(status_code=404)
return {
"queryset_name": queryset.name,
"table_name": queryset.table_name,
"variables":[v.name for v in queryset.variables]
}
@app.get("/queryset/")
def qset_list(request:Request):
"""
Returns a list of querysets
"""
with closing(Session()) as sess:
querysets = sess.query(QuerySet).all()
serialized = []
for queryset in querysets:
serialized.append({
"name": queryset.name,
"url": hyperlink(request,"queryset",queryset.name),
"variables": len(queryset.variables),
})
return serialized