refactor redundant joins, bugfix event insert

This commit is contained in:
johnwang16
2024-10-19 21:24:51 -04:00
parent 9d1fccfe29
commit 400edd35d1
4 changed files with 7 additions and 10 deletions

View File

@@ -549,7 +549,8 @@ class DB():
cur_SSID STRING(250), cur_SSID STRING(250),
cur_NetworkNodeMAC STRING(250), cur_NetworkNodeMAC STRING(250),
cur_PORT STRING(250), cur_PORT STRING(250),
cur_Type STRING(250) cur_Type STRING(250),
UNIQUE(cur_MAC)
); );
""") """)

View File

@@ -93,10 +93,7 @@ def save_scanned_devices (db):
# Proceed if variable contains valid MAC # Proceed if variable contains valid MAC
if check_mac_or_internet(local_mac): if check_mac_or_internet(local_mac):
# Check if local mac has been detected with other methods sql.execute (f"""INSERT OR IGNORE INTO CurrentScan (cur_MAC, cur_IP, cur_Vendor, cur_ScanMethod) VALUES ( '{local_mac}', '{local_ip}', Null, 'local_MAC') """)
sql.execute (f"SELECT COUNT(*) FROM CurrentScan WHERE cur_MAC = '{local_mac}'")
if sql.fetchone()[0] == 0 :
sql.execute (f"""INSERT INTO CurrentScan (cur_MAC, cur_IP, cur_Vendor, cur_ScanMethod) VALUES ( '{local_mac}', '{local_ip}', Null, 'local_MAC') """)
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
def print_scan_stats(db): def print_scan_stats(db):
@@ -769,4 +766,4 @@ def guess_type(vendor, mac, ip, name, default):
result = "Router" result = "Router"
return result return result

View File

@@ -207,9 +207,8 @@ def insert_events (db):
'', '',
1 1
FROM CurrentScan AS c FROM CurrentScan AS c
LEFT JOIN LatestEventsPerMAC AS d ON d.dev_MAC = c.cur_MAC LEFT JOIN LatestEventsPerMAC AS last_event ON c.cur_MAC = last_event.eve_MAC
LEFT JOIN LatestEventsPerMAC AS last_event ON d.dev_MAC = last_event.eve_MAC WHERE last_event.dev_PresentLastScan = 0 OR last_event.eve_MAC IS NULL
WHERE d.dev_PresentLastScan = 0 OR d.dev_MAC IS NULL
""") """)
# Check disconnections # Check disconnections

View File

@@ -747,7 +747,7 @@ def process_plugin_events(db, plugin, pluginsState, plugEventsArr):
sqlParams.append(tuple(tmpList)) sqlParams.append(tuple(tmpList))
# Generate the SQL INSERT query using the collected information. # Generate the SQL INSERT query using the collected information.
q = f'INSERT into {dbTable} ({columnsStr}) VALUES ({valuesStr})' q = f'INSERT OR IGNORE INTO {dbTable} ({columnsStr}) VALUES ({valuesStr})'
# Log a debug message showing the generated SQL query for mapping. # Log a debug message showing the generated SQL query for mapping.
mylog('debug', ['[Plugins] SQL query for mapping: ', q]) mylog('debug', ['[Plugins] SQL query for mapping: ', q])