Source code for aiopnsense.traffic

"""Diagnostics traffic methods for OPNsenseClient."""

from __future__ import annotations

from collections.abc import AsyncIterator, Mapping
import logging
from typing import Any

from ._error_policy import _attempt
from ._typing import AiopnsenseClientProtocol
from .client_transport import _STREAM_JSON_EVENT_RESET_KEY
from .const import DEFAULT_REQUEST_TIMEOUT_SECONDS
from .exceptions import OPNsenseError
from .helpers import _LOGGER, try_to_float, try_to_int

DIAGNOSTICS_TRAFFIC_ENDPOINT = "/api/diagnostics/traffic/interface"
DIAGNOSTICS_TRAFFIC_STREAM_ENDPOINT_PREFIX = "/api/diagnostics/traffic/stream"

_INTERFACE_FIELD_ALIASES: dict[str, tuple[str, ...]] = {
    "rx_bytes": ("rx_bytes", "inbytes", "bytes received", "bytes_received"),
    "tx_bytes": ("tx_bytes", "outbytes", "bytes transmitted", "bytes_transmitted"),
    "rx_packets": ("rx_packets", "inpkts", "packets received", "packets_received"),
    "tx_packets": ("tx_packets", "outpkts", "packets transmitted", "packets_transmitted"),
    "rx_errors": ("rx_errors", "inerrs", "input errors", "input_errors"),
    "tx_errors": ("tx_errors", "outerrs", "output errors", "output_errors"),
    "collisions": ("collisions",),
}


def _coalesce_identity(
    value: object | None,
    *,
    fallback: str,
    description: object | None = None,
) -> str:
    """Return a usable interface identity value with safe fallbacks.

    Args:
        value (object | None): Primary identity value candidate.
        fallback (str): Fallback identity to return when all candidates are empty.
        description (object | None): Secondary identity candidate from description.

    Returns:
        str: Normalized interface identity.
    """
    if isinstance(value, str):
        candidate = value.strip()
        if candidate:
            return candidate

    if isinstance(description, str):
        candidate = description.strip()
        if candidate:
            return candidate

    return fallback


def _first_int(row: Mapping[str, Any], aliases: tuple[str, ...]) -> int | None:
    """Return the first parseable integer from a row for the supplied aliases.

    Args:
        row (Mapping[str, Any]): Mapping that may contain alias keys.
        aliases (tuple[str, ...]): Candidate keys to inspect in order.

    Returns:
        int | None: First parseable integer value or ``None``.
    """
    for alias in aliases:
        value = try_to_int(row.get(alias))
        if value is not None:
            return value
    return None


def _source_interfaces(payload: Mapping[str, Any]) -> Mapping[str, Mapping[str, Any]]:
    """Return the interface mapping from supported OPNsense traffic payload shapes.

    Args:
        payload (Mapping[str, Any]): Raw OPNsense payload.

    Returns:
        Mapping[str, Mapping[str, Any]]: Normalized interface-name keyed mapping.
    """
    interfaces = payload.get("interfaces")
    if isinstance(interfaces, Mapping):
        return interfaces
    return {
        key: value for key, value in payload.items() if key != "time" and isinstance(value, Mapping)
    }


def normalize_traffic_payload(
    payload: Mapping[str, Any],
    *,
    interval: float,
    include_per_second_rates: bool = True,
) -> dict[str, Any]:
    """Normalize a raw OPNsense diagnostics traffic payload.

    Args:
        payload (Mapping[str, Any]): Raw diagnostics payload containing interface
            traffic counters.
        interval (float): Elapsed sample interval used to calculate per-second
            rates; non-positive values use one second.
        include_per_second_rates (bool): Whether to add derived per-second rate
            fields. When ``False``, only normalized counters are returned.

    Returns:
        dict[str, Any]: Normalized traffic sample with an ``interfaces`` mapping keyed by interface name.
    """
    sample_interval = interval if interval > 0 else 1.0
    sample_time = try_to_float(payload.get("time"))
    normalized: dict[str, Any] = {
        "time": sample_time,
        "interfaces": {},
    }

    for interface_name, row in _source_interfaces(payload).items():
        if not isinstance(interface_name, str) or not isinstance(row, Mapping):
            continue
        normalized_row: dict[str, Any] = {
            "interface": _coalesce_identity(row.get("interface"), fallback=interface_name),
            "name": _coalesce_identity(
                row.get("name"),
                fallback=interface_name,
                description=row.get("description"),
            ),
        }
        for normalized_name, aliases in _INTERFACE_FIELD_ALIASES.items():
            value = _first_int(row, aliases)
            if include_per_second_rates and value is not None and value < 0:
                continue
            if value is not None:
                normalized_row[normalized_name] = value

        rx_bytes = normalized_row.get("rx_bytes")
        tx_bytes = normalized_row.get("tx_bytes")
        rx_packets = normalized_row.get("rx_packets")
        tx_packets = normalized_row.get("tx_packets")
        if not any(
            isinstance(value, int) for value in (rx_bytes, tx_bytes, rx_packets, tx_packets)
        ):
            continue

        if include_per_second_rates:
            normalized_row["interval"] = sample_interval
            if isinstance(rx_bytes, int):
                normalized_row["rx_bytes_per_second"] = rx_bytes / sample_interval
                normalized_row["rx_bits_per_second"] = rx_bytes * 8 / sample_interval
            if isinstance(tx_bytes, int):
                normalized_row["tx_bytes_per_second"] = tx_bytes / sample_interval
                normalized_row["tx_bits_per_second"] = tx_bytes * 8 / sample_interval
            if isinstance(rx_packets, int):
                normalized_row["rx_packets_per_second"] = rx_packets / sample_interval
            if isinstance(tx_packets, int):
                normalized_row["tx_packets_per_second"] = tx_packets / sample_interval
        normalized["interfaces"][interface_name] = normalized_row

    return normalized


