from empire.python.typings import *
from empire.data_structures.struct import Struct
from empire.system.os_util import OSUtil


class CPUTimes(Struct):
    def __init__(self, raw: Any):
        super().__init__()
        self._user: float = -1
        self._system: float = -1
        self._idle: float = -1
        self._nice: float = -1
        self._iowait: float = -1
        self._irq: float = -1
        self._soft_irq: float = -1
        self._steal: float = -1
        self._guest: float = -1
        self._guest_nice: float = -1
        self._interrupt: float = -1
        self._dpc: float = -1
        super().__fields_decl_end__()

        self._init(raw)

    def _init(self, raw: Any):
        if not raw:
            return

        self._user = raw.user or -1.0
        self._system = raw.system or -1.0
        self._idle = raw.idle or -1.0

        if OSUtil.is_linux():
            self._nice = raw.nice or -1.0
            self._iowait = raw.iowait or -1.0
            self.irq = raw.irq or -1.0
            self._soft_irq = raw.softirq or -1.0
            self._steal = raw.steal
            self._guest = raw.guest
            self._guest_nice = raw.guest_nice

        if OSUtil.is_win32():
            self._interrupt = raw.interrupt
            self._dpc = raw.dpc

    @property
    def user(self) -> float:
        """
        Time spent by normal processes executing in user mode; on Linux this also includes guest time
        """
        return self._user

    @property
    def kernel(self) -> float:
        """
        Time spent by processes executing in kernel mode
        """
        return self._system

    @property
    def idle(self) -> float:
        """
        Time spent doing nothing
        """
        return self._idle

    @property
    def nice_linux(self) -> float:
        """
        Time spent by niced (prioritized) processes executing in user mode; on Linux this also includes guest_nice time
        """
        return self._nice

    @property
    def iowait_linux(self) -> float:
        """
        Time spent waiting for I/O to complete. This is not accounted in idle time counter.
        """
        return self._iowait

    @property
    def hardware_interrupts_linux(self) -> float:
        """
        Time spent for servicing hardware interrupts
        """
        return self._irq

    @property
    def software_interrupts_linux(self) -> float:
        """
        Time spent for servicing software interrupts
        """
        return self._soft_irq

    @property
    def virtualized_environment_linux(self) -> float:
        """
        Time spent by other operating systems running in a virtualized environment
        """
        return self._steal

    @property
    def guest_linux(self) -> float:
        """
        Time spent running a virtual CPU for guest operating systems under the control of the Linux kernel
        """
        return self._guest

    @property
    def guest_nice_linux(self) -> float:
        """
        Time spent running a virtual CPU for guest operating systems under the control of the Linux kernel
        """
        return self._guest_nice

    @property
    def interrupt_windows(self) -> float:
        """
        Time spent for servicing hardware interrupts ( similar to “irq” on UNIX)
        """
        return self._interrupt

    @property
    def deferred_procedure_calls_windows(self) -> float:
        """
        Time spent servicing deferred procedure calls (DPCs); DPCs are interrupts that run at a lower priority than standard interrupts.
        """
        return self._dpc

from empire.python.typings import *
from empire.data_structures.struct import Struct
from empire.system.os_util import OSUtil


class CPUTimes(Struct):
    def __init__(self, raw: Any):
        super().__init__()
        self._user: float = -1
        self._system: float = -1
        self._idle: float = -1
        self._nice: float = -1
        self._iowait: float = -1
        self._irq: float = -1
        self._soft_irq: float = -1
        self._steal: float = -1
        self._guest: float = -1
        self._guest_nice: float = -1
        self._interrupt: float = -1
        self._dpc: float = -1
        super().__fields_decl_end__()

        self._init(raw)

    def _init(self, raw: Any):
        if not raw:
            return

        self._user = raw.user or -1.0
        self._system = raw.system or -1.0
        self._idle = raw.idle or -1.0

        if OSUtil.is_linux():
            self._nice = raw.nice or -1.0
            self._iowait = raw.iowait or -1.0
            self.irq = raw.irq or -1.0
            self._soft_irq = raw.softirq or -1.0
            self._steal = raw.steal
            self._guest = raw.guest
            self._guest_nice = raw.guest_nice

        if OSUtil.is_win32():
            self._interrupt = raw.interrupt
            self._dpc = raw.dpc

    @property
    def user(self) -> float:
        """
        Time spent by normal processes executing in user mode; on Linux this also includes guest time
        """
        return self._user

    @property
    def kernel(self) -> float:
        """
        Time spent by processes executing in kernel mode
        """
        return self._system

    @property
    def idle(self) -> float:
        """
        Time spent doing nothing
        """
        return self._idle

    @property
    def nice_linux(self) -> float:
        """
        Time spent by niced (prioritized) processes executing in user mode; on Linux this also includes guest_nice time
        """
        return self._nice

    @property
    def iowait_linux(self) -> float:
        """
        Time spent waiting for I/O to complete. This is not accounted in idle time counter.
        """
        return self._iowait

    @property
    def hardware_interrupts_linux(self) -> float:
        """
        Time spent for servicing hardware interrupts
        """
        return self._irq

    @property
    def software_interrupts_linux(self) -> float:
        """
        Time spent for servicing software interrupts
        """
        return self._soft_irq

    @property
    def virtualized_environment_linux(self) -> float:
        """
        Time spent by other operating systems running in a virtualized environment
        """
        return self._steal

    @property
    def guest_linux(self) -> float:
        """
        Time spent running a virtual CPU for guest operating systems under the control of the Linux kernel
        """
        return self._guest

    @property
    def guest_nice_linux(self) -> float:
        """
        Time spent running a virtual CPU for guest operating systems under the control of the Linux kernel
        """
        return self._guest_nice

    @property
    def interrupt_windows(self) -> float:
        """
        Time spent for servicing hardware interrupts ( similar to “irq” on UNIX)
        """
        return self._interrupt

    @property
    def deferred_procedure_calls_windows(self) -> float:
        """
        Time spent servicing deferred procedure calls (DPCs); DPCs are interrupts that run at a lower priority than standard interrupts.
        """
        return self._dpc

Time spent by normal processes executing in user mode; on Linux this also includes guest time Time spent by processes executing in kernel mode Time spent doing nothing Time spent by niced (prioritized) processes executing in user mode; on Linux this also includes guest_nice time Time spent waiting for I/O to complete. This is not accounted in idle time counter. Time spent for servicing hardware interrupts Time spent for servicing software interrupts Time spent by other operating systems running in a virtualized environment Time spent running a virtual CPU for guest operating systems under the control of the Linux kernel Time spent running a virtual CPU for guest operating systems under the control of the Linux kernel Time spent for servicing hardware interrupts ( similar to “irq” on UNIX) Time spent servicing deferred procedure calls (DPCs); DPCs are interrupts that run at a lower priority than standard interrupts.