diff --git a/ffmap/misc.py b/ffmap/misc.py index f1e153c..a999e7d 100644 --- a/ffmap/misc.py +++ b/ffmap/misc.py @@ -48,3 +48,32 @@ def neighbor_color(quality,rt_protocol): elif quality < 230: color = "#79ff7c" return color + +def defrag_table(mysql,table,sleep): + minustime=0 + allrows=0 + start_time = time.time() + + qry = "ALTER TABLE `%s` ENGINE = InnoDB" % (table) + mysql.execute(qry) + mysql.commit() + + end_time = time.time() + if sleep > 0: + time.sleep(sleep) + + writelog(CONFIG["debug_dir"] + "/deletetime.txt", "Defragmented table %s: %.3f seconds" % (table,end_time - start_time)) + print("--- Defragmented table %s: %.3f seconds ---" % (table,end_time - start_time)) + +def defrag_all(mysql,doall=False): + alltables = ('gw','gw_admin','gw_netif','hoods','netifs','router','router_events','router_gw','router_ipv6','router_neighbor','router_netif','users') + stattables = ('router_stats','router_stats_gw','router_stats_neighbor','router_stats_netif','stats_global','stats_gw','stats_hood') + + for t in alltables: + defrag_table(mysql,t,1) + + if doall: + for t in stattables: + defrag_table(mysql,t,60) + + writelog(CONFIG["debug_dir"] + "/deletetime.txt", "-------") diff --git a/scripts/defragtable.py b/scripts/defragtable.py new file mode 100755 index 0000000..9a96725 --- /dev/null +++ b/scripts/defragtable.py @@ -0,0 +1,24 @@ +#!/usr/bin/python3 + +# Execute manually + +import os +import sys +sys.path.insert(0, os.path.abspath(os.path.dirname(__file__) + '/' + '..')) + +from ffmap.misc import defrag_table, writelog +from ffmap.config import CONFIG +from ffmap.mysqltools import FreifunkMySQL + +import time +start_time = time.time() + +mysql = FreifunkMySQL() +i = 1 +while i < len(sys.argv): + defrag_table(mysql,sys.argv[i],1) + i = i + 1 +mysql.close() + +writelog(CONFIG["debug_dir"] + "/deletetime.txt", "-------") +print("--- Total defrag duration: %.3f seconds ---" % (time.time() - start_time)) diff --git a/scripts/defragtables.py b/scripts/defragtables.py new file mode 100755 index 0000000..9cc09f6 --- /dev/null +++ b/scripts/defragtables.py @@ -0,0 +1,22 @@ +#!/usr/bin/python3 + +# Execute manually + +import os +import sys +sys.path.insert(0, os.path.abspath(os.path.dirname(__file__) + '/' + '..')) + +from ffmap.misc import defrag_all +from ffmap.mysqltools import FreifunkMySQL + +import time +start_time = time.time() + +mysql = FreifunkMySQL() +if(len(sys.argv)>1): + defrag_all(mysql,sys.argv[1]) +else: + defrag_all(mysql,False) +mysql.close() + +print("--- Total defrag duration: %.3f seconds ---" % (time.time() - start_time))