This commit is contained in:
2024-06-02 19:38:39 +03:00
parent c4244e86e6
commit 2048f9c57d
3 changed files with 222 additions and 128 deletions

View File

@ -8,6 +8,8 @@ from .db import Database
def parse_traceroute_output(data: str, origin: str):
# TODO: data validation
lines = data.strip().split("\n")
target = lines[0].split()[2]
@ -15,21 +17,26 @@ def parse_traceroute_output(data: str, origin: str):
trace = {"target": target, "created": created, "origin": origin, "hops": []}
prev_latency = 0
prev_latency = None
for line in lines[1:]:
hop_info = line.split()
print("LINE:", hop_info)
try:
# Regular lines.
number, name, ip, latency, _ = hop_info
latency = float(latency)
hop = {
"created": created,
"number": number,
"name": name,
"ip": ip.strip("()"),
"latency": float(latency),
"latency": latency,
"link_latency": round(latency if prev_latency else latency, 3),
}
prev_latency = latency
except ValueError:
# Asterisks, no data found for hop.
number, name = hop_info
hop = {
"created": created,
@ -37,6 +44,7 @@ def parse_traceroute_output(data: str, origin: str):
"name": name,
"ip": None,
"latency": None,
"link_latency": "?",
}
trace["hops"].append(hop)