fix(scripts): skip notes for finding IDs not in active cache

If a finding ID from the CSV isn't in ivanti_findings_cache it is now
silently skipped (resolved or outdated) rather than stored. Also aborts
early with a clear message if the cache is empty, prompting the user to
run a Sync first.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-13 17:43:44 -06:00
parent fbe4333e9b
commit 8392124df5

View File

@@ -107,17 +107,24 @@ def run(args):
con.row_factory = sqlite3.Row con.row_factory = sqlite3.Row
cur = con.cursor() cur = con.cursor()
# Fetch all known finding IDs so we can warn about mismatches # Fetch all known finding IDs — only IDs present here will be processed
cur.execute('SELECT total, findings_json FROM ivanti_findings_cache WHERE id = 1') import json
cur.execute('SELECT findings_json FROM ivanti_findings_cache WHERE id = 1')
cache_row = cur.fetchone() cache_row = cur.fetchone()
known_ids = set() known_ids = set()
if cache_row and cache_row['findings_json']: if cache_row and cache_row['findings_json']:
import json
try: try:
findings = json.loads(cache_row['findings_json']) known_ids = {str(f['id']) for f in json.loads(cache_row['findings_json'])}
known_ids = {str(f['id']) for f in findings}
except Exception: 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 # ----------------------------------------------------------------- process
inserted = 0 inserted = 0
@@ -127,9 +134,10 @@ def run(args):
for finding_id, note in rows: for finding_id, note in rows:
str_id = str(finding_id) str_id = str(finding_id)
if known_ids and str_id not in known_ids: if str_id not in known_ids:
print(f' WARNING: finding ID "{str_id}" not found in current cache — ' print(f' SKIP {str_id} not in active findings (resolved or never synced)')
f'note will be stored but won\'t display until a sync pulls that finding') skipped += 1
continue
# Check if a note already exists # Check if a note already exists
cur.execute('SELECT note FROM ivanti_finding_notes WHERE finding_id = ?', (str_id,)) cur.execute('SELECT note FROM ivanti_finding_notes WHERE finding_id = ?', (str_id,))