-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocessability.py
More file actions
458 lines (330 loc) · 14.4 KB
/
Copy pathprocessability.py
File metadata and controls
458 lines (330 loc) · 14.4 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
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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
"""
Process the results of the fetch from Cal HHS and turn the data into database tables.
Get the file list from the zip files and check the sources table contents. Verify a difference
in the files via the file_digest.
If a file has been removed, we can just remove it.
If a file is found in the Cal HHS data but not in the Opencal data, then we have to do something
to make sure of the table it needs to go into. We can constuct a table name for it and add it.
When the tables shift around because of new files, we should do something with the tables. This
should be managed in a separate script. But we will need to identify tables that are not associated
with active sources. We can archive them before they are deleted.
"""
import argparse
import json
import os
import os.path
import traceback
from datetime import datetime as dt
from pprint import pprint
import zipfile
import csv_helper
import excel_helper
import hashlib
import sql_helper
import requests
from sqlalchemy import create_engine
import config
import utils as u
def arguments():
parser = argparse.ArgumentParser()
parser.add_argument('--id')
parser.add_argument('--ids', nargs='*')
parser.add_argument('--dry-run', action="store_true", help="List zips but does not open them.")
parser.add_argument('--do-only', help="Do only X (an integer) datasets. From 1 to -1 (all).")
parser.add_argument('--skip-mod-check', action="store_true")
parser.add_argument('--only-mod-check', action="store_true", help="Run only the mod-check, then quit.")
parser.add_argument('--verbose', '-v', action="store_true")
return parser.parse_args()
def dprint(msg):
if args.verbose:
print(msg)
def should_auto_run(fname):
ext = fname.split('.')[-1].lower()
return ext in ['xlsx', 'csv'] and 'dictionary' not in fname
def update_table_mods(id):
if id is None:
sql = """select create_time, update_time, table_name
from information_schema.tables where table_schema = 'ca_hhs'"""
rows = sql_helper.db_exec(sql_helper.metadb, sql)
else:
sql = f"""select s1.table_name from datasets d1, sources s1
where d1.pk = s1.ds_pk and d1.name = '{id}'"""
tables = [f"'{r['table_name']}'" for r in db_exec(sql_helper.metadb, sql)]
tables = ', '.join(tables)
sql = f"""select create_time, update_time, table_name
from information_schema.tables
where table_schema = 'ca_hhs' and table_name in ({tables})"""
rows = sql_helper.db_exec(sql_helper.metadb, sql)
try:
sql_helper.db_exec(sql_helper.metadb, "start transaction")
dprint("started transaction for table mod dates.")
for row in rows:
#print(f"row: {row}")
if row['UPDATE_TIME'] is not None:
t = row['UPDATE_TIME']
else:
t = row['CREATE_TIME']
mt = int(t.timestamp())
table = row['TABLE_NAME']
sql = f"update sources set update_tables = {mt} where table_name = '{table}'"
sql_helper.db_exec(sql_helper.metadb, sql)
except:
sql_helper.db_exec(sql_helper.metadb, "rollback")
traceback.print_exc()
finally:
sql_helper.db_exec(sql_helper.metadb, "commit")
dprint("done.")
def verify_source_entries(datasets):
found = dict()
for name in datasets:
ds = datasets[name]
zip_file_last = ds[0]['zip_file'].split('/')[-1]
files = [r['file_name'] for r in ds]
full_fn = f"{name}/{zip_file_last}"
ds_pk = ds[0]['ds_pk']
# set up file-table connections...
#
sql = f"""select file_name, sheet_name, table_name from sources
where ds_pk = {ds_pk} and inactive is NULL and table_name is not NULL"""
rows = sql_helper.db_exec(sql_helper.metadb, sql)
tables_for_file = dict()
# a file might go to one table, but if is has sheets in an xlsx, it will go to many...
#
for row in rows:
if row['sheet_name'] is None:
tables_for_file[row['file_name']] = row['table_name']
else:
if row['file_name'] not in tables_for_file:
tables_for_file[row['file_name']] = list()
tables_for_file[row['file_name']].append(f"{row['sheet_name']}-->{row['table_name']}")
# now I can properly start.
#
found[name] = {'name': name,
'zip_file': zip_file_last,
'ds_pk': ds_pk,
'both': [], 'extra': [], 'missing': []}
try:
with zipfile.ZipFile(full_fn, mode="r") as z:
files = z.namelist()
#print(f"files # {len(files)}")
sql = f"select * from sources where ds_pk = {ds_pk} and inactive is NULL"
rows = sql_helper.db_exec(sql_helper.metadb, sql)
sources = dict(zip([r['file_name'] for r in rows], rows))
#print(f"sources # {len(list(sources.keys()))}")
found[name]['missing'] = list(set(sources.keys()) - set(files))
found[name]['extra'] = list(set(files) - set(sources.keys()))
found[name]['both'] = list(set(files) & set(sources.keys()))
except KeyboardInterrupt as kie:
print("KeyboardInterrupt")
quit()
except FileNotFoundError as fnfe:
print(f"\nFILE NOT FOUND: full_fn: {full_fn}")
except zipfile.BadZipFile as ze:
print(f"\nBAD ZIP FILE: full_fn: {full_fn}")
except:
traceback.print_exc()
print(f"\nERROR: full_fn: {full_fn}")
next_both = list()
for both in found[name]['both']:
if both in tables_for_file:
next_both.append(f"{both}-->{tables_for_file[both]}")
else:
next_both.append(both)
found[name]['both'] = next_both
next_missing = list()
for missing in found[name]['missing']:
if missing in tables_for_file:
next_missing.append(f"{missing}-->{tables_for_file[missing]}")
else:
next_missing.append(missing)
found[name]['missing'] = next_missing
#print("found:")
#pprint(found, compact=True)
return found
def print_ds_with_msgs(ds, msgs):
print("")
print("="*60)
pprint(ds, compact=True)
print("")
[print(msg) for msg in msgs]
def print_help():
print("")
print("The options here are:")
print(" done - will take you to the next dataset.")
print(" quit - will quit the entire script.")
print(" add file: <name> - will add a source without a table.")
print(" add file: <name> table: <name> - will add a source with this table.")
print(" add file: <name> sheet: <name> table: <name> - will add a source with this sheet name and table.")
print(" add file: <name> none: - will add a source that is not a table, such as a pdf or doc.")
print(" remove <name> - will remove the source or sources with this filename.")
print(" help - will print out this information.")
print("")
def process_remove(ds_pk, s):
parts = s.split(' ')
f_name = ' '.join(parts[1:])
sql = "update sources set inactive = unix_timestamp() " \
f"where ds_pk = {ds_pk} and inactive is NULL and file_name = '{f_name}'"
print(f"sql: {sql}")
sql_helper.db_exec(sql_helper.metadb, sql)
def process_add(ds_pk, s):
parts = s.split(' ')
print(f"process_add:: parts: {parts}")
pk = sql_helper.get_max_pk(sql_helper.metadb, 'sources') + 1
# TODO I could do correctness checks here....
#
# a file name and a sheet name could have a space, but a table name cannot.
sql = None
if not sql and 'table:' not in parts and 'sheet:' not in parts and 'none:' not in parts:
f_idx = 2
f_name = ' '.join(parts[f_idx:])
sql = f"insert into sources (pk, ds_pk, file_name, created, update_tables, update_tables_fails) values " \
f"({pk}, {ds_pk}, '{f_name}', unix_timestamp(), 0, 0)"
if not sql and 'table:' in parts and 'sheet:' not in parts and 'none:' not in parts:
f_idx = 2
t_idx = parts.index('table:')
f_name = ' '.join(parts[f_idx:t_idx])
t_name = parts[t_idx+1]
sql = f"insert into sources (pk, ds_pk, file_name, table_name, created, update_tables, update_tables_fails) " \
f"values ({pk}, {ds_pk}, '{f_name}', '{t_name}', unix_timestamp(), 0, 0)"
if not sql and 'table:' not in parts and 'none:' in parts:
f_idx = 2
n_idx = parts.index('none:')
f_name = ' '.join(parts[f_idx:n_idx])
sql = f"insert into sources (pk, ds_pk, file_name, created, update_tables, update_tables_fails) " \
f"values ({pk}, {ds_pk}, '{f_name}', unix_timestamp(), 0, 0)"
if not sql and 'table:' in parts and 'sheet:' in parts:
f_idx = 2
s_idx = parts.index('sheet:')
t_idx = parts.index('table:')
f_name = ' '.join(parts[f_idx:s_idx])
s_name = ' '.join(parts[s_idx+1:t_idx])
t_name = parts[t_idx+1]
sql = f"insert into sources (pk, ds_pk, file_name, sheet_name table_name, created, update_tables, update_tables_fails) values " \
f"({pk}, {ds_pk}, '{f_name}', '{s_name}', '{t_name}', unix_timestamp(), 0, 0)"
if sql:
try:
print(f"sql: {sql}")
sql_helper.db_exec(sql_helper.metadb, sql)
except:
traceback.print_exc()
else:
print("\nWARNING: no sql found for adding source?")
def ask_to_do_what(ds):
done = False
while not done:
try:
print("\ndo? ", end='')
r = input()
if r == 'done' or r == 'd':
done = True
elif r == 'quit':
quit()
elif r.startswith('add '):
process_add(ds['ds_pk'], r)
elif r.startswith('remove '):
process_remove(ds['ds_pk'], r)
elif r == 'help':
print_help()
else:
print("did not understand... Type \"help\"")
except KeyboardInterrupt:
done = True
def check_source_entries(datasets):
"""Fix the newly found conditions that do not require any thought.
If a file is newly found and is not a table-data file, then I can just
add it to the sources table automatically. If it is a table-data file,
or should be, then I cannot. If a file is missing and it is not a
table-data file, then I can just mark it as missing. If it is a
table-data file, then I cannot."""
print("")
source_pk = sql_helper.get_max_pk(sql_helper.metadb, 'sources')
sql = "select table_name from sources where table_name is not NULL and inactive is NULL"
existing_tables = [r['table_name'] for r in sql_helper.db_exec(sql_helper.metadb, sql)]
found = list()
for name in datasets:
ds = datasets[name]
#print(f"dataset[{name}]")
#pprint(ds, compact=True)
#print("")
has_extra = 'extra' in ds and len(ds['extra']) > 0
has_missing = 'missing' in ds and len(ds['missing']) > 0
if has_extra or has_missing:
found.append(name)
to_handle = len(found)
for name in found:
ds = datasets[name]
msgs = list()
for file in ds['missing']:
msgs.append(f"------ is missing: {file}")
for file in ds['extra']:
msgs.append(f"-- is newly found; {file}")
if len(msgs) > 0:
print_ds_with_msgs(ds, msgs)
print(f"to handle # {to_handle}")
ask_to_do_what(ds)
to_handle -= 1
if __name__ == '__main__':
import progressbar as pb
args = arguments()
if not args.skip_mod_check:
update_table_mods(args.id)
if args.only_mod_check:
print("\n--only-mod-check used, so quitting.")
quit()
else:
dprint("Skipping update_table_mods check...")
if args.id is None:
sql = """select * from datasets d1, sources s1
where d1.pk = s1.ds_pk and d1.inactive is NULL and s1.inactive is NULL and
(s1.update_tables is NULL or s1.update_tables < d1.update_zip_file)"""
else:
sql = f"""select * from datasets d1, sources s1
where d1.pk = s1.ds_pk and d1.inactive is NULL and s1.inactive is NULL and
d1.name = '{args.id}' and
(s1.update_tables is NULL or s1.update_tables < d1.update_zip_file)
order by d1.name, s1.file_name"""
rows = sql_helper.db_exec(sql_helper.metadb, sql)
dataset_sources = dict()
for row in rows:
if row['name'] not in dataset_sources:
dataset_sources[row['name']] = list()
dataset_sources[row['name']].append(row)
dprint("")
for name in dataset_sources:
dprint(f"{name}: sources # {len(dataset_sources[name])}")
# dataset_sources is a dict like: {'dataset-name': {row from datasets and sources}. etc.}
print(f"\ndataset_sources found # {len(dataset_sources)}")
datasets = verify_source_entries(dataset_sources)
check_source_entries(datasets)
quit()
#for ds in datasets:
# print(f"\nid: {ds['name']}")
# pprint(ds, compact=True)
"""
Now, each dataset entry looks like:
'primary-care-clinic-annual-utilization-data': {
'digest': None,
'local_update': '2025-07-11 17:46',
'name': 'primary-care-clinic-annual-utilization-data',
'ds_pk': 374,
'zip_file': 'primary-care-clinic-annual-utilization-data-537va2yo.zip'},
"""
quit()
# Check if any of the found zips are not represented in the sources table.
#
sources = sources_from_zips(datasets)
print(f"creatable sources # {len(creatable_sources)}")
for id in found:
found[id]['sources_count'] = len(found[id]['sources'])
if len(creatable_sources) > 0 and not args.dry_run:
add_sources(creatable_sources)
# Extract the files that can be used and re-build the main db tables with them.
#
for id in found:
print(f"id: {id}\n")
for source in found[id]['sources']:
#print(f"source: {source}")
source_obj = found[id]['sources'][source]
source_obj['file_name'] = source
print(f"source_obj: {source_obj}\n")
print("")