stattools: Improve code for calculation of global/hood/gw stats

This replaces UPDATE IF SELECT ELSE INSERT by ON DUPLICATE KEY
UPDATE. In addition, queries have been consolidated by ordering
data in advance and then using executemany.

Signed-off-by: Adrian Schmutzler <freifunk@adrianschmutzler.de>
This commit is contained in:
Adrian Schmutzler 2018-01-11 14:57:50 +01:00
parent 2a515b94c7
commit 516eb07924

View File

@ -214,19 +214,16 @@ def record_global_stats(mysql):
time = mysql.utctimestamp() time = mysql.utctimestamp()
status = router_status(mysql) status = router_status(mysql)
old = mysql.findone("SELECT time FROM stats_global WHERE time = %s LIMIT 1",(time,)) mysql.execute("""
INSERT INTO stats_global (time, clients, online, offline, unknown, orphaned)
if old: VALUES (%s, %s, %s, %s, %s, %s)
mysql.execute(""" ON DUPLICATE KEY UPDATE
UPDATE stats_global clients=VALUES(clients),
SET clients = %s, online = %s, offline = %s, unknown = %s, orphaned = %s online=VALUES(online),
WHERE time = %s offline=VALUES(offline),
""",(total_clients(mysql),status.get("online",0),status.get("offline",0),status.get("unknown",0),status.get("orphaned",0),time,)) unknown=VALUES(unknown),
else: orphaned=VALUES(orphaned)
mysql.execute(""" """,(time,total_clients(mysql),status.get("online",0),status.get("offline",0),status.get("unknown",0),status.get("orphaned",0),))
INSERT INTO stats_global (time, clients, online, offline, unknown, orphaned)
VALUES (%s, %s, %s, %s, %s, %s)
""",(time,total_clients(mysql),status.get("online",0),status.get("offline",0),status.get("unknown",0),status.get("orphaned",0),))
mysql.execute(""" mysql.execute("""
DELETE FROM stats_global DELETE FROM stats_global
@ -241,23 +238,22 @@ def record_hood_stats(mysql):
status = router_status_hood(mysql) status = router_status_hood(mysql)
clients = total_clients_hood(mysql) clients = total_clients_hood(mysql)
hdata = []
for hood in clients.keys(): for hood in clients.keys():
if not hood: if not hood:
hood = "Default" hood = "Default"
hdata.append((time,hood,clients[hood],status[hood].get("online",0),status[hood].get("offline",0),status[hood].get("unknown",0),status[hood].get("orphaned",0),))
old = mysql.findone("SELECT time FROM stats_hood WHERE time = %s AND hood = %s LIMIT 1",(time,hood,))
mysql.executemany("""
if old: INSERT INTO stats_hood (time, hood, clients, online, offline, unknown, orphaned)
mysql.execute(""" VALUES (%s, %s, %s, %s, %s, %s, %s)
UPDATE stats_hood ON DUPLICATE KEY UPDATE
SET clients = %s, online = %s, offline = %s, unknown = %s, orphaned = %s clients=VALUES(clients),
WHERE time = %s AND hood = %s online=VALUES(online),
""",(clients[hood],status[hood].get("online",0),status[hood].get("offline",0),status[hood].get("unknown",0),status[hood].get("orphaned",0),time,hood,)) offline=VALUES(offline),
else: unknown=VALUES(unknown),
mysql.execute(""" orphaned=VALUES(orphaned)
INSERT INTO stats_hood (time, hood, clients, online, offline, unknown, orphaned) """,hdata)
VALUES (%s, %s, %s, %s, %s, %s, %s)
""",(time,hood,clients[hood],status[hood].get("online",0),status[hood].get("offline",0),status[hood].get("unknown",0),status[hood].get("orphaned",0),))
mysql.execute(""" mysql.execute("""
DELETE FROM stats_hood DELETE FROM stats_hood
@ -272,20 +268,20 @@ def record_gw_stats(mysql):
status = router_status_gw(mysql) status = router_status_gw(mysql)
clients = total_clients_gw(mysql) clients = total_clients_gw(mysql)
gdata = []
for mac in clients.keys(): for mac in clients.keys():
old = mysql.findone("SELECT time FROM stats_gw WHERE time = %s AND mac = %s LIMIT 1",(time,mac,)) gdata.append((time,mac,clients[mac],status[mac].get("online",0),status[mac].get("offline",0),status[mac].get("unknown",0),status[mac].get("orphaned",0),))
if old: mysql.executemany("""
mysql.execute(""" INSERT INTO stats_gw (time, mac, clients, online, offline, unknown, orphaned)
UPDATE stats_gw VALUES (%s, %s, %s, %s, %s, %s, %s)
SET clients = %s, online = %s, offline = %s, unknown = %s, orphaned = %s ON DUPLICATE KEY UPDATE
WHERE time = %s AND mac = %s clients=VALUES(clients),
""",(clients[mac],status[mac].get("online",0),status[mac].get("offline",0),status[mac].get("unknown",0),status[mac].get("orphaned",0),time,mac,)) online=VALUES(online),
else: offline=VALUES(offline),
mysql.execute(""" unknown=VALUES(unknown),
INSERT INTO stats_gw (time, mac, clients, online, offline, unknown, orphaned) orphaned=VALUES(orphaned)
VALUES (%s, %s, %s, %s, %s, %s, %s) """,gdata)
""",(time,mac,clients[mac],status[mac].get("online",0),status[mac].get("offline",0),status[mac].get("unknown",0),status[mac].get("orphaned",0),))
mysql.execute(""" mysql.execute("""
DELETE FROM stats_gw DELETE FROM stats_gw