-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrunner.py
389 lines (353 loc) · 16 KB
/
runner.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
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
import datetime
import json
from aws_requests_auth.aws_auth import AWSRequestsAuth
import pymysql.connections
from sqlalchemy.ext.declarative import declarative_base
from google.cloud.sql.connector import Connector
from sqlalchemy.orm import sessionmaker
from sqlalchemy import create_engine, Column, String, Integer, DateTime, ForeignKey, select, insert, and_, or_
import argparse
import os
import requests
import logging
session = None
connector = Connector()
Model = declarative_base(name='Model')
LIST_DIRECTORIES_URL = 'https://j6yq55awhm4zltnzwiov7m6dvy0guuvi.lambda-url.us-east-1.on.aws/'
GET_DIRECTORY_FILES_URL = 'https://mrurqi3b2wc7unxcu7hsptsmmm0ccpwg.lambda-url.us-east-1.on.aws/'
PROCESS_FILES_URL = 'https://t22goxl5ubsigzprq2ru2pezxm0zxdug.lambda-url.us-east-1.on.aws/'
AWS_ACCESS_KEY = None
AWS_SECRET_KEY = None
# TODO: add command line arguments to switch between medrxiv and biorxiv for source and destination buckets
# region models
class Directory(Model):
__tablename__ = 'directories'
id = Column(Integer, primary_key=True)
path = Column(String(250))
scanned_dt = Column(DateTime)
def __init__(self, path, scanned_dt=None):
self.path = path
if scanned_dt:
self.scanned_dt = scanned_dt
class File(Model):
__tablename__ = 'files'
id = Column(Integer, primary_key=True)
archive_filename = Column(String(100))
xml_filename = Column(String(100))
parent_directory = Column(Integer, ForeignKey('directories.id'))
status = Column(String(50))
def __init__(self, anID, archive_filename, xml_filename, parent_directory, status):
self.id = anID
self.archive_filename = archive_filename
self.xml_filename = xml_filename
self.parent_directory = parent_directory
self.status = status
class FileEvent(Model):
__tablename__ = 'file_events'
id = Column(Integer, primary_key=True)
file_id = Column(Integer, ForeignKey('file.id'))
event_type = Column(String(50))
event_dt = Column(DateTime)
def __init__(self, anID, file_id, event_type, event_dt):
self.id = anID
self.file_id = file_id
self.event_type = event_type
self.event_dt = event_dt
# endregion
def init_db(instance: str, user: str, password: str, database: str) -> None:
def get_conn() -> pymysql.connections.Connection:
conn: pymysql.connections.Connection = connector.connect(
instance_connection_string=instance,
driver='pymysql',
user=user,
password=password,
database=database
)
return conn
engine = create_engine('mysql+pymysql://', creator=get_conn, echo=False)
global session
session = sessionmaker()
session.configure(bind=engine)
def update_directories_table():
path_list = []
existing_directories = session.execute(select(Directory)).all()
existing_paths = [x[0].path for x in existing_directories]
months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
print('getting current content by months')
for month in months:
payload_c = {'bucket': 'medrxiv-src-monthly', 'subdirectory': 'Current_Content/' + month}
r = get_with_auth(LIST_DIRECTORIES_URL, payload_c)
if r.status_code == 200:
response_json = r.json()
# print(len(response_json['directories']))
path_list.extend(response_json['directories'])
else:
print(r.status_code)
print(r.text)
insert_buffer = []
print(len(path_list))
for path in path_list:
if path not in existing_paths:
insert_buffer.append({'path': path})
print(len(insert_buffer))
payload_b = {'bucket': 'medrxiv-src-monthly', 'subdirectory': 'Back_Content/'}
print('getting back content')
r = get_with_auth(LIST_DIRECTORIES_URL, payload_b)
if r.status_code == 200:
response_json = r.json()
path_list = response_json['directories']
else:
print(r.status_code)
print(r.text)
print(len(path_list))
for path in path_list:
if path not in existing_paths:
insert_buffer.append({'path': path})
print(len(insert_buffer))
session.bulk_insert_mappings(Directory, insert_buffer)
session.commit()
def scan_new_directories(update_date=None):
if not update_date:
existing_directories = session.execute(
select(Directory)
.where(Directory.scanned_dt.is_(None))
).all()
else:
existing_directories = session.execute(
select(Directory)
.where(
or_(Directory.scanned_dt.is_(None), Directory.scanned_dt < update_date))
).all()
current_files = set([filename for filename, in session.execute(select(File.archive_filename)).all()])
logging.debug(f"{update_date}\t{len(existing_directories)}\t{len(current_files)}")
insert_buffer = []
for directory, in existing_directories:
logging.info('Working on ' + directory.path)
if directory.path == 'Back_Content/' or directory.path == 'Current_Content/':
directory.scanned_dt = datetime.datetime.now()
continue
payload = {'source-bucket': 'medrxiv-src-monthly', 'directory-prefix': directory.path}
response = get_with_auth(GET_DIRECTORY_FILES_URL, payload)
if response.status_code == 200:
response_json = response.json()
if response_json['file_count'] > 0:
path_list = response_json['paths']
logging.info(f"{len(path_list)} filepaths in this directory")
for path in path_list:
if path not in current_files:
insert_buffer.append(
{
'archive_filename': path,
'parent_directory': directory.id,
'status': 'discovered'
}
)
else:
logging.debug(json.dumps(payload))
logging.warning(response.status_code)
logging.warning(response.text)
directory.scanned_dt = datetime.datetime.now()
if len(insert_buffer) > 0:
logging.info(f"Inserting {len(insert_buffer)} records")
session.bulk_insert_mappings(File, insert_buffer)
insert_buffer.clear()
session.commit()
if len(insert_buffer) > 0:
session.bulk_insert_mappings(File, insert_buffer)
session.commit()
def update_current_month(partition_size, key, secret):
current_month_directory_path = datetime.datetime.now().strftime('Current_Content/%B_%Y/')
current_month_directory, = session.execute(
select(Directory)
.where(Directory.path == current_month_directory_path)
).first()
if not current_month_directory:
session.execute(insert(Directory).values(path=current_month_directory_path))
current_month_directory, = session.execute(
select(Directory)
.where(Directory.path == current_month_directory_path)
).first()
scan_directory(current_month_directory)
process_directory_files(current_month_directory, partition_size, key, secret)
def scan_directory(directory):
payload = {'source-bucket': 'medrxiv-src-monthly', 'directory-prefix': directory.path}
response = get_with_auth(GET_DIRECTORY_FILES_URL, payload)
insert_buffer = []
if response.status_code == 200:
response_json = response.json()
if response_json['file_count'] > 0:
path_list = response_json['paths']
for path in path_list:
insert_buffer.append(
{
'archive_filename': path,
'parent_directory': directory.id,
'status': 'discovered'
}
)
else:
print(response.status_code)
print(response.text)
directory.scanned_dt = datetime.datetime.now()
if len(insert_buffer) > 0:
session.bulk_insert_mappings(File, insert_buffer)
session.commit()
def process_directory_files(directory, partition_size, key, secret):
files_to_process = session.execute(
select(File)
.where(
and_(File.status != 'downloaded', File.parent_directory == directory.id)
)).all()
file_dict = {}
for file, in files_to_process:
file_dict[file.archive_filename] = file
filename_list = [fileobj.archive_filename for fileobj, in files_to_process]
data = {
'source-bucket': 'medrxiv-src-monthly',
'paths': [],
'destination': 'translator-text-workflow-dev_work',
'directory': 'medrxiv-xml/',
'key_id': key,
'secret': secret
}
while len(filename_list) > 0:
sublist = filename_list[:partition_size] if len(filename_list) > partition_size else filename_list
data['paths'] = sublist
response = get_with_auth(PROCESS_FILES_URL, data)
if response.status_code == 200:
response_json = response.json()
new_events_buffer = []
completed_filename_list = []
downloaded_files_dict = response_json['downloaded_files']
error_files_list = response_json['error_files']
print(f"{len(downloaded_files_dict)} succeeded, {len(error_files_list)} errors")
print(f"Time elapsed: {response_json['runtime']}ms")
for archive_filename in downloaded_files_dict.keys():
completed_filename_list.append(archive_filename)
file_object = file_dict[archive_filename]
xml_filename = downloaded_files_dict[archive_filename]
file_object.xml_filename = xml_filename
file_object.status = 'downloaded'
new_events_buffer.append({
'file_id': file_object.id,
'event_type': 'downloaded',
'event_dt': datetime.datetime.now()
})
for archive_filename in error_files_list:
completed_filename_list.append(archive_filename)
file_object = file_dict[archive_filename]
file_object.status = 'error'
new_events_buffer.append({
'file_id': file_object.id,
'event_type': 'error',
'event_dt': datetime.datetime.now()
})
for filename in completed_filename_list:
filename_list.remove(filename)
session.commit()
else:
print(response.status_code)
print(response.text)
def process_files_by_parts(partition_size, key, secret):
files_to_process = session.execute(
select(File).where(and_(File.status != 'downloaded', File.status != 'error')).limit(50000)).all()
file_dict = {}
logging.info(f'Found {len(files_to_process)} files to process')
for file, in files_to_process:
file_dict[file.archive_filename] = file
filename_list = [fileobj.archive_filename for fileobj, in files_to_process]
data = {
'source-bucket': 'medrxiv-src-monthly',
'paths': [],
'destination': 'translator-text-workflow-dev_work',
'directory': 'medrxiv-xml/',
'key_id': key,
'secret': secret
}
while len(filename_list) > 0:
sublist = filename_list[:partition_size] if len(filename_list) > partition_size else filename_list
data['paths'] = sublist
response = get_with_auth(PROCESS_FILES_URL, data)
if response.status_code == 200:
response_json = response.json()
new_events_buffer = []
completed_filename_list = []
downloaded_files_dict = response_json['downloaded_files']
error_files_list = response_json['error_files']
logging.info(f"{len(downloaded_files_dict)} succeeded, {len(error_files_list)} errors")
logging.debug(f"Time elapsed: {response_json['runtime']}ms")
for archive_filename in downloaded_files_dict.keys():
completed_filename_list.append(archive_filename)
file_object = file_dict[archive_filename]
xml_filename = downloaded_files_dict[archive_filename]
file_object.xml_filename = xml_filename
file_object.status = 'downloaded'
new_events_buffer.append({
'file_id': file_object.id,
'event_type': 'downloaded',
'event_dt': datetime.datetime.now()
})
for archive_filename in error_files_list:
completed_filename_list.append(archive_filename)
file_object = file_dict[archive_filename]
file_object.status = 'error'
new_events_buffer.append({
'file_id': file_object.id,
'event_type': 'error',
'event_dt': datetime.datetime.now()
})
for filename in completed_filename_list:
filename_list.remove(filename)
session.commit()
else:
logging.warning(response.status_code)
logging.warning(response.text)
def get_with_auth(url, payload):
host = url.replace('https:', '').replace('/', '')
auth = AWSRequestsAuth(aws_access_key=AWS_ACCESS_KEY,
aws_secret_access_key=AWS_SECRET_KEY,
aws_host=host,
aws_region='us-east-1',
aws_service='lambda')
headers = {"Content-type": "application/json"}
response = requests.get(url, auth=auth, json=payload, headers=headers, timeout=(5, 600))
# print(response.content)
return response
if __name__ == "__main__":
logging.basicConfig(format='%(asctime)s %(module)s:%(funcName)s:%(levelname)s: %(message)s', level=logging.DEBUG)
parser = argparse.ArgumentParser()
parser.add_argument('-i', '--instance', help='GCP DB instance name')
parser.add_argument('-d', '--database', help='database name')
parser.add_argument('-u', '--user', help='database username')
parser.add_argument('-p', '--password', help='database password')
parser.add_argument('-k', '--key', help='HMAC key id')
parser.add_argument('-s', '--secret', help='HMAC secret')
parser.add_argument('-a', '--aws-key', help='AWS access key')
parser.add_argument('-w', '--aws-secret', help='AWS secret key')
parser.add_argument('-t', '--task', help='task to execute')
parser.add_argument('-o', '--old', help='previous scan date to rescan (ISO 8601 date)')
parser.add_argument('-c', '--chunk', help='size of file processing chunks', type=int)
args = parser.parse_args()
init_db(
instance=args.instance if args.instance else os.getenv('MYSQL_DATABASE_INSTANCE', None),
user=args.user if args.user else os.getenv('MYSQL_DATABASE_USER', None),
password=args.password if args.password else os.getenv('MYSQL_DATABASE_PASSWORD', None),
database=args.database if args.database else 'biorxiv'
)
task = args.task if args.task else 'all'
hmac_key = args.key if args.key else os.getenv("HMAC_KEY_ID", None)
hmac_secret = args.secret if args.secret else os.getenv("HMAC_SECRET", None)
AWS_ACCESS_KEY = args.aws_key if args.aws_key else os.getenv("AWS_ACCESS_KEY", None)
AWS_SECRET_KEY = args.aws_secret if args.aws_secret else os.getenv("AWS_SECRET_KEY", None)
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = 'prod-creds.json'
chunk_size = args.chunk if args.chunk else 100
session = session()
if task == 'ls' or task == 'all':
update_directories_table()
if task == 'scan' or task == 'all':
old_date = datetime.datetime.fromisoformat(args.old) if args.old else datetime.datetime.now()
scan_new_directories(old_date)
if task == 'process' or task == 'all':
process_files_by_parts(chunk_size, hmac_key, hmac_secret)
if task == 'update' or task == 'all':
update_current_month(chunk_size, hmac_key, hmac_secret)