Skip to content

Commit

Permalink
Number of bugfixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Ketchel committed Dec 3, 2024
1 parent 7c605b6 commit 77bd5c5
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 10 deletions.
3 changes: 2 additions & 1 deletion wlanpi_core/api/api_v1/endpoints/utils_api.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json
import logging
from typing import Union

from fastapi import APIRouter, Response

Expand Down Expand Up @@ -122,7 +123,7 @@ async def ufw_information():
return Response(content=f"Internal Server Error", status_code=500)


@router.post("/ping", response_model=utils.PingResult)
@router.post("/ping", response_model=Union[utils.PingResult, utils.PingFailure])
async def execute_ping(request: utils.PingRequest):
"""
Pings a target and returns the results
Expand Down
1 change: 1 addition & 0 deletions wlanpi_core/schemas/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
Iperf3ClientRequest,
PingRequest,
PingResponse,
PingFailure,
PingResult,
ReachabilityTest,
SpeedTest,
Expand Down
13 changes: 9 additions & 4 deletions wlanpi_core/schemas/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,19 @@ class PingResult(
packet_loss_percent: float = Field(examples=[0.0])
duplicates: int = Field(examples=[0])
time_ms: float = Field(examples=[9012.0])
round_trip_ms_min: float = Field(examples=[24.108])
round_trip_ms_avg: float = Field(examples=[29.318])
round_trip_ms_max: float = Field(examples=[37.001])
round_trip_ms_stddev: float = Field(examples=[4.496])
round_trip_ms_min: Optional[float] = Field(examples=[24.108])
round_trip_ms_avg: Optional[float] = Field(examples=[29.318])
round_trip_ms_max: Optional[float] = Field(examples=[37.001])
round_trip_ms_stddev: Optional[float] = Field(examples=[4.496])
jitter: Optional[float] = Field(examples=[37.001], default=None)
responses: list[PingResponse] = Field()


class PingFailure(BaseModel):
destination: str = Field(examples=["google.com"])
message: str = Field(examples=["No route to host"])


class Iperf3ClientRequest(BaseModel):
host: str = Field(examples=["192.168.1.1"])
port: int = Field(examples=[5001], default=5001)
Expand Down
8 changes: 5 additions & 3 deletions wlanpi_core/services/utils_service.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import os
import re
import time
from typing import Optional
from typing import Optional, Union

from wlanpi_core.constants import UFW_FILE
from wlanpi_core.models.runcommand_error import RunCommandError
from wlanpi_core.schemas.utils import PingResult
from wlanpi_core.schemas.utils import PingResult, PingFailure
from wlanpi_core.utils.general import run_command_async
from wlanpi_core.utils.network import get_default_gateways, get_ip_address

Expand Down Expand Up @@ -210,7 +210,7 @@ async def ping(
interval: float = 1,
ttl: Optional[int] = None,
interface: Optional[str] = None,
) -> PingResult:
) -> Union[PingResult, PingFailure]:
def calculate_jitter(values: list[float], precision: int = 3) -> float:
return round(
sum([abs(values[i + 1] - values[i]) for i in range(len(values) - 1)])
Expand All @@ -227,6 +227,8 @@ def calculate_jitter(values: list[float], precision: int = 3) -> float:
command.append(host)
res = await run_command_async(command)
result: dict = res.output_from_json() # type: ignore
if not result:
return PingFailure(message=res.stderr, destination=host)
# Calculate jitter if we can
result["jitter"] = (
calculate_jitter([x["time_ms"] for x in result["responses"]])
Expand Down
4 changes: 2 additions & 2 deletions wlanpi_core/utils/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -383,10 +383,10 @@ def get_interface_details(
iw_list_data = parse_iw_list(run_command(["iw", "list"]).stdout.split("\n"))

return {
get_phy_interface_name(k.split(" ")[1].split("phy")[1]): {
get_phy_interface_name(k.split(" ")[1].split("phy")[1]).split()[0]: {
"phy_name": k.split(" ")[1],
"mac": get_interface_mac(
get_phy_interface_name(k.split(" ")[1].split("phy")[1])
get_phy_interface_name(k.split(" ")[1].split("phy")[1]).split()[0]
),
"details": v,
}
Expand Down

0 comments on commit 77bd5c5

Please sign in to comment.