forked from platypush/platypush
Added CpuFrequency
entity to system
.
This commit is contained in:
parent
a5b0a524f6
commit
1cee0459cf
4 changed files with 55 additions and 46 deletions
|
@ -44,6 +44,12 @@ export default {
|
||||||
<style lang="scss" scoped>
|
<style lang="scss" scoped>
|
||||||
@import "common";
|
@import "common";
|
||||||
|
|
||||||
|
.entity {
|
||||||
|
.icon {
|
||||||
|
margin-right: 1em;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
.sensor-container {
|
.sensor-container {
|
||||||
.head {
|
.head {
|
||||||
.value {
|
.value {
|
||||||
|
|
|
@ -28,20 +28,6 @@ class SensorResponse(SystemResponse):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
class CpuFrequencyResponse(CpuResponse):
|
|
||||||
# noinspection PyShadowingBuiltins
|
|
||||||
def __init__(self, min: int, max: int, current: int, *args, **kwargs):
|
|
||||||
super().__init__(
|
|
||||||
*args,
|
|
||||||
output={
|
|
||||||
'min': min,
|
|
||||||
'max': max,
|
|
||||||
'current': current,
|
|
||||||
},
|
|
||||||
**kwargs
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
class VirtualMemoryUsageResponse(MemoryResponse):
|
class VirtualMemoryUsageResponse(MemoryResponse):
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
|
@ -437,11 +423,6 @@ class SystemResponseList(SystemResponse):
|
||||||
super().__init__(output=[r.output for r in responses], *args, **kwargs)
|
super().__init__(output=[r.output for r in responses], *args, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
class CpuResponseList(CpuResponse, SystemResponseList):
|
|
||||||
def __init__(self, responses: List[CpuResponse], *args, **kwargs):
|
|
||||||
super().__init__(responses=responses, *args, **kwargs)
|
|
||||||
|
|
||||||
|
|
||||||
class DiskResponseList(DiskResponse, SystemResponseList):
|
class DiskResponseList(DiskResponse, SystemResponseList):
|
||||||
def __init__(self, responses: List[DiskResponse], *args, **kwargs):
|
def __init__(self, responses: List[DiskResponse], *args, **kwargs):
|
||||||
super().__init__(responses=responses, *args, **kwargs)
|
super().__init__(responses=responses, *args, **kwargs)
|
||||||
|
|
|
@ -14,8 +14,6 @@ from platypush.entities.system import (
|
||||||
CpuTimes as CpuTimesModel,
|
CpuTimes as CpuTimesModel,
|
||||||
)
|
)
|
||||||
from platypush.message.response.system import (
|
from platypush.message.response.system import (
|
||||||
CpuResponseList,
|
|
||||||
CpuFrequencyResponse,
|
|
||||||
VirtualMemoryUsageResponse,
|
VirtualMemoryUsageResponse,
|
||||||
SwapMemoryUsageResponse,
|
SwapMemoryUsageResponse,
|
||||||
DiskResponseList,
|
DiskResponseList,
|
||||||
|
@ -39,6 +37,8 @@ from platypush.message.response.system import (
|
||||||
from platypush.plugins import action
|
from platypush.plugins import action
|
||||||
from platypush.plugins.sensor import SensorPlugin
|
from platypush.plugins.sensor import SensorPlugin
|
||||||
from platypush.schemas.system import (
|
from platypush.schemas.system import (
|
||||||
|
CpuFrequency,
|
||||||
|
CpuFrequencySchema,
|
||||||
CpuInfo,
|
CpuInfo,
|
||||||
CpuInfoSchema,
|
CpuInfoSchema,
|
||||||
CpuStats,
|
CpuStats,
|
||||||
|
@ -107,7 +107,7 @@ class SystemPlugin(SensorPlugin, EntityManager):
|
||||||
@action
|
@action
|
||||||
def cpu_times(self, per_cpu=False, percent=True) -> Union[list, dict]:
|
def cpu_times(self, per_cpu=False, percent=True) -> Union[list, dict]:
|
||||||
"""
|
"""
|
||||||
Get the CPU times stats.
|
Get the CPU times per status, either as absolute time or a percentage.
|
||||||
|
|
||||||
:param per_cpu: Get per-CPU stats (default: False).
|
:param per_cpu: Get per-CPU stats (default: False).
|
||||||
:param percent: Get the stats in percentage (default: True).
|
:param percent: Get the stats in percentage (default: True).
|
||||||
|
@ -161,37 +161,37 @@ class SystemPlugin(SensorPlugin, EntityManager):
|
||||||
"""
|
"""
|
||||||
return CpuStatsSchema().dump(self._cpu_stats()) # type: ignore
|
return CpuStatsSchema().dump(self._cpu_stats()) # type: ignore
|
||||||
|
|
||||||
|
def _cpu_frequency_avg(self) -> CpuFrequency:
|
||||||
|
import psutil
|
||||||
|
|
||||||
|
freq = psutil.cpu_freq(percpu=False)
|
||||||
|
return CpuFrequencySchema().load(freq._asdict()) # type: ignore
|
||||||
|
|
||||||
|
def _cpu_frequency_per_cpu(self) -> List[CpuFrequency]:
|
||||||
|
import psutil
|
||||||
|
|
||||||
|
freq = psutil.cpu_freq(percpu=True)
|
||||||
|
return CpuFrequencySchema().load(freq._asdict(), many=True) # type: ignore
|
||||||
|
|
||||||
@action
|
@action
|
||||||
def cpu_frequency(
|
def cpu_frequency(
|
||||||
self, per_cpu: bool = False
|
self, per_cpu: bool = False
|
||||||
) -> Union[CpuFrequencyResponse, CpuResponseList]:
|
) -> Union[CpuFrequency, List[CpuFrequency]]:
|
||||||
"""
|
"""
|
||||||
Get CPU stats.
|
Get the CPU frequency, in MHz.
|
||||||
|
|
||||||
:param per_cpu: Get per-CPU stats (default: False).
|
:param per_cpu: Get per-CPU stats (default: False).
|
||||||
:return: :class:`platypush.message.response.system.CpuFrequencyResponse`
|
:return: If ``per_cpu=False``:
|
||||||
|
|
||||||
|
.. schema:: system.CpuFrequencySchema
|
||||||
|
|
||||||
|
If ``per_cpu=True`` then a list will be returned, where each item
|
||||||
|
identifies the CPU times of a core:
|
||||||
|
|
||||||
|
.. schema:: system.CpuFrequencySchema(many=True)
|
||||||
|
|
||||||
"""
|
"""
|
||||||
import psutil
|
return self._cpu_frequency_per_cpu() if per_cpu else self._cpu_frequency_avg()
|
||||||
|
|
||||||
freq = psutil.cpu_freq(percpu=per_cpu)
|
|
||||||
|
|
||||||
if per_cpu:
|
|
||||||
return CpuResponseList(
|
|
||||||
[
|
|
||||||
CpuFrequencyResponse(
|
|
||||||
min=f.min,
|
|
||||||
max=f.max,
|
|
||||||
current=f.current,
|
|
||||||
)
|
|
||||||
for f in freq
|
|
||||||
]
|
|
||||||
)
|
|
||||||
|
|
||||||
return CpuFrequencyResponse(
|
|
||||||
min=freq.min,
|
|
||||||
max=freq.max,
|
|
||||||
current=freq.current,
|
|
||||||
)
|
|
||||||
|
|
||||||
@action
|
@action
|
||||||
def load_avg(self) -> List[float]:
|
def load_avg(self) -> List[float]:
|
||||||
|
@ -790,6 +790,7 @@ class SystemPlugin(SensorPlugin, EntityManager):
|
||||||
ret = SystemInfoSchema().dump(
|
ret = SystemInfoSchema().dump(
|
||||||
{
|
{
|
||||||
'cpu': {
|
'cpu': {
|
||||||
|
'frequency': self._cpu_frequency_avg(),
|
||||||
'info': self._cpu_info,
|
'info': self._cpu_info,
|
||||||
'stats': self._cpu_stats(),
|
'stats': self._cpu_stats(),
|
||||||
'times': self._cpu_times_avg(),
|
'times': self._cpu_times_avg(),
|
||||||
|
@ -838,6 +839,14 @@ class SystemPlugin(SensorPlugin, EntityManager):
|
||||||
for key, time_percent in cpu['times'].items()
|
for key, time_percent in cpu['times'].items()
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
|
NumericSensor(
|
||||||
|
id='system:cpu:frequency',
|
||||||
|
name='Frequency',
|
||||||
|
value=round(cpu['frequency']['current'], 2),
|
||||||
|
min=cpu['frequency']['min'],
|
||||||
|
max=cpu['frequency']['max'],
|
||||||
|
unit='MHz',
|
||||||
|
),
|
||||||
PercentSensor(
|
PercentSensor(
|
||||||
id='system:cpu:percent',
|
id='system:cpu:percent',
|
||||||
name='Percent',
|
name='Percent',
|
||||||
|
|
|
@ -201,6 +201,17 @@ class CpuStats:
|
||||||
syscalls: int
|
syscalls: int
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class CpuFrequency:
|
||||||
|
"""
|
||||||
|
CPU frequency data class.
|
||||||
|
"""
|
||||||
|
|
||||||
|
current: float
|
||||||
|
min: float
|
||||||
|
max: float
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class CpuData:
|
class CpuData:
|
||||||
"""
|
"""
|
||||||
|
@ -209,6 +220,7 @@ class CpuData:
|
||||||
|
|
||||||
info: CpuInfo
|
info: CpuInfo
|
||||||
times: CpuTimes
|
times: CpuTimes
|
||||||
|
frequency: CpuFrequency
|
||||||
stats: CpuStats
|
stats: CpuStats
|
||||||
percent: float = percent_field()
|
percent: float = percent_field()
|
||||||
|
|
||||||
|
@ -222,6 +234,7 @@ class SystemInfo:
|
||||||
cpu: CpuData
|
cpu: CpuData
|
||||||
|
|
||||||
|
|
||||||
|
CpuFrequencySchema = class_schema(CpuFrequency, base_schema=DataClassSchema)
|
||||||
CpuInfoSchema = class_schema(CpuInfo, base_schema=CpuInfoBaseSchema)
|
CpuInfoSchema = class_schema(CpuInfo, base_schema=CpuInfoBaseSchema)
|
||||||
CpuTimesSchema = class_schema(CpuTimes, base_schema=CpuTimesBaseSchema)
|
CpuTimesSchema = class_schema(CpuTimes, base_schema=CpuTimesBaseSchema)
|
||||||
CpuStatsSchema = class_schema(CpuStats, base_schema=DataClassSchema)
|
CpuStatsSchema = class_schema(CpuStats, base_schema=DataClassSchema)
|
||||||
|
|
Loading…
Reference in a new issue