diff --git a/ffmap/gwtools.py b/ffmap/gwtools.py index c71fe74..dd406a1 100644 --- a/ffmap/gwtools.py +++ b/ffmap/gwtools.py @@ -76,3 +76,19 @@ def gw_bat(gw): else: s = "---" return s + +def delete_unlinked_gws(mysql): + # Delete entries in gw_* tables without corresponding gw in master table + + tables = ["gw_admin","gw_netif"] + + for t in tables: + start_time = time.time() + mysql.execute(""" + DELETE d FROM {} AS d + LEFT JOIN gw AS g ON g.id = d.gw + WHERE g.id IS NULL + """.format(t)) + print("--- Deleted %i rows from %s: %.3f seconds ---" % (mysql.cursor().rowcount,t,time.time() - start_time)) + mysql.commit() + diff --git a/ffmap/routertools.py b/ffmap/routertools.py index c60a56c..5bae737 100644 --- a/ffmap/routertools.py +++ b/ffmap/routertools.py @@ -364,6 +364,27 @@ def delete_orphaned_routers(mysql): """,(threshold,)) mysql.commit() +def delete_unlinked_routers(mysql): + # Delete entries in router_* tables without corresponding router in master table + + tables = ["router_events","router_gw","router_ipv6","router_neighbor","router_netif","router_stats","router_stats_gw","router_stats_neighbor","router_stats_netif"] + + for t in tables: + start_time = time.time() + mysql.execute(""" + DELETE d FROM {} AS d + LEFT JOIN router AS r ON r.id = d.router + WHERE r.id IS NULL + """.format(t)) + #mysql.execute(""" + # DELETE FROM {} + # WHERE {}.router NOT IN ( + # SELECT id FROM router + # ) + #""".format(t,t)) + print("--- Deleted %i rows from %s: %.3f seconds ---" % (mysql.cursor().rowcount,t,time.time() - start_time)) + mysql.commit() + def delete_old_stats(mysql): threshold=(utcnow() - datetime.timedelta(days=CONFIG["router_stat_days"])).timestamp() diff --git a/scripts/deleteunlinked.py b/scripts/deleteunlinked.py new file mode 100755 index 0000000..cbc5a77 --- /dev/null +++ b/scripts/deleteunlinked.py @@ -0,0 +1,21 @@ +#!/usr/bin/python3 + +# Deletes unlinked rows from gw_* and router_* tables + +import os +import sys +sys.path.insert(0, os.path.abspath(os.path.dirname(__file__) + '/' + '..')) + +from ffmap.routertools import delete_unlinked_routers +from ffmap.gwtools import delete_unlinked_gws +from ffmap.mysqltools import FreifunkMySQL + +import time +start_time = time.time() + +mysql = FreifunkMySQL() +delete_unlinked_routers(mysql) +delete_unlinked_gws(mysql) +mysql.close() + +print("\n--- Total duration: %.3f seconds ---\n" % (time.time() - start_time))