Speed up bulk satellite import by using executemany in a single transaction

This commit is contained in:
James Smith
2026-03-18 14:43:21 +00:00
parent 8f93340b9d
commit fa3c7cbd24
+30 -21
View File
@@ -2336,28 +2336,37 @@ def add_tracked_satellite(
def bulk_add_tracked_satellites(satellites_list: list[dict]) -> int: def bulk_add_tracked_satellites(satellites_list: list[dict]) -> int:
"""Insert many tracked satellites at once. Returns count of newly inserted.""" """Insert many tracked satellites at once. Returns count of newly inserted."""
added = 0 if not satellites_list:
return 0
rows = []
for sat in satellites_list:
try:
rows.append((
str(sat['norad_id']),
sat['name'],
sat.get('tle_line1'),
sat.get('tle_line2'),
int(sat.get('enabled', True)),
int(sat.get('builtin', False)),
))
except (KeyError, TypeError) as e:
logger.warning(f"Skipping malformed satellite entry: {e}")
norad_ids = [r[0] for r in rows]
placeholders = ','.join('?' * len(norad_ids))
count_sql = f'SELECT COUNT(*) FROM tracked_satellites WHERE norad_id IN ({placeholders})'
with get_db() as conn: with get_db() as conn:
for sat in satellites_list: before = conn.execute(count_sql, norad_ids).fetchone()[0]
try: conn.executemany(
cursor = conn.execute( 'INSERT OR IGNORE INTO tracked_satellites '
'INSERT OR IGNORE INTO tracked_satellites ' '(norad_id, name, tle_line1, tle_line2, enabled, builtin) '
'(norad_id, name, tle_line1, tle_line2, enabled, builtin) ' 'VALUES (?, ?, ?, ?, ?, ?)',
'VALUES (?, ?, ?, ?, ?, ?)', rows,
( )
str(sat['norad_id']), after = conn.execute(count_sql, norad_ids).fetchone()[0]
sat['name'], return after - before
sat.get('tle_line1'),
sat.get('tle_line2'),
int(sat.get('enabled', True)),
int(sat.get('builtin', False)),
),
)
if cursor.rowcount > 0:
added += 1
except (sqlite3.Error, KeyError) as e:
logger.warning(f"Error bulk-adding satellite: {e}")
return added
def update_tracked_satellite(norad_id: str, enabled: bool) -> bool: def update_tracked_satellite(norad_id: str, enabled: bool) -> bool: