diff --git a/backend/scripts/import_notes_from_csv.py b/backend/scripts/import_notes_from_csv.py index b7689a2..6ffe9f6 100644 --- a/backend/scripts/import_notes_from_csv.py +++ b/backend/scripts/import_notes_from_csv.py @@ -107,17 +107,24 @@ def run(args): con.row_factory = sqlite3.Row cur = con.cursor() - # Fetch all known finding IDs so we can warn about mismatches - cur.execute('SELECT total, findings_json FROM ivanti_findings_cache WHERE id = 1') + # Fetch all known finding IDs — only IDs present here will be processed + import json + cur.execute('SELECT findings_json FROM ivanti_findings_cache WHERE id = 1') cache_row = cur.fetchone() known_ids = set() if cache_row and cache_row['findings_json']: - import json try: - findings = json.loads(cache_row['findings_json']) - known_ids = {str(f['id']) for f in findings} + known_ids = {str(f['id']) for f in json.loads(cache_row['findings_json'])} except Exception: - pass # non-fatal — we'll still import and just skip the warning + pass + + if not known_ids: + print('ERROR: No findings found in the database cache.') + print(' Run a Sync from the dashboard first, then re-run this script.') + con.close() + sys.exit(1) + + print(f'{len(known_ids)} active finding(s) in cache.\n') # ----------------------------------------------------------------- process inserted = 0 @@ -127,9 +134,10 @@ def run(args): for finding_id, note in rows: str_id = str(finding_id) - if known_ids and str_id not in known_ids: - print(f' WARNING: finding ID "{str_id}" not found in current cache — ' - f'note will be stored but won\'t display until a sync pulls that finding') + if str_id not in known_ids: + print(f' SKIP {str_id} — not in active findings (resolved or never synced)') + skipped += 1 + continue # Check if a note already exists cur.execute('SELECT note FROM ivanti_finding_notes WHERE finding_id = ?', (str_id,))