SeaCogs/speedtest/models.py

76 lines
1.5 KiB
Python

from datetime import datetime
from pydantic import BaseModel
class Speedtest(BaseModel):
type: str
timestamp: datetime
ping: "Ping"
download: "Bandwidth"
upload: "Bandwidth"
isp: str
interface: "Interface"
server: "Server"
result: "Result"
@classmethod
def from_json(cls, data: dict) -> "Speedtest":
return cls(
type=data["type"],
timestamp=datetime.fromisoformat(data["timestamp"]),
ping=Ping(**data["ping"]),
download=Bandwidth(**data["download"]),
upload=Bandwidth(**data["upload"]),
isp=data["isp"],
interface=Interface(**data["interface"]),
server=Server(**data["server"]),
result=Result(**data["result"])
)
class Bandwidth(BaseModel):
bandwidth: float
bytes: int
elapsed: int
latency: "Latency"
@property
def mbps(self) -> float:
return self.bandwidth / 1_000_000
class Latency(BaseModel):
iqm: float
low: float
high: float
jitter: float
class Interface(BaseModel):
internalIp: str
name: str
macAddr: str
isVpn: bool
externalIp: str
class Ping(BaseModel):
jitter: float
latency: float
low: float
high: float
class Server(BaseModel):
id: int
name: str
location: str
country: str
host: str
port: int
ip: str
class Result(BaseModel):
id: str
url: str
persisted: bool
@property
def image(self) -> str:
return self.url + ".png"