core-extra/daemon/core/gui/graph/linkinfo.py

153 lines
5.1 KiB
Python
Raw Normal View History

2019-10-19 00:42:00 +01:00
"""
Link information, such as IPv4, IPv6 and throughput drawn in the canvas
"""
2019-12-06 20:46:00 +00:00
from core.api.grpc import core_pb2
2019-10-19 00:42:00 +01:00
class Throughput:
def __init__(self, canvas, core):
2019-10-19 00:42:00 +01:00
self.canvas = canvas
self.core = core
2019-10-19 00:42:00 +01:00
# edge canvas id mapped to throughput value
self.tracker = {}
# map an edge canvas id to a throughput canvas id
self.map = {}
2019-12-06 20:46:00 +00:00
# map edge canvas id to token
2019-10-22 21:17:47 +01:00
self.edge_id_to_token = {}
2019-10-19 00:42:00 +01:00
def load_throughput_info(self, interface_throughputs):
"""
load all interface throughouts from an event
:param repeated core_bp2.InterfaceThroughputinterface_throughputs: interface
throughputs
2019-10-19 00:42:00 +01:00
:return: nothing
"""
2019-12-06 20:46:00 +00:00
for throughput in interface_throughputs:
nid = throughput.node_id
iid = throughput.interface_id
tp = throughput.throughput
token = self.core.interface_to_edge.get((nid, iid))
if token:
edge = self.canvas.edges.get(token)
if edge:
edge_id = edge.id
self.edge_id_to_token[edge_id] = token
if edge_id not in self.tracker:
self.tracker[edge_id] = tp
else:
temp = self.tracker[edge_id]
self.tracker[edge_id] = (temp + tp) / 2
else:
self.core.interface_to_edge.pop((nid, iid), None)
2019-10-19 00:42:00 +01:00
2019-10-22 21:17:47 +01:00
def edge_is_wired(self, token):
"""
determine whether link is a WIRED link
:param token:
:return:
"""
canvas_edge = self.canvas.edges[token]
canvas_src_id = canvas_edge.src
canvas_dst_id = canvas_edge.dst
2019-12-06 20:46:00 +00:00
src = self.canvas.nodes[canvas_src_id].core_node
dst = self.canvas.nodes[canvas_dst_id].core_node
return not (
src.type == core_pb2.NodeType.WIRELESS_LAN
and dst.model == "mdr"
or src.model == "mdr"
and dst.type == core_pb2.NodeType.WIRELESS_LAN
)
2019-10-22 21:17:47 +01:00
def draw_wired_throughput(self, edge_id):
2019-12-06 20:46:00 +00:00
x0, y0, x1, y1 = self.canvas.coords(edge_id)
x = (x0 + x1) / 2
y = (y0 + y1) / 2
2019-10-22 21:17:47 +01:00
if edge_id not in self.map:
2019-12-06 20:46:00 +00:00
tpid = self.canvas.create_text(
x,
y,
tags="throughput",
font=("Arial", 8),
text="{0:.3f} kbps".format(0.001 * self.tracker[edge_id]),
2019-10-22 21:17:47 +01:00
)
2019-12-06 20:46:00 +00:00
self.map[edge_id] = tpid
2019-10-22 21:17:47 +01:00
else:
2019-12-06 20:46:00 +00:00
tpid = self.map[edge_id]
self.canvas.coords(tpid, x, y)
2019-10-22 21:17:47 +01:00
self.canvas.itemconfig(
2019-12-06 20:46:00 +00:00
tpid, text="{0:.3f} kbps".format(0.001 * self.tracker[edge_id])
2019-10-22 21:17:47 +01:00
)
def draw_wireless_throughput(self, edge_id):
token = self.edge_id_to_token[edge_id]
canvas_edge = self.canvas.edges[token]
canvas_src_id = canvas_edge.src
canvas_dst_id = canvas_edge.dst
src_node = self.canvas.nodes[canvas_src_id]
dst_node = self.canvas.nodes[canvas_dst_id]
2019-12-06 20:46:00 +00:00
not_wlan = (
dst_node
if src_node.core_node.type == core_pb2.NodeType.WIRELESS_LAN
else src_node
)
2019-10-22 21:17:47 +01:00
2019-12-06 20:46:00 +00:00
x, y = self.canvas.coords(not_wlan.id)
2019-10-22 21:17:47 +01:00
if edge_id not in self.map:
tp_id = self.canvas.create_text(
x + 50,
y + 25,
2019-12-06 20:46:00 +00:00
font=("Arial", 8),
tags="throughput",
2019-10-22 21:17:47 +01:00
text="{0:.3f} kbps".format(0.001 * self.tracker[edge_id]),
)
self.map[edge_id] = tp_id
# redraw throughput
else:
self.canvas.itemconfig(
self.map[edge_id],
text="{0:.3f} kbps".format(0.001 * self.tracker[edge_id]),
)
2019-10-19 00:42:00 +01:00
def draw_throughputs(self):
for edge_id in self.tracker:
2019-10-22 21:17:47 +01:00
if self.edge_is_wired(self.edge_id_to_token[edge_id]):
self.draw_wired_throughput(edge_id)
2019-10-19 00:42:00 +01:00
else:
2019-10-22 21:17:47 +01:00
self.draw_wireless_throughput(edge_id)
2019-10-19 00:42:00 +01:00
def process_grpc_throughput_event(self, interface_throughputs):
self.load_throughput_info(interface_throughputs)
self.draw_throughputs()
2019-12-06 20:46:00 +00:00
def move(self, edge):
tpid = self.map.get(edge.id)
if tpid:
if self.edge_is_wired(edge.token):
x0, y0, x1, y1 = self.canvas.coords(edge.id)
self.canvas.coords(tpid, (x0 + x1) / 2, (y0 + y1) / 2)
2019-10-22 21:17:47 +01:00
else:
2019-12-06 20:46:00 +00:00
if (
self.canvas.nodes[edge.src].core_node.type
== core_pb2.NodeType.WIRELESS_LAN
):
x, y = self.canvas.coords(edge.dst)
self.canvas.coords(tpid, x + 50, y + 20)
else:
x, y = self.canvas.coords(edge.src)
self.canvas.coords(tpid, x + 50, y + 25)
def delete(self, edge):
tpid = self.map.get(edge.id)
if tpid:
eid = edge.id
self.canvas.delete(tpid)
self.tracker.pop(eid)
self.map.pop(eid)
self.edge_id_to_token.pop(eid)