Added `CpuStats` entity to `system`.

This commit is contained in:
Fabio Manganiello 2023-04-18 01:19:06 +02:00
parent b4fbd3e915
commit a5b0a524f6
Signed by: blacklight
GPG Key ID: D90FBA7F76362774
6 changed files with 68 additions and 37 deletions

View File

@ -0,0 +1 @@
Device.vue

View File

@ -23,6 +23,14 @@
}
},
"cpu_stats": {
"name": "System",
"name_plural": "System",
"icon": {
"class": "fas fa-gauge"
}
},
"cpu_times": {
"name": "System",
"name_plural": "System",

View File

@ -70,3 +70,21 @@ if 'cpu_times' not in Base.metadata:
__mapper_args__ = {
'polymorphic_identity': __tablename__,
}
if 'cpu_stats' not in Base.metadata:
class CpuStats(Entity):
"""
``CpuStats`` ORM (container) model.
"""
__tablename__ = 'cpu_stats'
id = Column(
Integer, ForeignKey(Entity.id, ondelete='CASCADE'), primary_key=True
)
__mapper_args__ = {
'polymorphic_identity': __tablename__,
}

View File

@ -28,28 +28,6 @@ class SensorResponse(SystemResponse):
pass
class CpuStatsResponse(CpuResponse):
def __init__(
self,
ctx_switches: int,
interrupts: int,
soft_interrupts: int,
syscalls: int,
*args,
**kwargs
):
super().__init__(
*args,
output={
'ctx_switches': ctx_switches,
'interrupts': interrupts,
'soft_interrupts': soft_interrupts,
'syscalls': syscalls,
},
**kwargs
)
class CpuFrequencyResponse(CpuResponse):
# noinspection PyShadowingBuiltins
def __init__(self, min: int, max: int, current: int, *args, **kwargs):

View File

@ -6,15 +6,15 @@ from typing_extensions import override
from platypush.entities import Entity
from platypush.entities.managers import EntityManager
from platypush.entities.sensors import PercentSensor
from platypush.entities.sensors import NumericSensor, PercentSensor
from platypush.entities.system import (
Cpu,
CpuInfo as CpuInfoModel,
CpuStats as CpuStatsModel,
CpuTimes as CpuTimesModel,
)
from platypush.message.response.system import (
CpuResponseList,
CpuStatsResponse,
CpuFrequencyResponse,
VirtualMemoryUsageResponse,
SwapMemoryUsageResponse,
@ -41,6 +41,8 @@ from platypush.plugins.sensor import SensorPlugin
from platypush.schemas.system import (
CpuInfo,
CpuInfoSchema,
CpuStats,
CpuStatsSchema,
CpuTimes,
CpuTimesSchema,
SystemInfoSchema,
@ -144,22 +146,20 @@ class SystemPlugin(SensorPlugin, EntityManager):
return list(percent) # type: ignore
return percent
@action
def cpu_stats(self) -> CpuStatsResponse:
"""
Get CPU stats.
:return: :class:`platypush.message.response.system.CpuStatsResponse`
"""
def _cpu_stats(self) -> CpuStats:
import psutil
stats = psutil.cpu_stats()
return CpuStatsSchema().load(stats._asdict()) # type: ignore
return CpuStatsResponse(
ctx_switches=stats.ctx_switches,
interrupts=stats.interrupts,
soft_interrupts=stats.soft_interrupts,
syscalls=stats.syscalls,
)
@action
def cpu_stats(self) -> CpuStats:
"""
Get CPU stats.
:return: .. schema:: system.CpuStatsSchema
"""
return CpuStatsSchema().dump(self._cpu_stats()) # type: ignore
@action
def cpu_frequency(
@ -791,6 +791,7 @@ class SystemPlugin(SensorPlugin, EntityManager):
{
'cpu': {
'info': self._cpu_info,
'stats': self._cpu_stats(),
'times': self._cpu_times_avg(),
'percent': self.cpu_percent().output / 100.0, # type: ignore
},
@ -813,6 +814,18 @@ class SystemPlugin(SensorPlugin, EntityManager):
name='Info',
**cpu['info'],
),
CpuStatsModel(
id='system:cpu:stats',
name='Statistics',
children=[
NumericSensor(
id=f'system:cpu:stats:{key}',
name=key,
value=value,
)
for key, value in cpu['stats'].items()
],
),
CpuTimesModel(
id='system:cpu:times',
name='Times',

View File

@ -189,6 +189,18 @@ class CpuTimes:
guest_nice: Optional[float] = percent_field()
@dataclass
class CpuStats:
"""
CPU stats data class.
"""
ctx_switches: int
interrupts: int
soft_interrupts: int
syscalls: int
@dataclass
class CpuData:
"""
@ -197,6 +209,7 @@ class CpuData:
info: CpuInfo
times: CpuTimes
stats: CpuStats
percent: float = percent_field()
@ -211,5 +224,5 @@ class SystemInfo:
CpuInfoSchema = class_schema(CpuInfo, base_schema=CpuInfoBaseSchema)
CpuTimesSchema = class_schema(CpuTimes, base_schema=CpuTimesBaseSchema)
CpuDataSchema = class_schema(CpuTimes, base_schema=DataClassSchema)
CpuStatsSchema = class_schema(CpuStats, base_schema=DataClassSchema)
SystemInfoSchema = class_schema(SystemInfo, base_schema=DataClassSchema)