api/alfred: Fix check for existing router and delete fragments

The router id "router_id" is only evaluated based on the
router_netif table. If the corresponding entry in the router
table is missing, an error occurs.

To deal with that, we now use the "olddata" variable for ifs,
which is initialized based on the router table. If nothing is
found there, we trigger delete_router to get rid of fragments
in other tables.

The latter is necessary, as we identify routers by MAC addresses
and thus old entries will keep to be a problem if just a new
entry is added to router_netif.

Signed-off-by: Adrian Schmutzler <freifunk@adrianschmutzler.de>
This commit is contained in:
Adrian Schmutzler 2018-06-20 13:43:41 +02:00
parent 6d492a3a25
commit cf3517d9d2
1 changed files with 10 additions and 8 deletions

View File

@ -80,13 +80,15 @@ def import_nodewatcher_xml(mysql, mac, xml, banned, netifdict, statstime):
olddata = mysql.findone("SELECT sys_uptime, sys_time, firmware, hostname, hood, status, lat, lng, contact, description, position_comment, w2_active, w2_busy, w5_active, w5_busy FROM router WHERE id = %s LIMIT 1",(router_id,))
if olddata:
uptime = olddata["sys_uptime"]
# Filter old data (Alfred keeps data for 10 min.; old and new can mix if gateways do not sync)
# We only use data where system time is bigger than before (last entry) or more than 1 hour smaller (to catch cases without timeserver)
newtime = router_update["sys_time"].timestamp()
oldtime = olddata["sys_time"].timestamp()
if not (newtime > oldtime or newtime < (oldtime - 3600)):
return
# Filter old data (Alfred keeps data for 10 min.; old and new can mix if gateways do not sync)
# We only use data where system time is bigger than before (last entry) or more than 1 hour smaller (to catch cases without timeserver)
newtime = router_update["sys_time"].timestamp()
oldtime = olddata["sys_time"].timestamp()
if not (newtime > oldtime or newtime < (oldtime - 3600)):
return
else:
delete_router(mysql,router_id)
# keep hood up to date
if not router_update["hood"]:
@ -152,7 +154,7 @@ def import_nodewatcher_xml(mysql, mac, xml, banned, netifdict, statstime):
else:
router_update["w5_airtime"] = 0
if router_id:
if olddata:
# statistics
calculate_network_io(mysql, router_id, uptime, router_update)
ru = router_update