scripts: Add deleteunlinked.py

Signed-off-by: Adrian Schmutzler <freifunk@adrianschmutzler.de>
This commit is contained in:
Adrian Schmutzler 2018-01-13 00:00:27 +01:00
parent c605ffe2f0
commit 823858d981
3 changed files with 58 additions and 0 deletions

View File

@ -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()

View File

@ -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()

21
scripts/deleteunlinked.py Executable file
View File

@ -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))