Repository navigation
Expand file tree
/
Copy pathcli.py
More file actions
594 lines (485 loc) · 19.6 KB
/
Copy pathcli.py
File metadata and controls
594 lines (485 loc) · 19.6 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
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
#!/usr/bin/env python
"""Unified CLI for loading, checking, and downloading fetchlog files.
Selectors (one required):
--id ID single fetchlog id
--key KEY single S3 key or local path
--batch UUID all keys in a batch
--pattern REGEX query fetchlogs by regex pattern
--s3-prefix PREFIX list S3 directly under prefix
--from-file PATH read keys from a file (one per line)
KEY [KEY ...] positional keys
Actions:
(default) process through IngestClient
--preview parse only; no DB writes
--dry-run resolve keys and print; no processing
--download [PATH] download files locally; no DB
Modifiers:
--stage-only dump to staging, skip ETL load
--source-db DSN alternate DB for fetchlog lookups
--bucket NAME override FETCH_BUCKET
--limit N cap on keys processed (default 300)
Reporting:
--no-report Do not print summary tables after processing
--csv PATH write per-key stats to CSV
Transaction:
--commit persist (default: rollback)
--debug verbose logging
"""
import argparse
import csv
import logging
import os
import sys
from time import time
from datetime import datetime, timezone
import psycopg2
from ingest.lcsV2 import IngestClient
from ingest.resources import Resources
from ingest.utils import (
deconstruct_path,
download_from_location,
resolve_by_batch,
resolve_by_id,
resolve_by_keys,
resolve_by_pattern,
resolve_by_prefix,
resolve_from_file,
)
logger = logging.getLogger('CLI')
# ---------------------------------------------------------------------------
# Argument parsing
# ---------------------------------------------------------------------------
def parse_args():
p = argparse.ArgumentParser(
description="Ingest fetchlog files into the database",
formatter_class=argparse.RawDescriptionHelpFormatter,
)
# Selectors (mutually exclusive)
sel = p.add_mutually_exclusive_group()
sel.add_argument('--id', type=int, help='fetchlog id')
sel.add_argument('--batch', type=str, help='batch uuid')
sel.add_argument('--pattern', type=str, help='regex against fetchlog keys')
sel.add_argument('--s3-prefix', type=str, help='S3 key prefix')
sel.add_argument('--from-file', type=str, help='file with one key per line')
sel.add_argument('keys', nargs='*', default=[],
help='positional S3 keys or local paths')
# Actions (mutually exclusive)
act = p.add_mutually_exclusive_group()
act.add_argument('--preview', action='store_true',
help='parse only; no DB writes')
act.add_argument('--dry-run', action='store_true',
help='resolve keys and print; no processing')
act.add_argument('--download', nargs='?', const='',
metavar='PATH',
help='download files locally; no DB')
# Modifiers
p.add_argument('--stage-only', action='store_true',
help='dump to staging, skip ETL load')
p.add_argument('--source-db', type=str,
help='alt DB DSN for fetchlog lookups')
p.add_argument('--bucket', type=str,
help='override FETCH_BUCKET')
p.add_argument('--limit', type=int, default=300)
# Reporting
p.add_argument('--no-report', action='store_true',
help='suppress summary tables (default: show)')
p.add_argument('--csv', type=str, metavar='PATH',
help='write per-key stats to CSV')
# Transaction
p.add_argument('--commit', action='store_true',
help='persist changes (default: rollback)')
p.add_argument('--debug', action='store_true')
p.add_argument('--keep-staging', action='store_true',
help='use permanent tables for staging (allows '
'post-run inspection; overrides USE_TEMP_TABLES)')
p.add_argument('--diagnose', type=str, nargs='?', const='summary',
metavar='QUERY[,QUERY,...]',
help='run diagnostic queries; without argument runs '
'"summary" pack; "list" prints available names')
p.add_argument('--diagnose-dir', type=str, metavar='DIR',
help='write diagnostic results to CSV files in this directory '
'(otherwise print to console)')
# Handler invocation (for debugging)
hand = p.add_mutually_exclusive_group()
hand.add_argument('--run-cron', action='store_true',
help='invoke cronhandler() as if triggered by EventBridge')
hand.add_argument('--simulate-s3', type=str, metavar='KEY',
help='invoke handler() with a synthetic S3 event for KEY')
hand.add_argument('--simulate-sns', type=str, metavar='KEY',
help='invoke handler() with a synthetic SNS-wrapped S3 event')
args = p.parse_args()
is_handler = args.run_cron or args.simulate_s3 or args.simulate_sns
if not is_handler and not any([args.id, args.batch, args.pattern,
args.s3_prefix, args.from_file, args.keys]):
p.error("provide a key selector or a handler action")
return args
def setup_logging(args):
logging.basicConfig(
format='[%(asctime)s] %(levelname)s [%(name)s:%(lineno)s] %(message)s',
level=logging.DEBUG if args.debug else logging.INFO,
force=True,
)
for noisy in ('boto3', 'botocore', 'urllib3'):
logging.getLogger(noisy).setLevel(logging.WARNING)
# ---------------------------------------------------------------------------
# Key resolution
# ---------------------------------------------------------------------------
def resolve_keys(args, *, connection=None) -> list:
"""Dispatch to the right resolver based on which selector was given."""
from ingest.settings import settings
if args.id is not None:
logger.debug(f'Resolving by id - {args.id}')
return resolve_by_id(args.id, connection=connection)
if args.batch is not None:
logger.debug(f'Resolving by batch - {args.batch}/{args.limit}')
return resolve_by_batch(args.batch, limit=args.limit,
connection=connection)
if args.pattern is not None:
logger.debug(f'Resolving by pattern - {args.pattern}/{args.limit}')
return resolve_by_pattern(args.pattern, limit=args.limit,
connection=connection)
if args.s3_prefix is not None:
logger.debug(f'Resolving by prefix - {args.prefix}/{args.limit}')
return resolve_by_prefix(
args.bucket or settings.FETCH_BUCKET,
args.s3_prefix,
limit=args.limit,
connection=connection,
)
if args.from_file is not None:
logger.debug(f'Resolving from file')
return resolve_from_file(args.from_file, connection=connection)
if args.keys:
logger.debug(f'Resolving by keys - {connection is not None}')
return resolve_by_keys(args.keys, connection=connection)
raise ValueError("no key selector provided")
# ---------------------------------------------------------------------------
# Command handlers
# ---------------------------------------------------------------------------
def cmd_run_cron(args):
"""Invoke cronhandler() directly as if triggered by EventBridge."""
from ingest.handler import cronhandler
event = {
"source": "aws.events",
"detail-type": "Scheduled Event",
}
# Optional overrides from CLI args
if args.limit != 300: # only if explicitly set
event["pipeline_limit"] = args.limit
event["realtime_limit"] = args.limit
event["metadata_limit"] = args.limit
logger.info("Invoking cronhandler")
cronhandler(event, None)
def cmd_simulate_s3(key, args, wrap_in_sns=False):
"""Invoke handler() with a synthetic S3 event for a specific key."""
from ingest.handler import handler
from ingest.settings import settings
bucket = args.bucket or settings.FETCH_BUCKET
s3_record = {
"s3": {
"bucket": {"name": bucket},
"object": {"key": key},
},
}
if wrap_in_sns:
import json
event = {
"Records": [{
"EventSource": "aws:sns",
"Sns": {
"Message": json.dumps({"Records": [s3_record]}),
},
}],
}
else:
event = {"Records": [s3_record]}
logger.info(f"Invoking handler with synthetic S3 event: {bucket}/{key}")
handler(event, None)
def cmd_dry_run(rows):
"""Print resolved keys and exit."""
print(f"{'id':>8} key")
print("-" * 60)
for row in rows:
print(f"{row[0]:>8} {row[1]}")
print(f"\n{len(rows)} key(s)")
def cmd_download(rows, output_path):
"""Download files locally, no DB interaction."""
for row in rows:
_, key, _ = row
path = deconstruct_path(key)
download_from_location(path, output_path or None)
def cmd_process(rows, args, connection = None):
"""Process rows through IngestClient."""
if connection is None:
connection = _target_connection()
resources = Resources(connection=connection)
try:
results = []
if args.batch is not None:
result = process_all(rows, resources, args)
results.append(result)
else:
for row in rows:
result = process_one(row, resources, args)
results.append(result)
if not args.no_report:
print_report(results, committed=args.commit)
if args.csv:
write_csv_report(results, args.csv)
finally:
resources.close()
def process_all(rows, resources, args) -> dict:
"""Process one fetchlog row. Returns stats dict."""
result = {
"fetchlogs_id": 0,
"key": 'all files',
"status": "ok",
"error": "",
}
start = time()
try:
client = IngestClient(resources=resources)
# when we are saving the connection we need to reset
client.reset()
client.load_keys(rows)
if args.preview:
_print_client_summary(client)
result.update(client.summary())
result["elapsed_sec"] = round(time() - start, 3)
resources.rollback()
return result
#client.dump_locations(load=not args.stage_only)
#client.dump_measurements(load=not args.stage_only)
client.dump(load=not args.stage_only)
conn = resources.connection
#[print(x) for x in client.systems.values()]
# Stats before commit/rollback (staging still visible).
elapsed = round(time() - start, 3)
result.update(client.stats(conn, elapsed))
# Diagnostics also before commit/rollback.
#if args.diagnose:
# run_diagnostics(conn, fetchlogs_id, args)
if args.commit:
resources.commit()
else:
resources.rollback()
except Exception as e:
resources.rollback()
result["status"] = "error"
result["error"] = str(e)
result["elapsed_sec"] = round(time() - start, 3)
logger.exception(f"Failed on {fetchlogs_id}: {key}")
return result
def process_one(row, resources, args) -> dict:
"""Process one fetchlog row. Returns stats dict."""
fetchlogs_id, key, last_modified = row
result = {
"fetchlogs_id": fetchlogs_id,
"key": key,
"status": "ok",
"error": "",
}
start = time()
try:
client = IngestClient(resources=resources,
fetchlogs_id=fetchlogs_id)
client.reset()
client.load_key(key, fetchlogs_id, last_modified)
if args.preview:
_print_client_summary(client)
result.update(client.summary())
result["elapsed_sec"] = round(time() - start, 3)
resources.rollback()
return result
client.dump_locations(load=not args.stage_only)
client.dump_measurements(load=not args.stage_only)
conn = resources.connection
#[print(x) for x in client.systems.values()]
# Stats before commit/rollback (staging still visible).
elapsed = round(time() - start, 3)
result.update(client.stats(conn, elapsed))
# Diagnostics also before commit/rollback.
if args.diagnose:
run_diagnostics(conn, fetchlogs_id, args)
if args.commit:
resources.commit()
else:
resources.rollback()
except Exception as e:
resources.rollback()
result["status"] = "error"
result["error"] = str(e)
result["elapsed_sec"] = round(time() - start, 3)
logger.exception(f"Failed on {fetchlogs_id}: {key}")
return result
def run_diagnostics(connection, fetchlogs_id, args):
"""Execute the requested diagnostic queries."""
from ingest.diagnostics import QUERIES, resolve_names
from ingest.utils import get_table
import os
names = [n.strip() for n in args.diagnose.split(',') if n.strip()]
try:
names = resolve_names(names)
except ValueError as e:
logger.error(str(e))
return
out_dir = args.diagnose_dir
if out_dir:
os.makedirs(out_dir, exist_ok=True)
for name in names:
spec = QUERIES[name]
sql = spec["sql"] % {"fetchlogs_id": fetchlogs_id}
filename = None
if out_dir:
filename = os.path.join(
out_dir, f"{fetchlogs_id}_{name}.csv"
)
get_table(connection, sql,
filename=filename,
head=10 if filename else None,
title=spec["title"])
# ---------------------------------------------------------------------------
# Reporting
# ---------------------------------------------------------------------------
def _print_client_summary(client):
"""Print what the client parsed (used by --preview)."""
s = client.summary()
print(f"\n{'=' * 60}")
print(f"Client contents (fetchlogs_id={client.fetchlogs_id})")
print(f"{'=' * 60}")
print(f" nodes: {s['nodes']}")
print(f" systems: {s['systems']}")
print(f" sensors: {s['sensors']}")
print(f" measurements: {s['measurements']}")
print(f" flags: {s['flags']}")
def print_report(results, *, committed):
"""Print per-key summary table + totals."""
key_width = 60
header = (f"{'ID':>8} {'Key':<60} "
f"{'Nodes M/A':>11} {'Sys M/A':>11} {'Sens M/A':>11} "
f"{'Meas':>8} {'Rej':>6} {'Time':>8} {'Status':>8}")
print("\n" + "=" * len(header))
print(header)
print("=" * len(header))
totals = {"nodes_matched": 0, "nodes_added": 0,
"systems_matched": 0, "systems_added": 0,
"sensors_matched": 0, "sensors_added": 0,
"measurements_staged": 0, "rejects": 0}
for r in results:
key_short = r["key"] if len(r["key"]) <= 60 else "..." + r["key"][-57:]
if r["status"] == "ok":
for k in totals:
totals[k] += r.get(k, 0)
print(f"{r['fetchlogs_id']:>8} {key_short:<60} "
f"{r.get('nodes_matched', 0):>4}/{r.get('nodes_added', 0):<6} "
f"{r.get('systems_matched', 0):>4}/{r.get('systems_added', 0):<6} "
f"{r.get('sensors_matched', 0):>4}/{r.get('sensors_added', 0):<6} "
f"{r.get('measurements_staged', 0):>8} "
f"{r.get('rejects', 0):>6} "
f"{r.get('elapsed_sec', 0):>7.2f}s "
f"{r['status']:>8}")
else:
print(f"{r['fetchlogs_id']:>8} {key_short:<60} "
f"ERROR: {r['error'][:70]:<70} "
f"{r.get('elapsed_sec', 0):>7.2f}s")
print("=" * len(header))
print(f"{'':>8} {'TOTALS':<60} "
f"{totals['nodes_matched']:>4}/{totals['nodes_added']:<6} "
f"{totals['systems_matched']:>4}/{totals['systems_added']:<6} "
f"{totals['sensors_matched']:>4}/{totals['sensors_added']:<6} "
f"{totals['measurements_staged']:>8} "
f"{totals['rejects']:>6}")
ok = sum(1 for r in results if r["status"] == "ok")
err = len(results) - ok
print(f"\nProcessed: {len(results)} | OK: {ok} | Errors: {err}")
if not committed:
print("(Changes rolled back — use --commit to persist)")
def write_csv_report(results, path):
fields = [
"fetchlogs_id", "key", "status",
"nodes_added", "nodes_matched", "nodes_unmatched",
"systems_added", "systems_matched", "systems_unmatched",
"sensors_added", "sensors_matched", "sensors_unmatched",
"measurements_staged", "rejects",
"client_nodes", "client_systems", "client_sensors",
"client_measurements", "client_flags",
"fetchlog_has_error", "fetchlog_message",
"elapsed_sec", "error",
]
with open(path, "w", newline="") as f:
writer = csv.DictWriter(f, fieldnames=fields, extrasaction="ignore")
writer.writeheader()
for r in results:
writer.writerow(r)
logger.info(f"Wrote CSV report to {path}")
# ---------------------------------------------------------------------------
# Environment
# ---------------------------------------------------------------------------
def _target_connection():
"""DSN for the ingest target (always the local/dev DB)."""
from ingest.settings import settings
_target_dsn = settings.DATABASE_WRITE_URL
conn = psycopg2.connect(_target_dsn)
conn.set_session(autocommit=False)
return conn
def _source_connection(args):
"""Optional read-only connection for fetchlog lookups.
Returns None if --source-db not given, meaning resolvers use the
default (target) connection.
"""
if not args.source_db:
return None
conn = psycopg2.connect(args.source_db)
conn.set_session(autocommit=True)
return conn
# ---------------------------------------------------------------------------
# Main
# ---------------------------------------------------------------------------
def main():
args = parse_args()
setup_logging(args)
run_started_at = datetime.now(timezone.utc)
logger.debug(f"Run started at {run_started_at.isoformat()}")
if args.run_cron:
cmd_run_cron(args)
return
if args.simulate_s3:
cmd_simulate_s3(args.simulate_s3, args, wrap_in_sns=False)
return
if args.simulate_sns:
cmd_simulate_s3(args.simulate_sns, args, wrap_in_sns=True)
return
if args.diagnose == 'list':
from ingest.diagnostics import list_queries
print(list_queries())
return
if args.keep_staging:
from ingest.settings import settings
settings.USE_TEMP_TABLES = False
logger.info("Using permanent staging tables (--keep-staging)")
if args.bucket:
from ingest.settings import settings
settings.FETCH_BUCKET = args.bucket
logger.info(f"Using bucket override: {args.bucket}")
conn = _source_connection(args) if args.source_db else _target_connection()
try:
rows = resolve_keys(args, connection=conn)
finally:
if args.source_db and conn is not None:
conn.close()
if not rows:
logger.warning("No keys resolved")
return
logger.info(f"Resolved {len(rows)} key(s)")
if args.dry_run:
cmd_dry_run(rows)
elif args.download is not None:
cmd_download(rows, args.download)
else:
cmd_process(rows, args, conn)
if args.commit:
conn.commit()
else:
conn.rollback()
if __name__ == '__main__':
main()