-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdata_resource_manager.py
More file actions
324 lines (269 loc) · 12.2 KB
/
data_resource_manager.py
File metadata and controls
324 lines (269 loc) · 12.2 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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
"""Data Resource Manager.
The Data Resource Manager manages the lifecycles of all data resources.
It is responsible for creating the Flask application that sits at the
front of the data resource.
"""
from threading import Thread
from time import sleep
from data_resource_api.app.data_managers.data_manager import DataManager
from data_resource_api.app.utils.descriptor import Descriptor
from data_resource_api.app.utils.exception_handler import handle_errors
from data_resource_api.app.utils.json_converter import safe_json_dumps
from data_resource_api.db import Base, Checksum, Session
from data_resource_api.factories import DataResourceFactory
from data_resource_api.utils import exponential_backoff
from flask import Flask, make_response, request
from flask_restful import Api, Resource
from elasticapm.contrib.flask import ElasticAPM
from datetime import datetime as dt
import json
import os
class DataResource:
"""A Data Resource.
Attributes:
data_resource_name (str): The name of the API resource.
api_methods (dict): The API methods and associated configurations.
table_name (str): The name of the datastore.
table_schema (dict): The schema of the table for validation and generation.
api_object (object): The API object generated by the data resource manager.
datastore_object (object): The database ORM model generated by the data resource manager.
"""
def __init__(self):
self.data_resource_name = None
self.data_resource_methods = None
self.data_resource_object = None
self.data_model_name = None
self.data_model_schema = None
self.data_model_object = None
self.checksum = None
self.model_checksum = None
class AvailableServicesResource(Resource):
"""A Flask resource for listing available services.
Attributes:
endpoints (list): The collection of endpoints known to the data resource.
"""
def __init__(self):
self.endpoints = []
def add_endpoint(self, new_endpoint):
self.endpoints.append(new_endpoint)
def get(self):
return {"endpoints": self.endpoints}, 200
class DataResourceManagerSync(DataManager):
"""Data Resource Manager.
Attributes:
data_resource (list): A collection of all data resources managed by the data resouce manager.
app_config (object): The application configuration object.
"""
def __init__(self, **kwargs):
super().__init__("data-resource-manager", **kwargs)
self.data_store: DataResource = []
self.app = None
self.api = None
self.available_services = AvailableServicesResource()
self.data_resource_factory = DataResourceFactory()
# Core functions
def run(self, test_mode: bool = False):
self.wait_for_db()
def run_fn():
self.logger.info("Data Resource Manager Running...")
self.logger.debug(f"Base metadata: {list(Base.metadata.tables.keys())}")
self.monitor_data_models()
if test_mode:
run_fn()
return
while True:
run_fn()
sleep_time = self.config.get_sleep_interval()
self.logger.info(
f"Data Resource Manager Sleeping for {sleep_time} seconds..."
)
sleep(sleep_time)
def wait_for_db(self):
db_active = False
max_retries = 10
retries = 0
exponential_time = exponential_backoff(1, 1.5)
while not db_active and retries <= max_retries:
if retries != 0:
sleep_time = exponential_time()
self.logger.info(
f"Sleeping for {sleep_time} with exponential backoff..."
)
sleep(sleep_time)
retries += 1
self.logger.info("Checking database availability...")
try:
self.logger.info("Looking for DB...")
session = Session()
_ = session.query(Checksum).all()
db_active = True
self.logger.info("Successfully connected to DB.")
except Exception as e:
self.logger.info("Hit exception looking for checksum...")
# UndefinedTable
if e.code == "f405":
# TODO this is a race condition with DMM?
db_active = True
self.logger.info("Successfully connected to DB.")
elif e.code == "e3q8":
self.logger.info("Database is not available yet exception.")
self.logger.info(
"Waiting on database to become available.... {}/{}".format(
retries, max_retries
)
)
else:
self.logger.exception(f"Error occured upgrading database.")
finally:
session.close()
self.logger.info("Connected to DB.")
def create_app(self):
"""Create the base Flask application.
Returns:
app (object): The Flask application context.
"""
self.app = Flask(__name__)
self.app.config.from_object(self.app_config)
self.api = Api(self.app)
self.api.add_resource(self.available_services, "/", endpoint="all_services_ep")
self.app.register_error_handler(Exception, handle_errors)
@self.api.representation("application/json")
def output_json(data, code, headers=None):
resp = make_response(safe_json_dumps(data), code)
resp.headers.extend(headers or {})
return resp
is_testing = os.environ["APP_ENV"] == 'TESTING'
@self.app.after_request
def after_request(response):
""" Logging every request. """
if is_testing != True:
print(json.dumps({
"remote_addr": request.remote_addr,
"request_time": str(dt.utcnow()),
"method": request.method,
"path": request.path,
"scheme": request.scheme.upper(),
"statusCode": response.status_code,
"status": response.status,
"content_length": response.content_length,
"user_agent": str(request.user_agent)
}), flush=True)
return response
if is_testing != True:
apm_enabled = bool(int(os.getenv('APM_ENABLED', '0')))
if apm_enabled == True:
self.app.config['ELASTIC_APM'] = {
'SERVICE_NAME': 'data-resource-api',
'SECRET_TOKEN': os.getenv('APM_TOKEN', ''),
'SERVER_URL': os.getenv('APM_HOSTNAME', ''),
}
apm = ElasticAPM(self.app)
return self.app
def process_descriptor(self, descriptor: Descriptor):
model_exists = self.data_resource_exists(descriptor.data_resource_name)
self._process_descriptor(descriptor, model_exists)
def data_model_does_exist(self, descriptor: Descriptor):
try:
descriptor_file_name = descriptor.file_name
self.logger.debug(f"Looking at {descriptor_file_name}")
# Extract data for easier use
data_resource_name = descriptor.data_resource_name
table_name = descriptor.table_name
table_schema = descriptor.table_schema
api_schema = descriptor.api_schema
# calculate the checksum for this json
data_resource_checksum = descriptor.get_checksum()
restricted_fields = descriptor.restricted_fields
# determine if api changed
# model = self.db.get_model_checksum(table_name) # TODO what
# did this used to call?
data_resource_index = self.get_data_resource_index(data_resource_name)
if self.data_resource_changed(data_resource_name, data_resource_checksum):
data_resource = self.data_store[data_resource_index]
data_resource.checksum = data_resource_checksum
data_resource.data_resource_methods = api_schema
data_resource.data_model_name = table_name
data_resource.data_model_schema = table_schema
data_resource.data_model_object = self.orm_factory.create_orm_from_dict(
table_schema, table_name, api_schema
)
data_resource.model_checksum = self.db.get_model_checksum(table_name)
data_resource.data_resource_object.data_model = (
data_resource.data_model_object
)
data_resource.data_resource_object.table_schema = table_schema
data_resource.data_resource_object.api_schema = api_schema
data_resource.data_resource_object.restricted_fields = restricted_fields
self.data_store[data_resource_index] = data_resource
except Exception:
self.logger.exception("Error checking data resource")
def data_model_does_not_exist(self, descriptor: Descriptor):
try:
descriptor_file_name = descriptor.file_name
self.logger.debug(f"Looking at {descriptor_file_name}")
# Extract data for easier use
data_resource_name = descriptor.data_resource_name
table_name = descriptor.table_name
table_schema = descriptor.table_schema
api_schema = descriptor.api_schema
# calculate the checksum for this json
data_resource_checksum = descriptor.get_checksum()
restricted_fields = descriptor.restricted_fields
data_resource = DataResource()
data_resource.checksum = data_resource_checksum
data_resource.data_resource_name = data_resource_name
data_resource.data_resource_methods = api_schema
data_resource.data_model_name = table_name
data_resource.data_model_schema = table_schema
data_resource.data_model_object = self.orm_factory.create_orm_from_dict(
table_schema, table_name, api_schema
)
data_resource.model_checksum = self.db.get_model_checksum(table_name)
data_resource.data_resource_object = self.data_resource_factory.create_api_from_dict(
api_schema,
data_resource_name,
table_name,
self.api,
data_resource.data_model_object,
table_schema,
restricted_fields,
)
self.data_store.append(data_resource)
except Exception:
self.logger.exception("Error checking data resource")
# Data store functions
def data_resource_exists(self, data_resource_name):
"""Checks if a data resource is already registered with the data
resource manager.
Args:
data_resource_name (str): Name of the data resource.
Returns:
bool: True if the data resource exists. False if not.
"""
return self.data_exists(data_resource_name, "data_resource_name")
def data_resource_changed(self, data_resource_name, checksum):
"""Checks if the medata for a data model has been changed.
Args:
data_resource_name (str): Name of the data resource.
checksum (str): Computed MD5 checksum of the data resource schema.
Returns:
bool: True if the data resource has been changed. False if not.
"""
return self.data_changed(
data_resource_name, checksum, "data_resource_name", "checksum"
)
def get_data_resource_index(self, data_resource_name):
"""Retrieves the index of a specific data resource in the data
resources dict.
Args:
data_resource_name (str): Name of the data resource file on disk.
Returns:
int: Index of the data resource stored in memory, or -1 if not found.
"""
return self.get_data_index(data_resource_name, "data_resource_name")
class DataResourceManager(Thread, DataResourceManagerSync):
def __init__(self):
Thread.__init__(self)
DataResourceManagerSync.__init__(self)
def run(self):
DataResourceManagerSync.run(self)