# Failures the traffic probe and sample steps handle; anything else propagates.
_TRAFFIC_HANDLED_ERRORS: tuple[type[Exception], ...] = (
    TimeoutError,
    RuntimeError,
    TypeError,
    ValueError,
    AttributeError,
    OPNsenseError,
)


class TrafficMixin(AiopnsenseClientProtocol):
    """Diagnostics traffic methods for OPNsenseClient."""

    async def get_interface_traffic(self) -> dict[str, Any]:
        """Return a normalized diagnostics traffic snapshot.

        Returns:
            dict[str, Any]: Normalized diagnostics traffic sample. Returns an
                empty traffic sample when the endpoint fetch or response parsing
                fails. With ``throw_errors`` enabled, a failure is raised as an
                ``OPNsenseError`` subclass instead.
        """
        empty_sample: dict[str, Any] = {"time": None, "interfaces": {}}

        async def fetch_sample() -> dict[str, Any]:
            """Fetch the endpoint and return its normalized traffic sample.

            Returns:
                dict[str, Any]: Normalized sample, or the empty sample when the
                    endpoint is unavailable.
            """
            outcome = await self._fetch_endpoint_outcome(DIAGNOSTICS_TRAFFIC_ENDPOINT)
            if not outcome.is_data:
                _LOGGER.debug("Diagnostics traffic endpoint unavailable")
                return empty_sample
            return normalize_traffic_payload(
                outcome.mapping(), interval=1.0, include_per_second_rates=False
            )

        return await _attempt(
            self,
            fetch_sample,
            empty=empty_sample,
            name="get_interface_traffic",
            errors=_TRAFFIC_HANDLED_ERRORS,
            log_level=logging.DEBUG,
        )

    async def stream_interface_traffic(
        self,
        poll_interval: int = 1,
    ) -> AsyncIterator[dict[str, Any]]:
        """Yield normalized diagnostics traffic stream samples.

        Args:
            poll_interval (int): Requested stream interval in seconds. Values below
                1 are clamped to 1, and the clamped value selects the stream endpoint.

        Yields:
            dict[str, Any]: Normalized traffic samples after the initial valid timing
                event is discarded because it cannot represent an interval delta.
                Later samples use elapsed time between strictly increasing server
                timestamps; after a timing reset, the next valid sample uses the
                selected stream interval. The stream ends empty when checking the
                stream endpoint fails; with ``throw_errors`` enabled, that failure
                is raised as an ``OPNsenseError`` subclass instead.
        """
        interval = max(poll_interval, 1)
        endpoint = f"{DIAGNOSTICS_TRAFFIC_STREAM_ENDPOINT_PREFIX}/{interval}"

        async def check_stream_endpoint() -> bool:
            """Check the stream endpoint.

            Returns:
                bool: ``True`` when the stream endpoint is available.
            """
            if not (await self._check_endpoint_status(endpoint)).is_data:
                _LOGGER.debug("Diagnostics traffic stream endpoint unavailable")
                return False
            return True

        if not await _attempt(
            self,
            check_stream_endpoint,
            empty=False,
            name="stream_interface_traffic",
            errors=_TRAFFIC_HANDLED_ERRORS,
            log_level=logging.DEBUG,
        ):
            return

        event_count = 0
        previous_time: float | None = None
        stream_read_timeout = max(interval + 1, DEFAULT_REQUEST_TIMEOUT_SECONDS)
        stream_events = self._stream_json_events(
            endpoint,
            yield_reset_events=True,
            sock_read_timeout_seconds=stream_read_timeout,
        )
        try:
            async for event in stream_events:
                if event.get(_STREAM_JSON_EVENT_RESET_KEY) is True:
                    previous_time = None
                    continue

                event_time = try_to_float(event.get("time"))
                sample_interval = float(interval)
                if event_time is None or (
                    previous_time is not None and event_time <= previous_time
                ):
                    previous_time = None
                    continue
                event_count += 1
                if previous_time is None:
                    previous_time = event_time
                else:
                    sample_interval = event_time - previous_time
                    previous_time = event_time

                if event_count == 1:
                    continue
                yield normalize_traffic_payload(event, interval=sample_interval)
        finally:
            await stream_events.aclose()