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": { "cpu_times": {
"name": "System", "name": "System",
"name_plural": "System", "name_plural": "System",

View File

@ -70,3 +70,21 @@ if 'cpu_times' not in Base.metadata:
__mapper_args__ = { __mapper_args__ = {
'polymorphic_identity': __tablename__, '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 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): class CpuFrequencyResponse(CpuResponse):
# noinspection PyShadowingBuiltins # noinspection PyShadowingBuiltins
def __init__(self, min: int, max: int, current: int, *args, **kwargs): 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 import Entity
from platypush.entities.managers import EntityManager from platypush.entities.managers import EntityManager
from platypush.entities.sensors import PercentSensor from platypush.entities.sensors import NumericSensor, PercentSensor
from platypush.entities.system import ( from platypush.entities.system import (
Cpu, Cpu,
CpuInfo as CpuInfoModel, CpuInfo as CpuInfoModel,
CpuStats as CpuStatsModel,
CpuTimes as CpuTimesModel, CpuTimes as CpuTimesModel,
) )
from platypush.message.response.system import ( from platypush.message.response.system import (
CpuResponseList, CpuResponseList,
CpuStatsResponse,
CpuFrequencyResponse, CpuFrequencyResponse,
VirtualMemoryUsageResponse, VirtualMemoryUsageResponse,
SwapMemoryUsageResponse, SwapMemoryUsageResponse,
@ -41,6 +41,8 @@ from platypush.plugins.sensor import SensorPlugin
from platypush.schemas.system import ( from platypush.schemas.system import (
CpuInfo, CpuInfo,
CpuInfoSchema, CpuInfoSchema,
CpuStats,
CpuStatsSchema,
CpuTimes, CpuTimes,
CpuTimesSchema, CpuTimesSchema,
SystemInfoSchema, SystemInfoSchema,
@ -144,22 +146,20 @@ class SystemPlugin(SensorPlugin, EntityManager):
return list(percent) # type: ignore return list(percent) # type: ignore
return percent return percent
@action def _cpu_stats(self) -> CpuStats:
def cpu_stats(self) -> CpuStatsResponse:
"""
Get CPU stats.
:return: :class:`platypush.message.response.system.CpuStatsResponse`
"""
import psutil import psutil
stats = psutil.cpu_stats() stats = psutil.cpu_stats()
return CpuStatsSchema().load(stats._asdict()) # type: ignore
return CpuStatsResponse( @action
ctx_switches=stats.ctx_switches, def cpu_stats(self) -> CpuStats:
interrupts=stats.interrupts, """
soft_interrupts=stats.soft_interrupts, Get CPU stats.
syscalls=stats.syscalls,
) :return: .. schema:: system.CpuStatsSchema
"""
return CpuStatsSchema().dump(self._cpu_stats()) # type: ignore
@action @action
def cpu_frequency( def cpu_frequency(
@ -791,6 +791,7 @@ class SystemPlugin(SensorPlugin, EntityManager):
{ {
'cpu': { 'cpu': {
'info': self._cpu_info, 'info': self._cpu_info,
'stats': self._cpu_stats(),
'times': self._cpu_times_avg(), 'times': self._cpu_times_avg(),
'percent': self.cpu_percent().output / 100.0, # type: ignore 'percent': self.cpu_percent().output / 100.0, # type: ignore
}, },
@ -813,6 +814,18 @@ class SystemPlugin(SensorPlugin, EntityManager):
name='Info', name='Info',
**cpu['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( CpuTimesModel(
id='system:cpu:times', id='system:cpu:times',
name='Times', name='Times',

View File

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